Basic ask kitten

This commit is contained in:
Kovid Goyal 2018-03-23 18:03:43 +05:30
parent dc6ef63ddd
commit 3790af6897
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 61 additions and 1 deletions

0
kittens/ask/__init__.py Normal file
View File

51
kittens/ask/main.py Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
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))

View File

@ -2,6 +2,7 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
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)

View File

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