This repository has been archived on 2025-04-27. You can view files and clone it, but cannot push or open issues or pull requests.
CustomPromptV2/main.c

183 lines
3.3 KiB
C

#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 bgUserRoot = C_L_MAGENTA;
// COLORS FOR USERS UID >= 1000 (UID - 1000 = Index)
int bgUsers[1] = {
C_L_GREEN // UID 1000
};
// COLOR FOR USERS UID < 1000 || UID > 1010
int bgUserOther = C_L_YELLOW;
int bgHost = C_L_CYAN;
int bgUser;
int fgBranchClean = C_BLACK;
int fgBranchNotClean = C_RED;
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 = getenv("PWD");
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;
fclose(fp);
int changes = 1;
if (strlen(branch) > 0) {
fp = popen("/usr/bin/git status --porcelain 2>/dev/null", "r");
if (fp == NULL) exit(1);
char dummy[2];
if (fgets(dummy, 2, fp) == NULL) {
changes = 0;
}
fclose(fp);
}
uid_t uid = getuid();
if (uid == 0) {
bgUser = bgUserRoot;
} else if (uid >= 1000 && uid <= 1010) {
bgUser = bgUsers[uid - 1000];
} else {
bgUser = bgUserOther;
}
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" ");
if (changes != 0) {
fgcol(fgBranchNotClean);
}
printGlyph(BRANCH);
fgcol(C_BLACK);
wprintf(L" ");
for (int i = 0; i < strlen(branch); i++) {
if (branch[i] == '\n') break;
wprintf(L"%c", branch[i]);
}
wprintf(L" ");
seperate(bgBranch, bgEnd);
}
bgcol(bgEnd);
wprintf(L" $ ");
seperate(bgEnd, C_DEFAULT);
wprintf(L" ");
fgcol(0);
}