diff options
author | Mattori Birnbaum <mattori.birnbaum@gmail.com> | 2021-05-08 23:30:58 +0900 |
---|---|---|
committer | Christian Geier <geier@lostpackets.de> | 2023-10-28 16:40:56 +0200 |
commit | e05a2560925278a1fcf6ec33079899636bdf65e7 (patch) | |
tree | da9cf227eb5503f8377cf52aacf9856454577e70 | |
parent | ee9546b9ccc86cc1cbcf1cf035a9ea99f4b66d82 (diff) |
added special field formatting
-rw-r--r-- | khal/khalendar/event.py | 3 | ||||
-rw-r--r-- | khal/utils.py | 26 | ||||
-rw-r--r-- | tests/cli_test.py | 14 |
3 files changed, 38 insertions, 5 deletions
diff --git a/khal/khalendar/event.py b/khal/khalendar/event.py index f9e1e944..2197aee6 100644 --- a/khal/khalendar/event.py +++ b/khal/khalendar/event.py @@ -38,7 +38,6 @@ from ..custom_types import LocaleConfiguration from ..exceptions import FatalError from ..icalendar import cal_from_ics, delete_instance, invalid_timezone from ..parse_datetime import timedelta2str -from ..terminal import get_color from ..utils import generate_random_uid, is_aware, to_naive_utc, to_unix_time logger = logging.getLogger('khal') @@ -698,7 +697,7 @@ class Event: if "calendars" in env and self.calendar in env["calendars"]: cal = env["calendars"][self.calendar] - attributes["calendar-color"] = get_color(cal.get('color', '')) + attributes["calendar-color"] = cal.get('color', '') attributes["calendar"] = cal.get("displayname", self.calendar) else: attributes["calendar-color"] = attributes["calendar"] = '' diff --git a/khal/utils.py b/khal/utils.py index 05875dd6..65bb3000 100644 --- a/khal/utils.py +++ b/khal/utils.py @@ -35,6 +35,8 @@ import pytz import urwid from click import style +from .terminal import get_color + def generate_random_uid() -> str: """generate a random uid @@ -192,9 +194,14 @@ def human_formatter(format_string, width=None, colors=True): rows = [rows] results = [] for row in rows: + if 'calendar-color' in row: + row['calendar-color'] = get_color(row['calendar-color']) + s = format_string.format(**row) + if colors: s += style('', reset=True) + if width: results += color_wrap(s, width) else: @@ -212,9 +219,22 @@ def json_formatter(fields): single = type(rows) == dict if single: rows = [rows] - results = [json.dumps( - [dict(filter(lambda e: e[0] in fields, row.items())) for row in rows], - ensure_ascii=False)] + + filtered = [] + for row in rows: + f = dict(filter(lambda e: e[0] in fields, row.items())) + + if f.get('repeat-symbol', '') != '': + f["repeat-symbol"] = f["repeat-symbol"].strip() + if f.get('status', '') != '': + f["status"] = f["status"].strip() + if f.get('cancelled', '') != '': + f["cancelled"] = f["cancelled"].strip() + + filtered.append(f) + + results = [json.dumps(filtered, ensure_ascii=False)] + if single: return results[0] else: diff --git a/tests/cli_test.py b/tests/cli_test.py index 121dda0b..c9b66503 100644 --- a/tests/cli_test.py +++ b/tests/cli_test.py @@ -2,6 +2,7 @@ import datetime as dt import os import re import sys +import traceback import pytest from click.testing import CliRunner @@ -400,6 +401,19 @@ def test_at_json(runner): assert result.output.startswith('[{"start-time": "", "title": "myevent"}]') +def test_at_json_strip(runner): + runner = runner() + result = runner.invoke(main_khal, ['import', _get_ics_filepath( + 'event_rrule_recuid_cancelled')], input='0\ny\n') + assert not result.exception + result = runner.invoke(main_khal, ['at', '--json', 'repeat-symbol', + '--json', 'status', '--json', 'cancelled', '14.07.2014', '07:00']) + traceback.print_tb(result.exc_info[2]) + assert not result.exception + assert result.output.startswith( + '[{"repeat-symbol": "⟳", "status": "CANCELLED", "cancelled": "CANCELLED"}]') + + def test_at_day_format(runner): runner = runner(days=2) now = dt.datetime.now().strftime('%d.%m.%Y') |