Skip to content

Instantly share code, notes, and snippets.

@rafaltrojniak
Created June 4, 2020 15:19
Show Gist options
  • Save rafaltrojniak/95a8090341c195d8f8624fb8bbede70e to your computer and use it in GitHub Desktop.
Save rafaltrojniak/95a8090341c195d8f8624fb8bbede70e to your computer and use it in GitHub Desktop.
Try to provide JSON output from `current` endpoint in hamster by in-line determinig output format
diff --git a/src/hamster-cli.py b/src/hamster-cli.py
old mode 100644
new mode 100755
index 2da4bd75..1448c692
--- a/src/hamster-cli.py
+++ b/src/hamster-cli.py
@@ -25,6 +25,7 @@
import sys, os
import argparse
import re
+import json
import gi
gi.require_version('Gdk', '3.0') # noqa: E402
@@ -317,13 +318,33 @@ class HamsterCli(object):
def current(self, *args):
"""prints current activity. kinda minimal right now"""
+ print_format = 'text'
+ if args:
+ print_format = args[0]
+
facts = self.storage.get_todays_facts()
if facts and not facts[-1].end_time:
- print("{} {}".format(str(facts[-1]).strip(),
- facts[-1].delta.format(fmt="HH:MM")))
- else:
- print((_("No activity")))
+ fact = facts[-1]
+ if print_format == 'json':
+ print(json.dumps({
+ "name": fact.activity,
+ "start_time": str(fact.start_time),
+ "end_time": str(fact.end_time),
+ "duration_minutes": stuff.duration_minutes(fact.delta),
+ "category": fact.category,
+ "description": fact.description,
+ "tags": ": ".join(fact.tags),
+ }))
+ else:
+ """ Use text if format unknown """
+ print("{} {}".format(str(fact).strip(),
+ fact.delta.format(fmt="HH:MM")))
+ else:
+ if print_format == 'json':
+ print(json.dumps({}))
+ else:
+ print((_("No activity")))
def search(self, *args):
"""search for activities by name and optionally within a date range"""
@@ -424,7 +445,7 @@ Actions:
term
* export [html|tsv|ical|xml|json] [start-date [end-date]]: Export activities with
the specified format
- * current: Print current activity
+ * current [text|json]: Print current activity
* activities: List all the activities names, one per line.
* categories: List all the categories names, one per line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment