kittens should specify their own required input type

It is DRYer. Also replace the run_kitten function with just kitten that
takes no type of input argument. Backward compat for run_kitten is
maintained.
This commit is contained in:
Kovid Goyal 2018-05-18 22:55:42 +05:30
parent 26df57a1c7
commit 2a713cab60
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 30 additions and 17 deletions

View File

@ -325,6 +325,9 @@ def handle_result(args, data, target_window_id, boss):
boss.open_url(data['match'], None if program == 'default' else program, cwd=cwd)
handle_result.type_of_input = 'text'
if __name__ == '__main__':
# Run with kitty +kitten hints
ans = main(sys.argv)

View File

@ -39,7 +39,9 @@ def create_kitten_handler(kitten, orig_args):
from kitty.constants import config_dir
kitten = resolved_kitten(kitten)
m = import_kitten_main_module(config_dir, kitten)
return partial(m['end'], [kitten] + orig_args)
ans = partial(m['end'], [kitten] + orig_args)
ans.type_of_input = getattr(m['end'], 'type_of_input', None)
return ans
def set_debug(kitten):

View File

@ -517,35 +517,41 @@ class Boss:
output += str(s.linebuf.line(i))
return output
def _run_kitten(self, kitten, args=(), type_of_input='none'):
def _run_kitten(self, kitten, args=()):
w = self.active_window
tab = self.active_tab
if w is not None and tab is not None and w.overlay_for is None:
orig_args, args = list(args), list(args)
from kittens.runner import create_kitten_handler
end_kitten = create_kitten_handler(kitten, orig_args)
args[0:0] = [config_dir, kitten]
type_of_input = end_kitten.type_of_input
if type_of_input in ('text', 'history', 'ansi', 'ansi-history'):
data = w.as_text(as_ansi='ansi' in type_of_input, add_history='history' in type_of_input).encode('utf-8')
elif type_of_input == 'none':
elif type_of_input is None:
data = None
else:
raise ValueError('Unknown type_of_input: {}'.format(type_of_input))
from kittens.runner import create_kitten_handler
end_kitten = create_kitten_handler(kitten, orig_args)
copts = {k: self.opts[k] for k in ('select_by_word_characters', 'open_url_with')}
overlay_window = tab.new_special_window(
SpecialWindow(
['kitty', '+runpy', 'from kittens.runner import main; main()'] + args,
stdin=data,
env={'KITTY_COMMON_OPTS': json.dumps(copts), 'PYTHONWARNINGS': 'ignore'},
env={
'KITTY_COMMON_OPTS': json.dumps(copts),
'PYTHONWARNINGS': 'ignore',
'OVERLAID_WINDOW_LINES': str(w.screen.lines),
'OVERLAID_WINDOW_COLS': str(w.screen.columns),
},
overlay_for=w.id))
overlay_window.action_on_close = partial(self.on_kitten_finish, w.id, end_kitten)
return overlay_window
def run_kitten(self, type_of_input, kitten, *args):
def kitten(self, kitten, *args):
import shlex
cmdline = args[0] if args else ''
args = shlex.split(cmdline) if cmdline else []
self._run_kitten(kitten, args, type_of_input)
self._run_kitten(kitten, args)
def on_kitten_finish(self, target_window_id, end_kitten, source_window):
output = self.get_output(source_window, num_lines=None)

View File

@ -116,10 +116,12 @@ def parse_key_action(action):
except Exception:
log_error('Ignoring invalid send_text string: ' + args[1])
args[1] = ''
elif func in ('run_kitten', 'run_simple_kitten'):
if func == 'run_simple_kitten':
func = 'run_kitten'
args = rest.split(' ', 2)
elif func in ('run_kitten', 'run_simple_kitten', 'kitten'):
if func == 'kitten':
args = rest.split(' ', 1)
else:
args = rest.split(' ', 2)[1:]
func = 'kitten'
elif func == 'goto_tab':
args = (max(0, int(rest)), )
elif func == 'goto_layout' or func == 'kitty_shell':

View File

@ -468,21 +468,21 @@ map kitty_mod+backspace restore_font_size
#
# Open a currently visible URL using the keyboard. The program used to open the
# URL is specified in open_url_with.
map kitty_mod+e run_kitten text hints
map kitty_mod+e kitten hints
# Select a path/filename and insert it into the terminal. Useful, for instance to
# run git commands on a filename output from a previous git command.
map kitty_mod+p>f run_kitten text hints --type path --program -
map kitty_mod+p>f kitten hints --type path --program -
# Select a path/filename and open it with the default open program.
map kitty_mod+p>shift+f run_kitten text hints --type path
map kitty_mod+p>shift+f kitten hints --type path
# Select a line of text and insert it into the terminal. Use for the
# output of things like: ls -1
map kitty_mod+p>l run_kitten text hints --type line --program -
map kitty_mod+p>l kitten hints --type line --program -
# Select words and insert into terminal.
map kitty_mod+p>w run_kitten text hints --type word --program -
map kitty_mod+p>w kitten hints --type word --program -
# The hints kitten has many more modes of operation that you can map to different
# shortcuts. For a full description run: kitty +kitten hints --help