From 679e9e71de318c8a3bdd515613ecda4740388fe4 Mon Sep 17 00:00:00 2001 From: Hongbo Liu Date: Mon, 8 Aug 2022 13:59:33 +0800 Subject: [PATCH] [filename] support detect new file status (#785) * [filename] support detect new file status * update readme about new file status * add definition for new file --- README.md | 2 ++ lua/lualine/components/filename.lua | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e7327b..99ae41f 100644 --- a/README.md +++ b/README.md @@ -593,6 +593,7 @@ sections = { { 'filename', file_status = true, -- Displays file status (readonly status, modified status) + newfile_status = false -- Display new file status (new file means no write after created) path = 0, -- 0: Just the filename -- 1: Relative path -- 2: Absolute path @@ -604,6 +605,7 @@ sections = { modified = '[+]', -- Text to show when the file is modified. readonly = '[-]', -- Text to show when the file is non-modifiable or readonly. unnamed = '[No Name]', -- Text to show for unnamed buffers. + newfile = '[New]', -- Text to show for new created file before first writting } } } diff --git a/lua/lualine/components/filename.lua b/lua/lualine/components/filename.lua index f68dba7..39c4728 100644 --- a/lua/lualine/components/filename.lua +++ b/lua/lualine/components/filename.lua @@ -7,8 +7,14 @@ local modules = require('lualine_require').lazy_require { } local default_options = { - symbols = { modified = '[+]', readonly = '[-]', unnamed = '[No Name]' }, + symbols = { + modified = '[+]', + readonly = '[-]', + unnamed = '[No Name]', + newfile = '[New]', + }, file_status = true, + newfile_status = false, path = 0, shorting_target = 40, } @@ -21,6 +27,11 @@ local function count(base, pattern) return select(2, string.gsub(base, pattern, '')) end +local function is_new_file() + local filename = vim.fn.expand('%') + return vim.bo.buftype == '' and vim.fn.filereadable(filename) == 0 +end + ---shortens path by turning apple/orange -> a/orange ---@param path string ---@param sep string path separator @@ -77,6 +88,10 @@ M.update_status = function(self) data = data .. self.options.symbols.readonly end end + + if self.options.newfile_status and is_new_file() then + data = data .. self.options.symbols.newfile + end return data end