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)
command = '"'..command..'"'
local _,ismain = coroutine.running()
local _, ismain = coroutine.running()
local output
if ismain then
output = io.popen(command):read("*a")
@ -155,19 +155,20 @@ local function get_posh_prompt(rprompt)
return run_posh_command(prompt_exe)
end
local function set_posh_tooltip(command)
local tooltip
if command ~= nil and command ~= "" then
-- escape special characters properly, if any
command = string.gsub(command, '(\\+)"', '%1%1"')
command = string.gsub(command, '(\\+)$', '%1%1')
command = string.gsub(command, '"', '\\"')
command = string.gsub(command, '([&<>%(%)@%^|])', '^%1')
local function set_posh_tooltip(tip_command)
if tip_command ~= "" and tip_command ~= cached_prompt.tip_command then
-- Escape special characters properly, if any.
local escaped_tip_command = string.gsub(tip_command, '(\\+)"', '%1%1"'):gsub('(\\+)$', '%1%1'):gsub('"', '\\"'):gsub('([&<>%(%)@%^|])', '^%1')
local prompt_exe = string.format('%s print tooltip --shell=cmd %s --config=%s --command="%s"', omp_exe(), error_level_option(), omp_config(), command)
tooltip = run_posh_command(prompt_exe)
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)
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
cached_prompt.tooltip = (tooltip ~= "") and tooltip or nil
end
local function display_cached_prompt()
@ -274,39 +275,23 @@ end
-- 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)
-- Insert space first, in case it might affect the tip word, e.g. it could
-- split "gitcommit" into "git commit".
rl_buffer:insert(" ")
-- Get the new tip command.
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
-- Get the first word of command line as tip.
local tip_command = rl_buffer:getbuffer():gsub("^%s*([^%s]*).*$", "%1")
-- Generate a tooltip asynchronously (via coroutine) if available, otherwise
-- generate a tooltip immediately.
if not can_async() then
set_posh_tooltip(cached_prompt.tip_command)
set_posh_tooltip(tip_command)
clink.refilterprompt()
elseif cached_prompt.coroutine then
-- No action needed; a tooltip coroutine is already running.
else
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
cached_prompt.coroutine = nil
end