Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print journal entries #692

Merged
merged 43 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e7da8c1
Pretty print journal entry titles and dates.
alichtman Oct 25, 2019
1d4bbea
Removed extra newline at end of title
alichtman Oct 25, 2019
b4bfc2c
Use ansiwrap to properly wrap strings with ANSI escapes
alichtman Oct 26, 2019
75a8249
Add ansiwrap to pyproject.toml
alichtman Oct 26, 2019
01db72b
Allow configuration of colors
alichtman Nov 6, 2019
b2d3fc8
Add color configuration documentation
alichtman Nov 6, 2019
8053d25
Fix broken tests due to config change
alichtman Nov 6, 2019
eb1ed3c
Add tests for colors in configs
alichtman Nov 6, 2019
1a0847b
Add colorama dependency for all platforms
alichtman Nov 6, 2019
a23a818
Allow users to disable colorization of output
alichtman Nov 6, 2019
c63c999
Update poetry.lock
alichtman Nov 6, 2019
4b934e3
Add tag and body color customization options
alichtman Nov 6, 2019
94a2b78
Fix colorization of tags in title and body
alichtman Nov 6, 2019
6f3866c
Updated tests to use no color by default
alichtman Nov 6, 2019
6145682
Change pass to continue in verify_config()
alichtman Nov 6, 2019
baa3ffb
Better style in Entry.py
alichtman Nov 6, 2019
c3b1818
Reduce code duplication for tag highlighting
alichtman Nov 6, 2019
6830535
Properly colorize tags and print body
alichtman Nov 7, 2019
82d3c65
Reformatting and clean up
alichtman Nov 7, 2019
6ce83c7
Replace list comprehension with generator
alichtman Nov 7, 2019
89c4366
Handle invalid colors by not using a color
alichtman Nov 7, 2019
41fb8d4
Process ANSI escapes properly with behave
alichtman Nov 7, 2019
e9bfe4d
Fixed the 'spaces after tags directly next to punctuation' bug
alichtman Nov 7, 2019
dd81c7d
Closer to working tag colorization but not perfect
alichtman Nov 8, 2019
acc754e
Add tests printing for multiline journals
alichtman Nov 8, 2019
99426ce
Correctly indent first line of multiline entry
alichtman Nov 8, 2019
b6f6b29
Add test for multiline entries with tags
alichtman Nov 8, 2019
95b23d4
Remove redundant UNICODE flag
alichtman Nov 8, 2019
7747226
Progress towards proper tag colorization and body formatting
alichtman Nov 8, 2019
8d0aa98
Fix newline colorization bug
alichtman Nov 9, 2019
09c0ccc
And now the space just ends up before the tag instead of after it
alichtman Nov 9, 2019
c65c78c
Fix assertion syntax warning
alichtman Nov 9, 2019
fcc64fa
Moved tag test to tagging.feature file
alichtman Nov 9, 2019
f218374
Strip out debug code and clean up
alichtman Nov 9, 2019
139835c
Bold datetimes in title
alichtman Nov 9, 2019
ea46922
Bold all titles
alichtman Nov 9, 2019
712b471
Merge branch 'master' into pretty-print-entries
alichtman Nov 12, 2019
6ad5188
Merge branch 'master' into pretty-print-entries
alichtman Nov 12, 2019
09802cc
Remove PY2 and PY3 constants
alichtman Nov 16, 2019
8c9b040
Fix regression in features/steps/core.py
alichtman Nov 16, 2019
f2bf1ff
Fix tag_regex
alichtman Nov 16, 2019
edb0d8e
Remove redundant re.UNICODE flag
alichtman Nov 17, 2019
566ab5c
Remove extraneous code
alichtman Nov 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ and can be edited with a plain text editor.
- `highlight`
if `true`, tags will be highlighted in cyan.
- `linewrap`
controls the width of the output. Set to `false` if you don't
want to wrap long lines.
controls the width of the output. Set to `false` if you don't want to wrap long lines.
- `colors`
dictionary that controls the colors used to display journal entries. It has two subkeys, which are: `date` and `title`. Current valid values are: `BLACK`, `RED`, `GREEN`, `YELLOW`, `BLUE`, `MAGENTA`, `CYAN`, and `WHITE`. `colorama.Fore` is used for colorization, and you can find the [docs here](https://github.com/tartley/colorama#colored-output). To disable colored output, set the value to `NONE`. If you set the value of any color subkey to an invalid color, no color will be used.

!!! note
Although it seems intuitive to use the `#`
Expand Down
21 changes: 21 additions & 0 deletions features/core.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ Feature: Basic reading and writing to a journal
| But I'm better.
"""

Scenario: Printing a journal that has multiline entries
Given we use the config "multiline.yaml"
When we run "jrnl -n 1"
Then we should get no error
and the output should be
"""
2013-06-09 15:39 Multiple line entry.
| This is the first line.
| This line doesn't have any ending punctuation
|
| There is a blank line above this.
"""

Scenario: Writing an entry from command line
Given we use the config "basic.yaml"
When we run "jrnl 23 july 2013: A cold and stormy day. I ate crisps on the sofa."
Expand Down Expand Up @@ -58,3 +71,11 @@ Feature: Basic reading and writing to a journal
When we run "jrnl -on 2013-06-10 -s"
Then the output should be "2013-06-10 15:40 Life is good."

Scenario: Invalid color configuration
Given we use the config "invalid_color.yaml"
When we run "jrnl -on 2013-06-10 -s"
Then the output should be
"""
2013-06-10 15:40 Life is good.
"""
And we should get no error
5 changes: 5 additions & 0 deletions features/data/configs/basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
5 changes: 5 additions & 0 deletions features/data/configs/bug153.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ tagsymbols: '@'
template: false
timeformat: '%Y-%m-%d %H:%M'
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
17 changes: 17 additions & 0 deletions features/data/configs/invalid_color.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
default_hour: 9
default_minute: 0
editor: ""
encrypt: false
highlight: true
journals:
default: features/journals/simple.journal
linewrap: 80
tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
colors:
date: not-a-color
title: also-not-a-color
body: still-no-color
tags: me-too
5 changes: 5 additions & 0 deletions features/data/configs/markdown-headings-335.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ password: ''
tagsymbols: '@'
timeformat: '%Y-%m-%d %H:%M'
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
17 changes: 17 additions & 0 deletions features/data/configs/multiline-tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
default_hour: 9
default_minute: 0
editor: ""
encrypt: false
highlight: true
journals:
default: features/journals/multiline-tags.journal
linewrap: 80
tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
17 changes: 17 additions & 0 deletions features/data/configs/multiline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
default_hour: 9
default_minute: 0
editor: ""
encrypt: false
highlight: true
journals:
default: features/journals/multiline.journal
linewrap: 80
tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
12 changes: 12 additions & 0 deletions features/data/configs/no_colors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
default_hour: 9
default_minute: 0
editor: ""
encrypt: false
highlight: true
journals:
default: features/journals/simple.journal
linewrap: 80
tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
5 changes: 5 additions & 0 deletions features/data/configs/tags-216.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ password: ''
tagsymbols: '@'
timeformat: '%Y-%m-%d %H:%M'
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
5 changes: 5 additions & 0 deletions features/data/configs/tags-237.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ password: ''
tagsymbols: '@'
timeformat: '%Y-%m-%d %H:%M'
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
5 changes: 5 additions & 0 deletions features/data/configs/tags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ password: ''
tagsymbols: '@'
timeformat: '%Y-%m-%d %H:%M'
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
5 changes: 5 additions & 0 deletions features/data/configs/unreadabledates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ tagsymbols: "@"
template: false
timeformat: "%Y-%m-%d %H:%M"
indent_character: "|"
colors:
date: none
title: none
body: none
tags: none
7 changes: 7 additions & 0 deletions features/data/journals/multiline-tags.journal
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[2013-06-09 15:39] Multiple @line entry with @tags.
Tag with @punctuation. afterwards
@TagOnLineAloneWithOutPunctuation
@TagOnLineAloneWithPunctuation.
Text before @tag. And After.
@hi. Hello
hi Hello
5 changes: 5 additions & 0 deletions features/data/journals/multiline.journal
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[2013-06-09 15:39] Multiple line entry.
This is the first line.
This line doesn't have any ending punctuation

There is a blank line above this.
39 changes: 29 additions & 10 deletions features/steps/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from jrnl import __version__
from dateutil import parser as date_parser
from collections import defaultdict
from codecs import encode, decode
import os
import ast
import json
import yaml
import keyring
Expand Down Expand Up @@ -132,7 +134,7 @@ def has_error(context):

@then('we should get no error')
def no_error(context):
assert context.exit_status is 0, context.exit_status
assert context.exit_status == 0, context.exit_status


@then('the output should be parsable as json')
Expand Down Expand Up @@ -181,20 +183,30 @@ def check_json_output_path(context, path, value):
struct = struct[node]
assert struct == value, struct

def process_ANSI_escapes(text):
"""Escapes and 'unescapes' a string with ANSI escapes so that behave stdout
comparisons work properly. This will render colors, and works with unicode
characters. https://stackoverflow.com/a/57192592
:param str text: The text to be escaped and unescaped
:return: Colorized / escaped text
:rtype: str
"""
return decode(encode(text, 'latin-1', 'backslashreplace'), 'unicode-escape')


@then('the output should be')
@then('the output should be "{text}"')
def check_output(context, text=None):
text = (text or context.text).strip().splitlines()
out = context.stdout_capture.getvalue().strip().splitlines()
out = process_ANSI_escapes(context.stdout_capture.getvalue().strip()).splitlines()
assert len(text) == len(out), "Output has {} lines (expected: {})".format(len(out), len(text))
for line_text, line_out in zip(text, out):
assert line_text.strip() == line_out.strip(), [line_text.strip(), line_out.strip()]


@then('the output should contain "{text}" in the local time')
def check_output_time_inline(context, text):
out = context.stdout_capture.getvalue()
out = process_ANSI_escapes(context.stdout_capture.getvalue())
local_tz = tzlocal.get_localzone()
utc_time = date_parser.parse(text)
local_date = utc_time.astimezone(local_tz).strftime("%Y-%m-%d %H:%M")
Expand All @@ -205,7 +217,9 @@ def check_output_time_inline(context, text):
@then('the output should contain "{text}"')
def check_output_inline(context, text=None):
text = text or context.text
out = context.stdout_capture.getvalue()
out = process_ANSI_escapes(context.stdout_capture.getvalue())
if isinstance(out, bytes):
out = out.decode('utf-8')
alichtman marked this conversation as resolved.
Show resolved Hide resolved
assert text in out, text


Expand Down Expand Up @@ -245,12 +259,17 @@ def journal_doesnt_exist(context, journal_name="default"):
@then('the config should have "{key}" set to "{value}"')
@then('the config for journal "{journal}" should have "{key}" set to "{value}"')
def config_var(context, key, value, journal=None):
t, value = value.split(":")
value = {
"bool": lambda v: v.lower() == "true",
"int": int,
"str": str
}[t](value)
if not value[0] == "{":
t, value = value.split(":")
value = {
"bool": lambda v: v.lower() == "true",
"int": int,
"str": str
}[t](value)
else:
# Handle value being a dictionary
value = ast.literal_eval(value)

config = util.load_config(install.CONFIG_FILE_PATH)
if journal:
config = config["journals"][journal]
Expand Down
16 changes: 16 additions & 0 deletions features/tagging.feature
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,19 @@ Feature: Tagging
"""
@thought : 2
"""


Scenario: Printing a journal that has multiline entries with tags
Given we use the config "multiline-tags.yaml"
When we run "jrnl -n 1"
Then we should get no error
and the output should be
"""
2013-06-09 15:39 Multiple @line entry with @tags.
| Tag with @punctuation. afterwards
| @TagOnLineAloneWithOutPunctuation
| @TagOnLineAloneWithPunctuation.
| Text before @tag. And After.
| @hi. Hello
| hi Hello
"""
6 changes: 6 additions & 0 deletions features/upgrade.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x
Then the output should contain "Password"
and the output should contain "2013-06-10 15:40 Life is good"

Scenario: Upgrading a config without colors to colors
Given we use the config "no_colors.yaml"
When we run "jrnl -n 1"
Then the config should have "colors" set to "{'date':'none', 'title':'none', 'body':'none', 'tags':'none'}"

Scenario: Upgrade and parse journals with little endian date format
Given we use the config "upgrade_from_195_little_endian_dates.json"
When we run "jrnl -9" and enter "Y"
Expand All @@ -32,3 +37,4 @@ Feature: Upgrading Journals from 1.x.x to 2.x.x
10.06.2013 15:40 He said "[this] is the best time to be alive".
"""
Then the journal should have 2 entries

Loading