Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add tmux support #151

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions lua/kitty-scrollback/kitty_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,17 @@ M.get_text_term = function(kitty_data, get_text_opts, on_exit_cb)
esc,
esc
)
local flush_stdout_cmd = p.kitty_data.kitty_path .. [[ +runpy 'sys.stdout.flush()']]
local flush_stdout_cmd =
string.format([[%s +runpy 'sys.stdout.flush()']], p.kitty_data.kitty_path)
-- 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
.. ' | '
.. sed_cmd
-- TODO: find scenario where I needed sed and possibly remove?
-- - reproduced on v1.0.0 but can't repro on this with: bat --no-pager ~/.bashrc; printf "before \x1b[1;1H after\n"
-- - may not need, but need to write tests first
.. ' && '
.. flush_stdout_cmd
.. ' && '
.. start_set_title_cmd
local base_cmd
if kitty_data.mode == 'tmux' then
base_cmd = 'tmux capture-pane -e -p -S - -E -' .. ' | ' .. sed_cmd
else
base_cmd = kitty_get_text_cmd .. ' | ' .. sed_cmd .. ' && ' .. flush_stdout_cmd
end
local full_cmd = base_cmd .. ' && ' .. start_set_title_cmd
local stdout
local stderr
local tail_max = 10
Expand Down
22 changes: 14 additions & 8 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ local M = {}
---@field kitty_config_dir string kitty configuration directory path
---@field kitty_version table kitty version
---@field kitty_path string kitty executable path
---@field mode string 'tmux' for running in tmux mode

---@class KsbPrivate
---@field orig_columns number
Expand Down Expand Up @@ -297,6 +298,9 @@ M.setup = function(kitty_data_str)
end, { ['repeat'] = 80 }) -- 2 seconds

p.kitty_data = vim.fn.json_decode(kitty_data_str)
if os.getenv('TMUX') ~= nil then
p.kitty_data.mode = 'tmux'
end
load_requires() -- must be after p.kitty_data initialized

-- if a config named 'global' is found, that will be applied to all configurations regardless of prefix
Expand Down Expand Up @@ -335,7 +339,7 @@ M.setup = function(kitty_data_str)
ksb_kitty_cmds.signal_term_to_kitty_child_process(true)
end
end
if not ksb_health.check_kitty_version(true) then
if p.kitty_data.mode ~= 'tmux' and not ksb_health.check_kitty_version(true) then
local prompt_msg = 'kitty-scrollback.nvim: Fatal error, on version kitty '
.. table.concat(p.kitty_data.kitty_version, '.')
.. '. '
Expand All @@ -357,12 +361,14 @@ M.setup = function(kitty_data_str)
ksb_api.setup(p, opts)
ksb_keymaps.setup(p, opts)

local ok = ksb_hl.setup(p, opts)
if ok then
ksb_hl.set_highlights()
ksb_kitty_cmds.open_kitty_loading_window(ksb_hl.get_highlights_as_env()) -- must be after opts and set highlights
if ksb_hl.has_default_or_vim_colorscheme() then
vim.api.nvim_set_hl(0, 'Normal', p.orig_normal_hl)
if p.kitty_data.mode ~= 'tmux' then
local ok = ksb_hl.setup(p, opts)
if ok then
ksb_hl.set_highlights()
ksb_kitty_cmds.open_kitty_loading_window(ksb_hl.get_highlights_as_env()) -- must be after opts and set highlights
if ksb_hl.has_default_or_vim_colorscheme() then
vim.api.nvim_set_hl(0, 'Normal', p.orig_normal_hl)
end
end
end

Expand Down Expand Up @@ -421,7 +427,7 @@ M.launch = function()
-- see https://github.com/neovim/neovim/pull/23503
vim.o.columns = p.orig_columns

ksb_kitty_cmds.signal_winchanged_to_kitty_child_process()
-- ksb_kitty_cmds.signal_winchanged_to_kitty_child_process()
if opts.kitty_get_text.extent == 'screen' or opts.kitty_get_text.extent == 'all' then
set_cursor_position(kitty_data)
end
Expand Down
82 changes: 82 additions & 0 deletions python/launch_tmux_scrollback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3

import json
import os
import inspect
import subprocess
import shlex

ksb_dir = os.path.dirname(
os.path.dirname(os.path.abspath(inspect.getfile(lambda: None))))


# Compare to the pipe_data fn in `kitty_scrollback_nvim.py`
def pipe_data(target_window_id, window_title, pane_height, pane_width):
return {
'scrolled_by': 0,
'cursor_x': int(tmux_opt("cursor_x")) + 1,
'cursor_y': int(tmux_opt("cursor_y")) + 1,
'lines': pane_height,
'columns': pane_width,
'window_id': int(target_window_id),
'window_title': window_title,
'ksb_dir': ksb_dir,
}


def tmux_opt(option):
output = subprocess.run([
"tmux",
"display-message",
"-p",
f"#{{{option}}}"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode("utf-8")
return output.strip()


def main():
target_window_id = tmux_opt("window_index")
window_title = tmux_opt("pane_title")
pane_height = int(tmux_opt("pane_height"))
pane_width = int(tmux_opt(("pane_width")))
kitty_data_str = pipe_data(target_window_id, window_title, pane_height, pane_width)
kitty_data = json.dumps(kitty_data_str)

if window_title.startswith('kitty-scrollback.nvim'):
print(
f'[Warning] kitty-scrollback.nvim: skipping action, window {target_window_id} has title that '
'starts with "kitty-scrollback.nvim"')
print(json.dumps(kitty_data_str, indent=2))
return

nvim_args = (
'nvim',
'--clean',
'--noplugin',
'-n',
'--cmd',
' lua'
' vim.api.nvim_create_autocmd([[VimEnter]], {'
' group = vim.api.nvim_create_augroup([[KittyScrollBackNvimVimEnter]], { clear = true }),'
' pattern = [[*]],'
' callback = function()'
f' vim.opt.runtimepath:append([[{ksb_dir}]])'
' vim.api.nvim_exec_autocmds([[User]], { pattern = [[KittyScrollbackLaunch]], modeline = false })'
f' require([[kitty-scrollback.launch]]).setup_and_launch([[{kitty_data}]])'
' end,'
' })')

subprocess.run([
'tmux',
'display-popup',
'-E',
'-B',
'-x', 'P',
'-y', 'P',
'-w', str(pane_width),
'-h', str(pane_height),
shlex.join(nvim_args)
])


main()