sophuwu.site > bashprompt
idk a little stuff
sophuwu sophie@skisiel.com
Thu, 11 Jul 2024 00:05:11 +0200
commit

df9ebe6aa3d1d6cba0c16c5f9e792fa7e4db20bc

parent

c365abb5d3a27dd7fca14b1b28a2b2436626bda2

7 files changed, 268 insertions(+), 197 deletions(-)

jump to
M MakefileMakefile

@@ -1,5 +1,7 @@

-build: src/prompt.cpp +build: src/*.cpp mkdir -p build - g++ -std=c++17 -o build/bashprompt src/prompt.cpp + g++ -std=c++17 -o build/bashprompt src/*.cpp run: - ./build/bashprompt+ ./build/bashprompt +clean: + @echo "unimplemented"
D src/color-test.py

@@ -1,90 +0,0 @@

-#!/usr/bin/python3 -import sys -import os - - -def rain(): - r=[rg for rg in range(0, 5)] - a=[[5-i%6+i%6, i%6,0] for i in r[:-1]]+[[i%6, 5-i%6+i%6,0] for i in r[::-1]] - a=a+[i[::-1] for i in a][:-1][::-1]+[[i%6, 0, 5-i%6+i%6] for i in r[1:]]+[[5,0,j-1] for j in range(5,1,-1)] - for i in range(len(a)): - r,g,b=a[i] - a[i]=36*r+6*g+b+16 - return a - - -def avg(arr): return sum(arr)//len(arr) - - -def box(i): return str("\033[48;5;"+str(i)+"m \033[0m") - - -def ax(b,f): return box(b).replace("m ",";38;5;"+str(f)+"m▄\033[0m") - - -def boxs(ip:list()): - if len(ip) == 4: return ax(ip[0],ip[1])+ax(ip[2],ip[3]) - if len(ip) == 2: return ax(ip[0],ip[1]) - else: return "" - - -def rgb(ip): - ip=[int(i) for i in ip] - ip=[avg(ip[2:3]),0,ip[1],0,255-avg(ip[2:3]),ip[0]] - ip=[str(i) for i in ip] - s="\033[48;2;" - s+=";".join(ip[:3]) - s+=";38;2;" - s+=";".join(ip[3:]) - s+="m▄\033[0m" - return s - - -def ip2rgb(ip): - if type(ip)==type(""): ip=ip.split(".") - if not (type(ip) is type([]) and len(ip) == 4): raise TypeError - col=0 - if os.getenv("IPCOLOR")in["less","216","0","alt"]:col=16 - ip = [(int(i)*(216+col)//256)+(16-col) for i in ip] - return ip - - -HXC=[rain()[i] for i in range(len(rain())) if int(i)%6<4] -HX="0123456789abcdef" -def hx(i: int): - s="" - while i>0: - s=str(HX[i%16])+s - i=i//16 - return s - - -def hexstr(instr:str): return hx(sum([[int(j) for j in instr.split(".")][::-1][i]<<8*i for i in range(4)])) -def hexc(instr:str): - h=hexstr(instr) - a=[] - for i in h: - a+=[HXC[HX.index(i)]] - return a - -if os.getenv("IPADDR") is not None: - try: - arg=os.getenv("IPADDR") - ip=ip2rgb(arg) - o=boxs(ip) - print(o) - exit(0) - except: - exit(1) - finally: - exit(2) - - -for arg in sys.argv: - try: - ip = ip2rgb(arg) - i=[int(j) for j in arg.split(".")] - print(rgb(i)+str(arg).rjust(15)+" "+boxs(ip)) - except TypeError: - continue -
A src/common.cpp

@@ -0,0 +1,89 @@

+#include "common.hpp" + + +paStr PRT = {"\\[\\e[","m\\]"}; +paStr DBG = {"\033[","m"}; +paStr* esc = &PRT; +str wrap(str s) {return esc->s + s + esc->e;} + +str docmd(std::string inputted) { + FILE* file = popen(inputted.c_str(), "r"); + 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; +} + +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 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 n; + atoi(n, val); + return n; +} + +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; +} +str hex = "0123456789ABCDEF"; +str tohex(int n) { + str ret = ""; + for (int i = 0; i < 2; i++) { + ret = hex[n%16] + ret; + n /= 16; + } + return ret; +} + +int int2col(int n) { + if (n>255||n<0) return 0; + int col = 0; + char* env = getenv("IPCOLOR"); + if (env != NULL && str(env)=="alt") col=16; + n=(n*(216+col)/256)+(16-col); + return n; +} + +int avg(int a[], int n) { + int sum = 0; + for (int i = 0; i < n; i++) sum += a[i]; + return sum/n; +} + +int map10(char a) { + int x = a-'0'; + return 36*(5*x/10)+6*(5*(10-x)/10)+16; +} + +str color2(int top, int bot) { + str ret = "48;5;"; + ret += std::to_string(top); + ret += ";38;5;" + std::to_string(bot); + ret = wrap(ret); + ret+="▌"; // "▌" "▀" "▄" + return ret+wrap("0"); +}
A src/common.hpp

@@ -0,0 +1,35 @@

+/* + * common.hpp + * this is where all the common functions and variables are stored + */ +#ifndef BASHPROMPT_COMMON_H +#define BASHPROMPT_COMMON_H + + +#include <cstdio> +#include <cmath> // for sin +#include <cstdlib> // for rand +#include <vector> +#include <string> +typedef std::string str; + +struct paStr {str s, e;}; +extern paStr PRT; +extern paStr DBG; +extern paStr* esc; +extern str wrap(str s); + +extern str docmd(std::string inputted); +extern str envorcmd(str env, str cmd); +extern int atoi(int &n, const char *c, int i); +extern int intenv(const char* env); +extern std::vector<str> split(str s, char delim); +extern str tohex(int n); + +extern int int2col(int n); +extern int avg(int a[], int n); +extern int map10(char a); + +extern str color2(int top, int bot); + +#endif //BASHPROMPT_COMMON_H
A src/ip.hpp

