fix(cmd): do not cache an empty tooltip

This commit is contained in:
L. Yeung 2023-04-22 01:45:25 +08:00 committed by Jan De Dobbeleer
parent 770d1994bc
commit 1474979804

View file

@ -87,7 +87,7 @@ end
local function run_posh_command(command) local function run_posh_command(command)
command = '"'..command..'"' command = '"'..command..'"'
local _,ismain = coroutine.running() local _, ismain = coroutine.running()
local output local output
if ismain then if ismain then
output = io.popen(command):read("*a") output = io.popen(command):read("*a")
@ -155,19 +155,20 @@ local function get_posh_prompt(rprompt)
return run_posh_command(prompt_exe) return run_posh_command(prompt_exe)
end end
local function set_posh_tooltip(command) local function set_posh_tooltip(tip_command)
local tooltip if tip_command ~= "" and tip_command ~= cached_prompt.tip_command then
if command ~= nil and command ~= "" then -- Escape special characters properly, if any.
-- escape special characters properly, if any local escaped_tip_command = string.gsub(tip_command, '(\\+)"', '%1%1"'):gsub('(\\+)$', '%1%1'):gsub('"', '\\"'):gsub('([&<>%(%)@%^|])', '^%1')
command = string.gsub(command, '(\\+)"', '%1%1"')
command = string.gsub(command, '(\\+)$', '%1%1')
command = string.gsub(command, '"', '\\"')
command = string.gsub(command, '([&<>%(%)@%^|])', '^%1')
local prompt_exe = string.format('%s print tooltip --shell=cmd %s --config=%s --command="%s"', omp_exe(), error_level_option(), omp_config(), command) local prompt_exe = string.format('%s print tooltip --shell=cmd %s --config=%s --command="%s"', omp_exe(), error_level_option(), omp_config(), escaped_tip_command)
tooltip = run_posh_command(prompt_exe) local tooltip = run_posh_command(prompt_exe)
-- Do not cache an empty tooltip.
if tooltip == "" then
return
end
cached_prompt.tip_command = tip_command
cached_prompt.tooltip = tooltip
end end
cached_prompt.tooltip = (tooltip ~= "") and tooltip or nil
end end
local function display_cached_prompt() local function display_cached_prompt()
@ -274,39 +275,23 @@ end
-- Tooltips -- Tooltips
local function get_tip_command(line)
if USE_ENTIRE_COMMAND_LINE then
-- REVIEW: This is what oh-my-posh was doing -- was that intentional?
-- Return the entire command line, minus leading and trailing spaces.
return line:gsub("^%s*(.-)%s*$", "%1")
else
-- This returns the first word from the command line.
return line:match("[^ ]+") or ""
end
end
function ohmyposh_space(rl_buffer) function ohmyposh_space(rl_buffer)
-- Insert space first, in case it might affect the tip word, e.g. it could -- Insert space first, in case it might affect the tip word, e.g. it could
-- split "gitcommit" into "git commit". -- split "gitcommit" into "git commit".
rl_buffer:insert(" ") rl_buffer:insert(" ")
-- Get the first word of command line as tip.
-- Get the new tip command. local tip_command = rl_buffer:getbuffer():gsub("^%s*([^%s]*).*$", "%1")
local tip_command = get_tip_command(rl_buffer:getbuffer())
if tip_command == cached_prompt.tip_command then
return
end
cached_prompt.tip_command = tip_command
-- Generate a tooltip asynchronously (via coroutine) if available, otherwise -- Generate a tooltip asynchronously (via coroutine) if available, otherwise
-- generate a tooltip immediately. -- generate a tooltip immediately.
if not can_async() then if not can_async() then
set_posh_tooltip(cached_prompt.tip_command) set_posh_tooltip(tip_command)
clink.refilterprompt() clink.refilterprompt()
elseif cached_prompt.coroutine then elseif cached_prompt.coroutine then
-- No action needed; a tooltip coroutine is already running. -- No action needed; a tooltip coroutine is already running.
else else
cached_prompt.coroutine = coroutine.create(function () cached_prompt.coroutine = coroutine.create(function ()
set_posh_tooltip(cached_prompt.tip_command) set_posh_tooltip(tip_command)
if cached_prompt.coroutine == coroutine.running() then if cached_prompt.coroutine == coroutine.running() then
cached_prompt.coroutine = nil cached_prompt.coroutine = nil
end end