diff frontends/src/jp/cmd_blog.py @ 2158:970a348d3fe9

jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
author Goffi <goffi@goffi.org>
date Fri, 17 Feb 2017 00:33:55 +0100
parents b4a515e36631
children 62dfa6e02f54
line wrap: on
line diff
--- a/frontends/src/jp/cmd_blog.py	Thu Feb 16 01:02:33 2017 +0100
+++ b/frontends/src/jp/cmd_blog.py	Fri Feb 17 00:33:55 2017 +0100
@@ -224,6 +224,12 @@
         tags = data_format.dict2iter('tag', item, pop=True)
         return u', '.join(tags)
 
+    def format_updated(self, item, keys):
+        return self.format_time(item['updated'])
+
+    def format_published(self, item, keys):
+        return self.format_time(item['published'])
+
     def get_keys(self):
         """return keys to display according to verbosity or explicit key request"""
         verbosity = self.args.verbose
@@ -240,7 +246,7 @@
             if verbosity == 0:
                 return (u'title', u'content')
             elif verbosity == 1:
-                return (u"title", u"tags", u"author", u"author_jid", u"author_email", u"author_jid_verified", u"content")
+                return (u"title", u"tags", u"author", u"author_jid", u"author_email", u"author_jid_verified", u"published", u"updated", u"content")
             else:
                 return MB_KEYS
 
@@ -276,6 +282,15 @@
             if idx < len(items)-1:
                 print(u'')
 
+    def format_time(self, timestamp):
+        """return formatted date for timestamp
+
+        @param timestamp(str,int,float): unix timestamp
+        @return (unicode): formatted date
+        """
+        fmt = u"%d/%m/%Y %H:%M:%S"
+        return time.strftime(fmt, time.localtime(float(timestamp)))
+
     def fancy_output(self, data):
         """display blog is a nice to read way
 
@@ -284,19 +299,36 @@
         # thanks to http://stackoverflow.com/a/943921
         rows, columns = map(int, os.popen('stty size', 'r').read().split())
         items, metadata = data
+        verbosity = self.args.verbose
         sep = A.color(A.FG_BLUE, columns * u'▬')
         if items:
             print(u'\n' + sep  + '\n')
 
         for idx, item in enumerate(items):
             title = item.get(u'title')
-            tags = list(data_format.dict2iter('tag', item, pop=True))
+            if verbosity > 0:
+                author = item[u'author']
+                published, updated = item[u'published'], item.get('updated')
+            else:
+                author = published = updated = None
+            if verbosity > 1:
+                tags = list(data_format.dict2iter('tag', item, pop=True))
+            else:
+                tags = None
             content = item.get(u'content')
 
             if title:
                 print(A.color(A.BOLD, A.FG_CYAN, item[u'title']))
+            meta = []
+            if author:
+                meta.append(A.color(A.FG_YELLOW, author))
+            if published:
+                meta.append(A.color(A.FG_YELLOW, u'on ', self.format_time(published)))
+            if updated != published:
+                 meta.append(A.color(A.FG_YELLOW, u'(updated on ', self.format_time(updated), u')'))
+            print(u' '.join(meta))
             if tags:
-                print(A.color(A.FG_YELLOW, u', '.join(tags)))
+                print(A.color(A.FG_MAGENTA, u', '.join(tags)))
             if (title or tags) and content:
                 print("")
             if content: