From 9290a4ada7cdf9171a77b03124f2653afe3badca Mon Sep 17 00:00:00 2001 From: Mike Smith <10135646+mikesmithgh@users.noreply.github.com> Date: Wed, 6 Dec 2023 23:17:39 -0500 Subject: [PATCH] chore: read kitty executable from path --- lua/kitty-scrollback/api.lua | 2 +- lua/kitty-scrollback/health.lua | 7 ++- lua/kitty-scrollback/init.lua | 2 +- lua/kitty-scrollback/kitty_commands.lua | 26 ++++---- lua/kitty-scrollback/launch.lua | 1 + python/kitty_scrollback_nvim.py | 60 ++++++++++++------- scripts/kitty_not_found.txt | 18 ++++++ .../kitty_scrollback_spec.lua | 15 +++++ 8 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 scripts/kitty_not_found.txt diff --git a/lua/kitty-scrollback/api.lua b/lua/kitty-scrollback/api.lua index 7dd9316f..9a189dbb 100644 --- a/lua/kitty-scrollback/api.lua +++ b/lua/kitty-scrollback/api.lua @@ -191,7 +191,7 @@ M.checkhealth = function() if vim.fn.has('nvim-0.9') > 0 then vim .system({ - 'kitty', + p.kitty_data.kitty_path, '@', 'kitten', kitty_scrollback_kitten, diff --git a/lua/kitty-scrollback/health.lua b/lua/kitty-scrollback/health.lua index 7f8ee83b..1d46b6c4 100644 --- a/lua/kitty-scrollback/health.lua +++ b/lua/kitty-scrollback/health.lua @@ -15,7 +15,7 @@ end local function check_kitty_remote_control() vim.health.start('kitty-scrollback: Kitty remote control') local cmd = { - 'kitty', + p.kitty_data.kitty_path, '@', 'ls', } @@ -206,8 +206,9 @@ local function check_kitty_debug_config() local kitty_debug_config_kitten = vim.api.nvim_get_runtime_file('python/kitty_debug_config.py', false)[1] local debug_config_log = vim.fn.stdpath('data') .. '/kitty-scrollback.nvim/debug_config.log' - local result = - vim.system({ 'kitty', '@', 'kitten', kitty_debug_config_kitten, debug_config_log }):wait() + local result = vim + .system({ p.kitty_data.kitty_path, '@', 'kitten', kitty_debug_config_kitten, debug_config_log }) + :wait() if result.code == 0 then if vim.fn.filereadable(debug_config_log) then vim.health.ok(table.concat(vim.fn.readfile(debug_config_log), '\n ')) diff --git a/lua/kitty-scrollback/init.lua b/lua/kitty-scrollback/init.lua index faa54253..19b5395f 100644 --- a/lua/kitty-scrollback/init.lua +++ b/lua/kitty-scrollback/init.lua @@ -26,7 +26,7 @@ M.setup = function(configs) }) ---@brief [[ - ---:KittyScrollbackGenerateKittens Run `:checkhealth kitty-scrollback` in the context of Kitty + ---:KittyScrollbackCheckHealth Run `:checkhealth kitty-scrollback` in the context of Kitty --- --- See: ~ --- |kitty.api.checkhealth| diff --git a/lua/kitty-scrollback/kitty_commands.lua b/lua/kitty-scrollback/kitty_commands.lua index d184fca2..6f9d1c27 100644 --- a/lua/kitty-scrollback/kitty_commands.lua +++ b/lua/kitty-scrollback/kitty_commands.lua @@ -121,8 +121,12 @@ end M.get_text_term = function(kitty_data, get_text_opts, on_exit_cb) local esc = vim.fn.eval([["\e"]]) - local kitty_get_text_cmd = - string.format([[kitty @ get-text --match="id:%s" %s]], kitty_data.window_id, get_text_opts) + local kitty_get_text_cmd = string.format( + [[%s @ get-text --match="id:%s" %s]], + p.kitty_data.kitty_path, + kitty_data.window_id, + get_text_opts + ) local sed_cmd = string.format( [[sed -E ]] .. [[-e 's/%s\[\?25.%s\[.*;.*H%s\[.*//g' ]] -- remove control sequence added by --add-cursor flag @@ -132,7 +136,7 @@ M.get_text_term = function(kitty_data, get_text_opts, on_exit_cb) esc, esc ) - local flush_stdout_cmd = [[kitty +runpy 'sys.stdout.flush()']] + local flush_stdout_cmd = p.kitty_data.kitty_path .. [[ +runpy 'sys.stdout.flush()']] -- start to set title but do not complete see https://github.com/kovidgoyal/kitty/issues/719#issuecomment-952039731 local start_set_title_cmd = string.format([[printf '%s]2;']], esc) local full_cmd = kitty_get_text_cmd @@ -245,7 +249,7 @@ M.send_paste_buffer_text_to_kitty_and_quit = function(execute_command) end system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '@', 'send-text', '--match=id:' .. p.kitty_data.window_id, @@ -256,7 +260,7 @@ end M.list_kitty_windows = function() return system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '@', 'ls', }) @@ -267,7 +271,7 @@ M.close_kitty_loading_window = function(ignore_error) local winid = p.kitty_loading_winid p.kitty_loading_winid = nil return system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '@', 'close-window', '--match=id:' .. winid, @@ -278,7 +282,7 @@ end M.signal_winchanged_to_kitty_child_process = function() system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '@', 'signal-child', 'SIGWINCH', @@ -290,7 +294,7 @@ M.signal_term_to_kitty_child_process = function(force) vim.cmd.quitall({ bang = true }) else system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '@', 'signal-child', 'SIGTERM', @@ -303,7 +307,7 @@ M.open_kitty_loading_window = function(env) M.close_kitty_loading_window(true) end local kitty_cmd = vim.list_extend({ - 'kitty', + p.kitty_data.kitty_path, '@', 'launch', '--type', @@ -332,7 +336,7 @@ end M.get_kitty_colors = function(kitty_data, ignore_error, no_window_id) local match = no_window_id and nil or '--match=id:' .. kitty_data.window_id local ok, result = system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '@', 'get-colors', match, @@ -351,7 +355,7 @@ end M.send_text_to_clipboard = function(text) return system_handle_error({ - 'kitty', + p.kitty_data.kitty_path, '+kitten', 'clipboard', '/dev/stdin', diff --git a/lua/kitty-scrollback/launch.lua b/lua/kitty-scrollback/launch.lua index c1a911ad..3e0503d3 100644 --- a/lua/kitty-scrollback/launch.lua +++ b/lua/kitty-scrollback/launch.lua @@ -45,6 +45,7 @@ local M = {} ---@field kitty_opts KsbKittyOpts relevant kitty configuration values ---@field kitty_config_dir string kitty configuration directory path ---@field kitty_version table kitty version +---@field kitty_path string kitty executable path ---@class KsbPrivate ---@field orig_columns number diff --git a/python/kitty_scrollback_nvim.py b/python/kitty_scrollback_nvim.py index c486d3cf..34d30eb9 100755 --- a/python/kitty_scrollback_nvim.py +++ b/python/kitty_scrollback_nvim.py @@ -8,6 +8,7 @@ import json import os import inspect +import shutil ksb_dir = os.path.dirname( os.path.dirname(os.path.abspath(inspect.getfile(lambda: None)))) @@ -30,10 +31,11 @@ def get_kitty_shell_integration(kitty_opts, w): # based on kitty source window.py -def pipe_data(w, target_window_id, config): +def pipe_data(w, target_window_id, config, kitty_path): kitty_opts = get_options() kitty_shell_integration = get_kitty_shell_integration(kitty_opts, w) return { + 'kitty_path': kitty_path, 'kitty_scrollback_config': config, 'scrolled_by': w.screen.scrolled_by, 'cursor_x': w.screen.cursor.x + 1, @@ -110,6 +112,26 @@ def parse_cwd(args): return () +def nvim_err_cmd(err_file): + return ( + 'launch', + '--copy-env', + '--type', + 'overlay', + '--title', + 'kitty-scrollback.nvim :: error', + 'nvim', + ) + parse_nvim_args() + ( + '-c', + 'set laststatus=0', + '-c', + 'set fillchars=eob:\\ ', + '-c', + 'set filetype=checkhealth', + err_file, + ) + + @result_handler(type_of_input=None, no_ui=True, has_ready_notification=False) def handle_result(args: List[str], result: str, @@ -118,28 +140,22 @@ def handle_result(args: List[str], del args[0] w = boss.window_id_map.get(target_window_id) if w is not None: + kitty_path = shutil.which('kitty') + if not kitty_path: + boss.call_remote_control( + w, + nvim_err_cmd(f'{ksb_dir}/scripts/kitty_not_found.txt')) + return + config = parse_config(args) if config == 'crying cat --config-file': - err_cmd = ( - 'launch', - '--copy-env', - '--type', - 'overlay', - '--title', - 'kitty-scrollback.nvim', - 'nvim', - ) + parse_nvim_args() + ( - '-c', - 'set laststatus=0', - '-c', - 'set fillchars=eob:\\ ', - '-c', - 'set filetype=checkhealth', - f'{ksb_dir}/scripts/breaking_change_config_file.txt', - ) - - err_winid = boss.call_remote_control(w, err_cmd) + err_winid = boss.call_remote_control( + w, + nvim_err_cmd( + f'{ksb_dir}/scripts/breaking_change_config_file.txt')) + + # window logo is overridden by new neovim colorscheme set_logo_cmd = ('set-window-logo', '--no-response', '--alpha', @@ -149,12 +165,12 @@ def handle_result(args: List[str], f'{ksb_dir}/media/sad_kitty_thumbs_up.png') err_win = boss.window_id_map.get(err_winid) - err_winid = boss.call_remote_control(err_win, set_logo_cmd) + boss.call_remote_control(err_win, set_logo_cmd) return cwd = parse_cwd(args) env = parse_env(args) - kitty_data_str = pipe_data(w, target_window_id, config) + kitty_data_str = pipe_data(w, target_window_id, config, kitty_path) kitty_data = json.dumps(kitty_data_str) if w.title.startswith('kitty-scrollback.nvim'): diff --git a/scripts/kitty_not_found.txt b/scripts/kitty_not_found.txt new file mode 100644 index 00000000..9133e4af --- /dev/null +++ b/scripts/kitty_not_found.txt @@ -0,0 +1,18 @@ +404 kitty not found ~ + +ERROR Sorry 😿 + +Failed to find kitty executable. Please check your environment variable `PATH`. + + + `|`\___/`|` + =) `^`Y`^` (= + \ *^* / If you have any issues or questions using *kitty-scrollback.nvim* then + ` )=*=( ` please create an issue at + / \ https://github.com/mikesmithgh/kitty-scrollback.nvim/issues + | | + /| | | |\ + \| | `|`_`|`/\ + /_// ___/ + \_) + diff --git a/tests/kitty-scrollback/kitty_scrollback_spec.lua b/tests/kitty-scrollback/kitty_scrollback_spec.lua index c47f1d32..3a386039 100644 --- a/tests/kitty-scrollback/kitty_scrollback_spec.lua +++ b/tests/kitty-scrollback/kitty_scrollback_spec.lua @@ -66,6 +66,21 @@ describe('kitty-scrollback.nvim', function() $ echo meow 󰄛 󰣐  meow $ +]], + 'kitty-scrollback.nvim content did not match the terminal screen' + ) + end) + + -- during brew search a, the PATH env changes. if we are not pointing to the correct kitty executable, it will error out + it('should use correct kitty path during brew command', function() + h.assert_screen_equals( + h.feed_kitty({ + [[brew search a]], + [[\n]], -- enter + [[__open_ksb]], + }), + [[ +$ brew search a 󰄛 󰣐  ]], 'kitty-scrollback.nvim content did not match the terminal screen' )