Skip to content

Commit

Permalink
feat: migrate categories CLI action to views
Browse files Browse the repository at this point in the history
Migrate categories CLI action to view pattern,
provide text and JSON view
  • Loading branch information
rafaltrojniak committed Jun 3, 2020
1 parent ad4b573 commit e665e3f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/hamster-cli.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ def current(self, action):
def activities(self, facts):
pass

def categories(self, facts):
pass

class HamsterViewText(HamsterView):

def current(self, action):
Expand All @@ -247,6 +250,10 @@ def activities(self, facts):
for activity in facts:
print("{}@{}".format(activity['name'], activity['category']))

def categories(self, categories):
for category in categories:
print(category['name'])

class HamsterViewJSON(HamsterView):

def _dump_fact(self, fact):
Expand All @@ -270,6 +277,11 @@ def activities(self, facts):
output = [ {"name":activity['name'], "category":activity['category']}
for activity in facts]
print(json.dumps(output))

def categories(self, categories):
output = [ category['name'] for category in categories ]
print(json.dumps(output))

class HamsterCli(object):
"""Command line interface."""

Expand Down Expand Up @@ -350,8 +362,7 @@ def activities(self, *args):

def categories(self, *args):
'''Print the names of all the categories.'''
for category in self.storage.get_categories():
print(category['name'])
self.view.categories(self.storage.get_categories())


def list(self, *times):
Expand Down

0 comments on commit e665e3f

Please sign in to comment.