Cleanup handling of types of kitten input

This commit is contained in:
Kovid Goyal 2021-11-15 12:28:44 +05:30
parent c96e6822e1
commit 80b5f31256
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 43 additions and 30 deletions

View File

@ -88,19 +88,41 @@ function, telling kitty what kind of input your kitten would like. For example:
This will send the plain text of the active window to the kitten's
:file:`STDIN`. For text with formatting escape codes, use ``ansi``
instead. If you want line wrap markers as well, use ``screen-ansi``
or just ``screen``. For the scrollback buffer as well, use
``history``, ``ansi-history`` or ``screen-history``. To get
the currently selected text, use ``selection``. To get the output
of the first command run in the shell on screen, use ``first-output``
or ``first-output-ansi`` or ``first-output-screen-ansi``. To get the output
of the last command run in the shell, use ``output`` or ``output-ansi``
or ``output-screen-ansi``. To get the first command output below the last
scrolled position via scroll_to_prompt, use ``last-visited-output`` or
``last-visited-output-ansi`` or ``last-visited-output-screen-ansi``. Note that
using ``first-output`` or ``output`` or ``last-visited-output`` requires
:ref:`shell_integration`.
:file:`STDIN`. There are many other types of input you can ask for,
described in the table below:
.. table:: Types of input to kittens
:align: left
=========================== =======================================================================================================
Keyword Type of :file:`STDIN` input
=========================== =======================================================================================================
``text`` Plain text of active window
``ansi`` Formatted text of active window
``screen`` Plain text of active window with line wrap markers
``screen-ansi`` Formatted text of active window with line wrap markers
``history`` Plain text of active window and its scrollback
``ansi-history`` Formatted text of active window and its scrollback
``screen-history`` Plain text of active window and its scrollback with line wrap markers
``screen-ansi-history`` Formatted text of active window and its scrollback with line wrap markers
``output`` Plain text of the output from the last run command
``output-screen`` Plain text of the output from the last run command with wrap markers
``output-ansi`` Formatted text of the output from the last run command
``output-screen-ansi`` Formatted text of the output from the last run command with wrap markers
``selection`` The text currently selected with the mouse
=========================== =======================================================================================================
In addition to ``output``, that gets the output of the last run command,
``last_visited_output`` gives the output of the command last jumped to
and ``first_output`` gives the output of the first command currently on screen.
These can also be combined with ``screen`` and ``ansi`` for formatting.
.. note::
For the types based on the output of a command,
:ref:`shell_integration` is required.
Using kittens to script kitty, without any terminal UI

View File

@ -1311,26 +1311,17 @@ class Boss:
args[0:0] = [config_dir, kitten]
if input_data is None:
type_of_input = end_kitten.type_of_input
if type_of_input in ('text', 'history', 'ansi', 'ansi-history', 'screen', 'screen-history', 'screen-ansi', 'screen-ansi-history'):
data: Optional[bytes] = w.as_text(
as_ansi='ansi' in type_of_input,
add_history='history' in type_of_input,
add_wrap_markers='screen' in type_of_input
).encode('utf-8')
q = type_of_input.split('-') if type_of_input else []
if not q:
data: Optional[bytes] = None
elif q[0] in ('text', 'history', 'ansi', 'screen'):
data = w.as_text(as_ansi='ansi' in q, add_history='history' in q, add_wrap_markers='screen' in q).encode('utf-8')
elif type_of_input == 'selection':
sel = self.data_for_at(which='@selection', window=w)
data = sel.encode('utf-8') if sel else None
elif type_of_input is None:
data = None
elif type_of_input in ('first-output', 'first-output-screen', 'first-output-screen-ansi', 'first-output-ansi'):
q = type_of_input.split('-')
data = w.first_cmd_output_on_screen(as_ansi='ansi' in q, add_wrap_markers='screen' in q).encode('utf-8')
elif type_of_input in ('output', 'output-screen', 'output-screen-ansi', 'output-ansi'):
q = type_of_input.split('-')
data = w.last_cmd_output(as_ansi='ansi' in q, add_wrap_markers='screen' in q).encode('utf-8')
elif type_of_input in ('last-visited-output', 'last-visited-output-screen', 'last-visited-output-screen-ansi', 'last-visited-output-ansi'):
q = type_of_input.split('-')
data = w.last_visited_cmd_output(as_ansi='ansi' in q, add_wrap_markers='screen' in q).encode('utf-8')
elif q[0] in ('output', 'first_output', 'last_visited_output'):
func = {'output': w.last_cmd_output, 'first_output': w.first_cmd_output_on_screen, 'last_visited_output': w.last_visited_cmd_output}[q[0]]
data = func(as_ansi='ansi' in q, add_wrap_markers='screen' in q).encode('utf-8')
else:
raise ValueError(f'Unknown type_of_input: {type_of_input}')
else: