diff --git a/kittens/ask/__init__.py b/kittens/ask/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/kittens/ask/main.py b/kittens/ask/main.py new file mode 100644 index 000000000..9bd1c5939 --- /dev/null +++ b/kittens/ask/main.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2018, Kovid Goyal + +import json +import readline +import sys +from gettext import gettext as _ + +from kitty.cli import parse_args + +from ..tui.operations import alternate_screen, styled + + +def option_text(): + return '''\ +--type -t +choices=line +default=line +Type of input. Defaults to asking for a line of text. + + +--message -m +The message to display to the user. If not specified a default +message is shown. +''' + + +def main(args=sys.argv): + msg = 'Ask the user for input' + try: + args, items = parse_args(args[1:], option_text, '', msg, 'kitty ask') + except SystemExit as e: + print(e.args[0], file=sys.stderr) + input('Press enter to quit...') + return 1 + + with alternate_screen(): + if args.message: + print(styled(args.message), bold=True) + + readline.read_init_file() + + prompt = ': ' + if args.type == 'line': + prompt = _('Enter line: ') + try: + ans = input(prompt) + except (KeyboardInterrupt, EOFError): + return + print('OK:', json.dumps(ans)) diff --git a/kittens/tui/operations.py b/kittens/tui/operations.py index 1fcda719b..caf6cb528 100644 --- a/kittens/tui/operations.py +++ b/kittens/tui/operations.py @@ -2,6 +2,7 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2018, Kovid Goyal +import sys from contextlib import contextmanager from kitty.terminfo import string_capabilities @@ -151,3 +152,11 @@ def cursor(write): write(SAVE_CURSOR) yield write(RESTORE_CURSOR) + + +@contextmanager +def alternate_screen(f=None): + f = f or sys.stdout + print(set_mode('ALTERNATE_SCREEN'), end='', file=f) + yield + print(reset_mode('ALTERNATE_SCREEN'), end='', file=f) diff --git a/kittens/url_hints/main.py b/kittens/url_hints/main.py index fd9cdf7db..7027e244a 100644 --- a/kittens/url_hints/main.py +++ b/kittens/url_hints/main.py @@ -231,7 +231,7 @@ expression instead. --url-prefixes default={0} -Comma separeted list of recognized URL prefixes. Defaults to: +Comma separated list of recognized URL prefixes. Defaults to: {0} '''.format, ','.join(sorted(URL_PREFIXES)))