diff options
author | Mattori Birnbaum <mattori.birnbaum@gmail.com> | 2021-05-08 16:21:40 +0900 |
---|---|---|
committer | Christian Geier <geier@lostpackets.de> | 2023-10-28 16:40:56 +0200 |
commit | 15fcb7692c23c49ae5fd3571ce1babc10b3dfa07 (patch) | |
tree | 3ee6839043c1fe9a1358c5042751302789f7c799 | |
parent | 2eee84c92109915738a36035b59ffd3e906e2791 (diff) |
moved formatters to util
-rw-r--r-- | khal/cli.py | 2 | ||||
-rw-r--r-- | khal/controllers.py | 38 | ||||
-rw-r--r-- | khal/utils.py | 38 |
3 files changed, 40 insertions, 38 deletions
diff --git a/khal/cli.py b/khal/cli.py index 245f1d49..72adfb8c 100644 --- a/khal/cli.py +++ b/khal/cli.py @@ -35,7 +35,7 @@ from . import __version__, controllers, khalendar from .exceptions import FatalError from .settings import InvalidSettingsError, NoConfigFile, get_config from .terminal import colored -from .controllers import (human_formatter, json_formatter) +from .utils import human_formatter, json_formatter try: from setproctitle import setproctitle diff --git a/khal/controllers.py b/khal/controllers.py index 97ed3fe2..aeac9632 100644 --- a/khal/controllers.py +++ b/khal/controllers.py @@ -25,7 +25,6 @@ import logging import os import re import textwrap -import json from collections import OrderedDict, defaultdict from shutil import get_terminal_size from typing import Callable, Iterable, List, Optional @@ -50,6 +49,7 @@ from .icalendar import cal_from_ics, split_ics from .icalendar import sort_key as sort_vevent_key from .khalendar.vdir import Item from .terminal import merge_columns +from .utils import (human_formatter, json_formatter) logger = logging.getLogger('khal') @@ -74,42 +74,6 @@ def format_day(day: dt.date, format_string: str, locale, attributes=None): raise KeyError("cannot format day with: %s" % format_string) -def human_formatter(format_string, width=None, colors=True): - def fmt(rows): - single = type(rows) == dict - if single: - rows = [rows] - results = [] - for row in rows: - s = format_string.format(**row) - if colors: - s += style('', reset=True) - if width: - results += utils.color_wrap(s, width) - else: - results.append(s) - if single: - return results[0] - else: - return results - return fmt - - -def json_formatter(fields): - def fmt(rows): - 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)] - if single: - return results[0] - else: - return results - return fmt - - def calendar( collection: CalendarCollection, agenda_format=None, diff --git a/khal/utils.py b/khal/utils.py index cac1226e..ed5775b4 100644 --- a/khal/utils.py +++ b/khal/utils.py @@ -23,6 +23,7 @@ import datetime as dt +import json import random import re import string @@ -32,6 +33,7 @@ from typing import Iterator, List, Optional, Tuple import pytz import urwid +from click import style def generate_random_uid() -> str: @@ -180,3 +182,39 @@ def relative_timedelta_str(day: dt.date) -> str: def get_wrapped_text(widget: urwid.AttrMap) -> str: return widget.original_widget.get_edit_text() + + +def human_formatter(format_string, width=None, colors=True): + def fmt(rows): + single = type(rows) == dict + if single: + rows = [rows] + results = [] + for row in rows: + s = format_string.format(**row) + if colors: + s += style('', reset=True) + if width: + results += utils.color_wrap(s, width) + else: + results.append(s) + if single: + return results[0] + else: + return results + return fmt + + +def json_formatter(fields): + def fmt(rows): + 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)] + if single: + return results[0] + else: + return results + return fmt |