-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo
executable file
·211 lines (166 loc) · 4.75 KB
/
todo
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
#
# rotsix - (c) wtfpl 2017
# a smal todo list manager using notify-send
import getopt
import os, sys
from subprocess import call
# image is used by notify-send, set to "" to disable
home = os.environ["HOME"]
# image = f"{home}/usr/wallpapers/pattern/wall.png"
image = f"{home}/bin/todo-icon.svg"
def usage():
print("todo [-r | --read] # default option")
print("todo [-i | --insert] <number> <note>")
print("todo [-m | --move] '<origine> <destination>' /!\\")
print("todo [-a | --add] <note>")
print("todo [-d | --delete] <number>")
print("todo [-c | --clean]")
def move(orig, dest):
tmp = delete(orig)
insert(dest - 1, tmp.replace("\n", ""))
popup()
def insert(number, note):
# get current lines
try:
todo_file = open(f"{home}/.todo", "r")
lines = todo_file.readlines()
todo_file.close()
except FileNotFoundError:
print("Sorry, $HOME/.todo doesn't exist.")
print("Add an element and it will work properly again!")
usage()
sys.exit(1)
if number == len(lines):
add(note)
elif number < 0 or number > len(lines):
print("You can't insert this note here")
usage()
sys.exit(1)
lines.insert(number, note + "\n")
with open(f"{home}/.todo", "w") as todo_file:
for i in lines:
todo_file.write(i)
popup()
def add(note):
with open(f"{home}/.todo", "a") as todo_file:
todo_file.write(note + "\n")
popup()
return note
def delete(number):
# now we open
try:
todo_file = open(f"{home}/.todo", "r")
lines = todo_file.readlines()
todo_file.close()
except FileNotFoundError:
print("Sorry, $HOME/.todo doesn't exist.")
print("Add an element and it will work properly again!")
usage()
sys.exit(1)
todo_file = open(f"{home}/.todo", "w")
# deleting
if len(lines) != 0 and (number <= 0 or number > len(lines)):
todo_file.close()
usage()
print("You can't delete a note that doesn't exist")
sys.exit(1)
# putting back
tmp = None
for i in range(len(lines)):
if i != number - 1:
todo_file.write(lines[i])
else:
tmp = lines[i]
todo_file.close()
popup()
return tmp
def popup():
global image
# now we open/read/close
try:
todo_file = open(f"{home}/.todo", "r")
except FileNotFoundError:
print("Sorry, $HOME/.todo doesn't exist.")
print("Add an element and it will work properly again!")
usage()
sys.exit(1)
todo = todo_file.read()
todo_file.close()
# formating text
todo = todo.split("\n")
# message for notify-send
mess = (
"<span foreground='#75507b'> Todo Weekday</span>\n<span foreground='#627a92'>"
" ============</span>\n<span foreground='#b2a191'>"
)
i = 1
for note in todo:
if i == len(todo):
break
if i == len(todo) - 1:
mess += f"{str(i)}. {note}"
else:
mess += f"{str(i)}. {note}\n"
i += 1
if len(todo) == 1:
mess += "No tasks."
mess += "</span>"
# and final popup
call(["notify-send", "-i", image, mess])
def clean():
try:
todo_file = open(f"{home}/.todo", "w")
todo_file.close()
except FileNotFoundError:
print(f"Sorry, {home}/.todo doesn't exist.")
sys.exit(1)
popup()
def main(argv):
if len(argv) == 0:
popup()
sys.exit(0)
try:
opts, argv = getopt.getopt(
argv,
"hi:a:d:m:rc",
["help", "insert", "add", "delete", "move", "read", "clean"],
)
except getopt.GetoptError:
usage()
sys.exit(1)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
elif opt in ("-i", "--insert"):
try:
number = int(arg)
except ValueError:
usage()
sys.exit(1)
insert(number, argv[0])
elif opt in ("-a", "--add"):
add(arg)
elif opt in ("-d", "--delete"):
try:
number = int(arg)
except ValueError:
usage()
sys.exit(1)
delete(number)
elif opt in ("-r", "--read"):
popup()
elif opt in ("-c", "--clean"):
clean()
elif opt in ("-m", "--move"):
tmp = arg.split(" ")
try:
tmp[0] = int(tmp[0])
tmp[1] = int(tmp[1])
except ValueError:
usage()
sys.exit(1)
move(tmp[0], tmp[1])
popup()
if __name__ == "__main__":
main(sys.argv[1:])