Skip to content

Commit da72f54

Browse files
committed
Work with files
[+] write to files [~] read from files
1 parent 31d2dd7 commit da72f54

File tree

2 files changed

+84
-23
lines changed

2 files changed

+84
-23
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/target
22

33
/Cargo.lock
4+
tasks
5+
.tasks.done

src/main.rs

+82-23
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::env;
88
use std::fs;
99
use std::path::Path;
1010
use std::path::PathBuf;
11-
use std::process::exit;
11+
1212
fn main() {
1313
let args: Vec<String> = env::args().collect();
1414
let mut opts = Options::new();
@@ -60,50 +60,109 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]";
6060

6161
println!("taskdir: {}", taskpath.to_str().unwrap().to_string());
6262

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);
6664

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+
}
6871

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+
}
7584
}
7685

77-
fn hash(str: String) -> String {
86+
fn hash(str: &String) -> String {
7887
let mut hasher = Sha256::new();
7988
hasher.input_str(&str);
8089
hasher.result_str()
8190
}
8291

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+
91110
let contents = fs::read_to_string(taskpath).unwrap_or_else(|_| "".to_string());
92111
println!("{}", contents);
93112

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());
95114
println!("{}", contents);
96115

97116
let mut tasks: HashMap<String, String> = HashMap::new();
98117
let mut done: HashMap<String, String> = HashMap::new();
99118

100119
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());
102121
}
103122

104123
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());
106125
}
107126

108127
(tasks, done)
109128
}
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

Comments
 (0)