Added plain and shortmatch options.

This commit is contained in:
vietjtnguyen 2014-07-04 02:31:01 -07:00
parent 8314c85463
commit a25ea22e70
3 changed files with 29 additions and 17 deletions

View file

@ -34,20 +34,28 @@ class Entry:
body=self.body.rstrip("\n ") body=self.body.rstrip("\n ")
) )
def pprint(self, short=False): def short(self):
"""Returns the short version of the entry."""
date_str = self.date.strftime(self.journal.config['timeformat'])
return date_str + " " + self.title
def pprint(self, short=False, plain=False):
"""Returns a pretty-printed version of the entry. """Returns a pretty-printed version of the entry.
If short is true, only print the title.""" If short is true, only print the title."""
date_str = self.date.strftime(self.journal.config['timeformat']) date_str = self.date.strftime(self.journal.config['timeformat'])
if not short and self.journal.config['linewrap']: if not short and self.journal.config['linewrap']:
title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap']) title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap'])
body = "\n".join([ if plain:
textwrap.fill((line + " ") if (len(line) == 0) else line, body = self.body.rstrip("\n ")
self.journal.config['linewrap'], else:
initial_indent="| ", body = "\n".join([
subsequent_indent="| ", textwrap.fill((line + " ") if (len(line) == 0) else line,
drop_whitespace=False) self.journal.config['linewrap'],
for line in self.body.rstrip(" \n").splitlines() initial_indent="| ",
]) subsequent_indent="| ",
drop_whitespace=False)
for line in self.body.rstrip(" \n").splitlines()
])
else: else:
title = date_str + " " + self.title.rstrip("\n ") title = date_str + " " + self.title.rstrip("\n ")
body = self.body.rstrip("\n ") body = self.body.rstrip("\n ")

View file

@ -158,11 +158,11 @@ class Journal(object):
def __unicode__(self): def __unicode__(self):
return self.pprint() return self.pprint()
def pprint(self, short=False): def pprint(self, short=False, plain=False):
"""Prettyprints the journal's entries""" """Prettyprints the journal's entries"""
sep = "\n" sep = "\n"
pp = sep.join([e.pprint(short=short) for e in self.entries]) pp = sep.join([e.pprint(short=short, plain=plain) for e in self.entries])
if self.config['highlight']: # highlight tags if self.config['highlight'] and not plain: # highlight tags
if self.search_tags: if self.search_tags:
for tag in self.search_tags: for tag in self.search_tags:
tagre = re.compile(re.escape(tag), re.IGNORECASE) tagre = re.compile(re.escape(tag), re.IGNORECASE)
@ -199,7 +199,7 @@ class Journal(object):
if n: if n:
self.entries = self.entries[-n:] self.entries = self.entries[-n:]
def filter(self, tags=[], start_date=None, end_date=None, starred=False, strict=False, short=False): def filter(self, tags=[], start_date=None, end_date=None, shortmatch=None, starred=False, strict=False, short=False):
"""Removes all entries from the journal that don't match the filter. """Removes all entries from the journal that don't match the filter.
tags is a list of tags, each being a string that starts with one of the tags is a list of tags, each being a string that starts with one of the
@ -220,6 +220,7 @@ class Journal(object):
entry for entry in self.entries entry for entry in self.entries
if (not tags or tagged(entry.tags)) if (not tags or tagged(entry.tags))
and (not starred or entry.starred) and (not starred or entry.starred)
and (not shortmatch or entry.short() == shortmatch)
and (not start_date or entry.date > start_date) and (not start_date or entry.date > start_date)
and (not end_date or entry.date < end_date) and (not end_date or entry.date < end_date)
] ]

View file

