From 272d586e06847bc0a3a1a435782724a0814d13d1 Mon Sep 17 00:00:00 2001 From: Jonathan van der Steege Date: Sun, 12 Jun 2022 15:10:40 +0200 Subject: [PATCH] Add linewrap option 'auto' --- docs/reference-config-file.md | 3 ++- jrnl/Entry.py | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/reference-config-file.md b/docs/reference-config-file.md index 97ec290c..e03add1d 100644 --- a/docs/reference-config-file.md +++ b/docs/reference-config-file.md @@ -96,7 +96,8 @@ 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. +wrap long lines. Set to `auto` to let `jrnl` automatically determine +the terminal width. ### colors A dictionary that controls the colors used to display journal entries. diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 800345c6..9ed9b734 100644 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -7,6 +7,9 @@ import re import ansiwrap +import os +import logging + from .color import colorize from .color import highlight_tags_with_background_color @@ -105,6 +108,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()[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 title = ansiwrap.fill( date_str @@ -115,7 +130,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"] @@ -124,7 +139,7 @@ class Entry: colorize( ansiwrap.fill( line, - self.journal.config["linewrap"], + columns, initial_indent=indent, subsequent_indent=indent, drop_whitespace=True,