git.sophuwu.com > bashprompt   
              0
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
             /*
 * File: prompt.cpp
 * This program is used to generate a bash prompt with rainbow colors.
 * It can also be used to generate the necessary environment variables
 *
 */

#include <string>
#include <chrono>
#include <cstdio>
#include <ctime>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string_view>

typedef std::string str;

const std::string emotes[] =
{":)", ":3", ";D",":]", ":D", "xD",
";3", ";)", "=)",":P", ":O", ":b"};

std::string emote() {
    srand(std::chrono::duration_cast<std::chrono::milliseconds>(
    std::chrono::high_resolution_clock::now().time_since_epoch()).count());
    return emotes[rand() % 12];
}

str time_hhmm() {
    std::chrono::time_point now = std::chrono::system_clock::now();
    time_t in_time_t = std::chrono::system_clock::to_time_t(now);
    auto tt = std::localtime(&in_time_t);
    return std::to_string((int)(tt->tm_hour)) + ":" + std::to_string((int)(tt->tm_min));
}

void pipe(std::string inputted, char *bufout) {
    FILE* file = popen(inputted.c_str(), "r");
    fread(bufout, 1, 8, file);
    pclose(file);
}

std::string readfile(std::string filename){
    std::string b;
    FILE* file = fopen(filename.c_str(), "r");
    char buffer[1024];
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        b += buffer;
    }
    fclose(file);
    if (b.back()=='\n')b.pop_back();
    return b;
}

int stoi(std::string s) {
    int n = 0;
    for(;!s.empty();s.pop_back()){
        if( s.back() < '0' || s.back() > '9') continue;
        //s.back()
    }
    return n;
}

unsigned char unhex(char c) {
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    return 0;
}

int main(int argc, char* argv[]) {
    if (getenv("LINENO") == NULL || getenv("USER")==NULL || getenv("PWD")==NULL)return 1;
    std::string output = "";
    if (argc > 1) output += std::string(argv[1]);

    struct winsize term;
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &term) == -1) return 1;

    output += emote();
    std::string lineno = getenv("LINENO");
    output += lineno;
    output += " ";
    output += time_hhmm();
    output += " ";
    std::string host = std::string(readfile("/etc/hostname"));
    if (host.back()=='\n')host.pop_back();

    std::string user;
    user=std::string(getenv("USER"));
    if (user.back()=='\n')user.pop_back();

    char ip[8];
    pipe("hostname -I | awk -F '.' ' { printf(\"%X%X%X%X\",int($1),int($2),int($3),int($4)); } ' ", ip);
    unsigned char ipaddr[4];
    for (int i = 0; i < 4; i++)ipaddr[i] = unhex(ip[2*i])<<4 | unhex(ip[2*i+1]);
    printf("\033[1;38;5;%dm%s\033[0m", ipaddr[0], user.c_str());
    printf("\033[38;5;%dm%s\033[0m", ipaddr[1], "@");
    printf("\033[1;38;5;%dm%s\033[0m", ipaddr[2], host.c_str());
    printf("\033[1;38;5;%dm%d", ipaddr[3], ipaddr[3]);
    printf("\033[0m");

    std::string pwd = std::string(getenv("PWD"));
    printf("%s", pwd.c_str());

    return 0;
}