sophuwu.site > bashprompt
pee pee poo poo i forgor to commit :(
sophuwu sophie@skisiel.com
Thu, 25 Apr 2024 00:09:49 +0200
commit

5e51754a7aee9c4a64db3dc226732a4fddb4cff2

parent

e2fb9d8318ffe4eb241a04826dab3940a3336300

6 files changed, 64 insertions(+), 121 deletions(-)

jump to
M .gitignore.gitignore

@@ -2,4 +2,4 @@ .idea

a.out build pbin -cmake-build-debug +cmake-build-debug
D CMakeLists.txt

@@ -1,8 +0,0 @@

-cmake_minimum_required(VERSION 3.10) -project(bashprompt) - -set(CMAKE_CXX_STANDARD 17) - -include_directories(src) - -add_executable(bashprompt src/prompt.cpp)
D Makefile

@@ -1,8 +0,0 @@

- -build: - cmake -B build && cmake --build build || g++ --std=c++17 -o build/bashprompt src/prompt.cpp - -clean: build/ - @echo Cleaning up. - @rm -rf build - @echo Cleaned up successfully.
A README.md

@@ -0,0 +1,3 @@

+# Bash Prompt Generator + +Configurable Bash prompt generator with rainbow colors.
M src/prompt.cppsrc/prompt.cpp

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

* */ -#include <filesystem> -#include "rainbow.h" +#include <c++/11/string> +#include <chrono> +#include <cstdio> +#include <ctime> -int stoi(const std::string& str, int& i, int mode = 0) { - int result = 0; - int mult = 1; - 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; -} +const std::string emotes[] = +{":)", ":3", ";D",":]", ":D", "xD", +";3", ";)", "=)",":P", ":O", ":b"}; -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; +std::string emote() { + srand(time(0)); + return emotes[rand() % 12]; } -void exportEnv(const std::string& name, double value) { - printf(" %s=%lf", name.c_str(), value); +std::string padint(int n, int max) { + std::string s = std::to_string(n); + for(max=std::to_string(max).length();s.length()<max; s="0"+s); + return s; } -int getEnv(double& v, const std::string& name) { - if (char* env = std::getenv(name.c_str())) sscanf(env, "%lf", &v); - else return 1; - return 0; -} - -int randint() { - std::srand(std::time(nullptr)); - int n = std::rand() & 15; - n*=3; - n+=10; - return n; +void hm_time(int &h, int &m) { + 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); + h = (int)(tt->tm_hour); + m = (int)(tt->tm_min); } -int getAllEnvs(rainbow& r, int& count){ - if (char* env = std::getenv("RAINBOWPSC")) {sscanf(env, "%d", &count);} - else count = 0; - return getEnv(r.s , "RAINBOWPSS")||getEnv(r.c.r, "RAINBOWPSR") - ||getEnv(r.c.g, "RAINBOWPSG")||getEnv(r.c.b, "RAINBOWPSB"); +void pipe(std::string inputted, char *bufout) { + FILE* file = popen(inputted.c_str(), "r"); + fread(bufout, 1, 8, file); + pclose(file); } -const std::string emotes[] = -{":)", ":3", ";D", -":]", ":D", "xD", -";3", ";)", "=)", -":P", ":O", ":>"}; - - -std::string makePrompt(int& count) { - std::srand(std::time(nullptr)*count); - std::string path = std::filesystem::current_path(); - return std::to_string(count).append(" ") - .append(emotes[std::rand() % 12]).append(" ") - .append(path.substr(path.find_last_of('/')+1)); +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); + return b.substr(0, b.size() - 1); } -void printPrompt(rainbow& r, int& count) { - std::string outbuff = "\\[\\e[38;2;255;0;180m\\]<3\\[\\e[0m\\] "; - r.s=5.0*255.0/(double)(40+count%20-count%13+count%7); - std::string prompt = makePrompt(count); - for (int i = 0; i < prompt.length(); i++) { - outbuff.append("\\[\\e[38;2;") - .append(r.c.str()).append("m\\]") - .append(prompt.substr(i, 1)) - .append("\\[\\e[0m\\]"); - r.next(); - } - outbuff.append(" \\[\\e[38;2;").append(r.c.str()).append("m\\]\\\\$\\[\\e[0m\\] "); - printf("%s", outbuff.c_str()); +const char *hex="0123456789ABCDEF"; +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 (argc > 1 && argv[1][0]=='h') { - printf("To use this program\n"); - printf("add the following lines to your .bashrc:\n"); - printf("export PROMPT_COMMAND='`%s`'\n", argv[0]); - printf("PS1=`%s \"!\"`\n", argv[0]); - return 0; - } - rainbow r; - int count = 0; - int err = getAllEnvs(r, count); - if (err || !(int)r.s || (!(int)r.c.r && !(int)r.c.g && !(int)r.c.b)) r.init(randint()); - else r.next(); + 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[48;5;%dm \033[0m",ipaddr[0]); + printf("\033[48;5;%dm \033[0m",ipaddr[1]); + printf("\033[48;5;%dm \033[0m",ipaddr[2]); + printf("\033[48;5;%dm \033[0m\n",ipaddr[3]); - if (argc > 1 && argv[1][0]=='p') { - printPrompt(r, count); - return 0; - } - printf("export"); - exportEnv("RAINBOWPSS", r.s); - exportEnv("RAINBOWPSR", r.c.r); - exportEnv("RAINBOWPSG", r.c.g); - exportEnv("RAINBOWPSB", r.c.b); - printf(" RAINBOWPSC=%d", 1+count); - printf("\n"); + std::string host = readfile("/etc/hostname"); + int h, m; + hm_time(h, m); + printf("%.2d:%.2d\n", h, m); + printf("%s %s\n", host.c_str(), emote().c_str()); return 0; }
M src/rainbow.hsrc/rainbow.hpp

@@ -1,21 +1,11 @@

-/* - * File: rainbow.h - * Description: A header file for printing rainbows in C++ - * Creator: Sophuwu <sophie@sophuwu.site> - * Date: 2023-08-04 - * License: MIT - */ #ifndef RAINBOW_H #define RAINBOW_H 1 -#include <string> -void panic(std::string s) { - //fprintf(stderr, "Fatal error: %s\n", s.c_str()); - exit(1); -} +#include <string> +#include <cstdio> struct rgb { - double r, g, b; + int r, g, b; void clamp() { if (r > 255) r = 255; else if (r < 0) r = 0;

@@ -55,16 +45,16 @@ };

struct rainbow { rgb c; - double s = 0; // step + int s = 0; // step - void init(int len) { - if (len == 0) panic("Divison by zero."); + void init(int len = 10) { + if (len == 0) len = 10; c.set(255, 0, 0); - s = 5.0*255.0 / (double)len; + s = 5.0*255.0 / len; } void next() { - if (s == 0) panic("Empty infinity loop. Rainbow not initialized."); + if (s == 0) return; 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);