From 147497980486b18eda04dfe35faac197d419da91 Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Sat, 22 Apr 2023 01:45:25 +0800 Subject: [PATCH] fix(cmd): do not cache an empty tooltip --- src/shell/scripts/omp.lua | 49 ++++++++++++++------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/src/shell/scripts/omp.lua b/src/shell/scripts/omp.lua index 09983569..1c3fe7d2 100644 --- a/src/shell/scripts/omp.lua +++ b/src/shell/scripts/omp.lua @@ -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