todo_manager 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/LICENSE +13 -0
  2. data/README +16 -0
  3. data/bin/todo_manager +39 -0
  4. data/lib/todo_manager.rb +166 -0
  5. metadata +50 -0
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ Todo manager with a command-line interface
2
+
3
+ Author: Xiao Jia <me@xiao-jia.com>
4
+
5
+ Dependency:
6
+ ruby (mandatory; please install it using https://rvm.io if possible)
7
+ git (optional if you don't need to upload TODOs to gist)
8
+
9
+ Installation:
10
+ gem install highline
11
+ gem install todo_manager
12
+ # Personally, I also make an alias for the executable:
13
+ alias t=todo_manager
14
+
15
+ Usage:
16
+ todo_manager --help
data/bin/todo_manager ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'todo_manager'
4
+
5
+ commands = {
6
+ :'-h' => :help,
7
+ :'--help' => :help,
8
+ :help => :help,
9
+ :r => :remove,
10
+ :rm => :remove,
11
+ :remove => :remove,
12
+ :gist => :prepare,
13
+ :u => :update,
14
+ :up => :update,
15
+ :update => :update,
16
+ :c => :commit,
17
+ :cm => :commit,
18
+ :commit => :commit
19
+ }
20
+
21
+ tm = TodoManager.new
22
+
23
+ if ARGV.length == 0
24
+ tm.list
25
+ elsif ARGV.length == 1 and tm.is_todo_id? ARGV.first
26
+ tm.show ARGV.first
27
+ elsif ARGV.length >= 1 and cmd = commands[ARGV.first.to_sym] and tm.respond_to? cmd
28
+ if ARGV.length == 1
29
+ tm.send cmd
30
+ else
31
+ tm.send cmd, ARGV[1..-1].join(" ")
32
+ end
33
+ else
34
+ if tm.is_todo_id? ARGV.first
35
+ tm.edit ARGV.first, ARGV[1..-1].join(" ")
36
+ else
37
+ tm.add ARGV.join(" ")
38
+ end
39
+ end
@@ -0,0 +1,166 @@
1
+ require 'fileutils'
2
+ require 'tmpdir'
3
+ require 'yaml'
4
+ require 'highline/import'
5
+
6
+ class TodoManager
7
+
8
+ class Todo
9
+ attr_reader :id
10
+ attr_writer :text
11
+
12
+ def initialize id, text
13
+ @id = id
14
+ @text = text
15
+ end
16
+
17
+ def to_s
18
+ "#@id - #@text"
19
+ end
20
+
21
+ def to_hash
22
+ {:id => @id, :text => @text}
23
+ end
24
+ end
25
+
26
+ def initialize
27
+ FileUtils.touch todos_path
28
+ @todos = YAML.load(File.read(todos_path)) || []
29
+ @todos = @todos.collect {|t| Todo.new(t[:id], t[:text]) }
30
+ @todos.sort! {|x,y| x.id <=> y.id }
31
+ end
32
+
33
+ def help
34
+ puts usage = <<USAGE
35
+ Usage:
36
+ t --help Print this help message; also: `-h` or `help`
37
+ t List all todo items stored in $HOME/.todo
38
+ t "todo content" Add todo item
39
+ t a4 Show content of todo item a4
40
+ t b3 "new content" Change todo item b3
41
+ t remove c4 Finish (or cancel) todo item c4; also: `r` or `rm`
42
+ t gist <URL> Set an existing gist URL (stored in $HOME/.todorc)
43
+ t update Update todo items from gist; also: `u` or `up`
44
+ t commit pdate todo items to gist; also: `c` or `cm`
45
+ USAGE
46
+ end
47
+
48
+ def list
49
+ puts @todos
50
+ end
51
+
52
+ def is_todo_id? id
53
+ not find_by(id).empty?
54
+ end
55
+
56
+ def show id
57
+ puts find_by(id)
58
+ end
59
+
60
+ def add text
61
+ id = next_id
62
+ @todos << Todo.new(id, text)
63
+ save_todos
64
+ puts id
65
+ end
66
+
67
+ def remove id
68
+ @todos.delete_if {|todo| todo.id == id }
69
+ save_todos
70
+ end
71
+
72
+ def edit id, text
73
+ find_by(id).first.text = text
74
+ save_todos
75
+ end
76
+
77
+ def prepare gist=nil
78
+ if gist
79
+ File.open(todorc, 'w') {|f| f.write({:gist => gist}.to_yaml) }
80
+ else
81
+ rc = load_todorc
82
+ puts rc[:gist]
83
+ end
84
+ end
85
+
86
+ def update
87
+ open_gist do |gist|
88
+ if yesno "Overwrite #{todos_path}?"
89
+ copy :from => gist_todo, :to => todos_path
90
+ end
91
+ end
92
+ end
93
+
94
+ def commit
95
+ open_gist do |gist|
96
+ if yesno "Override #{gist}/#{gist_todo}?"
97
+ copy :from => todos_path, :to => gist_todo
98
+ `git add .`
99
+ `git commit -m 'update'`
100
+ `git push -f origin master`
101
+ end
102
+ end
103
+ end
104
+
105
+ protected
106
+ def open_gist
107
+ rc = load_todorc
108
+ Dir.mktmpdir do |dir|
109
+ Dir.chdir(dir) do
110
+ `git clone #{rc[:gist]} .`
111
+ yield rc[:gist]
112
+ end
113
+ end
114
+ end
115
+
116
+ def gist_todo
117
+ 'todo.yaml'
118
+ end
119
+
120
+ def copy opts={}
121
+ FileUtils.touch opts[:from]
122
+ FileUtils.cp opts[:from], opts[:to]
123
+ end
124
+
125
+ # Handy yes/no prompt for little Ruby scripts
126
+ def yesno prompt, default=true
127
+ s = default ? '[Y/n]' : '[y/N]'
128
+ d = default ? 'y' : 'n'
129
+ a = ''
130
+ until %w[y n].include? a
131
+ a = ask("#{prompt} #{s} ") {|q| q.limit = 1; q.case = :downcase }
132
+ a = d if a.length == 0
133
+ end
134
+ a == 'y'
135
+ end
136
+
137
+ def todos_path
138
+ File.expand_path('~/.todo')
139
+ end
140
+
141
+ def todorc
142
+ File.expand_path('~/.todorc')
143
+ end
144
+
145
+ def load_todorc
146
+ FileUtils.touch todorc
147
+ YAML.load(File.read(todorc)) || {}
148
+ end
149
+
150
+ def save_todos
151
+ File.open(todos_path, 'w') {|f| f.write @todos.collect(&:to_hash).to_yaml }
152
+ end
153
+
154
+ def next_id
155
+ ('a'..'z').each do |c|
156
+ ('1'..'5').each do |i|
157
+ id = c + i
158
+ return id unless is_todo_id? id
159
+ end
160
+ end
161
+ end
162
+
163
+ def find_by id
164
+ @todos.select {|todo| todo.id == id }
165
+ end
166
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: todo_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Xiao Jia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple todo manager gem with a command-line interface. You can also
15
+ upload your todo items to a git/gist repository.
16
+ email: me@xiao-jia.com
17
+ executables:
18
+ - todo_manager
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - README
23
+ - LICENSE
24
+ - lib/todo_manager.rb
25
+ - bin/todo_manager
26
+ homepage: http://xiao-jia.com/todo_manager/
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Todo Manager
50
+ test_files: []