From c16b2721fdad1d0bb108dc373c7d8977c7a6a5d6 Mon Sep 17 00:00:00 2001 From: Greg Hurrell Date: Mon, 5 Sep 2022 19:23:01 +0200 Subject: [PATCH] feat: show "fallback" in prompt title if, er, falling back As discussed here: - https://github.com/wincent/command-t/issues/393#issuecomment-1236835197 That comment suggested being a bit more verbose: CommandT [ find (fall-backed from git) ] but here I'm going with something a little less descriptive: CommandT [fallback] seeing as the extra detail in the title doesn't tell you _what_ went wrong; really, all we can do here is show a hint that _something_ went wrong, so we might as well be brief about it. Two things to note about implementation: - One is that I am playing around here trying to figure out how much metatable magic I want in here without making it all too "clever". So here I have a `__newindex` implementation that allows us to write `prompt.name = 'fallback'` instead of `prompt:set_name('fallback')`. The former is visually cleaner, but I don't necessarily like that it hides the fact that an imperative side-effect is going to take place. I will probably "dumb this down" as opposed to trying to use the pattern in other places in a uniform way. - The other is that my earlier claims about top-down React-style dataflow are pretty much not true any more. It _was_ true at the start, when each component rendered using data passed into it; now we have muddied the waters considerably with imperative methods. I will almost certainly want to revisit this topic in the future. --- .../commandt/private/finders/fallback.lua | 2 +- lua/wincent/commandt/private/prompt.lua | 21 +++++++++++++++++-- lua/wincent/commandt/private/ui.lua | 3 ++- lua/wincent/commandt/private/window.lua | 15 ++++++++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lua/wincent/commandt/private/finders/fallback.lua b/lua/wincent/commandt/private/finders/fallback.lua index f36ad92b..1eb2745a 100644 --- a/lua/wincent/commandt/private/finders/fallback.lua +++ b/lua/wincent/commandt/private/finders/fallback.lua @@ -6,6 +6,6 @@ return function(finder, directory, options) return function() finder.fallback = require('wincent.commandt.private.finders.file')(directory ~= '' and directory or '.', options) - return finder.fallback + return finder.fallback, 'fallback' end end diff --git a/lua/wincent/commandt/private/prompt.lua b/lua/wincent/commandt/private/prompt.lua index dbc2b8fe..94d26bda 100644 --- a/lua/wincent/commandt/private/prompt.lua +++ b/lua/wincent/commandt/private/prompt.lua @@ -10,6 +10,13 @@ local Prompt = {} local mt = { __index = Prompt, + __newindex = function(t, k, v) + if k == 'name' then + Prompt.set_name(t, v) + else + rawset(t, k, v) + end + end, } function Prompt.new(options) @@ -47,6 +54,17 @@ function Prompt:close() end end +function Prompt:set_name(name) + self._name = name + if self._window then + self._window:set_title(self:title()) + end +end + +function Prompt:title() + return self._name and ('CommandT [' .. self._name .. ']') or 'CommandT' +end + function Prompt:show() local bottom = nil local top = nil @@ -64,7 +82,6 @@ function Prompt:show() end if self._window == nil then - local title = self._name and ('CommandT [' .. self._name .. ']') or 'CommandT' self._window = Window.new({ bottom = bottom, buftype = 'prompt', @@ -79,7 +96,7 @@ function Prompt:show() self._window = nil end, on_leave = self._on_leave, - title = title, + title = self:title(), top = top, }) end diff --git a/lua/wincent/commandt/private/ui.lua b/lua/wincent/commandt/private/ui.lua index 31c61523..1285b1c2 100644 --- a/lua/wincent/commandt/private/ui.lua +++ b/lua/wincent/commandt/private/ui.lua @@ -99,7 +99,8 @@ ui.show = function(finder, options) -- Once we've proved a finder works, we don't ever want to use fallback. current_finder.fallback = nil elseif current_finder.fallback then - current_finder = current_finder.fallback() + current_finder, name = current_finder.fallback() + prompt.name = name or 'fallback' results = current_finder.run(query) end if #results == 0 then diff --git a/lua/wincent/commandt/private/window.lua b/lua/wincent/commandt/private/window.lua index d9c0ad2e..69e58bf5 100644 --- a/lua/wincent/commandt/private/window.lua +++ b/lua/wincent/commandt/private/window.lua @@ -198,6 +198,19 @@ function Window:replace_lines(lines, options) end end +function Window:set_title(title) + self._title = title + self._padded_title = title ~= '' and (' ' .. title .. ' ') or '' + self:_reposition() + vim.api.nvim_buf_set_lines( + self._title_buffer, + 0, -- start + -1, -- end + false, -- strict indexing + { self._padded_title } + ) +end + function Window:show() if self._main_buffer == nil then self._main_buffer = vim.api.nvim_create_buf( @@ -407,7 +420,7 @@ function Window:_reposition() col = position.col + #self._prompt, height = 1, row = math.max(0, position.row), - width = #(' ' .. self._title .. ' '), + width = #self._padded_title, }) ) end