sophuwu.site > bashprompt
added the rainbow lib and made the program read, incrament and export the rainbow env vars
sophuwu sophie@sophuwu.site
Sun, 08 Oct 2023 00:59:24 +0200
commit

9e438ede31f547104b4776521d484846bc7a511e

parent

146b22437e1ff0fd273468c671828a13b565fb75

2 files changed, 145 insertions(+), 14 deletions(-)

jump to
M main.cppmain.cpp

@@ -1,17 +1,44 @@

#include <cstdlib> #include <string> - -int stoi(const std::string& str) { +#include <ctime> +#include "rainbow.h" +int stoi(const std::string& str, int& i, int mode = 0) { int result = 0; int mult = 1; - for (int i = 0; i < str.length(); i++) { + for (; i < str.length(); i++) { + if (mode && str[i] == '.') break; if (str[i] == '-') mult = -1; else if (str[i] >= '0' && str[i] <= '9') result = result * 10 + mult*(str[i] - '0'); } return result; } -int main() { +double stof(const std::string& str) { + int i = 0; + double whole = stoi(str, i, 1); + double frac = stoi(str, i); + for (; frac > 1; frac /= 10); + return whole + frac; +} + +void exportEnv(const std::string& name, double value) { + printf(" %s=%f", name.c_str(), value); +} + +int getEnv(double& v, const std::string& name) { + if (char* env = std::getenv(name.c_str())) v = stof(env); + else return 1; + return 0; +} + +int randint() { + std::srand(std::time(nullptr)); + int n = std::rand() & 15; + n+=10; + return n; +} + +int main(int argc, char* argv[]) { /*Bash prompt should: - show current folder

@@ -20,17 +47,20 @@ - show random emoticon

- display in rainbow colors (choose rainbow length with env variable) */ - int bashline, rlenh, rlenv; - double i, r, g, b; + rainbow r; + if (argc == 2 && argv[1][0] == 's') r.init(15+stio(argv[1])); + else if (getEnv(r.s , "PSRS")||getEnv(r.c.r, "PSRR") + ||getEnv(r.c.g, "PSRG")||getEnv(r.c.b, "PSRB") + || !(int)r.s || (!(int)r.c.r && !(int)r.c.g && !(int)r.c.b)) + r.init(randint()); + else r.next(); - int var; - int var2; - if(char* env = std::getenv("XSOPHCOUN")) var = stoi(env); - else var = 0; - if(char* env = std::getenv("")) var2 = stoi(env); - else var2 = 0; - - printf("export PS1=\"$SOPHVAR $HISTCMD:$LINENO\""); + printf("export"); + exportEnv("PSRS", r.s); + exportEnv("PSRR", r.c.r); + exportEnv("PSRG", r.c.g); + exportEnv("PSRB", r.c.b); + printf("%c", '\n'); return 0; }
A rainbow.h

@@ -0,0 +1,101 @@

+/* + * File: rainbow.h + * Description: A header file for printing rainbows in C++ + * GitHub: https://github.com/sophuwu300/rainbow.h + * + * Creator: Sophuwu <sophie@sophuwu.site> + * - discord: sophuwu + * - website: https://sophuwu.site https://skisiel.com + * - github: Sophuwu300 + * + * Date: 2023-08-04 + * + * License: MIT: Must include license if used in project + * - Can edit, modify, and redistribute + * - Cannot hold liable + * - Must include license - this comment block + * - Must name creator - Sophuwu + * + * Examples and Documentation: + * - https://sophuwu.site/rainbow.h + * - https://github.com/sophuwu300/rainbow.h + * + */ + +#include <stdio.h> +#include <string> + +void panic(std::string s) { + //fprintf(stderr, "Fatal error: %s\n", s.c_str()); + exit(1); +} + +struct rgb { + double r, g, b; + + void clamp() { + if (r > 255) r = 255; else if (r < 0) r = 0; + if (g > 255) g = 255; else if (g < 0) g = 0; + if (b > 255) b = 255; else if (b < 0) b = 0; + } + + 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(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) { + case 0: return r; + case 1: return g; + case 2: return b; + } + return 0; + } + void indexAdd(int i, double v) { + switch (i%3) { + case 0: r += v; break; + case 1: g += v; break; + case 2: b += v; break; + } + clamp(); + } +}; + +struct rainbow { + rgb c; + double s = 0; // step + + void init(int len) { + if (len == 0) panic("Divison by zero."); + c.set(255, 0, 0); + s = 5.0*255.0 / (double)len; + } + + void next() { + if (s == 0) panic("Empty infinity loop. Rainbow not initialized."); + for (int i = 0; i < 3; i++) { + if ((c[i+1]==0||c[i+1]==255)&&(c[i+2]==0||c[i+2]==255)) { + if (c[i+1]==0&&c[i+2]==255) c.indexAdd(i, s); + else if (c[i+1]==255&&c[i+2]==0) c.indexAdd(i, -s); + } + } + } + void print(std::string s) {c.print(s);} + void print2d(std::string s, int len = 30) { + rainbow r; + r.init(len); + r.c.set(c); + for (int i = 0; i < s.length(); i++) { + r.print(s.substr(i, 1)); + r.next(); + } + } +};