Example #1
0
def handle_result(args: List[str], data: Dict[str, Any], target_window_id: int,
                  boss: BossType) -> None:
    if data['customize_processing']:
        m = load_custom_processor(data['customize_processing'])
        if 'handle_result' in m:
            m['handle_result'](args, data, target_window_id, boss,
                               data['extra_cli_args'])
            return None

    programs = data['programs'] or ('default', )
    matches: List[str] = []
    groupdicts = []
    for m, g in zip(data['match'], data['groupdicts']):
        if m:
            matches.append(m)
            groupdicts.append(g)
    joiner = data['multiple_joiner']
    try:
        is_int: Optional[int] = int(joiner)
    except Exception:
        is_int = None
    text_type = data['type']

    @lru_cache()
    def joined_text() -> str:
        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)

    for program in programs:
        if program == '-':
            w = boss.window_id_map.get(target_window_id)
            if w is not None:
                w.paste(joined_text())
        elif program == '@':
            set_clipboard_string(joined_text())
        elif program == '*':
            set_primary_selection(joined_text())
        else:
            cwd = None
            w = boss.window_id_map.get(target_window_id)
            if w is not None:
                cwd = w.cwd_of_child
            program = None if program == 'default' else program
            for m, groupdict in zip(matches, groupdicts):
                if groupdict:
                    m = []
                    for k, v in groupdict.items():
                        m.append('{}={}'.format(k, v or ''))
                boss.open_url(m, program, cwd=cwd)
Example #2
0
def linenum_handle_result(args: List[str], data: Dict[str, Any], target_window_id: int, boss: BossType, extra_cli_args: Sequence[str], *a: Any) -> None:
    path, line = linenum_process_result(data)
    if not path:
        return

    cmd = [x.format(path=path, line=line) for x in extra_cli_args or ('vim', '+{line}', '{path}')]
    w = boss.window_id_map.get(target_window_id)
    action = data['linenum_action']

    if action == 'self':
        if w is not None:
            is_copy_action = cmd[0] in ('-', '@', '*')
            if is_copy_action:
                text = ' '.join(cmd[1:])
                if cmd[0] == '-':
                    w.paste_bytes(text)
                elif cmd[0] == '@':
                    set_clipboard_string(text)
                elif cmd[0] == '*':
                    set_primary_selection(text)
            else:
                import shlex
                text = ' '.join(shlex.quote(arg) for arg in cmd)
                w.paste_bytes(f'{text}\r')
    elif action == 'background':
        import subprocess
        subprocess.Popen(cmd, cwd=data['cwd'])
    else:
        getattr(boss, {
            'window': 'new_window_with_cwd', 'tab': 'new_tab_with_cwd', 'os_window': 'new_os_window_with_cwd'
            }[action])(*cmd)
Example #3
0
File: main.py Project: TJohnW/kitty
def handle_result(args, data, target_window_id, boss):
    program = data['program']
    if program == '-':
        w = boss.window_id_map.get(target_window_id)
        if w is not None:
            w.paste(data['match'])
    elif program == '@':
        set_clipboard_string(data['match'])
    else:
        boss.open_url(data['match'], None if program == 'default' else program)
Example #4
0
def handle_result(args, data, target_window_id, boss, extra_cli_args, *a):
    # This function is responsible for performing some
    # action on the selected text.
    # matches is a list of the selected entries and groupdicts contains
    # the arbitrary data associated with each entry in mark() above
    matches, groupdicts = [], []
    for m, g in zip(data['match'], data['groupdicts']):
        if m:
            matches.append(m), groupdicts.append(g)
    for word, match_data in zip(matches, groupdicts):
        # Lookup the word in a dictionary, the open_url function
        # will open the provided url in the system browser
        # boss.open_url(f'https://www.google.com/search?q=define:{word}')
        # set_primary_selection(word)
        set_clipboard_string(word)
Example #5
0
def handle_result(args, data, target_window_id, boss):
    program = data['program']
    matches = tuple(filter(None, data['match']))
    if program == '-':
        w = boss.window_id_map.get(target_window_id)
        if w is not None:
            for m in matches:
                w.paste(m)
    elif program == '@':
        set_clipboard_string(matches[-1])
    else:
        cwd = None
        w = boss.window_id_map.get(target_window_id)
        if w is not None:
            cwd = w.cwd_of_child
        program = None if program == 'default' else program
        for m in matches:
            boss.open_url(m, program, cwd=cwd)
Example #6
0
File: main.py Project: toonn/kitty
def handle_result(args, data, target_window_id, boss):
    programs = data['programs'] or ('default', )
    matches = tuple(filter(None, data['match']))
    joiner = data['multiple_joiner']
    try:
        is_int = int(joiner)
    except Exception:
        is_int = None
    text_type = data['type']

    @lru_cache()
    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)

    for program in programs:
        if program == '-':
            w = boss.window_id_map.get(target_window_id)
            if w is not None:
                w.paste(joined_text())
        elif program == '@':
            set_clipboard_string(joined_text())
        else:
            cwd = None
            w = boss.window_id_map.get(target_window_id)
            if w is not None:
                cwd = w.cwd_of_child
            program = None if program == 'default' else program
            for m in matches:
                boss.open_url(m, program, cwd=cwd)
Example #7
0
def handle_result(args: List[str], result: 'ResultDict',
                  target_window_id: WindowId, boss: Boss) -> None:
    if 'copy' in result:
        set_clipboard_string(result['copy'])