summaryrefslogtreecommitdiff
path: root/src/vgstash_cli.py
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2018-09-29 23:27:05 -0700
committerzlg <zlg@zlg.space>2018-09-29 23:34:35 -0700
commit7288560c5678991527c7b2c2ed0d7f3979f70c67 (patch)
tree00ee2e3a79cf1aae646d9c9b44df6c1fcac4325c /src/vgstash_cli.py
parentsetup.py: Bump to alpha4 for PyPI (diff)
downloadvgstash-7288560c5678991527c7b2c2ed0d7f3979f70c67.tar.gz
vgstash-7288560c5678991527c7b2c2ed0d7f3979f70c67.tar.bz2
vgstash-7288560c5678991527c7b2c2ed0d7f3979f70c67.tar.xz
vgstash-7288560c5678991527c7b2c2ed0d7f3979f70c67.zip
cli: Add pretty printing to 'list' command
Also add the "--width" option to specify the maximum width of the table.
Diffstat (limited to '')
-rw-r--r--src/vgstash_cli.py50
1 files changed, 46 insertions, 4 deletions
diff --git a/src/vgstash_cli.py b/src/vgstash_cli.py
index 3d1d1db..e4d1a32 100644
--- a/src/vgstash_cli.py
+++ b/src/vgstash_cli.py
@@ -1,8 +1,11 @@
import vgstash
import sqlite3
import click
+import os
import sys
+from shutil import get_terminal_size
+
def get_db():
"""Fetch a vgstash DB object from the default location.
@@ -25,6 +28,43 @@ def init():
else:
raise sqlite3.OperationalError("Cannot create schema.")
+def row_format(row, width, header):
+ """
+ TODO
+ """
+ # There's another way to do this, involving gathering the entire results and
+ # *then* formatting them. That is incredibly wasteful of resources imo, so
+ # it may need some testing to see if it's better. Ideally, we'd only make
+ # the table as wide as needed; that can't happen unless we know the longest
+ # title's length...
+
+ twidth = int(width) - 35
+ if header == True:
+ click.echo("{:^4s} | {:<{w}s} | {:<8s} | {:^3s} | {:<7s}".format(
+ "ID",
+ "Title",
+ "System",
+ "Own",
+ "Status",
+ w=twidth)
+ )
+ click.echo("-" * int(width))
+
+ gidstr = "{: >4d}".format(row['rowid'])
+ titlestr = "{: <{w}s}".format(row['title'][:twidth], w=twidth)
+ systemstr = "{: ^8s}".format(row['system'][:8])
+ # unowned, physical, digital, both
+ ownltr = [' ', 'P', ' D', 'P D']
+ ownstr = "{: <3s}".format(ownltr[row['ownership']])
+ statltr = {
+ -1: 'U',
+ 0: 'N',
+ 1: 'P',
+ 2: 'B',
+ 3: 'C'
+ }
+ statstr = "{: <7s}".format((" " * row['progress'] * 2) + statltr[row['progress']])
+ print(" | ".join((gidstr, titlestr, systemstr, ownstr, statstr)))
@cli.command('add')
@click.argument('title', type=str)
@@ -66,9 +106,11 @@ def add(title, system, ownership, progress, notes):
@cli.command('list')
@click.argument('filter', required=False, default="allgames")
@click.option('--raw', '-r', is_flag=True, show_default=True, default=False, help="Output raw, pipe-delimited lines")
-def list_games(filter, raw):
+@click.option('--width', '-w', type=str, required=False, default=get_terminal_size(fallback=(80,24)).columns, help="The width of the table to output, in characters.")
+def list_games(filter, raw, width):
db = get_db()
res = db.list_games(filter)
+ first_pass = True
for r in res:
if 'notes' in r.keys() and len(r['notes']) > 0:
notes = r['notes'].replace('\n', '\\n')
@@ -83,8 +125,8 @@ def list_games(filter, raw):
str(r['ownership']),
str(r['progress']),
notes
- )
))
+ )
else:
- pass
- #pretty_print(r)
+ row_format(r, width, first_pass)
+ first_pass = False