@@ -8,7 +8,7 @@ use std::env;
8
8
use std:: fs;
9
9
use std:: path:: Path ;
10
10
use std:: path:: PathBuf ;
11
- use std :: process :: exit ;
11
+
12
12
fn main ( ) {
13
13
let args: Vec < String > = env:: args ( ) . collect ( ) ;
14
14
let mut opts = Options :: new ( ) ;
@@ -60,50 +60,109 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]";
60
60
61
61
println ! ( "taskdir: {}" , taskpath. to_str( ) . unwrap( ) . to_string( ) ) ;
62
62
63
- let donefile = format ! ( ".{}.done" , taskfile) ;
64
- let mut donepath = PathBuf :: from ( & taskdir) ;
65
- donepath. push ( donefile) ;
63
+ let ( mut tasks, done) = read_files ( & taskpath) ;
66
64
67
- let ( tasks, done) = read_files ( taskpath, donepath) ;
65
+ if matches. opt_present ( "done" ) {
66
+ for ( hash, task) in done {
67
+ println ! ( "{} - {}" , hash, task) ;
68
+ }
69
+ return ;
70
+ }
68
71
69
- let input = if !matches. free . is_empty ( ) {
70
- matches. free [ 0 ] . clone ( )
71
- } else {
72
- "Print tasks" . to_string ( )
73
- } ;
74
- println ! ( "{}" , input) ;
72
+ if !matches. free . is_empty ( ) {
73
+ let task = matches. free . join ( " " ) ;
74
+ tasks. insert ( hash ( & task) , task) ;
75
+ let delete_empty = matches. opt_present ( "d" ) ;
76
+ println ! ( "{:?}" , delete_empty) ;
77
+ write_files ( tasks, done, taskpath, delete_empty) ;
78
+ return ;
79
+ }
80
+
81
+ for ( hash, task) in tasks {
82
+ println ! ( "{} - {}" , hash, task) ;
83
+ }
75
84
}
76
85
77
- fn hash ( str : String ) -> String {
86
+ fn hash ( str : & String ) -> String {
78
87
let mut hasher = Sha256 :: new ( ) ;
79
88
hasher. input_str ( & str) ;
80
89
hasher. result_str ( )
81
90
}
82
91
83
- fn read_files (
84
- taskpath : PathBuf ,
85
- donefile : PathBuf ,
86
- ) -> ( HashMap < String , String > , HashMap < String , String > ) {
87
- if !Path :: new ( & taskpath) . exists ( ) {
88
- println ! ( "File {} does not exist..." , taskpath. to_str( ) . unwrap( ) ) ;
89
- exit ( 1 ) ;
90
- }
92
+ fn read_files ( taskpath : & PathBuf ) -> ( HashMap < String , String > , HashMap < String , String > ) {
93
+ // if !Path::new(&taskpath).exists() {
94
+ // println!("File {} does not exist...", taskpath.to_str().unwrap());
95
+ // exit(1);
96
+ // }
97
+ let donefile = format ! (
98
+ ".{}.done" ,
99
+ taskpath
100
+ . as_path( )
101
+ . file_name( )
102
+ . unwrap( )
103
+ . to_os_string( )
104
+ . into_string( )
105
+ . unwrap( )
106
+ ) ;
107
+ let mut donepath = PathBuf :: from ( taskpath. as_path ( ) . parent ( ) . unwrap ( ) . to_path_buf ( ) ) ;
108
+ donepath. push ( donefile) ;
109
+
91
110
let contents = fs:: read_to_string ( taskpath) . unwrap_or_else ( |_| "" . to_string ( ) ) ;
92
111
println ! ( "{}" , contents) ;
93
112
94
- let contents_done = fs:: read_to_string ( donefile ) . unwrap_or_else ( |_| "" . to_string ( ) ) ;
113
+ let contents_done = fs:: read_to_string ( donepath ) . unwrap_or_else ( |_| "" . to_string ( ) ) ;
95
114
println ! ( "{}" , contents) ;
96
115
97
116
let mut tasks: HashMap < String , String > = HashMap :: new ( ) ;
98
117
let mut done: HashMap < String , String > = HashMap :: new ( ) ;
99
118
100
119
for line in contents. lines ( ) {
101
- tasks. insert ( hash ( line. to_string ( ) ) , line. to_string ( ) ) ;
120
+ tasks. insert ( hash ( & line. to_string ( ) ) , line. to_string ( ) ) ;
102
121
}
103
122
104
123
for line in contents_done. lines ( ) {
105
- done. insert ( hash ( line. to_string ( ) ) , line. to_string ( ) ) ;
124
+ done. insert ( hash ( & line. to_string ( ) ) , line. to_string ( ) ) ;
106
125
}
107
126
108
127
( tasks, done)
109
128
}
129
+
130
+ fn write_files (
131
+ tasks : HashMap < String , String > ,
132
+ done : HashMap < String , String > ,
133
+ taskpath : PathBuf ,
134
+ delete_empty : bool ,
135
+ ) {
136
+ let donefile = format ! (
137
+ ".{}.done" ,
138
+ taskpath
139
+ . as_path( )
140
+ . file_name( )
141
+ . unwrap( )
142
+ . to_os_string( )
143
+ . into_string( )
144
+ . unwrap( )
145
+ ) ;
146
+ let mut donepath = PathBuf :: from ( taskpath. as_path ( ) . parent ( ) . unwrap ( ) . to_path_buf ( ) ) ;
147
+ donepath. push ( donefile) ;
148
+
149
+ if delete_empty {
150
+ if Path :: new ( & taskpath) . exists ( ) {
151
+ fs:: remove_file ( taskpath) . unwrap ( ) ;
152
+ fs:: remove_file ( donepath) . unwrap ( ) ;
153
+ }
154
+ return ;
155
+ }
156
+ let mut data = String :: new ( ) ;
157
+ for ( hash, task) in & tasks {
158
+ data = format ! ( "{}\n {} - {}\n " , data, hash, task) ;
159
+ }
160
+ println ! ( "{:?}" , tasks) ;
161
+ println ! ( "{:?}" , data) ;
162
+ fs:: write ( taskpath, data) . expect ( "Unable to write task file" ) ;
163
+ let mut data = String :: new ( ) ;
164
+ for ( hash, task) in done {
165
+ data = format ! ( "{}\n {} - {}\n " , data, hash, task) ;
166
+ }
167
+ fs:: write ( donepath, data) . expect ( "Unable to write donefile" ) ;
168
+ }
0 commit comments