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

More graceful handling of low linewrap values #1219

Merged
merged 7 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
16 changes: 16 additions & 0 deletions features/format.feature
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,22 @@ Feature: Custom formats
| basic_folder |
| basic_dayone |



Scenario Outline: Export fancy with small linewrap
Given we use the config "<config>.yaml"
And we use the password "test" if prompted
When we run "jrnl --config-override linewrap 35 --format fancy -3"
Then we should get no error
And the output should be 35 columns wide

Examples: configs
| config |
| basic_onefile |
| basic_encrypted |
| basic_folder |
| basic_dayone |

@todo
Scenario Outline: Exporting fancy
# Needs better emoji support
Expand Down
8 changes: 8 additions & 0 deletions features/steps/export_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
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")
def check_output_json(context):
out = context.stdout_capture.getvalue()
Expand Down
10 changes: 9 additions & 1 deletion jrnl/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ def _get_error_message(self, **kwargs):

Removing this file will allow jrnl to save its configuration.
"""
)
),
"LineWrapTooSmallForDateFormat": textwrap.dedent(
"""
The provided linewrap value of {config_linewrap} is too small by {columns} columns
to display the timestamps in the configured time format for journal {journal}.

You can avoid this error by specifying a linewrap value that is larger by at least {columns} in the configuration file or by using --config-override at the command line
"""
),
}

return error_messages[self.error_type].format(**kwargs)
Expand Down
21 changes: 20 additions & 1 deletion jrnl/plugins/fancy_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html

from jrnl.exception import JrnlError
from textwrap import TextWrapper

from .text_exporter import TextExporter
Expand All @@ -14,12 +15,14 @@ class FancyExporter(TextExporter):
names = ["fancy", "boxed"]
extension = "txt"

# Top border of the card
border_a = "┎"
border_b = "─"
border_c = "╮"
border_d = "╘"
border_e = "═"
border_f = "╕"

border_g = "┃"
border_h = "│"
border_i = "┠"
Expand All @@ -33,16 +36,19 @@ def export_entry(cls, entry):
"""Returns a fancy unicode representation of a single entry."""
date_str = entry.date.strftime(entry.journal.config["timeformat"])
linewrap = entry.journal.config["linewrap"] or 78
initial_linewrap = linewrap - len(date_str) - 2
initial_linewrap = max((1, linewrap - len(date_str) - 2))
micahellison marked this conversation as resolved.
Show resolved Hide resolved
body_linewrap = linewrap - 2
card = [
cls.border_a + cls.border_b * (initial_linewrap) + cls.border_c + date_str
]
check_provided_linewrap_viability(linewrap, card, entry.journal)

w = TextWrapper(
width=initial_linewrap,
initial_indent=cls.border_g + " ",
subsequent_indent=cls.border_g + " ",
)

title_lines = w.wrap(entry.title)
card.append(
title_lines[0].ljust(initial_linewrap + 1)
Expand Down Expand Up @@ -74,3 +80,16 @@ def export_entry(cls, entry):
def export_journal(cls, journal):
"""Returns a unicode representation of an entire journal."""
return "\n".join(cls.export_entry(entry) for entry in journal)


def check_provided_linewrap_viability(linewrap, card, journal):
if len(card[0]) > linewrap:
width_violation = len(card[0]) - linewrap
raise JrnlError(
"LineWrapTooSmallForDateFormat",
config_linewrap=linewrap,
columns=width_violation,
journal=journal,
)
else:
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved
pass
39 changes: 39 additions & 0 deletions tests/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from jrnl.exception import JrnlError
from jrnl.plugins.fancy_exporter import check_provided_linewrap_viability

import pytest


@pytest.fixture()
def datestr():

yield "2020-10-20 16:59"


from textwrap import TextWrapper
sriniv27 marked this conversation as resolved.
Show resolved Hide resolved


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):

journal = "test_journal"
content = build_card_header(datestr)

total_linewrap = 12

with pytest.raises(JrnlError) as e:
check_provided_linewrap_viability(total_linewrap, [content], journal)
assert e.value.error_type == "LineWrapTooSmallForDateFormat"