-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
131 lines (113 loc) · 3.35 KB
/
todo.js
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
const fs = require('fs')
const command = process.argv[2]
const payload = process.argv[3]
const addPayload = (payload) => {
if(payload===undefined){
console.log('Error: Missing todo string. Nothing added!')
return
}
fs.appendFileSync('todo.txt',payload+'\n')
console.log(`Added todo: "${payload}"`)
}
const showRemaining = ()=>{
try{
const list = fs.readFileSync('todo.txt').toString('utf8').split('\n')
list.pop()
list.reverse()
let size=list.length
let result = ''
for(i=0;i<size;i++){
result += `[${size-i}] ${list[i]}\n`
}
console.log(result)
}
catch(err){
console.log('There are no pending todos!')
}
}
const deleteTodo = (payload) => {
const list = fs.readFileSync('todo.txt').toString()
a=list.split('\n')
a.pop()
if(payload<1 || payload>a.length)
{
console.log(`Error: todo #${payload} does not exist. Nothing deleted.`)
return
}
a.splice(payload-1,1)
console.log(`Deleted todo #${payload}`)
let b=''
for(i=0;i<a.length;i++)
{
b=b+a[i]+'\n'
}
fs.writeFileSync('todo.txt',b)
}
const complete = (payload)=>{
const list = fs.readFileSync('todo.txt').toString()
a=list.split('\n')
a.pop()
if(payload<1 || payload>a.length)
{
console.log(`Error: todo #${payload} does not exist.`)
return
}
const deleted = a.splice(payload-1,1)
console.log(`Marked todo #${payload} as done.`)
let b=''
for(i=0;i<a.length;i++)
{
b=b+a[i]+'\n'
}
fs.writeFileSync('todo.txt',b)
fs.appendFileSync('done.txt',`x ${new Date().toISOString().slice(0,10)} ${deleted}\n`)
}
const help = ( ) =>{
let usage = `Usage :-
$ ./todo add "todo item" # Add a new todo
$ ./todo ls # Show remaining todos
$ ./todo del NUMBER # Delete a todo
$ ./todo done NUMBER # Complete a todo
$ ./todo help # Show usage
$ ./todo report # Statistics`
console.log(usage)
}
const statistics = ()=>{
const list = fs.readFileSync('todo.txt').toString()
a=list.split('\n')
a.pop()
const list1 = fs.readFileSync('done.txt').toString()
b=list1.split('\n')
b.pop()
const toBePrint = ((new Date().toISOString().slice(0,10))+' Pending : '+a.length+' Completed : '+b.length).trim().toString('utf8')
console.log(toBePrint)
}
switch(command){
case undefined: help()
break
case 'add' : addPayload(payload)
break
case 'ls' : showRemaining()
break
case 'del': if(payload===undefined){
console.log('Error: Missing NUMBER for deleting todo.')
break
}
else{
deleteTodo(Number(payload))
break
}
case 'done':
if(payload===undefined){
console.log('Error: Missing NUMBER for marking todo as done.')
break
}else{
complete(Number(payload))
break
}
case 'help' : help()
break
case 'report': statistics()
break
default : console.log('error')
}