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 <curses.h>
#include <dirent.h> #include <dirent.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,6 +12,7 @@
extern unsigned int settings; extern unsigned int settings;
extern unsigned int file_modifiers; extern unsigned int file_modifiers;
extern unsigned int color_count; extern unsigned int color_count;
extern color *colors;
unsigned long get_dir_size(char *path){ 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)) { if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
} else { } else {
dir_content[i].file_name_width = strlen(entry[i]->d_name); 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_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; unsigned long i = 0;
for (i = 0; i < *dir_file_count; i++) { for (i = 0; i < *dir_file_count; i++) {
unsigned long j = 0;
wattron(win, COLOR_PAIR(dir_content[i].file_type)); wattron(win, COLOR_PAIR(dir_content[i].file_type));
mvwprintw(win, i, 0, "%d", 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)); wattroff(win, COLOR_PAIR(dir_content[i].file_type));
} }
} }

View File

@ -16,6 +16,15 @@
#define FILE_MODIFIERS_SORT_REVERSE 64 #define FILE_MODIFIERS_SORT_REVERSE 64
/*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/ /*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/
#define COLOR_REGULAR 0
#define COLOR_DIR 1
#define COLOR_BLOCK 2
#define COLOR_CHARDEV 3
#define COLOR_SYMLINK 4
#define COLOR_FIFO 5
#define COLOR_SOCK 6
#define COLOR_PATH
#ifndef GUARD #ifndef GUARD
#define GUARD #define GUARD
@ -26,5 +35,9 @@ typedef struct File {
unsigned long file_name_width; unsigned long file_name_width;
unsigned long file_size_bytes; unsigned long file_size_bytes;
} file; } file;
typedef struct Color {
char *file_extension;
short color_pair;
} color;
#endif #endif

6
main.c
View File

@ -7,6 +7,7 @@
#include "window.h" #include "window.h"
#include "interactions.h" #include "interactions.h"
#include "defines.h" #include "defines.h"
#include "colors.h"
unsigned int terminal_height; unsigned int terminal_height;
unsigned int terminal_width; unsigned int terminal_width;
@ -75,6 +76,7 @@ int main() {
render_pass(win_t, win_b, win_l, win_m, win_r); render_pass(win_t, win_b, win_l, win_m, win_r);
} }
/*
threading_free(); threading_free();
pthread_join(thread_l, NULL); pthread_join(thread_l, NULL);
@ -82,6 +84,7 @@ int main() {
pthread_join(thread_m, NULL); pthread_join(thread_m, NULL);
pthread_join(thread_t, NULL); pthread_join(thread_t, NULL);
pthread_join(thread_b, NULL); pthread_join(thread_b, NULL);
*/
curs_set(1); curs_set(1);
endwin(); endwin();
@ -93,7 +96,6 @@ int main() {
void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){ void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){
if ((status & STATUS_UPDATE_SCREEN_MASK) & STATUS_UPDATE_SCREEN_RESIZE) { if ((status & STATUS_UPDATE_SCREEN_MASK) & STATUS_UPDATE_SCREEN_RESIZE) {
status |= STATUS_LOCK_MASK;
/*TODO: check if deallocation of window and reallocation is faster than this or not */ /*TODO: check if deallocation of window and reallocation is faster than this or not */
@ -116,7 +118,6 @@ void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WIN
mvwin(win_r, 1, ((terminal_width/3)*2)); mvwin(win_r, 1, ((terminal_width/3)*2));
status &= ~STATUS_LOCK_MASK;
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
@ -147,6 +148,7 @@ void init() {
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
threading_init(); /* found in threading.c */ threading_init(); /* found in threading.c */
colors_init();
} }