diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 461014fe..546c34e2 100644 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -33,21 +33,29 @@ class Entry: sep="\n" if self.body.rstrip("\n ") else "", body=self.body.rstrip("\n ") ) + + 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): + def pprint(self, short=False, plain=False): """Returns a pretty-printed version of the entry. If short is true, only print the title.""" date_str = self.date.strftime(self.journal.config['timeformat']) if not short and self.journal.config['linewrap']: title = textwrap.fill(date_str + " " + self.title, self.journal.config['linewrap']) - body = "\n".join([ - textwrap.fill((line + " ") if (len(line) == 0) else line, - self.journal.config['linewrap'], - initial_indent="| ", - subsequent_indent="| ", - drop_whitespace=False) - for line in self.body.rstrip(" \n").splitlines() - ]) + if plain: + body = self.body.rstrip("\n ") + else: + body = "\n".join([ + textwrap.fill((line + " ") if (len(line) == 0) else line, + self.journal.config['linewrap'], + initial_indent="| ", + subsequent_indent="| ", + drop_whitespace=False) + for line in self.body.rstrip(" \n").splitlines() + ]) else: title = date_str + " " + self.title.rstrip("\n ") body = self.body.rstrip("\n ") diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 35bdc395..c4cbd336 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -158,11 +158,11 @@ class Journal(object): def __unicode__(self): return self.pprint() - def pprint(self, short=False): + def pprint(self, short=False, plain=False): """Prettyprints the journal's entries""" sep = "\n" - pp = sep.join([e.pprint(short=short) for e in self.entries]) - if self.config['highlight']: # highlight tags + pp = sep.join([e.pprint(short=short, plain=plain) for e in self.entries]) + if self.config['highlight'] and not plain: # highlight tags if self.search_tags: for tag in self.search_tags: tagre = re.compile(re.escape(tag), re.IGNORECASE) @@ -199,7 +199,7 @@ class Journal(object): if 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. 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 if (not tags or tagged(entry.tags)) and (not starred or entry.starred) + and (not shortmatch or entry.short() == shortmatch) and (not start_date or entry.date > start_date) and (not end_date or entry.date < end_date) ] diff --git a/jrnl/cli.py b/jrnl/cli.py index 1428a535..6e234ab7 100644 --- a/jrnl/cli.py +++ b/jrnl/cli.py @@ -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.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('-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('-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) 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('--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('--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) @@ -54,10 +56,10 @@ def guess_mode(args, config): """Guesses the mode (compose, read or export) from the given arguments""" compose = True 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 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? compose = False 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 journal.filter(tags=args.text, start_date=args.start_date, end_date=args.end_date, + shortmatch=args.shortmatch, strict=args.strict, short=args.short, starred=args.starred) @@ -215,8 +218,8 @@ def run(manual_args=None): print(util.py2encode(journal.pprint())) # Various export modes - elif args.short: - print(util.py2encode(journal.pprint(short=True))) + elif args.short or args.plain: + print(util.py2encode(journal.pprint(short=args.short, plain=args.plain))) elif args.tags: print(util.py2encode(exporters.to_tag_list(journal)))