-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
559 lines (499 loc) · 14.4 KB
/
main.cpp
File metadata and controls
559 lines (499 loc) · 14.4 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// Created by 909 DL on 2025/7/20.
//
/*
* Copyright (C) 2025 DL909 - This file has been modified from its original version.
*
* Licensed under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include <iostream>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <pugixml.hpp>
#include <memory>
#include <list>
#include "fuzzy_match.h"
#include "Util/opt_processor.h"
#include "Tasks/Task.h"
#include "Util/util.h"
#include "Util/config_and_constants.h"
// parameter flag
enum MODE
{
ERROR = 0,
MARK = 1,
CHECK = 2,
DELETE = 3,
HELP = 4,
};
std::vector<int> used_index{};
struct scored_task_type
{
std::string name;
int score = 100;
};
inline void sort_scored_types(std::vector<scored_task_type> & types)
{
std::ranges::sort(types, [](const scored_task_type & a, const scored_task_type & b) {
return a.score > b.score;
});
}
int mark()
{
std::vector<scored_task_type> types;
for (const auto& name : UserTaskFactory::get_user_create_task_type_list())
{
types.emplace_back(name);
}
if (types.empty())
{
error_messages.emplace_back("Error: no valid task type");
return EXIT_FAILURE;
}
setlocale(LC_ALL, "");
// 2. 为ncurses I/O打开/dev/tty
FILE* tty_fp_out = fopen("/dev/tty", "w");
FILE* tty_fp_in = fopen("/dev/tty", "r");
if (!tty_fp_out || !tty_fp_in) {
std::cerr << "Error: Could not open /dev/tty" << std::endl;
return -1;
}
// 3. 使用newterm()初始化ncurses
// newterm的第一个参数为NULL,它会从TERM环境变量自动检测终端类型
SCREEN* term_screen = newterm(nullptr, tty_fp_out, tty_fp_in);
if (term_screen == nullptr) {
std::cerr << "Error: newterm() failed" << std::endl;
fclose(tty_fp_out);
fclose(tty_fp_in);
return -1;
}
// 设置当前活动的ncurses屏幕
set_term(term_screen);
// 4. 设置ncurses模式
noecho(); // 不回显用户输入
cbreak(); // 立即获取字符,无需等待回车
keypad(stdscr, TRUE); // 启用功能键 (如箭头)
curs_set(1);
int choice = 0;
const int number = std::min(LINES - 3 - (config.verbose_flag ? 1 : 0), static_cast<int>(types.size()) - 1);
std::string input;
int index=0;
int result = EXIT_SUCCESS;
bool continue_flag = true;
mvprintw(LINES - 1, 0, "> ");
while (continue_flag)
{
if (config.verbose_flag)
{
mvprintw(0, 0, "%s", (static_cast<std::string>(" ") * COLS).c_str());
mvprintw(0, 0, "choice : %d", choice);
}
int i = 0;
for (const auto & [name, score] : types)
{
mvprintw(LINES - 2 - i, 0, "%c %s", choice == i?'*' : ' ',name.c_str());
i++;
}
mvprintw(LINES - 1, 2, "%s", (static_cast<std::string>(" ") * (COLS - 2)).c_str());
mvprintw(LINES - 1, 2, "%s", input.c_str());
mvprintw(LINES - 1, index + 2, "");
switch (const int c = getch())
{
case KEY_UP:
choice = std::min(choice + 1, number);
break;
case KEY_DOWN:
choice = std::max(choice - 1, 0);
break;
case KEY_LEFT:
index = std::max(index - 1, 0);
break;
case KEY_RIGHT:
index = std::min(index + 1, static_cast<int>(input.length()));
break;
case 3: // ctrl + c
case 27: // esc
error_messages.emplace_back("Error: interrupted");
result = EXIT_FAILURE;
continue_flag = false;
continue;
case '\n':
continue_flag = false;
continue;
default:
{
if (c <= 31) // control ascii
{
continue;
}
if (c <= 126)
{
input.insert(index, std::string(1, static_cast<char>(c)));
index++;
}
else if (c == 127 || c == KEY_BACKSPACE)
{
if (index > 0)
{
input.erase(index - 1, 1);
index--;
}
else
{
continue;
}
}
}
for (auto & [name, score] : types)
{
score = fuzzy_match(input.c_str(), name.c_str());
}
sort_scored_types(types);
break;
}
}
echo();
endwin();
fclose(tty_fp_out);
fclose(tty_fp_in);
if (result != EXIT_SUCCESS)
{
return result;
}
pugi::xml_document doc;
doc.load_file(config.file_path.c_str());
pugi::xml_node root = doc.child("root");
if (root.empty())
{
root = doc.append_child("root");
}
if (UserTaskFactory::create(types[choice].name,root.last_child().child("index").text().as_int()+1, root) == EXIT_SUCCESS)
{
if (doc.save_file(config.file_path.c_str()))
{
std::cout << "successfully added task" << std::endl;
return EXIT_SUCCESS;
}
}
std::cerr << "failed to add task" << std::endl;
return EXIT_FAILURE;
}
int delete_mark(const long id)
{
pugi::xml_document doc;
doc.load_file(config.file_path.c_str());
pugi::xml_node root = doc.child("root");
if (root.empty())
{
std::cerr << "failed to load xml" << std::endl;
return EXIT_FAILURE;
}
pugi::xml_node current_task = root.child("task");
while (current_task)
{
pugi::xml_node next_task = current_task.next_sibling("task");
if (current_task.child("index").text().as_int() == id)
{
root.remove_child(current_task);
}
current_task = next_task;
}
return doc.save_file(config.file_path.c_str());
}
int help()
{
std::cout << "Cpp Command Mark" << std::endl;
std::cout << "usage:" << std::endl;
std::cout << "\t./CppCommandMark <option> [args]" << std::endl;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "\t--help(-h): show this help" << std::endl;
std::cout << "\t--check(-c): choose command and use ( default )" << std::endl;
std::cout << "\t--delete(-d): choose command and delete" << std::endl;
std::cout << "\t--mark(-x): mark command" << std::endl;
std::cout << "\t\t--directory(-d): manually choose where the command will run" << std::endl;
std::cout << "extra options:" << std::endl;
std::cout << "\t--verbose(-v): show verbose output" << std::endl;
std::cout << "\t--file(-f): manually choose where the information file is" << std::endl;
std::cout << "\t--wait(-w): pause program until enter ( for debugging )" << std::endl;
return EXIT_SUCCESS;
}
std::vector<std::unique_ptr<Task>> get_task_list()
{
std::vector<std::unique_ptr<Task>> task_list;
pugi::xml_document doc;
if (!doc.load_file(config.file_path.c_str()))
{
error_messages.emplace_back(std::format("get_task_list() error: could not load xml document from {}\n",config.file_path));
}
for (pugi::xml_node node : doc.child("root").children())
{
std::string name = node.child("type").text().as_string();
int index = node.child("index").text().as_int();
task_list.push_back(TaskFactory::create(name,index, node.child("data")));
used_index.push_back(index);
}
return task_list;
}
inline void sort_tasks(std::vector<std::unique_ptr<Task>>& tasks)
{
std::ranges::sort(tasks, [](const std::unique_ptr<Task>& a, const std::unique_ptr<Task>& b) {
return a->score == b->score ? a->index > b->index : a->score > b->score;
});
}
int choose(std::string& command, long& id)
{
std::vector<std::unique_ptr<Task>> tasks = get_task_list();
if (tasks.empty())
{
error_messages.emplace_back("choose() error: get_task_list() failed");
return -1;
}
setlocale(LC_ALL, "");
// 2. 为ncurses I/O打开/dev/tty
FILE* tty_fp_out = fopen("/dev/tty", "w");
FILE* tty_fp_in = fopen("/dev/tty", "r");
if (!tty_fp_out || !tty_fp_in) {
std::cerr << "Error: Could not open /dev/tty" << std::endl;
return -1;
}
// 3. 使用newterm()初始化ncurses
// newterm的第一个参数为NULL,它会从TERM环境变量自动检测终端类型
SCREEN* term_screen = newterm(nullptr, tty_fp_out, tty_fp_in);
if (term_screen == nullptr) {
std::cerr << "Error: newterm() failed" << std::endl;
fclose(tty_fp_out);
fclose(tty_fp_in);
return -1;
}
// 设置当前活动的ncurses屏幕
set_term(term_screen);
// 4. 设置ncurses模式
noecho(); // 不回显用户输入
cbreak(); // 立即获取字符,无需等待回车
keypad(stdscr, TRUE); // 启用功能键 (如箭头)
curs_set(1);
int choice = 0;
const int number = std::min(LINES - 3 - (config.verbose_flag ? 1 : 0), static_cast<int>(tasks.size()) - 1);
std::string input;
int index=0;
int result = EXIT_SUCCESS;
bool continue_flag = true;
mvprintw(LINES - 1, 0, "> ");
sort_tasks(tasks);
while (continue_flag)
{
if (config.verbose_flag)
{
mvprintw(0, 0, "%s", (static_cast<std::string>(" ") * COLS).c_str());
mvprintw(0, 0, "choice : %d", choice);
}
int task_index = 0;
int i = LINES - 2;
for (const auto & task : tasks)
{
if (i<(config.verbose_flag?1:0))
{
break;
}
i = i - task->rend(i,choice == task_index);
task_index ++;
}
mvprintw(LINES - 1, 2, "%s", (static_cast<std::string>(" ") * (COLS - 2)).c_str());
mvprintw(LINES - 1, 2, "%s", input.c_str());
mvprintw(LINES - 1, index + 2, "");
switch (const int c = getch())
{
case KEY_UP:
choice = std::min(choice + 1, number);
break;
case KEY_DOWN:
choice = std::max(choice - 1, 0);
break;
case KEY_LEFT:
index = std::max(index - 1, 0);
break;
case KEY_RIGHT:
index = std::min(index + 1, static_cast<int>(input.length()));
break;
case 3: // ctrl + c
case 27: // esc
error_messages.emplace_back(std::format("choose() error: interrupted\n"));
result = EXIT_FAILURE;
continue_flag = false;
continue;
case '\n':
continue_flag = false;
continue;
default:
{
if (c <= 31) // control ascii
{
continue;
}
if (c <= 126)
{
input.insert(index, std::string(1, static_cast<char>(c)));
index++;
}
else if (c == 127 || c == KEY_BACKSPACE)
{
if (index > 0)
{
input.erase(index - 1, 1);
index--;
}
else
{
continue;
}
}
}
for (const auto & task : tasks)
{
task->update_score(input);
}
sort_tasks(tasks);
break;
}
}
echo();
endwin();
command = tasks[choice]->get_output();
id = tasks[choice]->index;
fclose(tty_fp_out);
fclose(tty_fp_in);
return result;
}
// parameter processer function
bool mode_flag = false;
MODE mode = CHECK;
int p_check(const char* param, void* place_to_save)
{
if (mode_flag)
{
return 1;
}
mode = CHECK;
return 0;
}
int p_delete(const char* param, void* place_to_save)
{
if (mode_flag)
{
return 1;
}
mode = DELETE;
return 0;
}
int p_mark(const char* param, void* place_to_save)
{
if (mode_flag)
{
return 1;
}
mode = MARK;
return 0;
}
int p_directory(const char* param, void* place_to_save)
{
if (config.directory_flag)
{
return 1;
}
config.directory_flag = true;
config.path = param;
return 0;
}
int p_help(const char* param, void* place_to_save)
{
if (mode_flag)
{
return 1;
}
mode = HELP;
return 0;
}
int p_verbose(const char* param, void* place_to_save)
{
config.verbose_flag = true;
return 0;
}
int p_file(const char* param, void* place_to_save)
{
config.file_path = param;
return 0;
}
int p_wait(const char* param, void* place_to_save)
{
config.wait_flag = true;
return 0;
}
int main(const int argc, const char** argv)
{
int result = EXIT_SUCCESS;
std::string param;
long id;
const std::vector<struct option> options = {
{"check", 'c', false, p_check, nullptr},
{"delete", 'd', false, p_delete, nullptr},
{"mark", 'm', false, p_mark, nullptr},
{"help", 'h', false, p_help, nullptr},
{"directory", '\0', true, p_directory, nullptr},
{"verbose", 'v', false, p_verbose, nullptr},
{"file",'f',true,p_file,nullptr},
{"wait",'w',false,p_wait,nullptr}
};
if (process(argc, argv, options)!=0)
{
std::cout << "process arg error" << std::endl;
return EXIT_FAILURE;
};
if (config.wait_flag)
{
std::cout << getpid() << std::endl;
getchar();
}
switch (mode)
{
case ERROR:
result = EXIT_FAILURE;
break;
case MARK:
return mark();
case CHECK:
if ((result = choose(param, id)) != EXIT_SUCCESS)
{
break;
}
if ((result = leave_text_in_terminal(param.c_str())) != EXIT_SUCCESS)
{
error_messages.emplace_back(std::format("{}", param));
}
break;
case DELETE:
if ((result = choose(param, id)) != EXIT_SUCCESS)
{
break;
}
result = delete_mark(id);
break;
case HELP:
result = help();
}
if (result != EXIT_SUCCESS)
{
for (const auto& error : error_messages)
{
std::cout << error;
}
std::cout << std::endl;
}
return result;
}