aboutsummaryrefslogtreecommitdiffstats
path: root/todo
blob: eaecf645ab0e5f7df3315a4f6bef44e1dfa64336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
"""
Usage: todo [--id | --date | --priority] [--reverse] [--file <file>]

Options:
  -i --id        Sort by ID
  -d --date      Sort by date
  -p --priority  Sort by priority
  -r --reverse   Reverse sort order
  -f --file      Path to todo file
  -h --help      Show this screen
  -v --version   Show version
"""

from sys import exit
from collections import OrderedDict
from os import getenv, path
from docopt import docopt
import toml

def dict_sort_key(sort_key):
    '''Return a function for sorted() to sort a dict by the given key'''
    def key(item):
        return (sort_key not in item[1], item[1].get(sort_key, None))
    return key

def sort_dict_by_int(d, rev):
    '''Return dict sorted by int key'''
    sorted_dict = [ (key, d[key]) for key in sorted(d, key=int, reverse=rev) ]
    return OrderedDict(sorted_dict)

def sort_dict(todo_dict, sort_key, rev=False):
    todo_dict = sort_dict_by_int(todo_dict, rev)

    if sort_key != 'id':
        todo_list = list(todo_dict.items())
        todo_dict = sorted(todo_list, reverse=rev,
                           key=dict_sort_key(sort_key))

    return OrderedDict(todo_dict)

class Config():
    def __init__(self):
        self.todo_file = None
        self.sort_key  = None
        self.reverse   = False

def parse_args(args):
    config = Config()
    home_dir  = path.expanduser('~')
    data_home = getenv('XDG_DATA_HOME')

    if args['--id']:
        config.sort_key = 'id'
    elif args['--date']:
        config.sort_key = 'date'
    else:
        config.sort_key = 'priority'

    config.reverse = args['--reverse']

    if args['<file>']:
        config.todo_file = args['<file>']
    else:
        if data_home:
            config.todo_file = data_home + '/todo.toml'
        else:
            config.todo_file = home_dir + '/.local/share/todo.toml'

    return config

def color_bold(text):
    color_string = '\x1b[1m{}\x1b[0m'
    return color_string.format(text)

def color_red(text):
    color_string = '\x1b[38;2;250;050;050m{}\x1b[0m'
    return color_string.format(text)

def color_yellow(text):
    color_string = '\x1b[38;2;250;150;050m{}\x1b[0m'
    return color_string.format(text)

def color_blue(text):
    color_string = '\x1b[38;2;050;150;250m{}\x1b[0m'
    return color_string.format(text)

def format_priority(priority):
    text = '({})'.format(priority)
    if priority == 'A':
        return color_red(text)
    elif priority == 'B':
        return color_yellow(text)
    elif priority == 'C':
        return color_blue(text)
    else:
        return text

def main():
    arguments = docopt(__doc__, version='todo 0.0.1.alpha')
    config = parse_args(arguments)
    print(arguments)

    todo_dict = toml.load(config.todo_file)

    unsorted = OrderedDict(sorted(todo_dict.items()))
    todo_dict = sort_dict(unsorted, config.sort_key, config.reverse)

    print(todo_dict)

    for t_id in todo_dict:
        task = todo_dict[t_id]
        output = "{}. ".format(t_id)
        if 'priority' in task:
            output += '{} '.format(format_priority(task['priority']))
        if 'date' in task:
            output += "{}".format(task['date'])
        if 'description' in task:
            output += "\n    {}".format(task['description'])
        if 'url' in task:
            output += "\n    URL: {}".format(task['url'])
        if 'context' in task:
            output += "\n    Contexts: {}".format(task['context'])

        print(output)

if __name__ == '__main__':
    main()