From b4bfc2c1383a19dddd6659e09dc2bfa6af8ca688 Mon Sep 17 00:00:00 2001 From: Aaron Lichtman Date: Sat, 26 Oct 2019 02:23:11 +0200 Subject: [PATCH] Use ansiwrap to properly wrap strings with ANSI escapes --- features/steps/core.py | 8 +++++--- jrnl/Entry.py | 6 +++--- poetry.lock | 23 ++++++++++++++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/features/steps/core.py b/features/steps/core.py index 83981d13..1204381e 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -5,8 +5,10 @@ from behave import given, when, then from jrnl import cli, install, Journal, util, plugins from jrnl import __version__ from dateutil import parser as date_parser +from ansiwrap import strip_color from collections import defaultdict import os +import re import json import yaml import keyring @@ -170,7 +172,7 @@ def check_json_output_path(context, path, value): @then('the output should be "{text}"') def check_output(context, text=None): text = (text or context.text).strip().splitlines() - out = context.stdout_capture.getvalue().strip().splitlines() + out = strip_color(context.stdout_capture.getvalue().strip()).splitlines() assert len(text) == len(out), "Output has {} lines (expected: {})".format(len(out), len(text)) for line_text, line_out in zip(text, out): assert line_text.strip() == line_out.strip(), [line_text.strip(), line_out.strip()] @@ -178,7 +180,7 @@ def check_output(context, text=None): @then('the output should contain "{text}" in the local time') def check_output_time_inline(context, text): - out = context.stdout_capture.getvalue() + out = strip_color(context.stdout_capture.getvalue()) local_tz = tzlocal.get_localzone() utc_time = date_parser.parse(text) local_date = utc_time.astimezone(local_tz).strftime("%Y-%m-%d %H:%M") @@ -189,7 +191,7 @@ def check_output_time_inline(context, text): @then('the output should contain "{text}"') def check_output_inline(context, text=None): text = text or context.text - out = context.stdout_capture.getvalue() + out = strip_color(context.stdout_capture.getvalue()) if isinstance(out, bytes): out = out.decode('utf-8') assert text in out, text diff --git a/jrnl/Entry.py b/jrnl/Entry.py index 51a0ea43..a3fea34d 100755 --- a/jrnl/Entry.py +++ b/jrnl/Entry.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import re -import textwrap +import ansiwrap from datetime import datetime from .util import split_title, bold, colorize_red @@ -80,11 +80,11 @@ class Entry: indent = "" if not short and self.journal.config['linewrap']: # Color date red and make sure first line of title is bolded - title = textwrap.fill(colorize_red(date_str) + " " + bold(self.title), self.journal.config['linewrap']) + title = ansiwrap.fill(colorize_red(date_str) + " " + bold(self.title), self.journal.config['linewrap']) # Make sure all lines after the first are bolded, too title = "".join([bold(part) + "\n" for part in title.split("\n")]).strip() body = "\n".join([ - textwrap.fill( + ansiwrap.fill( line, self.journal.config['linewrap'], initial_indent=indent, diff --git a/poetry.lock b/poetry.lock index e772b2d2..9cfcbe16 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,3 +1,14 @@ +[[package]] +category = "main" +description = "textwrap, but savvy to ANSI colors and styles" +name = "ansiwrap" +optional = false +python-versions = "*" +version = "0.8.4" + +[package.dependencies] +textwrap3 = ">=0.9.2" + [[package]] category = "dev" description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." @@ -356,6 +367,14 @@ optional = false python-versions = ">=2.6, !=3.0.*, !=3.1.*" version = "1.12.0" +[[package]] +category = "main" +description = "textwrap from Python 3.6 backport (plus a few tweaks)" +name = "textwrap3" +optional = false +python-versions = "*" +version = "0.9.2" + [[package]] category = "dev" description = "Python Library for Tom's Obvious, Minimal Language" @@ -384,10 +403,11 @@ version = "1.5.1" pytz = "*" [metadata] -content-hash = "9896cf59c7552b6ad95219ee5555c7445a3fab39c2e4f4c6f3d991a36635e44b" +content-hash = "6ff68790de1a4cec0f5a1cf145ecaf3c0028571c2ab8ece0987b0af982096e98" python-versions = "^3.7" [metadata.hashes] +ansiwrap = ["7b053567c88e1ad9eed030d3ac41b722125e4c1271c8a99ade797faff1f49fb1", "ca0c740734cde59bf919f8ff2c386f74f9a369818cdc60efe94893d01ea8d9b7"] appdirs = ["9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92", "d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e"] asn1crypto = ["2f1adbb7546ed199e3c90ef23ec95c5cf3585bac7d11fb7eb562a3fe89c64e87", "9d5c20441baf0cb60a4ac34cc447c6c189024b6b4c6cd7877034f4965c464e49"] asteval = ["7c81fee6707a7a28e8beae891b858535a7e61f9ce275a0a4cf5f428fbc934cb8"] @@ -424,6 +444,7 @@ pyxdg = ["1948ff8e2db02156c0cccd2529b43c0cff56ebaa71f5f021bbd755bc1419190e", "fe pyyaml = ["0113bc0ec2ad727182326b61326afa3d1d8280ae1122493553fd6f4397f33df9", "01adf0b6c6f61bd11af6e10ca52b7d4057dd0be0343eb9283c878cf3af56aee4", "5124373960b0b3f4aa7df1707e63e9f109b5263eca5976c66e08b1c552d4eaf8", "5ca4f10adbddae56d824b2c09668e91219bb178a1eee1faa56af6f99f11bf696", "7907be34ffa3c5a32b60b95f4d95ea25361c951383a894fec31be7252b2b6f34", "7ec9b2a4ed5cad025c2278a1e6a19c011c80a3caaac804fd2d329e9cc2c287c9", "87ae4c829bb25b9fe99cf71fbb2140c448f534e24c998cc60f39ae4f94396a73", "9de9919becc9cc2ff03637872a440195ac4241c80536632fffeb6a1e25a74299", "a5a85b10e450c66b49f98846937e8cfca1db3127a9d5d1e31ca45c3d0bef4c5b", "b0997827b4f6a7c286c01c5f60384d218dca4ed7d9efa945c3e1aa623d5709ae", "b631ef96d3222e62861443cc89d6563ba3eeb816eeb96b2629345ab795e53681", "bf47c0607522fdbca6c9e817a6e81b08491de50f3766a7a0e6a5be7905961b41", "f81025eddd0327c7d4cfe9b62cf33190e1e736cc6e97502b3ec425f574b3e7a8"] secretstorage = ["20c797ae48a4419f66f8d28fc221623f11fc45b6828f96bdb1ad9990acb59f92", "7a119fb52a88e398dbb22a4b3eb39b779bfbace7e4153b7bc6e5954d86282a8a"] six = ["3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", "d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73"] +textwrap3 = ["5008eeebdb236f6303dcd68f18b856d355f6197511d952ba74bc75e40e0c3414", "bf5f4c40faf2a9ff00a9e0791fed5da7415481054cef45bb4a3cfb1f69044ae0"] toml = ["229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c", "235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e", "f1db651f9657708513243e61e6cc67d101a39bad662eaa9b5546f789338e07a3"] tornado = ["349884248c36801afa19e342a77cc4458caca694b0eda633f5878e458a44cb2c", "398e0d35e086ba38a0427c3b37f4337327231942e731edaa6e9fd1865bbd6f60", "4e73ef678b1a859f0cb29e1d895526a20ea64b5ffd510a2307b5998c7df24281", "559bce3d31484b665259f50cd94c5c28b961b09315ccd838f284687245f416e5", "abbe53a39734ef4aba061fca54e30c6b4639d3e1f59653f0da37a0003de148c7", "c845db36ba616912074c5b1ee897f8e0124df269468f25e4fe21fe72f6edd7a9", "c9399267c926a4e7c418baa5cbe91c7d1cf362d505a1ef898fde44a07c9dd8a5"] tzlocal = ["4ebeb848845ac898da6519b9b31879cf13b6626f7184c496037b818e238f2c4e"]