Skip to content

Commit

Permalink
added auto-save settings option
Browse files Browse the repository at this point in the history
- simplified docopt example usage
- some variable renaming with positive logic
  • Loading branch information
AnthonyDiGirolamo committed Dec 2, 2015
1 parent db02e60 commit b47757a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -81,6 +81,7 @@ define new themes. Here is a short example:
[settings]
file = ~/todo.txt
archive = ~/done.txt
auto-save = True
colorscheme = myawesometheme

Color Schemes
Expand Down
1 change: 1 addition & 0 deletions README.rst
Expand Up @@ -93,6 +93,7 @@ colorscheme or even define new themes. Here is a short example:
[settings]
file = ~/todo.txt
archive = ~/done.txt
auto-save = True
colorscheme = myawesometheme

Color Schemes
Expand Down
36 changes: 24 additions & 12 deletions todotxt_machine/cli.py
Expand Up @@ -4,9 +4,7 @@
"""todotxt-machine
Usage:
todotxt-machine
todotxt-machine TODOFILE [DONEFILE]
todotxt-machine [--config FILE]
todotxt-machine [--config FILE] [TODOFILE] [DONEFILE]
todotxt-machine (-h | --help)
todotxt-machine --version
todotxt-machine --show-default-bindings
Expand All @@ -27,6 +25,7 @@
from time import sleep

# import ipdb; # ipdb.set_trace()

# import pprint
# pp = pprint.PrettyPrinter(indent=4).pprint

Expand All @@ -46,19 +45,20 @@
from todotxt_machine.colorscheme import ColorScheme
from todotxt_machine.keys import KeyBindings

lock = threading.Lock()
autosave_lock = threading.Lock()

def autosave():
if (disable_autosave):
if not enable_autosave:
return

# print "autosaving..."
with lock:
with autosave_lock:
# view.save_todos() # TODO: Saved message isn't displayed, need to force redraw urwid ui?
view.todos.save()

# Check the flag once again, as the flag may have been set
# after the last check but before this statement is executed.
if (disable_autosave == False):
if enable_autosave:
global timer
timer = threading.Timer(30.0, autosave)
timer.start()
Expand Down Expand Up @@ -92,6 +92,7 @@ def main():

# Parse command line
arguments = docopt(__doc__, version=todotxt_machine.version)
# pp(arguments) ; exit(0)

# Validate readline editing mode option (docopt doesn't handle this)
# if arguments['--readline-editing-mode'] not in ['vi', 'emacs']:
Expand All @@ -116,6 +117,16 @@ def main():
# load the colorscheme defined in the user config, else load the default scheme
colorscheme = ColorScheme(dict( cfg.items('settings') ).get('colorscheme', 'default'), cfg)

# Get auto-saving setting (defaults to False)
global enable_autosave
enable_autosave = dict( cfg.items('settings')).get('auto-save', False)
if (type(enable_autosave) != bool and
(str(enable_autosave).lower() == 'true' or
str(enable_autosave).lower() == '1')):
enable_autosave = True
else:
enable_autosave = False

# Load the todo.txt file specified in the [settings] section of the config file
# a todo.txt file on the command line takes precedence
todotxt_file = dict( cfg.items('settings') ).get('file', arguments['TODOFILE'])
Expand Down Expand Up @@ -149,17 +160,18 @@ def main():
global view
view = UrwidUI(todos, keyBindings, colorscheme)

global disable_autosave
disable_autosave = False
timer.start()

view.main()
view.main() # start up the urwid UI event loop

# UI is now shut down

# Shut down the auto-saving thread.
disable_autosave = True
enable_autosave = False
timer.cancel()

with lock:
# Final save
with autosave_lock:
# print("Writing: {0}".format(todotxt_file_path))
view.todos.save()

Expand Down

0 comments on commit b47757a

Please sign in to comment.