mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-06-29 14:06:14 +02:00
Add linewrap option 'auto' (#1507)
* Add linewrap option 'auto' * Specify the exception thrown * Add BDD test * Specify name instead of number * Create test for linewrap auto and fancy format * Fix linewrap auto for fancy format
This commit is contained in:
parent
252c63f4dd
commit
80bfff384e
5 changed files with 66 additions and 7 deletions
|
@ -2,12 +2,14 @@
|
|||
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
import ansiwrap
|
||||
|
||||
from jrnl.color import colorize
|
||||
from jrnl.color import highlight_tags_with_background_color
|
||||
from .color import colorize
|
||||
from .color import highlight_tags_with_background_color
|
||||
|
||||
|
||||
class Entry:
|
||||
|
@ -104,6 +106,18 @@ class Entry:
|
|||
)
|
||||
|
||||
if not short and self.journal.config["linewrap"]:
|
||||
columns = self.journal.config["linewrap"]
|
||||
|
||||
if columns == "auto":
|
||||
try:
|
||||
columns = os.get_terminal_size().columns
|
||||
except OSError:
|
||||
logging.debug(
|
||||
"Can't determine terminal size automatically 'linewrap': '%s'",
|
||||
self.journal.config["linewrap"],
|
||||
)
|
||||
columns = 79
|
||||
|
||||
# Color date / title and bold title
|
||||
title = ansiwrap.fill(
|
||||
date_str
|
||||
|
@ -114,7 +128,7 @@ class Entry:
|
|||
self.journal.config["colors"]["title"],
|
||||
is_title=True,
|
||||
),
|
||||
self.journal.config["linewrap"],
|
||||
columns,
|
||||
)
|
||||
body = highlight_tags_with_background_color(
|
||||
self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"]
|
||||
|
@ -123,7 +137,7 @@ class Entry:
|
|||
colorize(
|
||||
ansiwrap.fill(
|
||||
line,
|
||||
self.journal.config["linewrap"],
|
||||
columns,
|
||||
initial_indent=indent,
|
||||
subsequent_indent=indent,
|
||||
drop_whitespace=True,
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# Copyright (C) 2012-2022 jrnl contributors
|
||||
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
import logging
|
||||
import os
|
||||
from textwrap import TextWrapper
|
||||
|
||||
from jrnl.exception import JrnlException
|
||||
|
@ -36,7 +38,22 @@ class FancyExporter(TextExporter):
|
|||
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
|
||||
|
||||
if entry.journal.config["linewrap"]:
|
||||
linewrap = entry.journal.config["linewrap"]
|
||||
|
||||
if linewrap == "auto":
|
||||
try:
|
||||
linewrap = os.get_terminal_size().columns
|
||||
except OSError:
|
||||
logging.debug(
|
||||
"Can't determine terminal size automatically 'linewrap': '%s'",
|
||||
entry.journal.config["linewrap"],
|
||||
)
|
||||
linewrap = 79
|
||||
else:
|
||||
linewrap = 79
|
||||
|
||||
initial_linewrap = max((1, linewrap - len(date_str) - 2))
|
||||
body_linewrap = linewrap - 2
|
||||
card = [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue