mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-27 18:51:41 +02:00
Get rid of argparse from kitty-icat as well
This commit is contained in:
33
kitty/cli.py
33
kitty/cli.py
@@ -157,6 +157,7 @@ def parse_option_spec(spec=OPTIONS):
|
|||||||
seq = []
|
seq = []
|
||||||
disabled = []
|
disabled = []
|
||||||
mpat = re.compile('([a-z]+)=(.+)')
|
mpat = re.compile('([a-z]+)=(.+)')
|
||||||
|
current_cmd = None
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
@@ -250,7 +251,7 @@ def wrap(text, limit=80):
|
|||||||
return reversed(lines)
|
return reversed(lines)
|
||||||
|
|
||||||
|
|
||||||
def print_help_for_seq(seq):
|
def print_help_for_seq(seq, usage, message, appname):
|
||||||
from kitty.icat import screen_size
|
from kitty.icat import screen_size
|
||||||
try:
|
try:
|
||||||
linesz = min(screen_size().cols, 76)
|
linesz = min(screen_size().cols, 76)
|
||||||
@@ -265,11 +266,14 @@ def print_help_for_seq(seq):
|
|||||||
j = '\n' + (' ' * indent)
|
j = '\n' + (' ' * indent)
|
||||||
a((' ' * leading_indent) + j.join(wrap(text, limit=linesz - indent)))
|
a((' ' * leading_indent) + j.join(wrap(text, limit=linesz - indent)))
|
||||||
|
|
||||||
a('{}: {} [options] [program-to-run ...]'.format(title('Usage'), bold(yellow(appname))))
|
usage = usage or '[program-to-run ...]'
|
||||||
|
a('{}: {} [options] {}'.format(title('Usage'), bold(yellow(appname)), usage))
|
||||||
a('')
|
a('')
|
||||||
wa('Run the {appname} terminal emulator. You can also specify the {program} to run inside {appname} as normal'
|
message = message or (
|
||||||
' arguments following the {options}. For example: {appname} /bin/sh'.format(
|
'Run the |_ {appname}| terminal emulator. You can also specify the |_ program| to run inside |_ {appname}| as normal'
|
||||||
appname=italic(appname), options=italic('options'), program=italic('program')))
|
' arguments following the |_ options|. For example: {appname} /bin/sh'
|
||||||
|
).format(appname=appname)
|
||||||
|
wa(prettify(message))
|
||||||
a('')
|
a('')
|
||||||
a('{}:'.format(title('Options')))
|
a('{}:'.format(title('Options')))
|
||||||
for opt in seq:
|
for opt in seq:
|
||||||
@@ -283,7 +287,7 @@ def print_help_for_seq(seq):
|
|||||||
|
|
||||||
text = '\n'.join(blocks) + '\n\n' + version()
|
text = '\n'.join(blocks) + '\n\n' + version()
|
||||||
if sys.stdout.isatty():
|
if sys.stdout.isatty():
|
||||||
p = subprocess.Popen(['less', '-isR'], stdin=subprocess.PIPE)
|
p = subprocess.Popen(['less', '-isRXF'], stdin=subprocess.PIPE)
|
||||||
p.communicate(text.encode('utf-8'))
|
p.communicate(text.encode('utf-8'))
|
||||||
raise SystemExit(p.wait())
|
raise SystemExit(p.wait())
|
||||||
else:
|
else:
|
||||||
@@ -305,11 +309,12 @@ def defval_for_opt(opt):
|
|||||||
|
|
||||||
class Options:
|
class Options:
|
||||||
|
|
||||||
def __init__(self, seq):
|
def __init__(self, seq, usage, message, appname):
|
||||||
self.alias_map = {}
|
self.alias_map = {}
|
||||||
self.seq = seq
|
self.seq = seq
|
||||||
self.names_map = {}
|
self.names_map = {}
|
||||||
self.values_map = {}
|
self.values_map = {}
|
||||||
|
self.usage, self.message, self.appname = usage, message, appname
|
||||||
for opt in seq:
|
for opt in seq:
|
||||||
if isinstance(opt, str):
|
if isinstance(opt, str):
|
||||||
continue
|
continue
|
||||||
@@ -327,7 +332,7 @@ class Options:
|
|||||||
|
|
||||||
def needs_arg(self, alias):
|
def needs_arg(self, alias):
|
||||||
if alias in ('-h', '--help'):
|
if alias in ('-h', '--help'):
|
||||||
print_help_for_seq(self.seq)
|
print_help_for_seq(self.seq, self.usage, self.message, self.appname or appname)
|
||||||
raise SystemExit(0)
|
raise SystemExit(0)
|
||||||
opt = self.opt_for_alias(alias)
|
opt = self.opt_for_alias(alias)
|
||||||
if opt['dest'] == 'version':
|
if opt['dest'] == 'version':
|
||||||
@@ -364,14 +369,12 @@ class Namespace:
|
|||||||
setattr(self, name, kwargs[name])
|
setattr(self, name, kwargs[name])
|
||||||
|
|
||||||
|
|
||||||
def parse_cmdline(options, args=None):
|
def parse_cmdline(oc, disabled, args=None):
|
||||||
NORMAL, EXPECTING_ARG = 'NORMAL', 'EXPECTING_ARG'
|
NORMAL, EXPECTING_ARG = 'NORMAL', 'EXPECTING_ARG'
|
||||||
state = NORMAL
|
state = NORMAL
|
||||||
if args is None:
|
if args is None:
|
||||||
args = sys.argv[1:]
|
args = sys.argv[1:]
|
||||||
args = deque(args)
|
args = deque(args)
|
||||||
seq, disabled = options
|
|
||||||
oc = Options(seq)
|
|
||||||
current_option = None
|
current_option = None
|
||||||
|
|
||||||
while args:
|
while args:
|
||||||
@@ -416,9 +419,11 @@ def options_spec():
|
|||||||
return options_spec.ans
|
return options_spec.ans
|
||||||
|
|
||||||
|
|
||||||
def parse_args(args=None):
|
def parse_args(args=None, ospec=options_spec, usage=None, message=None, appname=None):
|
||||||
options = parse_option_spec(options_spec())
|
options = parse_option_spec(ospec())
|
||||||
return parse_cmdline(options, args=args)
|
seq, disabled = options
|
||||||
|
oc = Options(seq, usage, message, appname)
|
||||||
|
return parse_cmdline(oc, disabled, args=args)
|
||||||
|
|
||||||
|
|
||||||
def create_opts(args):
|
def create_opts(args):
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
# vim:fileencoding=utf-8
|
# vim:fileencoding=utf-8
|
||||||
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
import argparse
|
|
||||||
import codecs
|
import codecs
|
||||||
import fcntl
|
import fcntl
|
||||||
import mimetypes
|
import mimetypes
|
||||||
@@ -18,15 +17,12 @@ import tty
|
|||||||
import zlib
|
import zlib
|
||||||
from base64 import standard_b64encode
|
from base64 import standard_b64encode
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from gettext import gettext as _
|
|
||||||
from math import ceil, floor
|
from math import ceil, floor
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
from time import monotonic
|
from time import monotonic
|
||||||
|
|
||||||
try:
|
from kitty.constants import appname
|
||||||
from kitty.constants import appname
|
from kitty.cli import parse_args
|
||||||
except ImportError:
|
|
||||||
appname = ''
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fsenc = sys.getfilesystemencoding() or 'utf-8'
|
fsenc = sys.getfilesystemencoding() or 'utf-8'
|
||||||
@@ -44,20 +40,16 @@ class OpenFailed(ValueError):
|
|||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
|
|
||||||
def option_parser():
|
OPTIONS = '''\
|
||||||
parser = argparse.ArgumentParser(
|
'''
|
||||||
prog=appname + '-icat' if appname else 'icat',
|
|
||||||
description=_('Display images in the terminal')
|
|
||||||
)
|
def options_spec():
|
||||||
a = parser.add_argument
|
if not hasattr(options_spec, 'ans'):
|
||||||
a(
|
options_spec.ans = OPTIONS.format(
|
||||||
'items',
|
appname='{}-icat'.format(appname),
|
||||||
nargs='+',
|
|
||||||
help=_(
|
|
||||||
'Image files or directories. Directories are scanned recursively.'
|
|
||||||
)
|
)
|
||||||
)
|
return options_spec.ans
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
Size = namedtuple('Size', 'rows cols width height')
|
Size = namedtuple('Size', 'rows cols width height')
|
||||||
@@ -251,13 +243,17 @@ def main(args=sys.argv):
|
|||||||
raise SystemExit(
|
raise SystemExit(
|
||||||
'Terminal does not support reporting screen sizes via the TIOCGWINSZ ioctl'
|
'Terminal does not support reporting screen sizes via the TIOCGWINSZ ioctl'
|
||||||
)
|
)
|
||||||
args = option_parser().parse_args(args[1:])
|
msg = (
|
||||||
if not args.items:
|
'A cat like utility to display images in the terminal.'
|
||||||
|
' You can specify multiple images files and/or directories.'
|
||||||
|
' Directories are scanned recursively for image files.')
|
||||||
|
args, items = parse_args(args[1:], options_spec, 'image-file ...', msg, '{} icat'.format(appname))
|
||||||
|
if not items:
|
||||||
raise SystemExit('You must specify at least one file to cat')
|
raise SystemExit('You must specify at least one file to cat')
|
||||||
if not detect_support():
|
if not detect_support():
|
||||||
raise SystemExit('This terminal emulator does not support the graphics protocol, use a terminal emulator such as kitty that does support it')
|
raise SystemExit('This terminal emulator does not support the graphics protocol, use a terminal emulator such as kitty that does support it')
|
||||||
errors = []
|
errors = []
|
||||||
for item in args.items:
|
for item in items:
|
||||||
try:
|
try:
|
||||||
if os.path.isdir(item):
|
if os.path.isdir(item):
|
||||||
for x in scan(item):
|
for x in scan(item):
|
||||||
|
|||||||
Reference in New Issue
Block a user