diff options
Diffstat (limited to 'src/vgstash_cli.py')
-rwxr-xr-x | src/vgstash_cli.py | 127 |
1 files changed, 97 insertions, 30 deletions
diff --git a/src/vgstash_cli.py b/src/vgstash_cli.py index 29f6026..046443d 100755 --- a/src/vgstash_cli.py +++ b/src/vgstash_cli.py @@ -33,7 +33,6 @@ def init(): else: raise sqlite3.OperationalError("Cannot create schema.") - def row_format(row, width, header): """ Prints a row from the result set into a nice table. @@ -77,10 +76,20 @@ def row_format(row, width, header): @click.argument('ownership', type=str, required=False, default=vgstash.DEFAULT_CONFIG['ownership']) @click.argument('progress', type=str, required=False, default=vgstash.DEFAULT_CONFIG['progress']) @click.argument('notes', type=str, required=False, default="") -def add(title, system, ownership, progress, notes): +@click.argument('p_date', type=str, required=False, default="") +@click.argument('b_date', type=str, required=False, default="") +@click.argument('c_date', type=str, required=False, default="") +def add(title, system, ownership, progress, notes, p_date, b_date, c_date): db = get_db() - game = vgstash.Game(title, system, ownership, progress, notes) + game = vgstash.Game(title, system, ownership, progress, notes, p_date, b_date, c_date) try: + # Convert user-input (meant to be RFC2822/ISO8601 dates) to timestamps + if game.p_date: + game.p_date = vgstash.iso_to_unix(game.p_date) + if game.b_date: + game.b_date = vgstash.iso_to_unix(game.b_date) + if game.c_date: + game.c_date = vgstash.iso_to_unix(game.c_date) db.add_game(game, update=False) own_clause = ( "do not own it", @@ -96,13 +105,15 @@ def add(title, system, ownership, progress, notes): "have beaten", "have completed", ) - note_clause = "" if len(game.notes) == 0 else " It also has notes." - click.echo("Added {} for {}. You {} and {} it.{}".format( + note_clause = "" if len(game.notes) == 0 else " It has notes." + date_clause = "" if not (game.p_date or game.b_date or game.c_date) else " It has date data." + click.echo("Added {} for {}. You {} and {} it.{}{}".format( game.title, game.system, own_clause[game.ownership], progress_clause[game.progress], note_clause, + date_clause )) except sqlite3.IntegrityError as e: print(e) @@ -112,33 +123,89 @@ def add(title, system, ownership, progress, notes): @cli.command('list') @click.argument('filter', type=click.Choice(vgstash.FILTERS.keys()), required=False, default="allgames") @click.option('--raw', '-r', is_flag=True, show_default=True, default=False, help="Output raw, pipe-delimited lines") -@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): +def list_games(filter, raw): db = get_db() - res = db.list_games(filter) - first_pass = True - # res can be False if the filter doesn't exist, but Click should catch it - # and spit out an error before this function even starts. - for r in res: - if 'notes' in r.keys() and len(r['notes']) > 0: - notes = r['notes'].replace('\n', '\\n') - notes = notes.replace('\r', '\\r') - else: - notes = '' - if raw: - click.echo("|".join(( - r['title'], - r['system'], - str(r['ownership']), - str(r['progress']), - notes - )) - ) - else: - row_format(r, width, first_pass) - first_pass = False - + row_data = db.list_games(filter) + + if raw: + for r in row_data: + l = [] + for c in r: + if c == None: + l.append(str('')) + elif type(c) == int: + l.append(str(c)) + else: + tc = c.replace('\n', '\\n') + tc = tc.replace('\r', '\\r') + l.append(tc) + click.echo("|".join(l)) + return + # Get column names, and a list of widths ready to go with them + columns = row_data[0].keys() + widths = [] + for i in range(len(columns)): + widths.append(len(columns[i])) + + # Make a cache to manipulate the data with + row_cache = [] + for r in row_data: + cache_row = [] + for c in r: + cache_row.append(c) + row_cache.append(cache_row) + # We should have a full, mutable cache now! + + + for r in row_cache: + for i in range(len(columns)): + # process fields that need massaging for display + if r[i]: + if columns[i] == ("p_date"): + r[i] = vgstash.unix_to_iso(r[i]) + if columns[i] == ("b_date"): + r[i] = vgstash.unix_to_iso(r[i]) + if columns[i] == ("c_date"): + r[i] = vgstash.unix_to_iso(r[i]) + if columns[i] == "notes" and len(r[i]) > 0: + r[i] = "*" + if columns[i] == "progress": + r[i] = vgstash.vtok(r[i], vgstash.PROGRESS)[0].capitalize() + if columns[i] == "ownership": + r[i] = vgstash.vtok(r[i], vgstash.OWNERSHIP)[0].capitalize() + if r[i] == None: + r[i] = "" + if isinstance(r[i], int): + r[i] = str(r[i]) + # Store width in relevant list + w = len(str(r[i])) + if w > widths[i]: + widths[i] = w + + # print the top header + l = [] + left_fst = "{: <{w}s}" + right_fst = "{: >{w}s}" + center_fst = "{: ^{w}s}" + + for i in range(len(columns)): + l.append(center_fst.format(columns[i], w=widths[i])) + click.echo(" | ".join(l)) + + l = [] + for w in widths: + l.append("-"*w) + click.echo("-+-".join(l)) + + # print the collection now that the hard part is done! + for r in row_cache: + l = [] + for i in range(len(columns)): + # TODO: set different fstring based on column name + l.append(left_fst.format(r[i], w=widths[i])) + click.echo(" | ".join(l)) + @cli.command('delete') @click.argument('title', required=True) @click.argument('system', required=True) |