@@ -0,0 +1,30 @@

+/* + * ip.hpp + * this is to get the ip address of the machine and convert it to a color + */ + +#ifndef BASHPROMPT_IP_H +#define BASHPROMPT_IP_H +#include "common.hpp" + +struct IP { + int values[4] = {0,0,0,0}; + str string = ""; + void fromString(str s) { + std::vector<str> parts = split(s, '.'); + for (int i = 0; i < 4; i++) values[i] = std::stoi(parts[i]); + string = s; + } + void fromCmd(str cmd = "hostname -i") { + fromString(docmd(cmd)); + } + str toColor() { + str ret = ""; + ret+= color2(values[0], values[1]); + ret+= color2(values[2], values[3]); + return ret; + } +}; + + +#endif //BASHPROMPT_IP_H
M src/prompt.cppsrc/prompt.cpp

@@ -5,14 +5,10 @@ * It can also be used to generate the necessary environment variables

* */ -#include <string> -#include <cstdio> -#include <vector> -#include <cmath> // for sin - +#include "common.hpp" +#include "ip.hpp" #include "rainbow.hpp" -typedef std::string str; void exportenv(str k, str v) {printf("export %s=\"%s\"\n", k.c_str(), v.c_str());}

@@ -24,44 +20,9 @@ str emote(){

return eyelist.substr(randint(eyelist.length()-1),1) + mouthlist.substr(randint(mouthlist.length()-1),1); } -str docmd(std::string inputted) { - FILE* file = popen(inputted.c_str(), "r"); - 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; -} -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 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 n; - atoi(n, val); - return n; -} - -typedef std::pair<str,str> paStr; struct escape { - paStr PS = paStr("\\[\\e[","m\\]"); - paStr XT = paStr("\033[","m"); str output = ""; - bool debugON = false; - str wrap(str s) { if(debugON)return XT.first+s+XT.second; return PS.first + s + PS.second; } str color(str color,bool background=false) { if (color.length()==0) return ""; if (color.length() <= 3)color="5;"+color;

@@ -108,28 +69,7 @@ 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; -} -str hex = "0123456789ABCDEF"; -str tohex(int n) { - str ret = ""; - for (int i = 0; i < 2; i++) { - ret = hex[n%16] + ret; - n /= 16; - } - return ret; -} int main(int argc,char** argv) { escape PS1;

@@ -140,7 +80,13 @@ printf("PROMPT_COMMAND='eval \"$(%s)\"'\n",argv[0]);

return 0; } if (arg == "view") { - PS1.debugON = true; + esc = &DBG; + } + if (arg=="ip") { + IP ip; + ip.fromCmd(); + printf("%s\n",ip.toColor().c_str()); + return 0; } } int lineno = intenv("LINENO");

@@ -148,52 +94,21 @@ if (lineno == 0)printf("%s\n","export LINENO COLUMNS LINES ");

