current state

This commit is contained in:
Luca Conte 2024-08-11 23:34:01 +00:00
parent 53a322ef30
commit 4e0a3e54a3
1 changed files with 148 additions and 0 deletions

148
main.c Normal file
View File

@ -0,0 +1,148 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <wchar.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>
#define C_DEFAULT 39
#define C_BLACK 30
#define C_RED 31
#define C_GREEN 32
#define C_YELLOW 33
#define C_BLUE 34
#define C_MAGENTA 35
#define C_CYAN 36
#define C_L_GRAY 37
#define C_D_GRAY 90
#define C_L_RED 91
#define C_L_GREEN 92
#define C_L_YELLOW 93
#define C_L_BLUE 94
#define C_L_MAGENTA 95
#define C_L_CYAN 96
#define C_WHITE 97
#define SEPERATOR L'\xe0bc'
#define USER L'\xf2c0'
#define DEVICE L'\xf4a9'
#define BRANCH L'\xf418'
#define FOLDER L'\xf114'
int fg = C_BLACK;
int bgHost = C_L_CYAN;
int bgUser = C_L_GREEN;
int bgBranch = C_L_GRAY;
int bgEnd = C_CYAN;
int bgFolder[] = { C_WHITE, C_CYAN };
int numFolderColors = 2;
void fgcol(int col) {
wprintf(L"\\[\x1b[%dm\\]", col);
}
void bgcol(int col) {
wprintf(L"\\[\x1b[%dm\\]",col + 10);
}
void printGlyph(wchar_t unicode) {
wprintf(L"%lc", unicode);
}
void seperate(int col1, int col2) {
fgcol(col1);
bgcol(col2);
printGlyph(SEPERATOR);
fgcol(fg);
}
int main(void) {
char path_buf[1024];
getcwd(path_buf, sizeof(path_buf));
char hostname_buf[1024];
gethostname(hostname_buf, sizeof(hostname_buf));
struct passwd *p = getpwuid(getuid());
char* username_buf = p->pw_name;
FILE *fp;
char branch[1024];
fp = popen("/usr/bin/git rev-parse --abbrev-ref HEAD 2>/dev/null", "r");
if (fp == NULL) exit(1);
if (fgets(branch, sizeof(branch), fp) == NULL) branch[0] = 0x00;
if (strcmp(username_buf, "root") == 0) {
bgUser = C_L_MAGENTA;
}
setlocale(LC_CTYPE, "");
fgcol(C_BLACK);
bgcol(bgHost);
wprintf(L" ");
printGlyph(DEVICE);
wprintf(L" %s ", hostname_buf);
seperate(bgHost, bgUser);
wprintf(L" ");
printGlyph(USER);
wprintf(L" %s ", username_buf);
int folder = 0;
seperate(bgUser, bgFolder[0]);
wprintf(L" ");
printGlyph(FOLDER);
wprintf(L" ");
for (int i = 1; i < strlen(path_buf); i++) {
if (path_buf[i] == '/') {
if (path_buf[i + 1] != 0x00) {
wprintf(L" ");
seperate(bgFolder[folder % numFolderColors], bgFolder[(folder + 1) % numFolderColors]);
wprintf(L" ");
printGlyph(FOLDER);
wprintf(L" ");
folder++;
}
} else {
wprintf(L"%c", path_buf[i]);
}
}
if (strlen(path_buf)!=1) {
wprintf(L" ");
}
seperate(bgFolder[folder % numFolderColors], C_DEFAULT);
wprintf(L"\n");
if (strlen(branch) > 0) {
bgcol(bgBranch);
wprintf(L" ");
printGlyph(BRANCH);
wprintf(L" ");
for (int i = 0; i < strlen(branch); i++) {
if (branch[i] == '\n') break;
wprintf(L"%c", branch[i]);
}
seperate(bgBranch, bgEnd);
}
bgcol(bgEnd);
wprintf(L" $ ");
seperate(bgEnd, C_DEFAULT);
wprintf(L" ");
fgcol(0);
}