Skip to content

Commit

Permalink
Enhance command mode UX
Browse files Browse the repository at this point in the history
Override help text; Add display list of formats and flags
  • Loading branch information
fakefred committed Feb 13, 2020
1 parent 736fff8 commit 47f0200
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
67 changes: 41 additions & 26 deletions memethesis/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from argparse import ArgumentParser
import sys
import re
from .fancyprint import color
from os import path
from .fancyprint import color, style
from .memethesizers import *
from .format_utils import *
from .interactive import interactive
Expand All @@ -12,30 +13,22 @@


def main():
argparser = ArgumentParser(description='All Your Memes Are Belong To Us!')
argparser = ArgumentParser(add_help=False)

''' Frantic payload of arguments. '''
argparser.add_argument(
'-i', '--interactive', action='store_true',
help='interactive mode'
)
'-h', '--help', action='store_true')
argparser.add_argument(
'-f', '--format', choices=FMT_NAMES,
help=f'the meme format to use'
)
'-l', '--list', action='store_true')
argparser.add_argument(
'-o', '--output',
help='the filename to save the meme as (default: ./meme.jpg)'
)
'-i', '--interactive', action='store_true')
argparser.add_argument(
'-p', '--preview', action='store_true',
help='display the meme without saving it, unless -o/--output is specified'
)

'-f', '--format', choices=FMT_NAMES)
argparser.add_argument(
'-o', '--output')
argparser.add_argument(
'-p', '--preview', action='store_true')
argparser.add_argument(
'-c', '--caption',
help='caption text to add above your meme'
)
'-c', '--caption')

# parse flags from formats.yml
for fk, fv in PANEL_TYPES.items():
Expand All @@ -48,10 +41,32 @@ def main():

args = vars(argparser.parse_args())

if args['interactive']:
if args['help']:
print(''.join(
open(
path.join(path.dirname(__file__), 'help.txt')
).readlines()))
sys.exit(0)
elif args['list']:
# format:
# flag[: description]
ls = ''
for fmt in FMT_NAMES:
ls += style(fmt, sty=1) + '\n'
types = PANEL_TYPES[fmt]
for ty in types:
meta = FORMATS[fmt]['panels'][ty]
ls += ' --' + ty
if 'description' in meta and meta['description']:
ls += ': ' + meta['description']
ls += '\n'
print(ls)
sys.exit(0)

elif args['interactive']:
interactive()
else:
# non-interactive (pure-cli) mode
# command mode
# validate format
if not args['format']:
print(color(
Expand Down Expand Up @@ -91,11 +106,11 @@ def main():

if args['output']:
o = args['output']
path = ((o if re.search('\.(jpe?g|png)$', o, flags=re.I)
else o + '.jpg')
if o else 'meme.jpg')
meme.save(path)
print(color(f'Meme saved to {path}.', fgc=2))
fp = ((o if re.search('\.(jpe?g|png)$', o, flags=re.I)
else o + '.jpg')
if o else 'meme.jpg')
meme.save(fp)
print(color(f'Meme saved to {fp}.', fgc=2))


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion memethesis/format_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_panel_types(fmts: dict) -> dict:
def get_panel_descriptions(fmts: dict) -> dict:
descrips = {
fk: {
pk: pv['description']
pk: pv['description'] if 'description' in pv else ''
for pk, pv in fv['panels'].items()
} for fk, fv in fmts.items()
}
Expand Down
24 changes: 24 additions & 0 deletions memethesis/help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Memethesis CLI - All Your Memes Are Belong To Us!

arguments:
-h, --help show this help message and exit
-l, --list show a list of meme formats and exit
-i, --interactive interactive mode (certain formats only)

-f, --format FORMAT the meme format to use
-c, --caption CAPTION caption text to add above your meme

-o, --output OUTPUT save the meme as (jpg/png)
-p, --preview display meme without saving it,
unless -o/--output is specified

usage:
# command mode
$ memethesis -f FORMAT [-p|-o filename] --flag0 TEXT0 --flag1 TEXT1 ...

# remember to wrap spaced strings in quotes
# example:
$ memethesis -f drake -o meme.png --dislike 'one thing' --like 'another thing'

# interactive mode
$ memethesis -i

0 comments on commit 47f0200

Please sign in to comment.