diff --git a/bin/ti b/bin/ti index 86e6070..8c7d291 100755 --- a/bin/ti +++ b/bin/ti @@ -14,6 +14,7 @@ Usage: ti (l|log) [today] ti (e|edit) ti (i|interrupt) + ti --no-color ti -h | --help Options: @@ -37,6 +38,8 @@ import os, subprocess, tempfile from os import path import sys +from colorama import * + class JsonStore(object): def __init__(self, filename): @@ -58,12 +61,37 @@ class JsonStore(object): json.dump(data, f, separators=(',', ': '), indent=2) +def red(str): + if use_color: + return Fore.RED + str + Fore.RESET + else: + return str + +def green(str): + if use_color: + return Fore.GREEN + str + Fore.RESET + else: + return str + +def yellow(str): + if use_color: + return Fore.YELLOW + str + Fore.RESET + else: + return str + +def blue(str): + if use_color: + return Fore.BLUE + str + Fore.RESET + else: + return str + + def action_on(name, time): data = store.load() work = data['work'] if work and 'end' not in work[-1]: - print('You are already working on ' + work[-1]['name'] + + print('You are already working on ' + yellow(work[-1]['name']) + '. Stop it or use a different sheet.', file=sys.stderr) raise SystemExit(1) @@ -75,7 +103,7 @@ def action_on(name, time): work.append(entry) store.dump(data) - print('Start working on ' + name + '.') + print('Start working on ' + green(name) + '.') def action_fin(time, back_from_interrupt=True): @@ -86,7 +114,7 @@ def action_fin(time, back_from_interrupt=True): current = data['work'][-1] current['end'] = time store.dump(data) - print('So you stopped working on ' + current['name'] + '.') + print('So you stopped working on ' + red(current['name']) + '.') if back_from_interrupt and len(data['interrupt_stack']) > 0: name = data['interrupt_stack'].pop()['name'] @@ -113,7 +141,7 @@ def action_interrupt(name, time): interrupt_stack.append(interrupted) store.dump(data) - action_on('interrupt: ' + name, time) + action_on('interrupt: ' + green(name), time) print('You are now %d deep in interrupts.' % len(interrupt_stack)) @@ -130,7 +158,7 @@ def action_note(content): store.dump(data) - print('Yep, noted to `' + current['name'] + '`.') + print('Yep, noted to ' + yellow(current['name']) + '.') def action_tag(tags): @@ -162,9 +190,8 @@ def action_status(): start_time = parse_isotime(current['start']) diff = timegap(start_time, datetime.utcnow()) - print('You have been working on `{0[name]}` for {1}.' - .format(current, diff)) - + print('You have been working on {0} for {1}.' + .format(green(current['name']), diff)) def action_log(period): data = store.load() @@ -320,15 +347,21 @@ def helpful_exit(msg=__doc__): def parse_args(argv=sys.argv): + global use_color + argv = [arg.decode('utf-8') for arg in argv] + if '--no-color' in argv: + use_color = False + argv.remove('--no-color') + # prog = argv[0] if len(argv) == 1: helpful_exit('You must specify a command.') head = argv[1] tail = argv[2:] - + if head in ['-h', '--help', 'h', 'help']: helpful_exit() @@ -397,4 +430,5 @@ store = JsonStore(os.getenv('SHEET_FILE', None) or os.path.expanduser('~/.ti-sheet')) if __name__ == '__main__': + use_color = True main() diff --git a/requirements.txt b/requirements.txt index 40e4b7a..d0c3677 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ cram pygreen ghp-import pyyaml +colorama diff --git a/test/actions.t b/test/actions.t index 4832ed5..d096e1c 100644 --- a/test/actions.t +++ b/test/actions.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-actions - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Running an unknown action diff --git a/test/fin.t b/test/fin.t index 5dae820..b3ab89c 100644 --- a/test/fin.t +++ b/test/fin.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-fin - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Running fin when not working diff --git a/test/format.t b/test/format.t index 988d1d4..7567aca 100644 --- a/test/format.t +++ b/test/format.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-format - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Confirm no sheet file diff --git a/test/interrupt.t b/test/interrupt.t index f7e2fb9..07a602a 100644 --- a/test/interrupt.t +++ b/test/interrupt.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-actions - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Go two deep in interrupts diff --git a/test/note.t b/test/note.t index 6b181c9..8bac5bc 100644 --- a/test/note.t +++ b/test/note.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Note when not working diff --git a/test/on.t b/test/on.t index bcfe5bb..e2be3da 100644 --- a/test/on.t +++ b/test/on.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-on - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Start working diff --git a/test/status.t b/test/status.t index 20cba43..3f2ed39 100644 --- a/test/status.t +++ b/test/status.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-status - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Status when not working diff --git a/test/tag.t b/test/tag.t index 84e2ac4..6861c1f 100644 --- a/test/tag.t +++ b/test/tag.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-tag - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" When not working diff --git a/test/timing.t b/test/timing.t index 0d72b39..eafc6c7 100644 --- a/test/timing.t +++ b/test/timing.t @@ -1,7 +1,7 @@ Setup $ export SHEET_FILE=$TMP/sheet-timing - $ alias ti="$TESTDIR/../bin/ti" + $ alias ti="$TESTDIR/../bin/ti --no-color" Timing a start