enforce positive initial linewrap

Check column widths

update gitignore

throw error when linewrap too small

simply check for large enough linewrap value
This commit is contained in:
Suhas 2021-03-04 17:28:43 -05:00
parent d3589fae60
commit 2d4ba1bf2f
6 changed files with 80 additions and 4 deletions

3
.gitignore vendored
View file

@ -61,3 +61,6 @@ coverage.xml
.coverage .coverage
.vscode/tasks.json .vscode/tasks.json
todo.txt todo.txt
tags
.vimrc

View file

@ -302,9 +302,9 @@ Feature: Custom formats
Scenario Outline: Export fancy with small linewrap Scenario Outline: Export fancy with small linewrap
Given we use the config "<config>.yaml" Given we use the config "<config>.yaml"
And we use the password "test" if prompted And we use the password "test" if prompted
When we run "jrnl --config-override linewrap 12 --format fancy -3" When we run "jrnl --config-override linewrap 35 --format fancy -3"
Then we should get no error Then we should get no error
And the output should be "12" columns wide And the output should be 35 columns wide
Examples: configs Examples: configs
| config | | config |

View file

@ -12,6 +12,14 @@ from behave import given
from behave import then from behave import then
@then("the output should be {width:d} columns wide")
def check_output_width(context, width):
out = context.stdout_capture.getvalue()
out_lines = out.splitlines()
for line in out_lines:
assert len(line) <= width
@then("the output should be parsable as json") @then("the output should be parsable as json")
def check_output_json(context): def check_output_json(context):
out = context.stdout_capture.getvalue() out = context.stdout_capture.getvalue()

View file

@ -30,7 +30,21 @@ class JrnlError(Exception):
Removing this file will allow jrnl to save its configuration. Removing this file will allow jrnl to save its configuration.
""" """
) ),
"LineWrapTooSmallForDateFormat": textwrap.dedent(
"""
The provided linewrap value of {config_linewrap} is too small
to display the timestamps in the configured time format.
"""
),
"LineWrapTooSmallForFancy": textwrap.dedent(
"""
The provided linewrap value of {config_linewrap} is too small
to properly format journal contents in fancy/boxed format.
Either provide a larger value for the linewrap or display the
journal in another format.
"""
),
} }
return error_messages[self.error_type].format(**kwargs) return error_messages[self.error_type].format(**kwargs)

View file

@ -3,6 +3,7 @@
# Copyright (C) 2012-2021 jrnl contributors # Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
from jrnl.exception import JrnlError
from textwrap import TextWrapper from textwrap import TextWrapper
from .text_exporter import TextExporter from .text_exporter import TextExporter
@ -14,12 +15,14 @@ class FancyExporter(TextExporter):
names = ["fancy", "boxed"] names = ["fancy", "boxed"]
extension = "txt" extension = "txt"
# Top border of the card
border_a = "" border_a = ""
border_b = "" border_b = ""
border_c = "" border_c = ""
border_d = "" border_d = ""
border_e = "" border_e = ""
border_f = "" border_f = ""
border_g = "" border_g = ""
border_h = "" border_h = ""
border_i = "" border_i = ""
@ -33,16 +36,18 @@ class FancyExporter(TextExporter):
"""Returns a fancy unicode representation of a single entry.""" """Returns a fancy unicode representation of a single entry."""
date_str = entry.date.strftime(entry.journal.config["timeformat"]) date_str = entry.date.strftime(entry.journal.config["timeformat"])
linewrap = entry.journal.config["linewrap"] or 78 linewrap = entry.journal.config["linewrap"] or 78
initial_linewrap = linewrap - len(date_str) - 2 initial_linewrap = max((1, linewrap - len(date_str) - 2))
body_linewrap = linewrap - 2 body_linewrap = linewrap - 2
card = [ card = [
cls.border_a + cls.border_b * (initial_linewrap) + cls.border_c + date_str cls.border_a + cls.border_b * (initial_linewrap) + cls.border_c + date_str
] ]
cls.check_linewrap(linewrap, card)
w = TextWrapper( w = TextWrapper(
width=initial_linewrap, width=initial_linewrap,
initial_indent=cls.border_g + " ", initial_indent=cls.border_g + " ",
subsequent_indent=cls.border_g + " ", subsequent_indent=cls.border_g + " ",
) )
title_lines = w.wrap(entry.title) title_lines = w.wrap(entry.title)
card.append( card.append(
title_lines[0].ljust(initial_linewrap + 1) title_lines[0].ljust(initial_linewrap + 1)
@ -70,6 +75,13 @@ class FancyExporter(TextExporter):
card.append(cls.border_l + cls.border_b * body_linewrap + cls.border_m) card.append(cls.border_l + cls.border_b * body_linewrap + cls.border_m)
return "\n".join(card) return "\n".join(card)
@classmethod
def check_linewrap(cls, linewrap, card):
if len(card[0]) > linewrap:
raise JrnlError("LineWrapTooSmallForDateFormat", config_linewrap=linewrap)
else:
pass
@classmethod @classmethod
def export_journal(cls, journal): def export_journal(cls, journal):
"""Returns a unicode representation of an entire journal.""" """Returns a unicode representation of an entire journal."""

39
tests/test_export.py Normal file
View file

@ -0,0 +1,39 @@
from jrnl.exception import JrnlError
from jrnl.plugins.fancy_exporter import FancyExporter
import pytest
@pytest.fixture()
def datestr():
yield "2020-10-20 16:59"
from textwrap import TextWrapper
def provide_date_wrapper(initial_linewrap):
wrapper = TextWrapper(
width=initial_linewrap, initial_indent=" ", subsequent_indent=" "
)
return wrapper
def build_card_header(datestr):
top_left_corner = "┎─╮"
content = top_left_corner + datestr
return content
class TestFancy:
def test_too_small_linewrap(self, datestr):
content = build_card_header(datestr)
total_linewrap = 12
with pytest.raises(JrnlError) as e:
FancyExporter.check_linewrap(total_linewrap, [content])
assert e.value.error_type == "LineWrapTooSmallForDateFormat"