Add a few more special command line arguments for launch

Now all ``KITTY_PIPE_DATA`` is also available via command line argument substitution
Fixes #3593
This commit is contained in:
Kovid Goyal 2021-05-09 07:45:53 +05:30
parent 63d76ee837
commit 9a6c2aa1ea
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 67 additions and 17 deletions

View File

@ -18,6 +18,10 @@ To update |kitty|, :doc:`follow the instructions <binary>`.
- macOS: When the Apple Color Emoji font lacks an emoji glyph search for it in other
installed fonts (:iss:`3591`)
- Add a few more special commandline arguments for the launch command. Now all
``KITTY_PIPE_DATA`` is also available via command line argument substitution
(:iss:`3593`)
0.20.3 [2021-05-06]
----------------------

View File

@ -54,9 +54,34 @@ Special arguments
-------------------
There are a few special placeholder arguments that can be specified as part of
the command line. Namely ``@selection`` which is replaced by the current
selection and ``@active-kitty-window-id`` which is replaced by the id of the
currently active kitty window. For example::
the command line:
``@selection``
replaced by the currently selected text
``@active-kitty-window-id``
replaced by the id of the currently active kitty window
``@line-count``
replaced by the number of lines in STDIN. Only present when passing some
data to STDIN
``@input-line-number``
replaced the number of lines a pager should scroll to match the current
scroll position in kitty. See :opt:`scrollback_pager` for details
``@scrolled-by``
replaced by the number of lines kitty is currently scrolled by
``@cursor-x``
replaced by the current cursor x position
``@cursor-y``
replaced by the current cursor y position
For example::
map f1 launch my-program @active-kitty-window-id

View File

@ -1160,7 +1160,10 @@ class Boss:
prev_tab = previous_tab
def process_stdin_source(self, window: Optional[Window] = None, stdin: Optional[str] = None) -> Tuple[Optional[Dict[str, str]], Optional[bytes]]:
def process_stdin_source(
self, window: Optional[Window] = None,
stdin: Optional[str] = None, copy_pipe_data: Optional[Dict] = None
) -> Tuple[Optional[Dict[str, str]], Optional[bytes]]:
w = window or self.active_window
if not w:
return None, None
@ -1174,6 +1177,8 @@ class Boss:
if stdin is not None:
pipe_data = w.pipe_data(stdin, has_wrap_markers=add_wrap_markers) if w else None
if pipe_data:
if copy_pipe_data is not None:
copy_pipe_data.update(pipe_data)
env = {
'KITTY_PIPE_DATA':
'{scrolled_by}:{cursor_x},{cursor_y}:{lines},{columns}'.format(**pipe_data)

View File

@ -285,6 +285,20 @@ def launch(
kw['location'] = opts.location
if opts.copy_colors and active:
kw['copy_colors_from'] = active
pipe_data: Dict[str, Any] = {}
if opts.stdin_source != 'none':
q = str(opts.stdin_source)
if opts.stdin_add_formatting:
if q in ('@screen', '@screen_scrollback', '@alternate', '@alternate_scrollback'):
q = '@ansi_' + q[1:]
if opts.stdin_add_line_wrap_markers:
q += '_wrap'
penv, stdin = boss.process_stdin_source(window=active, stdin=q, copy_pipe_data=pipe_data)
if stdin:
kw['stdin'] = stdin
if penv:
env.update(penv)
cmd = args or None
if opts.copy_cmdline and active_child:
cmd = active_child.foreground_cmdline
@ -298,6 +312,21 @@ def launch(
x = s
elif x == '@active-kitty-window-id':
x = str(active.id)
elif x == '@input-line-number':
if 'input_line_number' in pipe_data:
x = str(pipe_data['input_line_number'])
elif x == '@line-count':
if 'lines' in pipe_data:
x = str(pipe_data['lines'])
elif x in ('@cursor-x', '@cursor-y', '@scrolled-by'):
if active is not None:
screen = active.screen
if x == '@scrolled-by':
x = str(screen.scrolled_by)
elif x == '@cursor-x':
x = str(screen.cursor.x)
elif x == '@cursor-y':
x = str(screen.cursor.y)
final_cmd.append(x)
exe = find_exe(final_cmd[0])
if not exe:
@ -310,19 +339,6 @@ def launch(
kw['cmd'] = final_cmd
if opts.type == 'overlay' and active:
kw['overlay_for'] = active.id
if opts.stdin_source != 'none':
q = str(opts.stdin_source)
if opts.stdin_add_formatting:
if q in ('@screen', '@screen_scrollback', '@alternate', '@alternate_scrollback'):
q = '@ansi_' + q[1:]
if opts.stdin_add_line_wrap_markers:
q += '_wrap'
penv, stdin = boss.process_stdin_source(window=active, stdin=q)
if stdin:
kw['stdin'] = stdin
if penv:
env.update(penv)
if opts.type == 'background':
cmd = kw['cmd']
if not cmd: