From de1afb3391c80c223b4c53d2032ce94e207e0b27 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Thu, 18 Feb 2021 00:26:19 +0100 Subject: [PATCH] Add full filename path in window's title Optimal implementation to limite computations in core.step. --- data/core/doc/init.lua | 14 +++++++++++--- data/core/docview.lua | 6 ++++++ data/core/init.lua | 20 +++++++++++++++----- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 096c6aa9..6b63151a 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -65,11 +65,17 @@ function Doc:reset_syntax() end +function Doc:set_filename(filename) + filename = common.normalize_path(filename) + self.filename = filename + self.abs_filename = common.home_encode(system.absolute_path(filename)) +end + + function Doc:load(filename) local fp = assert( io.open(filename, "rb") ) - filename = common.normalize_path(filename) self:reset() - self.filename = filename + self:set_filename(filename) self.lines = {} for line in fp:lines() do if line:byte(-1) == 13 then @@ -94,7 +100,9 @@ function Doc:save(filename) fp:write(line) end fp:close() - self.filename = common.normalize_path(filename or self.filename) + if filename then + self:set_filename(filename) + end self:reset_syntax() self:clean() end diff --git a/data/core/docview.lua b/data/core/docview.lua index 4bffd56f..1a2d3cd9 100644 --- a/data/core/docview.lua +++ b/data/core/docview.lua @@ -90,6 +90,12 @@ function DocView:get_name() end +function DocView:get_filename() + local post = self.doc:is_dirty() and "*" or "" + return (self.doc.abs_filename and self.doc.abs_filename or "unsaved") .. post +end + + function DocView:get_scrollable_size() return self:get_line_height() * (#self.doc.lines - 1) + self.size.y end diff --git a/data/core/init.lua b/data/core/init.lua index a0602468..3b548eae 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -719,6 +719,17 @@ function core.on_event(type, ...) end +local function get_title_filename(view) + local doc_filename = view.get_filename and view:get_filename() or view:get_name() + return (doc_filename ~= "---") and doc_filename or "" +end + + +local function compose_window_title(title) + return title == "" and "lite" or title .. " - lite" +end + + function core.step() -- handle events local did_keymap = false @@ -761,11 +772,10 @@ function core.step() end -- update window title - local name = core.active_view:get_name() - local title = (name ~= "---") and ( (core.active_view:is(DocView) and core.active_view.doc.filename or name) .. " - lite") or "lite" - if title ~= core.window_title then - system.set_window_title(title) - core.window_title = title + local current_title = get_title_filename(core.active_view) + if current_title ~= core.window_title then + system.set_window_title(compose_window_title(current_title)) + core.window_title = current_title end -- draw