improvenment to color managenment

This commit is contained in:
nova 2025-05-10 23:33:27 +02:00
parent 882e6fc85f
commit 7f0e65eaf3
5 changed files with 82 additions and 52 deletions

View File

@ -54,24 +54,27 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
lstat(dir_content[i].file_name, file); lstat(dir_content[i].file_name, file);
if (S_ISDIR(file->st_mode)) { if (S_ISDIR(file->st_mode)) {
dir_content[i].file_type = COLOR_DIR; dir_content[i].file_type = FILE_TYPE_DIR;
dir_content[i].color_pair = COLOR_DIR; dir_content[i].color_pair = COLOR_DIR;
} else if (file->st_mode & S_IXUSR) {
dir_content[i].file_type = FILE_TYPE_EXEC;
dir_content[i].color_pair = COLOR_EXEC;
} else if (S_ISBLK(file->st_mode)) { } else if (S_ISBLK(file->st_mode)) {
dir_content[i].file_type = COLOR_BLOCK; dir_content[i].file_type = FILE_TYPE_BLOCK;
dir_content[i].color_pair = COLOR_BLOCK; dir_content[i].color_pair = COLOR_BLOCK;
/*} else if (S_ISCHR(file->st_mode)) { /*} else if (S_ISCHR(file->st_mode)) {
dir_content[i].file_type = COLOR_CHARDEV; */ dir_content[i].file_type = COLOR_CHARDEV; */
} else if (S_ISLNK(file->st_mode)) { } else if (S_ISLNK(file->st_mode)) {
dir_content[i].file_type = COLOR_SYMLINK; dir_content[i].file_type = FILE_TYPE_SYMLINK;
dir_content[i].color_pair = COLOR_SYMLINK; dir_content[i].color_pair = COLOR_SYMLINK;
} else if (S_ISFIFO(file->st_mode)) { } else if (S_ISFIFO(file->st_mode)) {
dir_content[i].file_type = COLOR_FIFO; dir_content[i].file_type = FILE_TYPE_FIFO;
dir_content[i].color_pair = COLOR_FIFO; dir_content[i].color_pair = COLOR_FIFO;
} else if (S_ISSOCK(file->st_mode)) { } else if (S_ISSOCK(file->st_mode)) {
dir_content[i].file_type = COLOR_SOCK; dir_content[i].file_type = FILE_TYPE_SOCK;
dir_content[i].color_pair = COLOR_SOCK; dir_content[i].color_pair = COLOR_SOCK;
} else if (S_ISREG(file->st_mode)) { } else if (S_ISREG(file->st_mode)) {
dir_content[i].file_type = COLOR_REGULAR; dir_content[i].file_type = FILE_TYPE_REGULAR;
dir_content[i].color_pair = COLOR_REGULAR; dir_content[i].color_pair = COLOR_REGULAR;
unsigned long j = 0; unsigned long j = 0;
char *extension = strrchr(entry[i]->d_name, '.'); char *extension = strrchr(entry[i]->d_name, '.');

View File

@ -12,6 +12,7 @@ extern file *rgt_content;
extern unsigned long rgt_file_count; extern unsigned long rgt_file_count;
extern unsigned int settings; extern unsigned int settings;
extern unsigned int status;
void parse_colors(char *line, short *fg, short *bg){ void parse_colors(char *line, short *fg, short *bg){
int tmp; int tmp;
@ -38,6 +39,12 @@ void colors_init() {
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */ init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
if (status & STATUS_USER_ROOT) {
init_pair(10, COLOR_RED, COLOR_BLACK); /* path */
} else {
init_pair(10, COLOR_GREEN, COLOR_BLACK); /* path */
}
FILE *dircolors = fopen("/etc/DIR_COLORS", "r"); FILE *dircolors = fopen("/etc/DIR_COLORS", "r");
@ -50,48 +57,50 @@ void colors_init() {
short fg; short fg;
short bg; short bg;
char supported_filetype_count = 8; char supported_filetype_count = 9;
char initial_pass = 0; char initial_pass = 0;
while ((tmp = getline(&line, &size, dircolors)) != -1 && initial_pass != supported_filetype_count) { while ((tmp = getline(&line, &size, dircolors)) != -1 && initial_pass != supported_filetype_count) {
fg = 7; fg = 7;
bg = 0; bg = 0;
token = strtok(line, " "); token = strtok(line, " ");
if (!strcmp(token, "DIR")) { if (token[0] != '#' && token[0] != '\n') {
initial_pass++; if (!strcmp(token, "DIR")) {
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(1, fg, bg); /* directory */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "RESET")){ init_pair(1, fg, bg); /* directory */
initial_pass++; } else if (!strcmp(token, "EXEC")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(2, fg, bg); /* regular file */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "LINK")){ init_pair(2, fg, bg); /* exec */
initial_pass++; } else if (!strcmp(token, "RESET")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(3, fg, bg); /* symlink */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "SOCK")){ init_pair(3, fg, bg); /* regular file */
initial_pass++; } else if (!strcmp(token, "LINK")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(7, fg, bg); /* socket */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "FIFO")){ init_pair(4, fg, bg); /* symlink */
initial_pass++; } else if (!strcmp(token, "BLK")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(6, fg, bg); /* fifo */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "BLK")){ init_pair(5, fg, bg); /* block device */
initial_pass++; } else if (!strcmp(token, "CHR")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(4, fg, bg); /* block device */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "CHR")){ init_pair(6, fg, bg); /* character device */
initial_pass++; } else if (!strcmp(token, "SOCK")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(5, fg, bg); /* character device */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "ORPHAN")){ init_pair(7, fg, bg); /* socket */
initial_pass++; } else if (!strcmp(token, "FIFO")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(8, fg, bg); /* orphan */ parse_colors(line, &fg, &bg);
} else if (!strcmp(token, "EXEC")){ init_pair(8, fg, bg); /* fifo */
initial_pass++; } else if (!strcmp(token, "ORPHAN")){
parse_colors(line, &fg, &bg); initial_pass++;
init_pair(9, fg, bg); /* exec */ parse_colors(line, &fg, &bg);
init_pair(9, fg, bg); /* orphan */
}
} }
} }
@ -114,9 +123,9 @@ void colors_init() {
colors[i].file_extension = malloc(sizeof(extension)); colors[i].file_extension = malloc(sizeof(extension));
strcpy(colors[i].file_extension, extension); strcpy(colors[i].file_extension, extension);
colors[i].color_pair = i+9; colors[i].color_pair = i+11;
parse_colors(line, &fg, &bg); parse_colors(line, &fg, &bg);
init_pair(i+10, fg, bg); init_pair(i+11, fg, bg);
i++; i++;
} }

