sophuwu.site > bashprompt
idk
sophuwu sophie@skisiel.com
Thu, 02 May 2024 05:58:29 +0200
commit

742813259e116eb18341998bcb29cf821f7d40e1

parent

7a17147f8798a36e97a576d95fb354f3ec8bbe4f

3 files changed, 72 insertions(+), 71 deletions(-)

jump to
M MakefileMakefile

@@ -1,6 +1,5 @@

build: src/prompt.cpp mkdir -p build g++ -std=c++17 -o build/bashprompt src/prompt.cpp - run: ./build/bashprompt
M src/prompt.cppsrc/prompt.cpp

@@ -8,97 +8,102 @@

#include <string> #include <chrono> #include <cstdio> +#include <iostream> #include <ctime> #include <sys/ioctl.h> #include <unistd.h> +#include <vector> +#include <exception> + +#include "rainbow.hpp" typedef std::string str; +int randint(int max) { + srand(std::chrono::duration_cast<std::chrono::milliseconds>( + std::chrono::high_resolution_clock::now().time_since_epoch()).count()); + return rand() % max; +} + const str emotes[] = {":)", ":3", ";D",":]", ":D", "xD",";3", ";)", "=)",":P", ":O", ":b"}; str emote() { - srand(std::chrono::duration_cast<std::chrono::milliseconds>( - std::chrono::high_resolution_clock::now().time_since_epoch()).count()); - return emotes[rand() % 12]; + return emotes[randint(12)]; +} + +str intpad(int n, int len) { + str s = std::to_string(n); + while (s.length() < len) s = "0" + s; + return s; +} + +int str2int(str &s) { + int n = 0; + while (!s.empty()) { + if (s[0] < '0' || s[0] > '9') { + s.erase(0, 1); + break; + } + n = n * 10 + s[0] - '0'; + s.erase(0, 1); + } + return n; } 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)); + return intpad((int)(tt->tm_hour),2) + ":" + intpad((int)(tt->tm_min),2); } -void pipe(std::string inputted, char *bufout) { +str docmd(std::string inputted) { FILE* file = popen(inputted.c_str(), "r"); - fread(bufout, 1, 8, file); + char buff[1024]; + int n = fread(buff, 1, sizeof(buff), file); pclose(file); + str r = std::string(buff, n); + if (r.back() == '\n') r.pop_back(); + return r; } -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; +str envorcmd(str env, str cmd) { + str ret = ""; + try { ret = std::string(getenv(env.c_str())); } + catch (std::exception e) { ret = docmd(cmd); } + return ret; } -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("USER") == NULL)system("export USER=$(whoami)"); - if (getenv("PWD") == NULL)system("export PWD=$(pwd)"); - if (getenv("LINENO") == NULL)system("export LINENO"); - - 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(); + rainbow r; + + try { + str rainenv = std::string(getenv("RAINBOW")); + for(int i = 0; i < 3; i++) { + r.c.indexAdd(i, str2int(rainenv)); + } + r.s = str2int(rainenv); + r.next(); + } catch (std::exception e) { + r.init((int)term.ws_row-10+randint(10)); + } - std::string user; - user=std::string(getenv("USER")); + str output=""; + str user = envorcmd("USER", "whoami"); + str ip = docmd("hostname -I | awk -F '.' ' { for(i=1;i<5;i++){printf(\"%.3d\", $i);}; } ' "); + for(int i=0;i<user.length();i++){ + output+="\\e[1;38;5;"+ip.substr((i%4)*3,3)+"m"+user.substr(i,1)+"\\e[0m"; + } + printf("%s", output.c_str()); + /*str pwd; + try { pwd = std::string(getenv("PWD")); } + catch (std::exception e) { pwd = ""; } 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()); - + output += " "; + output += pwd;*/ return 0; -}+}
M src/rainbow.hppsrc/rainbow.hpp

@@ -17,13 +17,10 @@ std::string str(char sep = ';') {

clamp(); return std::to_string((int)r) + sep + std::to_string((int)g) + sep + std::to_string((int)b); } - - void set(double rr, double gg, double bb) {r = rr;g = gg;b = bb; clamp();} + void set(int rr, int gg, int bb) {r = rr;g = gg;b = bb; clamp();} void set(rgb c) {r = c.r;g = c.g;b = c.b; clamp();} void print(std::string s) {printf("\033[38;2;%sm%s\033[0m", str().c_str(), s.c_str());} - void printAt(std::string s, int x, int y) {printf("\033[%d;%dH\033[38;2;%sm%s\033[0m", y, x, str().c_str(), s.c_str());} - void setPx(int x, int y) {printf("\033[%d;%dH\033[48;2;%sm \033[0m", y, x, str().c_str());} double operator[](int i) { switch (i%3) {

@@ -33,7 +30,7 @@ case 2: return b;

} return 0; } - void indexAdd(int i, double v) { + void indexAdd(int i, int v) { switch (i%3) { case 0: r += v; break; case 1: g += v; break;

@@ -47,8 +44,8 @@ struct rainbow {

rgb c; int s = 0; // step - void init(int len = 10) { - if (len == 0) len = 10; + void init(int len = 15) { + if (len <= 0) len = 15; c.set(255, 0, 0); s = 5.0*255.0 / len; }