-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacos.cppm
More file actions
67 lines (51 loc) · 1.82 KB
/
Copy pathmacos.cppm
File metadata and controls
67 lines (51 loc) · 1.82 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
module;
#include <cstdio>
#include <cstdlib>
export module d2x.platform:macos;
import std;
#if defined(__APPLE__)
namespace d2x {
namespace platform_impl {
export constexpr std::string_view XLINGS_INSTALL_CMD = "curl -fsSL https://d2learn.org/xlings-install.sh | bash";
export std::pair<int, std::string> run_command_capture(const std::string& cmd) {
std::string full = cmd + " 2>&1"; // redirect stderr to stdout
FILE* pipe = ::popen(full.c_str(), "r");
if (!pipe) {
std::cout << std::format("Failed to open pipe for command: {}\n", cmd);
std::cout.flush();
return {-1, std::string{}};
}
std::string output;
std::array<char, 256> buffer{};
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
output += buffer.data();
}
int status = ::pclose(pipe);
return {status, output};
}
export void clear_console() {
std::system("clear");
}
export std::string get_home_dir() {
if (const char* home = std::getenv("HOME")) return home;
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) {
::setenv(key.c_str(), value.c_str(), 1);
}
// println implementation using std::cout for macOS
export template<typename... Args>
void println(std::format_string<Args...> fmt, Args&&... args) {
std::cout << std::format(fmt, std::forward<Args>(args)...) << '\n';
std::cout.flush();
}
export inline void println(const std::string& msg) {
std::cout << msg << '\n';
std::cout.flush();
}
} // namespace platform_impl
}
#endif // defined(__APPLE__)