View File

@ -3,6 +3,7 @@
#define STATUS_UPDATE_SCREEN_MASK 12 /* 1100*/ #define STATUS_UPDATE_SCREEN_MASK 12 /* 1100*/
#define STATUS_UPDATE_SCREEN_0 4 #define STATUS_UPDATE_SCREEN_0 4
#define STATUS_UPDATE_SCREEN_RESIZE 8 #define STATUS_UPDATE_SCREEN_RESIZE 8
#define STATUS_USER_ROOT 16
#define SETTINGS_HAS_COLOR 1 #define SETTINGS_HAS_COLOR 1
@ -20,15 +21,28 @@
#define FILE_STATUS_SELECTED 2; #define FILE_STATUS_SELECTED 2;
#define FILE_STATUS_IS_REGULAR_FILE 4 #define FILE_STATUS_IS_REGULAR_FILE 4
#define COLOR_UNKNOWN 0
#define COLOR_DIR 1 #define COLOR_DIR 1
#define COLOR_EXEC 2 #define COLOR_EXEC 2 /* not really a filetype, moreso if it is executable */
#define COLOR_REGULAR 3 #define COLOR_REGULAR 3
#define COLOR_SYMLINK 4 #define COLOR_SYMLINK 4
#define COLOR_BLOCK 5 #define COLOR_BLOCK 5
#define COLOR_CHARDEV 6 #define COLOR_CHARDEV 6
#define COLOR_FIFO 0 #define COLOR_SOCK 7
#define COLOR_SOCK 8 #define COLOR_FIFO 8
#define COLOR_PATH 9 #define COLOR_ORPHAN 9
#define COLOR_PATH 10
#define FILE_TYPE_UNKNOWN COLOR_UNKNOWN
#define FILE_TYPE_DIR COLOR_DIR
#define FILE_TYPE_EXEC COLOR_EXEC
#define FILE_TYPE_REGULAR COLOR_REGULAR
#define FILE_TYPE_SYMLINK COLOR_SYMLINK
#define FILE_TYPE_BLOCK COLOR_BLOCK
#define FILE_TYPE_CHARDEV COLOR_CHARDEV
#define FILE_TYPE_SOCK COLOR_SOCK
#define FILE_TYPE_FIFO COLOR_FIFO
#define FILE_TYPE_ORPHAN COLOR_ORPHAN
#ifndef GUARD #ifndef GUARD
#define GUARD #define GUARD

4
main.c
View File

@ -2,6 +2,7 @@
#include <curses.h> #include <curses.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include "threading.h" #include "threading.h"
#include "window.h" #include "window.h"
@ -146,6 +147,9 @@ void init() {
/*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/ /*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
if (getuid() == 0) {
status += STATUS_USER_ROOT;
}
threading_init(); /* found in threading.c */ threading_init(); /* found in threading.c */
colors_init(); colors_init();

View File

@ -32,9 +32,9 @@ void window_top(WINDOW *win){
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} else { } else {
for (i = 0; i < top_width; i++) { wattron(win, COLOR_PAIR(COLOR_PATH));
mvwprintw(win, 0, i, "%c", top_content[i]); mvwprintw(win, i, 0, "%s", top_content);
} wattroff(win, COLOR_PAIR(COLOR_PATH));
pthread_mutex_unlock(&mutex_top); pthread_mutex_unlock(&mutex_top);
} }
} }