feat(cmd): align tooltip behaviors

This commit is contained in:
L. Yeung 2022-06-12 22:23:23 +08:00 committed by Jan De Dobbeleer
parent b670c95701
commit 820e28c85c

View file

@ -22,7 +22,8 @@ end
local endedit_time local endedit_time
local last_duration local last_duration
local tip_word local tip
local tooltip_active = false
local cached_prompt = {} local cached_prompt = {}
local function omp_exe() local function omp_exe()
@ -104,14 +105,13 @@ local function get_posh_prompt(rprompt)
return run_posh_command(prompt_exe) return run_posh_command(prompt_exe)
end end
local function get_posh_tooltip(command) local function set_posh_tooltip(command)
local prompt_exe = string.format('%s print tooltip --shell=cmd --config=%s --command="%s"', omp_exe(), omp_config(), command) local prompt_exe = string.format('%s print tooltip --shell=cmd --config=%s --command="%s"', omp_exe(), omp_config(), command)
local tooltip = run_posh_command(prompt_exe) local tooltip = run_posh_command(prompt_exe)
if tooltip == "" then if tooltip ~= "" then
-- If no tooltip, generate normal rprompt. tooltip_active = true
tooltip = get_posh_prompt(true) cached_prompt.right = tooltip
end end
return tooltip
end end
-- set priority lower than z.lua -- set priority lower than z.lua
@ -129,10 +129,7 @@ function p:filter(prompt)
return cached_prompt.left return cached_prompt.left
end end
function p:rightfilter(prompt) function p:rightfilter(prompt)
if tip_word == nil then if cached_prompt.tip_space and can_async() then
-- No tooltip needed, so generate prompt normally.
cached_prompt.right = get_posh_prompt(true)
elseif cached_prompt.tip_space and can_async() then
-- Generate tooltip asynchronously in response to Spacebar. -- Generate tooltip asynchronously in response to Spacebar.
if cached_prompt.coroutine then if cached_prompt.coroutine then
-- Coroutine is already in progress. The cached right prompt will -- Coroutine is already in progress. The cached right prompt will
@ -140,7 +137,7 @@ function p:rightfilter(prompt)
else else
-- Create coroutine to generate tooltip rprompt. -- Create coroutine to generate tooltip rprompt.
cached_prompt.coroutine = coroutine.create(function () cached_prompt.coroutine = coroutine.create(function ()
cached_prompt.right = get_posh_tooltip(tip_word) set_posh_tooltip(tip)
cached_prompt.tip_done = true cached_prompt.tip_done = true
-- Refresh the prompt once the tooltip is generated. -- Refresh the prompt once the tooltip is generated.
clink.refilterprompt() clink.refilterprompt()
@ -148,7 +145,7 @@ function p:rightfilter(prompt)
end end
if cached_prompt.tip_done then if cached_prompt.tip_done then
-- Once the tooltip is ready, clear the Spacebar flag so that if the -- Once the tooltip is ready, clear the Spacebar flag so that if the
-- command word changes and the Spacebar is pressed again, we can -- tip changes and the Spacebar is pressed again, we can
-- generate a new tooltip. -- generate a new tooltip.
cached_prompt.tip_done = nil cached_prompt.tip_done = nil
cached_prompt.tip_space = nil cached_prompt.tip_space = nil
@ -157,7 +154,11 @@ function p:rightfilter(prompt)
else else
-- Tooltip is needed, but not in response to Spacebar, so refresh it -- Tooltip is needed, but not in response to Spacebar, so refresh it
-- immediately. -- immediately.
cached_prompt.right = get_posh_tooltip(tip_word) set_posh_tooltip(tip)
end
if not tooltip_active then
-- Tooltip is not active, generate rprompt normally.
cached_prompt.right = get_posh_prompt(true)
end end
return cached_prompt.right, false return cached_prompt.right, false
end end
@ -192,16 +193,16 @@ end
-- Tooltips -- Tooltips
function ohmyposh_space(rl_buffer) function ohmyposh_space(rl_buffer)
local new_tip = string.gsub(rl_buffer:getbuffer(), "^%s*(.-)%s*$", "%1")
rl_buffer:insert(" ") rl_buffer:insert(" ")
local words = string.explode(rl_buffer:getbuffer(), ' ', [["]]) if new_tip ~= tip then
if words[1] ~= tip_word then tip = new_tip -- remember the tip for use when filtering the prompt
tip_word = words[1] -- remember the first word for use when filtering the prompt
cached_prompt.tip_space = can_async() cached_prompt.tip_space = can_async()
clink.refilterprompt() -- invoke the prompt filters so omp can update the prompt per the tip word clink.refilterprompt() -- invoke the prompt filters so OMP can update the prompt per the tip
end end
end end
if rl.setbinding then if rl.setbinding then
clink.onbeginedit(function () tip_word = nil cached_prompt = {} end) clink.onbeginedit(function () tip = nil cached_prompt = {} end)
rl.setbinding(' ', [["luafunc:ohmyposh_space"]], 'emacs') rl.setbinding(' ', [["luafunc:ohmyposh_space"]], 'emacs')
end end