-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTasklistCmdlineApp.hpp
More file actions
189 lines (168 loc) · 7.29 KB
/
Copy pathTasklistCmdlineApp.hpp
File metadata and controls
189 lines (168 loc) · 7.29 KB
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
/*
* Copyright 2018-2025 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <chrono>
#include <cinttypes>
#include <ctime>
#include <iostream>
#include "objectbox-sync.hpp"
#include "objectbox.hpp"
#include "tasklist.obx.hpp"
/// The ObjectBox tasks-list app example enabled for sync.
/// It uses a simple command-line interface with commands like `new`, `done`, `ls`, `exit`, `help`.
/// The main interaction is done in the processCommand() method.
class TasklistCmdlineApp : public obx::SyncChangeListener {
/// String commands are parsed into these enum values.
enum class Command { New, Done, Exit, List, Help, Unknown };
obx::Store& store;
obx::Box<Task> taskBox;
/// The query to select only tasks that are not finished yet.
/// As a member, the query is just build once and can be used multiple times.
obx::Query<Task> unfinishedTasksQuery;
public:
explicit TasklistCmdlineApp(obx::Store& obxStore)
: store(obxStore),
taskBox(obxStore),
unfinishedTasksQuery(taskBox.query(Task_::date_finished.equals(0)).build()) {}
/// This is the central function processing the commands.
/// It demonstrates ObjectBox database API usage, e.g. putting and reading objects.
Command processCommand(const std::string& cmd, const std::string& arg) {
Command command = getCommand(cmd);
switch (command) {
case Command::New: {
Task object{}; // Initialized with default values, e.g. 0 for ID and date fields.
object.text = arg;
object.date_created = millisSinceEpoch();
taskBox.put(object);
// Note: after put(), the object has an ID assigned, so we can print it.
std::cout << "New task: " << object.id << " - " << object.text << std::endl;
break;
}
case Command::Done: {
obx_id id = std::stoull(arg);
std::unique_ptr<Task> task = taskBox.get(id);
if (!task) {
std::cerr << "Task ID " << arg << " not found" << std::endl;
} else if (task->date_finished != 0) {
std::cerr << "Task ID " << id << " is already done" << std::endl;
} else {
task->date_finished = millisSinceEpoch();
std::cout << "Task ID " << id << " marked as done at " << fmtTime(task->date_finished) << std::endl;
taskBox.put(*task);
}
break;
}
case Command::List: {
std::vector<std::unique_ptr<Task>> tasks;
if (arg == "-a") {
tasks = taskBox.getAll();
} else if (arg.empty()) {
tasks = unfinishedTasksQuery.findUniquePtrs();
} else {
std::cerr << "Unknown ls argument " << arg << std::endl;
printHelp();
break;
}
listTasks(tasks);
break;
}
case Command::Exit:
break; // Handled by caller (we return the parsed command)
case Command::Help:
printHelp();
break;
case Command::Unknown:
default:
std::cerr << "Unknown command " << cmd << std::endl;
fflush(stderr);
printHelp();
break;
}
return command;
}
/// The main loop: reads commands from the console and delegates them to processCommand().
int run() {
printHelp();
std::string input;
std::string cmd;
std::string arg;
while (std::getline(std::cin, input)) { // quit the program with ctrl-d
if (input.empty()) continue;
splitInput(input, cmd, arg);
try {
Command command = processCommand(cmd, arg);
if (command == Command::Exit) break;
} catch (const std::exception& e) {
std::cerr << "Error executing " << input << std::endl << e.what();
return 1;
}
}
return 0;
}
/// This is a callback from ObjectBox Sync when the task list has changed.
void changed(const std::vector<obx::SyncChange>& changes) noexcept override {
std::cout << "Task list has changed (synced):" << std::endl;
std::vector<std::unique_ptr<Task>> list = taskBox.getAll();
listTasks(list);
}
protected:
void splitInput(const std::string& input, std::string& outCmd, std::string& outArg) const {
std::string::size_type pos = input.find(' ');
if (pos == std::string::npos) {
outCmd = input;
outArg.clear();
} else {
outCmd = input.substr(0, pos);
outArg = input.substr(pos + 1);
}
}
Command getCommand(const std::string& cmd) const {
if (cmd == "new") return Command::New;
if (cmd == "done") return Command::Done;
if (cmd == "exit") return Command::Exit;
if (cmd == "ls") return Command::List;
if (cmd == "help") return Command::Help;
return Command::Unknown;
}
void printHelp() const {
std::cout << "Available commands are: " << std::endl
<< " ls [-a] list tasks - unfinished or all (-a flag)" << std::endl
<< " new Task text create a new task with the text 'Task text'" << std::endl
<< " done ID mark task with the given ID as done" << std::endl
<< " exit close the program" << std::endl
<< " help display this help" << std::endl;
}
int64_t millisSinceEpoch() const {
auto time = std::chrono::system_clock::now().time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(time).count();
}
/// Formats the given UNIX timestamp as a human-readable time
std::string fmtTime(int64_t timestamp) const {
if (timestamp == 0) return "";
auto time_point = std::chrono::system_clock::from_time_t(timestamp / 1000);
auto time_t = std::chrono::system_clock::to_time_t(time_point);
char buffer[20];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", std::localtime(&time_t));
return std::string(buffer);
}
void listTasks(std::vector<std::unique_ptr<Task>>& list) const {
printf("%4s %-20s %-20s %s\n", "ID", "Created", "Finished", "Text");
for (const auto& task : list) {
printf("%4" PRIu64 " %-20s %-20s %s\n", task->id, fmtTime(task->date_created).c_str(),
fmtTime(task->date_finished).c_str(), task->text.c_str());
}
}
};