-
Notifications
You must be signed in to change notification settings - Fork 0
/
redo.red
119 lines (100 loc) · 2.71 KB
/
redo.red
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
Red [
title: "ReDo - Command Line ToDo App"
author: "Semseddin (Endo64) Moldibi"
version: 1.0.0
date: 2022-01-21
license: "MIT"
file: %redo.red
home: https://github.com/endo64/redo
]
;Application context
app: context [
title: "Redo - Command Line ToDo v1.0.0"
help-text: {Commands:
no argument : list todos
<text> : add new todo
<id> - : delete todo
<id> : toggle todo
<id> <text> : update todo
* : purge todos (delete marked todos)
? : show this help
}
todos: []
todo-file: %redo.todo
;Keep overwritten system words
load*: :system/words/load
save*: :system/words/save
add*: :system/words/add
remove*: :system/words/remove
run: function [/local id] [
;Charsets & Parse rules
ws: charset reduce [#" " tab]
nws: complement ws
digit: charset [#"0" - #"9"]
idr: [copy id 1 3 digit]
;Parse command line arguments
parse trim system/script/args [
end (list)
|
"?" (help)
|
"*" (purge save list)
|
idr some ws "-" (remove load* id save list)
|
idr some ws copy text to end (update load* id text save list)
|
idr end (toggle load* id save list)
|
copy text to end (add text save list)
]
]
help: does [print [title newline help-text newline]]
load: function [/extern todos] [
if exists? todo-file [
todos: load* todo-file
]
]
save: function [] [
save* todo-file new-line/skip todos on 2
]
list: function [] [
if empty? todos [print "Nothing To-Do! ✨" exit]
print ["ID" tab "Done?" tab "Text"]
i: 1
foreach [mark text] todos [
print [pad/with/left i 3 #"0" tab pick "✘✔" mark + 1 tab text]
i: i + 1
]
]
add: function [text] [
append todos reduce [0 text]
]
update: function [id text] [
unless pos: pos? id [exit]
change next pos text
]
remove: function [id] [
unless pos: pos? id [exit]
remove*/part pos 2
]
toggle: function [id] [
unless pos: pos? id [exit]
change pos 1 - pos/1
]
purge: does [
todos: parse todos [collect [some [keep quote 0 keep string! | skip]]]
]
pos?: function [id] [
if empty? m: at todos 2 * id - 1 [
print ["ID:" id "not found." newline]
return none
]
m
]
]
;App start
do bind [
load
run
] app