Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2157:b4a515e36631 | 2158:970a348d3fe9 |
---|---|
222 | 222 |
223 def format_tags(self, item, keys): | 223 def format_tags(self, item, keys): |
224 tags = data_format.dict2iter('tag', item, pop=True) | 224 tags = data_format.dict2iter('tag', item, pop=True) |
225 return u', '.join(tags) | 225 return u', '.join(tags) |
226 | 226 |
227 def format_updated(self, item, keys): | |
228 return self.format_time(item['updated']) | |
229 | |
230 def format_published(self, item, keys): | |
231 return self.format_time(item['published']) | |
232 | |
227 def get_keys(self): | 233 def get_keys(self): |
228 """return keys to display according to verbosity or explicit key request""" | 234 """return keys to display according to verbosity or explicit key request""" |
229 verbosity = self.args.verbose | 235 verbosity = self.args.verbose |
230 if self.args.keys: | 236 if self.args.keys: |
231 if not set(MB_KEYS).issuperset(self.args.keys): | 237 if not set(MB_KEYS).issuperset(self.args.keys): |
238 return self.args.keys | 244 return self.args.keys |
239 else: | 245 else: |
240 if verbosity == 0: | 246 if verbosity == 0: |
241 return (u'title', u'content') | 247 return (u'title', u'content') |
242 elif verbosity == 1: | 248 elif verbosity == 1: |
243 return (u"title", u"tags", u"author", u"author_jid", u"author_email", u"author_jid_verified", u"content") | 249 return (u"title", u"tags", u"author", u"author_jid", u"author_email", u"author_jid_verified", u"published", u"updated", u"content") |
244 else: | 250 else: |
245 return MB_KEYS | 251 return MB_KEYS |
246 | 252 |
247 def default_output(self, data): | 253 def default_output(self, data): |
248 """simple key/value output""" | 254 """simple key/value output""" |
274 print header + value | 280 print header + value |
275 # we want a separation line after each item but the last one | 281 # we want a separation line after each item but the last one |
276 if idx < len(items)-1: | 282 if idx < len(items)-1: |
277 print(u'') | 283 print(u'') |
278 | 284 |
285 def format_time(self, timestamp): | |
286 """return formatted date for timestamp | |
287 | |
288 @param timestamp(str,int,float): unix timestamp | |
289 @return (unicode): formatted date | |
290 """ | |
291 fmt = u"%d/%m/%Y %H:%M:%S" | |
292 return time.strftime(fmt, time.localtime(float(timestamp))) | |
293 | |
279 def fancy_output(self, data): | 294 def fancy_output(self, data): |
280 """display blog is a nice to read way | 295 """display blog is a nice to read way |
281 | 296 |
282 this output doesn't use keys filter | 297 this output doesn't use keys filter |
283 """ | 298 """ |
284 # thanks to http://stackoverflow.com/a/943921 | 299 # thanks to http://stackoverflow.com/a/943921 |
285 rows, columns = map(int, os.popen('stty size', 'r').read().split()) | 300 rows, columns = map(int, os.popen('stty size', 'r').read().split()) |
286 items, metadata = data | 301 items, metadata = data |
302 verbosity = self.args.verbose | |
287 sep = A.color(A.FG_BLUE, columns * u'▬') | 303 sep = A.color(A.FG_BLUE, columns * u'▬') |
288 if items: | 304 if items: |
289 print(u'\n' + sep + '\n') | 305 print(u'\n' + sep + '\n') |
290 | 306 |
291 for idx, item in enumerate(items): | 307 for idx, item in enumerate(items): |
292 title = item.get(u'title') | 308 title = item.get(u'title') |
293 tags = list(data_format.dict2iter('tag', item, pop=True)) | 309 if verbosity > 0: |
310 author = item[u'author'] | |
311 published, updated = item[u'published'], item.get('updated') | |
312 else: | |
313 author = published = updated = None | |
314 if verbosity > 1: | |
315 tags = list(data_format.dict2iter('tag', item, pop=True)) | |
316 else: | |
317 tags = None | |
294 content = item.get(u'content') | 318 content = item.get(u'content') |
295 | 319 |
296 if title: | 320 if title: |
297 print(A.color(A.BOLD, A.FG_CYAN, item[u'title'])) | 321 print(A.color(A.BOLD, A.FG_CYAN, item[u'title'])) |
322 meta = [] | |
323 if author: | |
324 meta.append(A.color(A.FG_YELLOW, author)) | |
325 if published: | |
326 meta.append(A.color(A.FG_YELLOW, u'on ', self.format_time(published))) | |
327 if updated != published: | |
328 meta.append(A.color(A.FG_YELLOW, u'(updated on ', self.format_time(updated), u')')) | |
329 print(u' '.join(meta)) | |
298 if tags: | 330 if tags: |
299 print(A.color(A.FG_YELLOW, u', '.join(tags))) | 331 print(A.color(A.FG_MAGENTA, u', '.join(tags))) |
300 if (title or tags) and content: | 332 if (title or tags) and content: |
301 print("") | 333 print("") |
302 if content: | 334 if content: |
303 print content | 335 print content |
304 | 336 |