implementation of file type & extension based coloring

This commit is contained in:
nova
2025-05-08 21:15:14 +02:00
parent 37d5531aa7
commit 6f7415a913
3 changed files with 55 additions and 7 deletions

View File

@ -1,4 +1,4 @@
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include <curses.h>
#include <dirent.h>
#include <stdlib.h>
@ -12,6 +12,7 @@
extern unsigned int settings;
extern unsigned int file_modifiers;
extern unsigned int color_count;
extern color *colors;
unsigned long get_dir_size(char *path){
@ -43,9 +44,42 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
} else {
dir_content[i].file_name_width = strlen(entry[i]->d_name);
dir_content[i].file_name = malloc(dir_content[i].file_name_width * sizeof(char));
dir_content[i].file_name = malloc(sizeof(entry[i]->d_name));
dir_content[i].file_name = entry[i]->d_name;
dir_content[i].file_type = entry[i]->d_type;
struct stat *file;
file = malloc(sizeof(struct stat));
memset(file, ' ', sizeof(struct stat));
lstat(dir_content[i].file_name, file);
if (S_ISDIR(file->st_mode)) {
dir_content[i].file_type = COLOR_DIR;
} else if (S_ISBLK(file->st_mode)) {
dir_content[i].file_type = COLOR_BLOCK;
} else if (S_ISCHR(file->st_mode)) {
dir_content[i].file_type = COLOR_CHARDEV;
} else if (S_ISLNK(file->st_mode)) {
dir_content[i].file_type = COLOR_SYMLINK;
} else if (S_ISFIFO(file->st_mode)) {
dir_content[i].file_type = COLOR_FIFO;
} else if (S_ISSOCK(file->st_mode)) {
dir_content[i].file_type = COLOR_SOCK;
} else if (S_ISREG(file->st_mode)) {
dir_content[i].file_type = COLOR_REGULAR;
unsigned long j = 0;
char *extension = strrchr(entry[i]->d_name, '.');
if (extension) {
for (j = 0; j < color_count; j++) {
if (!strcmp(colors[j].file_extension, extension)) {
dir_content[i].file_type = colors[j].color_pair;
}
}
} else {
}
} else {
dir_content[i].file_type = COLOR_REGULAR;
}
free(file);
}
}
@ -60,10 +94,9 @@ void print_dir(WINDOW *win, unsigned long *dir_file_count, file *dir_content){
unsigned long i = 0;
for (i = 0; i < *dir_file_count; i++) {
unsigned long j = 0;
wattron(win, COLOR_PAIR(dir_content[i].file_type));
mvwprintw(win, i, 0, "%d", dir_content[i].file_type);
mvwaddstr(win, i, 3,dir_content[i].file_name);
mvwaddstr(win, i, 3,dir_content[i].file_name);
wattroff(win, COLOR_PAIR(dir_content[i].file_type));
}
}