Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmithgh committed Oct 30, 2023
1 parent ca4d730 commit 0f2de0a
Show file tree
Hide file tree
Showing 12 changed files with 1,013 additions and 29 deletions.
61 changes: 51 additions & 10 deletions lua/kitty-scrollback/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,26 @@ M.toggle_footer = function()
end
end

M.generate_kittens = function(all)
M.generate_kittens = function(all, generate_modes)
generate_modes = (generate_modes and next(generate_modes)) and generate_modes or { 'maps' }
local target_gen_modes = {}
for _, gen_mode in pairs(generate_modes) do
target_gen_modes[gen_mode] = true
end

local kitty_scrollback_kitten =
vim.api.nvim_get_runtime_file('python/kitty_scrollback_nvim.py', false)[1]
local example_path =
vim.api.nvim_get_runtime_file('lua/kitty-scrollback/configs/example.lua', false)[1]

local action_alias = 'kitty_scrollback_nvim'
local builtin_configs = {
local alias_config = {
'# kitty-scrollback.nvim Kitten alias',
'action_alias ' .. action_alias .. ' kitten ' .. kitty_scrollback_kitten,
'',
}

local builtin_map_configs = {
'# Browse scrollback buffer in nvim',
'map ctrl+shift+h ' .. action_alias,
'# Browse output of the last shell command in nvim',
Expand Down Expand Up @@ -114,24 +123,56 @@ M.generate_kittens = function(all)
[[# Example kitty-scrollback.nvim nvim overrides]],
[[--no-nvim-args --env NVIM_APPNAME=ksb-nvim]],
[[--nvim-args +'colorscheme tokyonight']],
[[--nvim-args +'lua vim.defer_fn(function() vim.api.nvim_set_option_value("filetype", "markdown", { buf = 0 }); vim.cmd("silent! CellularAutomaton make_it_rain") end, 1000)']],
[[--nvim-args +'lua vim.defer_fn(function() vim.api.nvim_set_option_value("filetype", "markdown", { buf = 0 }); vim.cmd("silent! CellularAutomaton make_it_rain") end, 6000)']],
})

local kitten_configs = vim.list_extend(
vim.list_extend(vim.tbl_extend('force', builtin_configs, {}), example_configs),
local kitten_map_configs = vim.list_extend(
vim.list_extend(vim.tbl_extend('force', builtin_map_configs, {}), example_configs),
nvim_args
)

local builtin_command_configs = vim.tbl_map(function(config)
return config:gsub(
'^.*map%s%S+.*kitty_scrollback_nvim',
'kitty @ kitten ' .. kitty_scrollback_kitten
)
end, builtin_map_configs)

local kitten_command_configs = vim.tbl_map(function(config)
return config:gsub(
'^.*map%s%S+.*kitty_scrollback_nvim',
'kitty @ kitten ' .. kitty_scrollback_kitten
)
end, kitten_map_configs)

local configs = {}

if all then
if target_gen_modes['maps'] then
vim.list_extend(configs, alias_config)
vim.list_extend(configs, kitten_map_configs)
table.insert(configs, '')
end
if target_gen_modes['commands'] then
vim.list_extend(configs, kitten_command_configs)
end
else
if target_gen_modes['maps'] then
vim.list_extend(configs, alias_config)
vim.list_extend(configs, builtin_map_configs)
table.insert(configs, '')
end
if target_gen_modes['commands'] then
vim.list_extend(configs, builtin_command_configs)
end
end

local bufid = vim.api.nvim_create_buf(true, true)
vim.api.nvim_set_option_value('filetype', 'kitty', {
buf = bufid,
})
vim.api.nvim_set_current_buf(bufid)
if all then
vim.api.nvim_buf_set_lines(bufid, 0, -1, false, kitten_configs)
else
vim.api.nvim_buf_set_lines(bufid, 0, -1, false, builtin_configs)
end
vim.api.nvim_buf_set_lines(bufid, 0, -1, false, configs)
end

M.checkhealth = function()
Expand Down
61 changes: 45 additions & 16 deletions lua/kitty-scrollback/configs/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ local prefix = 'ksb_example_'

return {
configs = {
--- Example configuration invoking callbacks with delays and prints timestamps
--- Includes after_setup, after_launch, and after_ready callback functions
--- and prints kitty_data and options that are passed to after_ready function
--- This example is not practical and only for demonstration purposes
---@return KsbCallbacks
[prefix .. 'callbacks'] = function()
local msg = {}
return {
Expand All @@ -24,6 +29,7 @@ return {
end,
after_ready = function(kitty_data, opts)
vim.defer_fn(function()
vim.cmd.startinsert()
table.insert(
msg,
'# kitty-scrollback after_ready callback triggered @ ' .. vim.fn.strftime('%c')
Expand All @@ -32,17 +38,8 @@ return {
table.insert(msg, '# ' .. vim.fn.json_encode(kitty_data))
table.insert(msg, '# opts:')
table.insert(msg, '# ' .. vim.fn.json_encode(vim.inspect(opts)))
local curbuf = vim.api.nvim_get_current_buf()
vim.cmd.startinsert()
vim.fn.timer_start(250, function(t) ---@diagnostic disable-line: redundant-parameter
if curbuf ~= vim.api.nvim_get_current_buf() then
vim.fn.timer_stop(t)
vim.api.nvim_buf_set_lines(0, 0, -1, false, msg)
vim.cmd.stopinsert()
end
end, {
['repeat'] = 12,
})
vim.api.nvim_buf_set_lines(0, 0, -1, false, msg)
vim.cmd.stopinsert()
end, 3000)
end,
},
Expand Down Expand Up @@ -173,13 +170,13 @@ return {
bg = '#000099',
},
KittyScrollbackNvimPasteWinNormal = {
link = 'Pmenu',
link = 'IncSearch',
},
KittyScrollbackNvimPasteWinFloatBorder = {
link = 'Pmenu',
link = 'IncSearch',
},
KittyScrollbackNvimPasteWinFloatTitle = {
link = 'Title',
link = 'IncSearch',
},
},
}
Expand All @@ -206,7 +203,37 @@ return {
[prefix .. 'paste_win_filetype'] = function()
return {
paste_window = {
filetype = 'fish',
filetype = 'markdown',
},
callbacks = {
after_ready = vim.schedule_wrap(function()
local msg = {
'',
'\t',
'',
'# kitty-scrollback.nvim example',
'',
'## Change paste window filetype to `markdown`',
'',
'```lua',
'paste_window = {',
' filetype = "markdown", -- change this to your desired filetype',
'},',
'```',
}
local curbuf = vim.api.nvim_get_current_buf()
vim.cmd.startinsert()
vim.fn.timer_start(250, function(t) ---@diagnostic disable-line: redundant-parameter
if curbuf ~= vim.api.nvim_get_current_buf() then
vim.fn.timer_stop(t)
vim.api.nvim_buf_set_lines(0, 0, -1, false, msg)
vim.cmd.stopinsert()
vim.fn.setcursorcharpos(2, 4)
end
end, {
['repeat'] = 12,
})
end),
},
}
end,
Expand Down Expand Up @@ -243,7 +270,7 @@ return {
[prefix .. 'paste_win_winblend'] = function()
return {
paste_window = {
winblend = 10,
winblend = 50,
},
}
end,
Expand Down Expand Up @@ -272,6 +299,8 @@ return {
}
end,
[prefix .. 'restore_opts'] = function()
vim.o.termguicolors = true
vim.o.number = true
return {
restore_options = true,
}
Expand Down
1 change: 1 addition & 0 deletions lua/kitty-scrollback/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ M.advice = function()
'',
'Current *shell_integration* options:',
[[` KITTY_SHELL_INTEGRATION=']] .. shell_integration .. [['`]],
[[` KITTY_SHELL_INTEGRATION=']] .. (vim.env.KITTY_SHELL_INTEGRATION or 'nil') .. [['`]],
'',
'See https://sw.kovidgoyal.net/kitty/conf/#opt-kitty.shell_integration for more information',
'on *shell_integration*',
Expand Down
2 changes: 1 addition & 1 deletion lua/kitty-scrollback/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ local function highlight_definitions()
},
KittyScrollbackNvimVim = {
default = true,
fg = '#188b25',
fg = '#8faa80',
},
KittyScrollbackNvimPasteWinNormal = {
default = true,
Expand Down
6 changes: 5 additions & 1 deletion lua/kitty-scrollback/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ M.setup = function(configs)
--- |kitty.api.generate_kittens|
---@brief ]]
vim.api.nvim_create_user_command('KittyScrollbackGenerateKittens', function(o)
require('kitty-scrollback.api').generate_kittens(o.bang)
require('kitty-scrollback.api').generate_kittens(o.bang, o.fargs)
end, {
bang = true,
nargs = '*',
complete = function()
return { 'maps', 'commands' }
end,
})

---@brief [[
Expand Down
1 change: 1 addition & 0 deletions lua/kitty-scrollback/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ M.show_status_window = function()
if opts.status_window.enabled then
local kitty_icon = '󰄛'
local love_icon = ''
-- local vim_icon = ''
local vim_icon = ''
local width = 9
if opts.status_window.style_simple then
Expand Down
33 changes: 33 additions & 0 deletions python/kitty_background_process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import List
from typing import Iterable
from kitty.boss import Boss
from kitty.fast_data_types import get_options
from kitty.debug_config import debug_config
from kittens.tui.handler import result_handler
import re
import os
import inspect

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


def main():
raise SystemExit('Must be run as kitten kitty_debug_config')


@result_handler(type_of_input=None, no_ui=True, has_ready_notification=False)
def handle_result(args: List[str],
result: str,
target_window_id: int,
boss: Boss) -> None:
del args[0]
# boss.run_background_process(['touch', 'yip'], '/Users/mike/tmp')
kitten_args = [
'--no-nvim-args',
'--config',
'ksb_example_get_text_last_non_empty_output_plain'
]
boss.run_kitten_with_metadata(
'/Users/mike/gitrepos/kitty-scrollback.nvim/python/kitty_scrollback_nvim.py',
kitten_args)
17 changes: 17 additions & 0 deletions python/kitty_mouse_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List
from kitty.boss import Boss
from kittens.tui.handler import result_handler


def main():
raise SystemExit('Must be run as kitten kitty_scroll_prompt')


@result_handler(type_of_input=None, no_ui=True, has_ready_notification=False)
def handle_result(args: List[str],
result: str,
target_window_id: int,
boss: Boss) -> None:
w = boss.window_id_map.get(target_window_id)
if w is not None:
print(w.child.environ) # this prints an empty dictionary
35 changes: 35 additions & 0 deletions python/kitty_scroll_prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import List
from kitty.boss import Boss
from kittens.tui.handler import result_handler
from kitty.key_encoding import CTRL, SHIFT
from kitty.fast_data_types import PRESS, send_mouse_event
from kitty.fast_data_types import click_mouse_cmd_output, move_cursor_to_mouse_if_in_prompt


def main():
raise SystemExit('Must be run as kitten kitty_scroll_prompt')


@result_handler(type_of_input=None, no_ui=True, has_ready_notification=False)
def handle_result(args: List[str],
result: str,
target_window_id: int,
boss: Boss) -> None:
del args[0]
direction = -1 # default to one previous
if len(args) > 0:
direction = int(args[0])
select_cmd_output = False
if len(args) > 1:
select_cmd_output = (args[1].lower() == 'true')
w = boss.window_id_map.get(target_window_id)
if w is not None:
if direction == 0:
click_mouse_cmd_output(w.os_window_id,
w.tab_id,
w.id,
select_cmd_output)
w.mouse_handle_click('prompt')
else:
w.scroll_to_prompt(direction)
w.mouse_handle_click('prompt')
15 changes: 14 additions & 1 deletion python/kitty_scrollback_nvim.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@ def main():
raise SystemExit('Must be run as kitten kitty_scrollback_nvim')


def get_kitty_shell_integration(kitty_opts, w):
# KITTY_SHELL_INTEGRATION env var takes precedence over config
# kitty v0.30.1 is returning empty window envs so fallback on opts
# see https://github.com/kovidgoyal/kitty/issues/6749
shell_integration_opts = kitty_opts.shell_integration or frozenset(
{'enabled'})
shell_integration = w.child.environ.get(
'KITTY_SHELL_INTEGRATION',
' '.join(list(shell_integration_opts)))
return shell_integration.split()


# based on kitty source window.py
def pipe_data(w, target_window_id, ksb_dir, config):
kitty_opts = get_options()
kitty_shell_integration = get_kitty_shell_integration(kitty_opts, w)
return {
'kitty_scrollback_config': config,
'scrolled_by': w.screen.scrolled_by,
Expand All @@ -32,7 +45,7 @@ def pipe_data(w, target_window_id, ksb_dir, config):
'ksb_dir': ksb_dir,
'kitty_opts': {
"shell_integration":
w.child.environ.get('KITTY_SHELL_INTEGRATION', 'disabled').split(), # env takes precedence over config
kitty_shell_integration,
"scrollback_fill_enlarged_window":
kitty_opts.scrollback_fill_enlarged_window,
"scrollback_lines":
Expand Down
Loading

0 comments on commit 0f2de0a

Please sign in to comment.