#!/usr/bin/env python3 """ Usage: todo [--id | --date | --priority] [--reverse] [--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 sort_toml(toml_dict, sort_key, rev=False): return sorted( list(toml_dict.items()), key=lambda d: (sort_key not in d[1], d[1].get(sort_key, None)), reverse=rev) 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['']: config.todo_file = args[''] 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 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())) sorted_toml = OrderedDict(sort_toml(unsorted, config.sort_key, config.reverse)) print(sorted_toml) for t_id in sorted_toml: task = sorted_toml[t_id] output = "{}. ".format(t_id) if 'priority' in task: output += "({}) ".format(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()