PS1.r = rain(lineno); srand((unsigned int)(intenv("RANDOM")+lineno)); - std::vector<str> st; - st.push_back(envorcmd("USER","whoami")); - st.push_back(envorcmd("HOSTNAME","hostname")); - st.push_back(docmd("date +'%T'")); - st.push_back(docmd("free -m | awk '/Mem/ {printf(\"%.2f/%.0fG\\n\", $3/1000, $2/1000);}'")); - int ipn[4] = {0,0,0,0}; - str ip = docmd("hostname -i"); - int j = 0; - for (int i = 0; i < 4; i++) { - j = atoi(ipn[i],ip.c_str(),j)+1; - } - ip=""; - for (int i = 0; i < 4; i++) { - ip += tohex(ipn[i]); - } - st.push_back(ip); + IP ip; + ip.fromCmd(); - 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] + " "; - } - PS1.add("\\${?#0} ", "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),std::to_string(ipn[i%4])); - //for (int i = 0; i<s.length();i++) PS1.add(s.substr(i,1),std::to_string(ipn[i%4])); - PS1.add("| ","150;150;150"); + if (lineno%10==0) PS1.output+=ip.toColor(); + else PS1.add("<3", "255;0;200"); + PS1.add(" "); + PS1.add("\\${?}", "202"); + PS1.add(" "); PS1.rain(std::to_string(lineno)); PS1.add(" "); + PS1.rain(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]); - } + size_t a = pwd.find_last_of("/"); + pwd PS1.add(" "); PS1.rain("$"); PS1.add(" ");
A test/color-test.py

@@ -0,0 +1,90 @@

+#!/usr/bin/python3 +import sys +import os + + +def rain(): + r=[rg for rg in range(0, 5)] + a=[[5-i%6+i%6, i%6,0] for i in r[:-1]]+[[i%6, 5-i%6+i%6,0] for i in r[::-1]] + a=a+[i[::-1] for i in a][:-1][::-1]+[[i%6, 0, 5-i%6+i%6] for i in r[1:]]+[[5,0,j-1] for j in range(5,1,-1)] + for i in range(len(a)): + r,g,b=a[i] + a[i]=36*r+6*g+b+16 + return a + + +def avg(arr): return sum(arr)//len(arr) + + +def box(i): return str("\033[48;5;"+str(i)+"m \033[0m") + + +def ax(b,f): return box(b).replace("m ",";38;5;"+str(f)+"m▄\033[0m") + + +def boxs(ip:list()): + if len(ip) == 4: return ax(ip[0],ip[1])+ax(ip[2],ip[3]) + if len(ip) == 2: return ax(ip[0],ip[1]) + else: return "" + + +def rgb(ip): + ip=[int(i) for i in ip] + ip=[avg(ip[2:3]),0,ip[1],0,255-avg(ip[2:3]),ip[0]] + ip=[str(i) for i in ip] + s="\033[48;2;" + s+=";".join(ip[:3]) + s+=";38;2;" + s+=";".join(ip[3:]) + s+="m▄\033[0m" + return s + + +def ip2rgb(ip): + if type(ip)==type(""): ip=ip.split(".") + if not (type(ip) is type([]) and len(ip) == 4): raise TypeError + col=0 + if os.getenv("IPCOLOR")in["less","216","0","alt"]:col=16 + ip = [(int(i)*(216+col)//256)+(16-col) for i in ip] + return ip + + +HXC=[rain()[i] for i in range(len(rain())) if int(i)%6<4] +HX="0123456789abcdef" +def hx(i: int): + s="" + while i>0: + s=str(HX[i%16])+s + i=i//16 + return s + + +def hexstr(instr:str): return hx(sum([[int(j) for j in instr.split(".")][::-1][i]<<8*i for i in range(4)])) +def hexc(instr:str): + h=hexstr(instr) + a=[] + for i in h: + a+=[HXC[HX.index(i)]] + return a + +if os.getenv("IPADDR") is not None: + try: + arg=os.getenv("IPADDR") + ip=ip2rgb(arg) + o=boxs(ip) + print(o) + exit(0) + except: + exit(1) + finally: + exit(2) + + +for arg in sys.argv: + try: + ip = ip2rgb(arg) + i=[int(j) for j in arg.split(".")] + print(rgb(i)+str(arg).rjust(15)+" "+boxs(ip)) + except TypeError: + continue +