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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
#include <filesystem>
#include <fstream>
#include <unordered_map>
#include "functions.hpp"
#include "opts/cxxopts.hpp"
namespace fs = std::filesystem;
std::unordered_map<std::string, std::string> tasks = {};
std::unordered_map<std::string, std::string> tasksDone = {};
std::unordered_map<std::string, std::string> prefixes = {};
fs::path taskpath, donepath;
std::string taskdir;
std::string taskfile;
std::string sha256_hash(std::string text) {
std::vector<unsigned char> hash(picosha2::k_digest_size);
picosha2::hash256(text.begin(), text.end(), hash.begin(), hash.end());
std::string hex_str = picosha2::bytes_to_hex_string(hash.begin(), hash.end());
return hex_str;
}
std::string prefix(std::string hash) {
std::string prefix;
for (size_t i = 1; i <= hash.length(); i++) {
prefix = hash.substr(0, i);
if (tasks.find(prefix) == tasks.end())
return prefix;
}
return hash;
}
void get_prefixes() {
for (const auto &n : tasks) {
prefixes[prefix(n.first)] = n.first;
}
}
void readFiles() {
// read task file
// std::cout << "taskfile: " << taskpath << std::endl;
std::ifstream intaskfile(taskpath);
std::string line;
while (std::getline(intaskfile, line)) {
std::istringstream iss(line);
tasks[sha256_hash(line)] = line;
}
intaskfile.close();
// read done file
// std::cout << "done taskfile: " << donepath << std::endl;
std::ifstream indonefile(donepath);
while (std::getline(indonefile, line)) {
std::istringstream iss(line);
tasksDone[sha256_hash(line)] = line;
}
indonefile.close();
get_prefixes();
}
void writeFiles() {
std::ofstream outtaskfile(taskpath);
if (outtaskfile.is_open()) {
for (const auto &n : tasks) {
outtaskfile << n.second << std::endl;
}
}
outtaskfile.close();
std::ofstream outdonefile(donepath);
if (outdonefile.is_open()) {
for (const auto &n : tasksDone) {
outdonefile << n.second << std::endl;
}
}
outdonefile.close();
}
int main(int argc, char *argv[]) {
cxxopts::Options options("MyProgram", "One line description of MyProgram");
options.allow_unrecognised_options().add_options()(
"positional",
"Positional arguments: these are the arguments that are entered "
"without an option",
cxxopts::value<std::vector<std::string>>())(
"e,edit", "edit TASK to contain TEXT", cxxopts::value<std::string>())(
"f,finish", "mark TASK as finished", cxxopts::value<std::string>())(
"r,remove", "Remove TASK from list", cxxopts::value<std::string>())(
"l,list", "work on LIST", cxxopts::value<std::string>())(
"t,taskdir", "work on the lists in DIR",
cxxopts::value<std::string>()->default_value("tasks"))(
"d,delete-if-empty", "delete the task file if it becomes empty")(
"g,grep", "print only tasks that contain WORD",
cxxopts::value<std::string>())(
"v,verbose", "print more detailed output (full task ids, etc)",
cxxopts::value<bool>()->default_value("false"))(
"q,quiet", "print less detailed output (no task ids, etc)",
cxxopts::value<bool>()->default_value("false"))(
"D,done", "list done tasks instead of unfinished ones",
cxxopts::value<bool>()->default_value("false"))("h,help", "HELP");
options.parse_positional({"positional"});
auto result = options.parse(argc, argv);
const auto &arguments = result.arguments();
if (result.count("help")) {
std::cout << options.help() << std::endl;
exit(0);
}
if (result.count("taskdir")) {
taskdir = result["taskdir"].as<std::string>();
} else {
namespace fs = std::filesystem;
taskdir = fs::current_path();
}
if (result.count("list")) {
taskfile = result["list"].as<std::string>();
} else {
taskfile = "tasks";
}
taskpath = fs::path(taskdir) / fs::path(taskfile);
donepath = fs::path(taskdir) / fs::path("." + taskfile + ".done");
readFiles();
// edit task
if (result.count("edit")) {
auto task_prefix = result["edit"].as<std::string>();
auto task_hash = prefixes[task_prefix];
if (tasks.find(task_hash) != tasks.end()) {
std::string str;
auto &v = result["positional"].as<std::vector<std::string>>();
for (const auto &s : v) {
str += s + " ";
}
auto src_str = trim(str);
if (src_str.length() > 0) {
tasks[sha256_hash(src_str)] = src_str;
tasks.erase(task_hash);
writeFiles();
}
} else {
std::cout << "Task not found: " << task_prefix << std::endl;
}
exit(0);
}
// finish task
if (result.count("finish")) {
auto task_prefix = result["finish"].as<std::string>();
auto task_hash = prefixes[task_prefix];
if (tasks.find(task_hash) != tasks.end()) {
tasksDone[task_hash] = tasks[task_hash];
tasks.erase(task_hash);
writeFiles();
} else {
std::cout << "Task not found: " << task_prefix << std::endl;
}
exit(0);
}
// remove task
if (result.count("remove")) {
std::cout << "remove task" << std::endl;
auto task_prefix = result["remove"].as<std::string>();
auto task_hash = prefixes[task_prefix];
if (tasks.find(task_hash) != tasks.end()) {
tasks.erase(task_hash);
writeFiles();
} else {
std::cout << "Task not found: " << task_prefix << std::endl;
}
exit(0);
}
// add new task
if (result.count("positional")) {
std::string str;
auto &v = result["positional"].as<std::vector<std::string>>();
for (const auto &s : v) {
str += s + " ";
}
auto src_str = trim(str);
tasks[sha256_hash(src_str)] = src_str;
std::string hash = sha256_hash(src_str);
std::cout << "sha256('" << src_str << "'): " << hash << std::endl;
std::string p = prefix(hash);
std::cout << "prefix: " << p << std::endl;
writeFiles();
exit(0);
}
// print tasks
if (result.count("done")) {
for (const auto &n : tasksDone) {
std::cout << n.first << ": " << n.second << std::endl;
}
} else {
for (const auto &n : prefixes) {
std::cout << n.first << ": " << tasks[n.second] << std::endl;
}
}
return 0;
}
|