sophuwu.site > bashprompt   
              91
            
             /*
 * 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>

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

std::string emote() {
    srand(time(0));
    return emotes[rand() % 12];
}

std::string padint(int n, int max) {
    std::string s = std::to_string(n);
    for(max=std::to_string(max).length();s.length()<max; s="0"+s);
    return s;
}

void hm_time(int &h, int &m) {
    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);
    h = (int)(tt->tm_hour);
    m = (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;
}

const char *hex="0123456789ABCDEF";
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[]) {

    std::string host = std::string(readfile("/etc/hostname"));
    std::string user;
    user=std::string(getenv("USER"));
    if (host=="soph") {
        host = "::";
        if (user == "sophuwu") user = "uwu";
    }


    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());
    if (ipaddr[0]==192||ipaddr[0]==127||ipaddr[0]==10)printf("\033[38;5;%dm%s\033[0m", ipaddr[1], "@");
    else printf("\033[38;5;%dm%s\033[0m", ipaddr[1], "@ext.");
    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\n");


    int h, m;
    hm_time(h, m);
    printf("%.2d:%.2d\n", h, m);
    printf("%s %s\n", host.c_str(), emote().c_str());

    return 0;
}