-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxlings.cppm
More file actions
82 lines (61 loc) · 2.06 KB
/
xlings.cppm
File metadata and controls
82 lines (61 loc) · 2.06 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
export module d2x.xlings;
import std;
import d2x.platform;
import d2x.utils;
namespace d2x {
namespace xlings {
[[nodiscard]] bool has_xlings() {
// check by xlings binary path
if (std::filesystem::exists(platform::get_xlings_bin())) {
return true;
}
auto [status, output] = d2x::platform::run_command_capture("xlings --version");
auto clean_output = d2x::utils::strip_ansi(output);
// match xlings x.x.x format to check if xlings is installed
return status == 0 && std::regex_match(clean_output, std::regex("xlings (\\d+\\.\\d+\\.\\d+)"));
}
export bool ensure_xlings_installed() {
if (!has_xlings()) {
if (!d2x::utils::ask_yes_no("xlings 未安装,是否现在安装?", true)) {
std::println("已取消安装");
return false;
}
if (!d2x::platform::xlings_install()) {
std::println("xlings 安装失败");
return false;
}
}
return true;
}
export bool install(const std::string& pkgname) {
std::println("开始安装 -> {}", pkgname);
ensure_xlings_installed();
// Install the package
std::string command = "xlings install d2x:" + pkgname;
std::println("正在执行: {}", command);
int status = platform::exec(command.c_str());
if (status == 0) {
return true;
}
std::println("安装失败,命令返回状态码: {}", status);
return false;
}
export void list(const std::string& query = "") {
ensure_xlings_installed();
std::string command = "xim -s d2x:" + query;
auto [status, output] = d2x::platform::run_command_capture(command);
if (status != 0) {
std::println("查询失败: {}", output);
return;
}
// Strip ANSI escape codes and print from first '{'
//auto clean_output = d2x::utils::strip_ansi(output);
//auto pos = clean_output.find('{');
//if (pos != std::string::npos) {
// clean_output = clean_output.substr(pos);
//}
//std::println("{}", clean_output);
std::println("{}", output);
}
} // namespace xlings
} // namespace d2x