From 4f163338dd73225904fb2bb62c7e899d7351fb7a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 2 Jun 2019 09:23:20 +0530 Subject: [PATCH] Allow controlling how multiple selections are handled in the hints kitten Fixes #1665 --- docs/changelog.rst | 9 +++++++++ kittens/hints/main.py | 42 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 51d04463f..29b4d4fa3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,15 @@ Changelog |kitty| is a feature full, cross-platform, *fast*, GPU based terminal emulator. To update |kitty|, :doc:`follow the instructions `. +0.14.2 [future] +--------------------- + +- hints kitten: Add a :option:`kitty +kitten hints --multiple-joiner` option to + control how multiple selections are serialized when copying to clipboard + or inserting into the terminal. You can have them one separate lines, + separated by arbitrary characters, or even serialized as JSON (:iss:`1665`) + + 0.14.1 [2019-05-29] --------------------- diff --git a/kittens/hints/main.py b/kittens/hints/main.py index 4b3296398..8c4d3ec5a 100644 --- a/kittens/hints/main.py +++ b/kittens/hints/main.py @@ -246,7 +246,9 @@ def run_loop(args, text, all_marks, index_map): handler = Hints(text, all_marks, index_map, args) loop.loop(handler) if handler.chosen and loop.return_code == 0: - return {'match': handler.chosen, 'program': args.program} + return {'match': handler.chosen, 'program': args.program, + 'multiple_joiner': args.multiple_joiner, + 'type': args.type} raise SystemExit(loop.return_code) @@ -374,6 +376,17 @@ Select multiple matches and perform the action on all of them together at the en In this mode, press :kbd:`Esc` to finish selecting. +--multiple-joiner +default=auto +String to use to join multiple selections when copying to the clipboard or +inserting into the terminal. The special strings: "space", "newline", "empty", +"json" and "auto" are interpreted as a space character, a newline an empty +joiner, a JSON serialized list and an automatic choice, based on the type of +text being selected. In addition, integers are interpreted as zero-based +indices into the list of selections. You can use 0 for the first selection and +-1 for the last. + + --add-trailing-space default=auto choices=auto,always,never @@ -422,13 +435,34 @@ def main(args): def handle_result(args, data, target_window_id, boss): program = data['program'] matches = tuple(filter(None, data['match'])) + joiner = data['multiple_joiner'] + try: + is_int = int(joiner) + except Exception: + is_int = None + text_type = data['type'] + + def joined_text(): + if is_int is not None: + try: + return matches[is_int] + except IndexError: + return matches[-1] + if joiner == 'json': + import json + return json.dumps(matches, ensure_ascii=False, indent='\t') + if joiner == 'auto': + q = '\n\r' if text_type in ('line', 'url') else ' ' + else: + q = {'newline': '\n\r', 'space': ' '}.get(joiner, '') + return q.join(matches) + if program == '-': w = boss.window_id_map.get(target_window_id) if w is not None: - for m in matches: - w.paste(m) + w.paste(joined_text()) elif program == '@': - set_clipboard_string(matches[-1]) + set_clipboard_string(joined_text()) else: cwd = None w = boss.window_id_map.get(target_window_id)