Add linewrap option 'auto'

This commit is contained in:
Jonathan van der Steege 2022-06-12 15:10:40 +02:00
parent 9639c5386f
commit 272d586e06
2 changed files with 19 additions and 3 deletions

View file

@ -96,7 +96,8 @@ If `true`, tags will be highlighted in cyan.
### linewrap ### linewrap
Controls the width of the output. Set to `false` if you don't want to Controls the width of the output. Set to `false` if you don't want to
wrap long lines. wrap long lines. Set to `auto` to let `jrnl` automatically determine
the terminal width.
### colors ### colors
A dictionary that controls the colors used to display journal entries. A dictionary that controls the colors used to display journal entries.

View file

@ -7,6 +7,9 @@ import re
import ansiwrap import ansiwrap
import os
import logging
from .color import colorize from .color import colorize
from .color import highlight_tags_with_background_color from .color import highlight_tags_with_background_color
@ -105,6 +108,18 @@ class Entry:
) )
if not short and self.journal.config["linewrap"]: if not short and self.journal.config["linewrap"]:
columns = self.journal.config["linewrap"]
if columns == "auto":
try:
columns = os.get_terminal_size()[0]
except: # noqa: E722
logging.debug(
"Can't determine terminal size automatically 'linewrap': '%s'",
self.journal.config["linewrap"],
)
columns = 79
# Color date / title and bold title # Color date / title and bold title
title = ansiwrap.fill( title = ansiwrap.fill(
date_str date_str
@ -115,7 +130,7 @@ class Entry:
self.journal.config["colors"]["title"], self.journal.config["colors"]["title"],
is_title=True, is_title=True,
), ),
self.journal.config["linewrap"], columns,
) )
body = highlight_tags_with_background_color( body = highlight_tags_with_background_color(
self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"] self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"]
@ -124,7 +139,7 @@ class Entry:
colorize( colorize(
ansiwrap.fill( ansiwrap.fill(
line, line,
self.journal.config["linewrap"], columns,
initial_indent=indent, initial_indent=indent,
subsequent_indent=indent, subsequent_indent=indent,
drop_whitespace=True, drop_whitespace=True,