feat: pretty colors!

This commit is contained in:
2026-10-09 16:41:04 +02:00
parent 7dc85ea994
commit 2396e4c869
2 changed files with 100 additions and 16 deletions

68
src/ansi.h Normal file
View File

@@ -0,0 +1,68 @@
#define ANSI_FG_BLACK "\x1b[30m"
#define ANSI_FG_RED "\x1b[31m"
#define ANSI_FG_GREEN "\x1b[32m"
#define ANSI_FG_YELLOW "\x1b[33m"
#define ANSI_FG_BLUE "\x1b[34m"
#define ANSI_FG_PURPLE "\x1b[35m"
#define ANSI_FG_CYAN "\x1b[36m"
#define ANSI_FG_WHITE "\x1b[37m"
#define ANSI_FG_BLACK_BRIGHT "\x1b[90m"
#define ANSI_FG_RED_BRIGHT "\x1b[91m"
#define ANSI_FG_GREEN_BRIGHT "\x1b[92m"
#define ANSI_FG_YELLOW_BRIGHT "\x1b[93m"
#define ANSI_FG_BLUE_BRIGHT "\x1b[94m"
#define ANSI_FG_PURPLE_BRIGHT "\x1b[95m"
#define ANSI_FG_CYAN_BRIGHT "\x1b[96m"
#define ANSI_FG_WHITE_BRIGHT "\x1b[97m"
#define ANSI_FG_BLACK_BOLD "\x1b[1;30m"
#define ANSI_FG_RED_BOLD "\x1b[1;31m"
#define ANSI_FG_GREEN_BOLD "\x1b[1;32m"
#define ANSI_FG_YELLOW_BOLD "\x1b[1;33m"
#define ANSI_FG_BLUE_BOLD "\x1b[1;34m"
#define ANSI_FG_PURPLE_BOLD "\x1b[1;35m"
#define ANSI_FG_CYAN_BOLD "\x1b[1;36m"
#define ANSI_FG_WHITE_BOLD "\x1b[1;37m"
#define ANSI_FG_BLACK_BRIGHT_BOLD "\x1b[1;90m"
#define ANSI_FG_RED_BRIGHT_BOLD "\x1b[1;91m"
#define ANSI_FG_GREEN_BRIGHT_BOLD "\x1b[1;92m"
#define ANSI_FG_YELLOW_BRIGHT_BOLD "\x1b[1;93m"
#define ANSI_FG_BLUE_BRIGHT_BOLD "\x1b[1;94m"
#define ANSI_FG_PURPLE_BRIGHT_BOLD "\x1b[1;95m"
#define ANSI_FG_CYAN_BRIGHT_BOLD "\x1b[1;96m"
#define ANSI_FG_WHITE_BRIGHT_BOLD "\x1b[1;97m"
#define ANSI_BG_BLACK "\x1b[0;40m"
#define ANSI_BG_RED "\x1b[0;41m"
#define ANSI_BG_GREEN "\x1b[0;42m"
#define ANSI_BG_YELLOW "\x1b[0;43m"
#define ANSI_BG_BLUE "\x1b[0;44m"
#define ANSI_BG_PURPLE "\x1b[0;45m"
#define ANSI_BG_CYAN "\x1b[0;46m"
#define ANSI_BG_WHITE "\x1b[0;47m"
#define ANSI_BG_BLACK_BRIGHT "\x1b[0;100m"
#define ANSI_BG_RED_BRIGHT "\x1b[0;101m"
#define ANSI_BG_GREEN_BRIGHT "\x1b[0;102m"
#define ANSI_BG_YELLOW_BRIGHT "\x1b[0;103m"
#define ANSI_BG_BLUE_BRIGHT "\x1b[0;104m"
#define ANSI_BG_PURPLE_BRIGHT "\x1b[0;105m"
#define ANSI_BG_CYAN_BRIGHT "\x1b[0;106m"
#define ANSI_BG_WHITE_BRIGHT "\x1b[0;107m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ANSI_UNKNOWN -1
#define ANSI_CURSOR_LEFT 0
#define ANSI_CURSOR_RIGHT 1
#define ANSI_CURSOR_UP 2
#define ANSI_CURSOR_DOWN 3
#define ANSI_DELETE_WORD 4
#define ANSI_CURSOR_LEFT_WORD 5
#define ANSI_CURSOR_RIGHT_WORD 6
#define ANSI_CURSOR_UP_WORD 7
#define ANSI_CURSOR_DOWN_WORD 8

View File

@@ -9,6 +9,7 @@
#include <errno.h>
#include <termios.h>
#include "ansi.h"
#define ESC \x1B
@@ -36,18 +37,6 @@ void pretty_path_home(char** path) {
}
#define ANSI_UNKNOWN -1
#define ANSI_CURSOR_LEFT 0
#define ANSI_CURSOR_RIGHT 1
#define ANSI_CURSOR_UP 2
#define ANSI_CURSOR_DOWN 3
#define ANSI_DELETE_WORD 4
#define ANSI_CURSOR_LEFT_WORD 5
#define ANSI_CURSOR_RIGHT_WORD 6
#define ANSI_CURSOR_UP_WORD 7
#define ANSI_CURSOR_DOWN_WORD 8
int read_ansi() {
switch (getchar()) {
case '[':
@@ -133,6 +122,16 @@ size_t strnocspn(char* str, size_t n, size_t o, char* reject) {
}
char* status_string(int status) {
switch (status) {
case 0:
return "";
}
return "❗";
}
int main(int argc, char** argv) {
int retval = 0;
char* tokens[LINESIZE] = {};
@@ -190,10 +189,27 @@ int main(int argc, char** argv) {
pretty_path_home(&pwd_disp);
if (laststatus != 0) {
fprintf(stdout, "❌");
}
snprintf(prompt, LINESIZE, "%s@%s %s$ ", user, host, pwd_disp);
snprintf(
prompt,
LINESIZE,
ANSI_BG_BLUE ANSI_FG_BLACK
"%s" // status
" %s" // user
"@"
ANSI_FG_BLACK
"%s " // host
ANSI_COLOR_RESET ANSI_FG_BLUE
""
ANSI_FG_BLUE
"%s" // directory
ANSI_COLOR_RESET
"$ "
ANSI_COLOR_RESET,
status_string(laststatus), user, host, pwd_disp
);
printf("%s", prompt);