Skip to content

Commit

Permalink
Merge pull request sharat87#11 from MattHulse/colorize_tasks
Browse files Browse the repository at this point in the history
Colorize tasks
  • Loading branch information
sharat87 committed Oct 23, 2014
2 parents 850765f + 7652635 commit 71fb76f
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 18 deletions.
52 changes: 43 additions & 9 deletions bin/ti
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Usage:
ti (l|log) [today]
ti (e|edit)
ti (i|interrupt)
ti --no-color
ti -h | --help
Options:
Expand All @@ -37,6 +38,8 @@ import os, subprocess, tempfile
from os import path
import sys

from colorama import *

class JsonStore(object):

def __init__(self, filename):
Expand All @@ -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)

Expand All @@ -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):
Expand All @@ -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']
Expand All @@ -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))


Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -397,4 +430,5 @@ store = JsonStore(os.getenv('SHEET_FILE', None) or
os.path.expanduser('~/.ti-sheet'))

if __name__ == '__main__':
use_color = True
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ cram
pygreen
ghp-import
pyyaml
colorama
2 changes: 1 addition & 1 deletion test/actions.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/fin.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/format.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/interrupt.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/note.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/on.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/status.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/tag.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/timing.t
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 71fb76f

Please sign in to comment.