summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattori Birnbaum <mattori.birnbaum@gmail.com>2021-05-08 16:12:30 +0900
committerChristian Geier <geier@lostpackets.de>2023-10-28 16:40:56 +0200
commit2eee84c92109915738a36035b59ffd3e906e2791 (patch)
treebe99ff84397eb7fa44bab99778aa357c6fe17b1e
parent3809a3183a41b30ba1177718ed547e44d8967902 (diff)
added json option to search
-rw-r--r--khal/cli.py10
-rw-r--r--khal/controllers.py9
-rw-r--r--tests/cli_test.py10
3 files changed, 26 insertions, 3 deletions
diff --git a/khal/cli.py b/khal/cli.py
index ade17dab..245f1d49 100644
--- a/khal/cli.py
+++ b/khal/cli.py
@@ -35,6 +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)
try:
from setproctitle import setproctitle
@@ -593,9 +594,10 @@ def _get_cli():
@multi_calendar_option
@click.option('--format', '-f',
help=('The format of the events.'))
+ @click.option('--json', help=("Fields to output in json"), multiple=True)
@click.argument('search_string')
@click.pass_context
- def search(ctx, format, search_string, include_calendar, exclude_calendar):
+ def search(ctx, format, json, search_string, include_calendar, exclude_calendar):
'''Search for events matching SEARCH_STRING.
For recurring events, only the master event and different overwritten
@@ -614,8 +616,12 @@ def _get_cli():
term_width, _ = get_terminal_size()
now = dt.datetime.now()
env = {"calendars": ctx.obj['conf']['calendars']}
+ if len(json) == 0:
+ formatter = human_formatter(format)
+ else:
+ formatter = json_formatter(json)
for event in events:
- desc = textwrap.wrap(controllers.human_formatter(format)(
+ desc = textwrap.wrap(formatter(
event.format(relative_to=now, env=env)), term_width)
event_column.extend(
[colored(d, event.color,
diff --git a/khal/controllers.py b/khal/controllers.py
index 21cbaf39..97ed3fe2 100644
--- a/khal/controllers.py
+++ b/khal/controllers.py
@@ -97,9 +97,16 @@ def human_formatter(format_string, width=None, colors=True):
def json_formatter(fields):
def fmt(rows):
- return [json.dumps(
+ 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
diff --git a/tests/cli_test.py b/tests/cli_test.py
index 870397a5..a82aa108 100644
--- a/tests/cli_test.py
+++ b/tests/cli_test.py
@@ -451,6 +451,16 @@ def test_search(runner):
assert result.output.startswith('\x1b[34m\x1b[31m18:00')
+def test_search_json(runner):
+ runner = runner(days=2)
+ now = dt.datetime.now().strftime('%d.%m.%Y')
+ result = runner.invoke(main_khal, 'new {} 18:00 myevent'.format(now).split())
+ result = runner.invoke(main_khal, ['search', '--json', 'start-end-time-style',
+ '--json', 'title', '--json', 'description', 'myevent'])
+ assert not result.exception
+ assert result.output.startswith('[{"start-end-time-style": "18:00')
+
+
def test_no_default_new(runner):
runner = runner(default_calendar=False)
result = runner.invoke(main_khal, 'new 18:00 beer'.split())