@@ -3,6 +3,7 @@ extern crate colored;
3
3
extern crate dirs;
4
4
extern crate serde;
5
5
extern crate serde_json;
6
+ extern crate home;
6
7
7
8
#[ macro_use]
8
9
extern crate serde_derive;
@@ -12,9 +13,9 @@ use std::process;
12
13
13
14
use clap:: { App , Arg } ;
14
15
15
- mod todo_item ;
16
+ mod item ;
16
17
17
- use todo_item :: TodoItem ;
18
+ use item :: ItemRepository ;
18
19
19
20
fn main ( ) {
20
21
let matches = App :: new ( "Todo" )
@@ -35,18 +36,7 @@ fn main() {
35
36
. help ( "edit an entry" )
36
37
. value_delimiter ( "-" ) ,
37
38
)
38
- . arg (
39
- Arg :: with_name ( "TEXT" )
40
- . help ( "The todo item text" )
41
- . index ( 1 )
42
- )
43
- . arg (
44
- Arg :: with_name ( "priority" )
45
- . short ( "p" )
46
- . long ( "priority" )
47
- . help ( "change the priority of an entry" )
48
- . value_delimiter ( " " ) ,
49
- )
39
+ . arg ( Arg :: with_name ( "TEXT" ) . help ( "The todo item text" ) . index ( 1 ) )
50
40
. arg (
51
41
Arg :: with_name ( "complete" )
52
42
. short ( "c" )
@@ -57,112 +47,61 @@ fn main() {
57
47
)
58
48
. get_matches ( ) ;
59
49
60
- let f = match todo_item :: get_todo_file ( ) {
61
- Ok ( text ) => text ,
50
+ let mut repository = match ItemRepository :: new ( ) {
51
+ Ok ( r ) => r ,
62
52
Err ( e) => {
63
- eprintln ! ( "Could not read todo file : {}" , e) ;
53
+ eprintln ! ( "Failed to load items : {}" , e) ;
64
54
process:: exit ( 1 ) ;
65
55
}
66
56
} ;
67
- let mut items: Vec < TodoItem > = match serde_json:: from_str ( & f) {
68
- Ok ( items) => items,
69
- Err ( _) => Vec :: new ( ) ,
70
- } ;
71
-
72
- // Sort items by priority 1 = highest, Infinity = lowest
73
- items. sort_by ( |a, b| a. priority . cmp ( & b. priority ) ) ;
74
57
75
58
// Delete items
76
59
if let Some ( item_id) = matches. value_of ( "delete" ) {
77
- let item_id = match item_id. parse :: < usize > ( ) {
78
- Ok ( id) => id ,
60
+ match item_id. parse :: < usize > ( ) {
61
+ Ok ( id) => repository . delete ( id ) ,
79
62
Err ( e) => {
80
63
eprintln ! ( "Could not mark item as complete: {}" , e) ;
81
64
process:: exit ( 1 ) ;
82
65
}
83
66
} ;
84
-
85
- if item_id >= items. len ( ) {
86
- eprintln ! ( "Could not find item with id: {}" , item_id) ;
87
- process:: exit ( 1 ) ;
88
- }
89
-
90
- items. remove ( item_id) ;
91
67
}
92
68
93
69
// Toggle completion of items
94
70
if let Some ( item_id) = matches. value_of ( "complete" ) {
95
71
match item_id. parse :: < usize > ( ) {
96
- Ok ( id) => {
97
- match items. get_mut ( id) {
98
- Some ( item) => item. toggle_complete ( ) ,
99
- None => eprintln ! ( "Could not mark item {} as complete, it doesn't exist" , id)
100
- } ;
101
- }
72
+ Ok ( id) => repository. toggle ( id) ,
102
73
Err ( e) => {
103
74
eprintln ! ( "Could not mark item as complete: {}" , e) ;
104
75
process:: exit ( 1 ) ;
105
76
}
106
77
} ;
107
78
}
108
79
109
- // Edit existing item
110
- if let Some ( item_id) = matches. value_of ( "edit" ) {
111
- match item_id. parse :: < usize > ( ) {
112
- Ok ( id) => {
113
- match items. get_mut ( id) {
114
- Some ( item) => {
115
- item. text = matches. value_of ( "TEXT" ) . unwrap_or ( "EMPTY" ) . to_string ( )
116
- }
117
- None => ( ) ,
118
- } ;
119
- }
120
- Err ( e) => {
121
- eprintln ! ( "Could not edit item: {}" , e) ;
122
- process:: exit ( 1 ) ;
123
- }
124
- } ;
125
- }
126
-
127
- // Change priority of item
128
- if let Some ( item_id) = matches. value_of ( "priority" ) {
129
- match item_id. parse :: < usize > ( ) {
130
- Ok ( id) => {
131
- // FIXME: Yuck
132
- if let Some ( item) = items. get_mut ( id) {
133
- if let Some ( priority) = matches. value_of ( "TEXT" ) {
134
- if let Ok ( priority) = priority. parse :: < usize > ( ) {
135
- item. priority = priority
136
- }
137
- }
80
+ if let Some ( text) = matches. value_of ( "TEXT" ) {
81
+ if let Some ( item_id) = matches. value_of ( "edit" ) {
82
+ match item_id. parse :: < usize > ( ) {
83
+ Ok ( id) => repository. update_text ( id, text) ,
84
+ Err ( e) => {
85
+ eprintln ! ( "Could not edit item: {}" , e) ;
86
+ process:: exit ( 1 ) ;
138
87
}
139
88
}
140
- Err ( e) => {
141
- eprintln ! ( "Could not edit item: {}" , e) ;
142
- process:: exit ( 1 ) ;
143
- }
144
- } ;
145
- }
146
-
147
- if let Some ( text) = matches. value_of ( "TEXT" ) {
148
- items. push ( TodoItem :: new ( text, false , 1 ) ) ;
149
- }
150
-
151
- if let Err ( e) = todo_item:: update_todo_file ( & items) {
152
- eprintln ! ( "Failed to update todo file: {}" , e) ;
153
- process:: exit ( 1 ) ;
89
+ } else {
90
+ repository. add ( text) ;
91
+ }
154
92
}
155
93
156
- for ( i , item) in items . into_iter ( ) . enumerate ( ) {
94
+ for item in repository . items ( ) {
157
95
let text = if item. completed {
158
96
item. text . green ( )
159
97
} else {
160
98
item. text . yellow ( )
161
99
} ;
162
100
163
- println ! ( "{} - {}" , i , text) ;
101
+ println ! ( "{} - {}" , item . id , text) ;
164
102
}
165
103
166
- // this probably ins't necesarry...but it feels wrong to _assume_
167
- process:: exit ( 0 ) ;
168
- }
104
+ if let Err ( e) = repository. publish ( ) {
105
+ eprintln ! ( "Failed to publish todo file: {}" , e) ;
106
+ }
107
+ }
0 commit comments