@ -34,12 +34,14 @@ def parse_args(args=None):
reading = parser.add_argument_group('Reading', 'Specifying either of these parameters will display posts of your journal') reading = parser.add_argument_group('Reading', 'Specifying either of these parameters will display posts of your journal')
reading.add_argument('-from', dest='start_date', metavar="DATE", help='View entries after this date') reading.add_argument('-from', dest='start_date', metavar="DATE", help='View entries after this date')
reading.add_argument('-until', '-to', dest='end_date', metavar="DATE", help='View entries before this date') reading.add_argument('-until', '-to', dest='end_date', metavar="DATE", help='View entries before this date')
reading.add_argument('-shortmatch', dest='shortmatch', metavar="SHORT", help='View entries with this exact short format')
reading.add_argument('-and', dest='strict', action="store_true", help='Filter by tags using AND (default: OR)') reading.add_argument('-and', dest='strict', action="store_true", help='Filter by tags using AND (default: OR)')
reading.add_argument('-starred', dest='starred', action="store_true", help='Show only starred entries') reading.add_argument('-starred', dest='starred', action="store_true", help='Show only starred entries')
reading.add_argument('-n', dest='limit', default=None, metavar="N", help="Shows the last n entries matching the filter. '-n 3' and '-3' have the same effect.", nargs="?", type=int) reading.add_argument('-n', dest='limit', default=None, metavar="N", help="Shows the last n entries matching the filter. '-n 3' and '-3' have the same effect.", nargs="?", type=int)
exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal') exporting = parser.add_argument_group('Export / Import', 'Options for transmogrifying your journal')
exporting.add_argument('--short', dest='short', action="store_true", help='Show only titles or line containing the search tags') exporting.add_argument('--short', dest='short', action="store_true", help='Show only titles or line containing the search tags')
exporting.add_argument('--plain', dest='plain', action="store_true", help='Print without highlighting or text wrapping')
exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurences') exporting.add_argument('--tags', dest='tags', action="store_true", help='Returns a list of all tags and number of occurences')
exporting.add_argument('--export', metavar='TYPE', dest='export', help='Export your journal. Options: json, text, markdown', default=False, const=None) exporting.add_argument('--export', metavar='TYPE', dest='export', help='Export your journal. Options: json, text, markdown', default=False, const=None)
exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='The output of the file can be provided when using with --export', default=False, const=None) exporting.add_argument('-o', metavar='OUTPUT', dest='output', help='The output of the file can be provided when using with --export', default=False, const=None)
@ -54,10 +56,10 @@ def guess_mode(args, config):
"""Guesses the mode (compose, read or export) from the given arguments""" """Guesses the mode (compose, read or export) from the given arguments"""
compose = True compose = True
export = False export = False
if args.decrypt is not False or args.encrypt is not False or args.export is not False or any((args.short, args.tags, args.edit)): if args.decrypt is not False or args.encrypt is not False or args.export is not False or any((args.short, args.plain, args.tags, args.edit)):
compose = False compose = False
export = True export = True
elif any((args.start_date, args.end_date, args.limit, args.strict, args.starred)): elif any((args.start_date, args.end_date, args.shortmatch, args.limit, args.strict, args.starred)):
# Any sign of displaying stuff? # Any sign of displaying stuff?
compose = False compose = False
elif args.text and all(word[0] in config['tagsymbols'] for word in u" ".join(args.text).split()): elif args.text and all(word[0] in config['tagsymbols'] for word in u" ".join(args.text).split()):
@ -205,6 +207,7 @@ def run(manual_args=None):
old_entries = journal.entries old_entries = journal.entries
journal.filter(tags=args.text, journal.filter(tags=args.text,
start_date=args.start_date, end_date=args.end_date, start_date=args.start_date, end_date=args.end_date,
shortmatch=args.shortmatch,
strict=args.strict, strict=args.strict,
short=args.short, short=args.short,
starred=args.starred) starred=args.starred)
@ -215,8 +218,8 @@ def run(manual_args=None):
print(util.py2encode(journal.pprint())) print(util.py2encode(journal.pprint()))
# Various export modes # Various export modes
elif args.short: elif args.short or args.plain:
print(util.py2encode(journal.pprint(short=True))) print(util.py2encode(journal.pprint(short=args.short, plain=args.plain)))
elif args.tags: elif args.tags:
print(util.py2encode(exporters.to_tag_list(journal))) print(util.py2encode(exporters.to_tag_list(journal)))