-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.cppm
More file actions
63 lines (48 loc) · 1.7 KB
/
windows.cppm
File metadata and controls
63 lines (48 loc) · 1.7 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
module;
#include <cstdio>
#include <cstdlib>
export module d2x.platform:windows;
#if defined(_WIN32)
import std;
namespace d2x {
namespace platform_impl {
export constexpr std::string_view XLINGS_INSTALL_CMD = "powershell -Command \"irm https://d2learn.org/xlings-install.ps1.txt | iex\"";
export std::pair<int, std::string> run_command_capture(const std::string& cmd) {
FILE* pipe = _popen(cmd.c_str(), "r");
if (!pipe) {
return {-1, std::string{}};
}
std::string output;
std::array<char, 256> buffer{};
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
output += buffer.data();
}
int code = _pclose(pipe);
return {code, output};
}
export void clear_console() {
// run by cmd
std::system("cls");
}
export std::string get_home_dir() {
if (const char* home = std::getenv("USERPROFILE")) return home;
if (const char* appdata = std::getenv("APPDATA")) return appdata;
return ".";
}
export inline std::string get_xlings_bin() {
return get_home_dir() + "\\.xlings\\subos\\current\\bin\\xlings";
}
export void set_env_variable(const std::string& key, const std::string& value) {
_putenv_s(key.c_str(), value.c_str());
}
// println implementation forwarding to std::println for Windows
export template<typename... Args>
void println(std::format_string<Args...> fmt, Args&&... args) {
std::println(fmt, std::forward<Args>(args)...);
}
export inline void println(const std::string& msg) {
std::println("{}", msg);
}
} // namespace platform_impl
} // namespace d2x
#endif // defined(_WIN32)