-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.cpp
More file actions
94 lines (76 loc) · 2.17 KB
/
Copy pathhelper.cpp
File metadata and controls
94 lines (76 loc) · 2.17 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
#include "helper.hpp"
#include <unistd.h>
#include <cstdlib>
#include <readline/history.h>
#include <filesystem>
namespace fs = std::filesystem;
std::vector<std::string> extract_key_value(const std::string &token) {
auto pos = token.find('=');
if (pos == std::string::npos) {
std::cerr << "export: invalid format, expected VAR=value\n";
return {};
}
std::string key = token.substr(0, pos);
std::string value = token.substr(pos+1);
return {key, value};
}
void print_vector(const std::vector<std::string> &vector) {
for (const auto &it : vector) {
std::cout << it << "\n";
}
}
void print_table(const std::unordered_map<std::string, std::string> &shellvar_map) {
for (const auto& [key, value] : shellvar_map) {
// Print each key-value pair
std::cout << "Key: [" << key << "] Value: [" << value << "]\n";
}
}
void print_prompt() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != nullptr) {
fs::path path(cwd);
std::string dir_name = path.filename().string();
if (dir_name.empty()) dir_name = "/";
std::cout << "\033[1;32m" << dir_name << " > \033[0m";
} else {
std::cout << "\033[1;32m> \033[0m";
}
std::cout.flush();
}
namespace History{
static std::vector<std::string> history;
void add(const std::string &line){
history.push_back(line);
}
void print(){
for (const auto &it : history) {
std::cout << it << "\n";
}
}
void print(int num){
for (size_t i = history.size() - num; i < history.size(); --i) {
std::cout << history[i] << "\n";
}
}
void clear(){
history.clear();
}
void remove(int index){
history.erase(history.begin() + index);
}
const std::vector<std::string>& get_all(){
return history;
}
void load_from_file(){
const char* home = getenv("HOME");
if (!home) return;
std::string history_file = std::string(home) + "/.dax_history";
read_history(history_file.c_str());
}
void save_to_file(){
const char* home = getenv("HOME");
if (!home) return;
std::string history_file = std::string(home) + "/.dax_history";
write_history(history_file.c_str());
}
}