Skip to content

Commit

Permalink
chore: read kitty executable from path
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmithgh committed Dec 7, 2023
1 parent f22f493 commit 9290a4a
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lua/kitty-scrollback/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions lua/kitty-scrollback/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
Expand Down Expand Up @@ -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 '))
Expand Down
2 changes: 1 addition & 1 deletion lua/kitty-scrollback/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
26 changes: 15 additions & 11 deletions lua/kitty-scrollback/kitty_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -256,7 +260,7 @@ end

M.list_kitty_windows = function()
return system_handle_error({
'kitty',
p.kitty_data.kitty_path,
'@',
'ls',
})
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand Down
1 change: 1 addition & 0 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 38 additions & 22 deletions python/kitty_scrollback_nvim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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'):
Expand Down
18 changes: 18 additions & 0 deletions scripts/kitty_not_found.txt
Original file line number Diff line number Diff line change
@@ -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
| |
/| | | |\
\| | `|`_`|`/\
/_// ___/
\_)

15 changes: 15 additions & 0 deletions tests/kitty-scrollback/kitty_scrollback_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down

0 comments on commit 9290a4a

Please sign in to comment.