finally finished mostly
sophuwu sophie@skisiel.com
Thu, 09 May 2024 03:26:37 +0200
2 files changed,
88 insertions(+),
34 deletions(-)
M
src/prompt.cpp
→
src/prompt.cpp
@@ -15,13 +15,15 @@ #include <vector>
#include <exception> #include <map> #include <cmath> +#include <sys/sysinfo.h> +#include <filesystem> #include "rainbow.hpp" typedef std::string str; int randint(int max) { - srand(std::chrono::duration_cast<std::chrono::milliseconds>( + srand(std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch()).count()); return rand() % max; }@@ -52,21 +54,26 @@ catch (std::exception e) { ret = docmd(cmd); }
return ret; } +int atoi(int &n, const char *c, int i=0){ + n=0; + for (; c[i] >= '0' && c[i] <= '9'; i++) n = n*10 + (int)c[i] - (int)'0'; + return i; +} + int intenv(const char* env) { int n=0; const char* val = getenv(env); - if (val == NULL) return -1; - for (int i = 0; val[i] >= '0' && val[i] <= '9'; i++) n = n*10 + (int)val[i] - (int)'0'; + if (val == NULL) return n; + atoi(n, val); return n; } -str time_ddd_hh_mm() { - //strftime(buffer, 80, "%a %H:%M", now); +str time_hh_mm() { time_t t = time(0); struct tm * now = localtime(&t); char buffer[80]; - strftime(buffer, 80, "%a %H:%M %Z", now); - return str(buffer); + int n = strftime(buffer, 80, "%H:%M", now); + return str(buffer, n); } typedef std::pair<str,str> paStr;@@ -89,7 +96,8 @@ if (fg!="") s = wrap(color(fg)) + s;
if (bg!="") s = wrap(color(bg,true)) + s; output += s + wrap("0"); } - void rainbow(rainbow &r, str s) { + struct rainbow r; + void rain(str s) { for (int i = 0; i < s.length(); i++) { add(s.substr(i,1), r.c.str()); r.next();@@ -100,45 +108,91 @@ };
int wave(int x) {return (int)(6+1.54*3.14159*(sin(x/(3.14159))));} +rainbow rain(int wavefactor=6) { + rainbow r; + const char* rainenv = getenv("RAINBOW"); + if (rainenv == NULL)r.init(25-randint(15)); + else { + atoi(r.s, rainenv, atoi(r.c.b, rainenv, atoi(r.c.g, rainenv,atoi(r.c.r, rainenv, 0)+1)+1)+1); + r.next(); + } + exportenv("RAINBOW", r.c.str()+";"+std::to_string(r.s)); + for(int i = 0; i < abs(wave(wavefactor)); i++)r.next(); + return r; +} + +str getpwd() { + str pwd; + try { pwd = std::string(getenv("PWD")); } + catch (std::exception e) { pwd = ""; } + if (pwd.back()=='\n')pwd.pop_back(); + return pwd; +} + +std::vector<str> split(str s, char delim) { + std::vector<str> ret; + str temp = ""; + for (int i = 0; i < s.length(); i++) { + if (s[i] == delim) { + ret.push_back(temp); + temp = ""; + } else temp += s[i]; + } + ret.push_back(temp); + return ret; +} + int main(int argc,char** argv) { escape PS1; for (int i=1;i<argc;i++) { str arg = argv[i]; if (arg == "--help" || arg == "help" || arg == "-h" || arg == "-?") { - std::cout << "Usage: " << argv[0] << " [view|ps]" << std::endl; + std::cout << "PROMPT_COMMAND='eval \"$(" << argv[0] << ")\"'" << std::endl; return 0; } if (arg == "view") { PS1.debugON = true; } } - int lineno = intenv("RBP_N")+1; - rainbow r; - r.c.set(intenv("RBP_R"), intenv("RBP_G"), intenv("RBP_B")); - r.s = intenv("RBP_S"); - if (r.s < 1 || (r.c[0]==r.c[1] && r.c[1]==r.c[2])) - r.init(25-randint(15)); - else r.next(); - if (!PS1.debugON) { - for (int i = 0; i < 3; i++) exportenv("RBP_" + str("RGB").substr(i % 3, 1), r.c[i]); - exportenv("RBP_S", r.s); - exportenv("RBP_N", lineno); - } - for(int i = 0; i < abs(wave(lineno)); i++) { - r.next(); - } + int lineno = intenv("LINENO"); + if (lineno == 0)std::cout << "export LINENO COLUMNS LINES "; + PS1.r = rain(lineno); - std::vector<str> host; - host.push_back(envorcmd("HOSTNAME","hostname")); - host.push_back(envorcmd("USER","whoami")); + std::vector<str> st; + st.push_back(envorcmd("USER","whoami")); + st.push_back(envorcmd("HOSTNAME","hostname")); + st.push_back(time_hh_mm()); + int len = 0; + for (int i = 0; i < st.size(); i++)if (st[i].length()>len) len = st[i].length(); + + for (int i = 0; i < st.size(); i++){ + while (st[i].length() < len) st[i] = " "+st[i]; + st[i] = " " + st[i] + " "; + } str ip = docmd("hostname -I | awk -F '.' ' { for(i=1;i<5;i++){printf(\"%.3d\", $i);}; } ' "); - PS1.add("$?", "190;180;30"); - PS1.add("|", "100;100;100"); - PS1.add(time_ddd_hh_mm(),ip.substr(3*((1+lineno)%4),3)); - PS1.add("|","100;100;100"); - PS1.rainbow(r, std::to_string(lineno)); + PS1.add("\\$?", "190;180;30"); + PS1.add(emote(), std::to_string(randint(214)+30)); + PS1.add("|", "150;150;150"); + str s = std::string(st[lineno%st.size()]); + int s_len = (s.length()/4); + if (s.length()%2==0) s_len++; + for (int i = 0; i<4; i++) PS1.add(s.substr(i*s_len,s_len),ip.substr(3*((i+lineno)%4),3)); + PS1.add("| ","150;150;150"); + PS1.rain(std::to_string(lineno)); PS1.add(" "); - PS1.rainbow(r, emote()); + str pwd = getpwd(); + std::vector<str> dirs = split(pwd,'/'); + if (dirs.size() == 0) dirs.push_back(""); + if (dirs[0].length() == 0) dirs[0] = "/"; + int i = dirs.size()-2; + if (i < 0) i = 0; + else PS1.add("..", "150;150;150"); + for (; i < dirs.size(); i++) { + PS1.add("/", "150;150;150"); + PS1.rain(dirs[i]); + } + PS1.add(" "); + PS1.rain("$"); PS1.add(" "); PS1.set();
M
src/rainbow.hpp
→
src/rainbow.hpp
@@ -22,7 +22,7 @@ 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());} - double operator[](int i) { + int operator[](int i) { switch (i%3) { case 0: return r; case 1: return g;