git.sophuwu.com > bashprompt
made ip easier to set on different hosts with IPStr and IPCmd env vars to overwrite the default functions
sophuwu sophie@skisiel.com
Sat, 22 Mar 2025 10:20:15 +0100
commit

49f54d5604159385f4639b5e65827f0d62544981

parent

a496f29133132012cba840d5c7f6997825e891b0

4 files changed, 53 insertions(+), 16 deletions(-)

jump to
M src/common.cppsrc/common.cpp

@@ -6,8 +6,8 @@ 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"); +str docmd(const char* inputted) { + FILE* file = popen(inputted, "r"); char buff[1024]; int n = fread(buff, 1, sizeof(buff), file); pclose(file);

@@ -19,7 +19,7 @@

str envorcmd(str env, str cmd) { str ret = ""; try { ret = std::string(getenv(env.c_str())); } - catch (std::exception e) { ret = docmd(cmd); } + catch (std::exception e) { ret = docmd(cmd.c_str()); } return ret; }

@@ -28,6 +28,7 @@ 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;
M src/common.hppsrc/common.hpp

@@ -18,7 +18,7 @@ extern paStr DBG;

extern paStr* esc; extern str wrap(str s); -extern str docmd(std::string inputted); +extern str docmd(const char* inputted); extern str envorcmd(str env, str cmd); extern int atoi(int &n, const char *c, int i); extern int intenv(const char* env);
M src/ip.hppsrc/ip.hpp

@@ -8,21 +8,57 @@ #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; + int values[4]; + int fromString(str s) { + int pos = 0; + int c = 0; + for (int i=0;i<4;i++) { + values[i] = 0; + } + for (int i=0;pos<s.length()&&i<4;pos++){ + c=(int)(s.c_str()[pos]); + if (c>=48&&c<=57) values[i] = (values[i]*10) + (c - 48); + else if (i>=3) break; + else i++; + } + c = 0; + for (int i=0;i<4;i++) { + if (values[i]>255||values[i]<0) return 0; + c += values[i]; + } + if (c == 0) return 0; + return 1; } - void fromCmd(str cmd = "hostname -i") { - fromString(docmd(cmd)); + int fromEnv() { + const char * env = getenv("IPStr"); + if (env != NULL) return fromString(std::string(env)); + return 0; + } + int fromCmd() { + str s; + const char * env = getenv("IPCmd"); + if (env != NULL && fromString(docmd(env).c_str())) return 1; + return fromString(docmd("hostname -i").c_str()); + } + int get() { + int ok = fromEnv(); + if (!ok)ok = fromCmd(); + return ok; } str toColor() { str ret = ""; ret+= color2(int2col(values[0]), int2col(values[1])); ret+= color2(int2col(values[2]), int2col(values[3])); return ret; + } + str toString() { + str s=""; + for (int i = 0; i < 4; i++) { + s+=std::to_string(values[i]); + s+= "."; + } + s.pop_back(); + return s; } };
M src/prompt.cppsrc/prompt.cpp

@@ -97,8 +97,8 @@ esc = &DBG;

} if (arg=="ip") { IP ip; - ip.fromCmd(); - printf("%s\n",ip.toColor().c_str()); + if (ip.get()==0)return 1; + printf("%s\n%s\n",ip.toString().c_str(),ip.toColor().c_str()); return 0; } }

@@ -110,10 +110,10 @@ str user=" ";

const char* ipcol = getenv("IPCOLOR"); if (ipcol == NULL || !(std::string(ipcol)=="none"||std::string(ipcol)=="NONE")) { IP ip; - ip.fromCmd(); + int ok = ip.get(); user = envorcmd("USER", "whoami"); user = " " + user + " "; - PS1.output+=ip.toColor()+" "; + if (ok) PS1.output+=ip.toColor()+" "; }