summaryrefslogtreecommitdiff
path: root/got.cpp
blob: e60b4c665e4a0b9248cc22e947b5b7febe8a59b6 (plain)
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
#include <sstream>
#include <algorithm>
#include "command.h"
#include "got.h"
#include "sqliteutil.h"


namespace got {
	std::string getDBpath() {
		return std::string{getenv("HOME")} + "/.timetrap.db";
	}

	// Returns {sheet, note}
	std::optional<std::pair<std::string, std::string>> getRunning() {
		std::string output = runCommand({"sqlite3", getDBpath(), ".mode csv", "select sheet, note from entries where end is null"});
		auto table = sqlite::parseCSV(move(output));
		if (table.empty()) return std::nullopt;
		else return std::make_pair(table[0][0], table[0][1]);
	}

	std::vector<std::string> getSheets() {
		std::string output = runCommand({"sqlite3", getDBpath(), "select distinct sheet from entries"});
		std::istringstream ss{output};
		std::vector<std::string> lines;
		std::string line;
		while (std::getline(ss, line)) {
			if (line.size() > 0) lines.push_back(move(line));
		}
		std::sort(lines.begin(), lines.end());
		return lines;
	}

	void editRunning(const std::string &descr) {
		runCommand({"got", "edit", descr});
	}

	void checkOut() {
		runCommand({"got", "out"});
	}

	void checkIn(const std::string &sheet) {
		runCommand({"got", "sheet", sheet});
		runCommand({"got", "in"});
	}
}