From 5b8a091e9c8248245c14298aa47e9de9af8a9813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Tr=C3=B3jniak?= Date: Sun, 7 Jun 2020 21:55:10 +0200 Subject: [PATCH] feat: use reports multiple formats for `current` Use `reports` module that provide multiple output formats to format output of `current` action. --- setup.py | 5 ++++- src/hamster-cli.py | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) mode change 100644 => 100755 src/hamster-cli.py diff --git a/setup.py b/setup.py index 589ed7178..5d5fbed34 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,11 @@ #!/usr/bin/env python2 import distutils import os -from distutils.core import setup from distutils import sysconfig +try: + from setuptools import setup +except ImportError: + from distutils.core import setup data_dir = os.path.join(sysconfig.get_python_lib(), "hamster", "data") diff --git a/src/hamster-cli.py b/src/hamster-cli.py old mode 100644 new mode 100755 index 9c6786787..a90cdcfdf --- a/src/hamster-cli.py +++ b/src/hamster-cli.py @@ -231,6 +231,10 @@ def __init__(self): self.storage = client.Storage() + def setFormat(self, format:str): + self.format = format + + def assist(self, *args): assist_command = args[0] if args else "" @@ -318,11 +322,19 @@ def list(self, *times): def current(self, *args): """prints current activity. kinda minimal right now""" 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"))) + + if not self.format or self.format == 'text': + 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"))) else: - print((_("No activity"))) + fact=[] + if facts: + fact = [facts[-1]] + now = dt.datetime.now() + reports.simple(fact, now, now, self.format) def search(self, *args): @@ -467,6 +479,10 @@ def version(self): choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'), default='WARNING', help="Set the logging level (default: %(default)s)") + parser.add_argument("-f", "--format", + choices=('text', 'html', 'tsv', 'xml', 'ical'), + default='', + help="Set output format (default: %(default)s)") parser.add_argument("action", nargs="?", default="overview") parser.add_argument('action_args', nargs=argparse.REMAINDER, default=[]) @@ -488,6 +504,8 @@ def version(self): else: action = args.action + hamster_client.setFormat(args.format) + if action in ("about", "add", "edit", "overview", "preferences"): if action == "add" and args.action_args: assert not unknown_args, "unknown options: {}".format(unknown_args)