hints kitten: Allow passing extra CLI args when using --customize-procesing

This commit is contained in:
Kovid Goyal
2019-11-13 17:27:55 +05:30
parent 4ade6587e8
commit 432d0f3318
2 changed files with 12 additions and 11 deletions

View File

@@ -38,9 +38,10 @@ contents:
import re import re
def mark(text, args, Mark, *a): def mark(text, args, Mark, extra_cli_args, *a):
# This function is responsible for finding all # This function is responsible for finding all
# matching text. # matching text. extra_cli_args are any extra arguments
# passed on the command line when invoking the kitten.
# We mark all individual word for potential selection # We mark all individual word for potential selection
for idx, m in enumerate(re.finditer(r'\w+', text)): for idx, m in enumerate(re.finditer(r'\w+', text)):
start, end = m.span() start, end = m.span()
@@ -50,7 +51,7 @@ contents:
yield Mark(idx, start, end, mark_text, {}) yield Mark(idx, start, end, mark_text, {})
def handle_result(args, data, target_window_id, boss): def handle_result(args, data, target_window_id, boss, extra_cli_args, *a):
# This function is responsible for performing some # This function is responsible for performing some
# action on the selected text. # action on the selected text.
# matches is a list of the selected entries and groupdicts contains # matches is a list of the selected entries and groupdicts contains

View File

@@ -257,7 +257,7 @@ def mark(pattern, post_processors, text, args):
yield Mark(idx, s, e, mark_text, groupdict) yield Mark(idx, s, e, mark_text, groupdict)
def run_loop(args, text, all_marks, index_map): def run_loop(args, text, all_marks, index_map, extra_cli_args=()):
loop = Loop() loop = Loop()
handler = Hints(text, all_marks, index_map, args) handler = Hints(text, all_marks, index_map, args)
loop.loop(handler) loop.loop(handler)
@@ -265,7 +265,7 @@ def run_loop(args, text, all_marks, index_map):
return { return {
'match': handler.text_matches, 'programs': args.program, 'match': handler.text_matches, 'programs': args.program,
'multiple_joiner': args.multiple_joiner, 'customize_processing': args.customize_processing, 'multiple_joiner': args.multiple_joiner, 'customize_processing': args.customize_processing,
'type': args.type, 'groupdicts': handler.groupdicts 'type': args.type, 'groupdicts': handler.groupdicts, 'extra_cli_args': extra_cli_args
} }
raise SystemExit(loop.return_code) raise SystemExit(loop.return_code)
@@ -330,14 +330,14 @@ def load_custom_processor(customize_processing):
return runpy.run_path(custom_path, run_name='__main__') return runpy.run_path(custom_path, run_name='__main__')
def run(args, text): def run(args, text, extra_cli_args=()):
try: try:
text = parse_input(text) text = parse_input(text)
pattern, post_processors = functions_for(args) pattern, post_processors = functions_for(args)
if args.customize_processing: if args.customize_processing:
m = load_custom_processor(args.customize_processing) m = load_custom_processor(args.customize_processing)
if 'mark' in m: if 'mark' in m:
all_marks = tuple(m['mark'](text, args, Mark)) all_marks = tuple(m['mark'](text, args, Mark, extra_cli_args))
else: else:
all_marks = tuple(mark(pattern, post_processors, text, args)) all_marks = tuple(mark(pattern, post_processors, text, args))
else: else:
@@ -359,7 +359,7 @@ def run(args, text):
input('Press Enter to quit.') input('Press Enter to quit.')
raise SystemExit(1) raise SystemExit(1)
return run_loop(args, text, all_marks, index_map) return run_loop(args, text, all_marks, index_map, extra_cli_args)
# CLI {{{ # CLI {{{
@@ -480,17 +480,17 @@ def main(args):
print(e.args[0], file=sys.stderr) print(e.args[0], file=sys.stderr)
input(_('Press Enter to quit')) input(_('Press Enter to quit'))
return return
if items: if items and not args.customize_processing:
print('Extra command line arguments present: {}'.format(' '.join(items)), file=sys.stderr) print('Extra command line arguments present: {}'.format(' '.join(items)), file=sys.stderr)
input(_('Press Enter to quit')) input(_('Press Enter to quit'))
return run(args, text) return run(args, text, items)
def handle_result(args, data, target_window_id, boss): def handle_result(args, data, target_window_id, boss):
if data['customize_processing']: if data['customize_processing']:
m = load_custom_processor(data['customize_processing']) m = load_custom_processor(data['customize_processing'])
if 'handle_result' in m: if 'handle_result' in m:
return m['handle_result'](args, data, target_window_id, boss) return m['handle_result'](args, data, target_window_id, boss, data['extra_cli_args'])
programs = data['programs'] or ('default',) programs = data['programs'] or ('default',)
matches, groupdicts = [], [] matches, groupdicts = [], []