aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 789719bce076ef90442ee70aa00987780e6886b5 (plain) (blame)
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
#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>())(
      "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)")(
      "q,quiet", "print less detailed output (no task ids, etc)")(
      "D,done", "list done tasks instead of unfinished ones")("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();

  for (const auto &n : prefixes) {
    std::cout << "Key:[" << n.first << "] Value:[" << tasks[n.second] << "]\n";
  }
  std::cout << std::endl;

  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(str)] = str;
    std::string hash = sha256_hash(str);
    std::cout << "sha256('" << str << "'): " << hash << std::endl;

    std::string p = prefix(hash);
    std::cout << "prefix: " << p << std::endl;

    writeFiles();
  }
  return 0;
}