Add colored filenames based on file types

This commit is contained in:
Piotr Marendowski 2024-03-14 15:58:03 +01:00
parent 8199ec3ecb
commit fa1eb500ac
4 changed files with 78 additions and 36 deletions

View file

@ -36,5 +36,3 @@ uninstall:
clean: clean:
$(RM) $(TARGET) $(RM) $(TARGET)
ccc.o: $(CONF)

69
ccc.c
View file

@ -71,8 +71,15 @@ int main(int argc, char** argv)
start_color(); start_color();
} }
init_pair(1, COLOR_BLUE, -1); /* foreground, background */ /* colors */
init_pair(2, COLOR_CYAN, -1); /* active color */ init_pair(1, COLOR_BLACK, -1); /* */
init_pair(2, COLOR_RED, -1); /* */
init_pair(3, COLOR_GREEN, -1); /* LNK */
init_pair(4, COLOR_YELLOW, -1); /* BLK */
init_pair(5, COLOR_BLUE, -1); /* DIR */
init_pair(6, COLOR_MAGENTA, -1); /* */
init_pair(7, COLOR_CYAN, -1); /* */
init_pair(8, COLOR_WHITE, -1); /* REG */
half_width = COLS / 2; half_width = COLS / 2;
init_windows(); init_windows();
@ -220,7 +227,7 @@ int main(int argc, char** argv)
} }
/* /*
* Read the provided directory and list all files to window 0 * Read the provided directory and list all files to window 1
* ep->d_name -> filename * ep->d_name -> filename
*/ */
void list_files(const char *path) void list_files(const char *path)
@ -246,12 +253,13 @@ void list_files(const char *path)
filename[0] = '\0'; filename[0] = '\0';
strcat(filename, cwd); strcat(filename, cwd);
strcat(filename, "/"); strcat(filename, "/");
strcat(filename, ep->d_name); /* add file name */ strcat(filename, ep->d_name); /* add file name */
long index = add_file_stat(filename); long index = add_file_stat(filename);
char *line = get_line(index); char *line = get_line(index);
mvwprintw(directory_content, count, 0, "%s", line); mvwprintw(directory_content, count, 0, "%s", line);
free(line); free(line);
count++; count++;
} }
@ -289,7 +297,6 @@ long add_file_stat(char *filepath)
/* format last modified time to a string */ /* format last modified time to a string */
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
/* get file size */ /* get file size */
double bytes = file_stat.st_size; double bytes = file_stat.st_size;
if (S_ISDIR(file_stat.st_mode)) { if (S_ISDIR(file_stat.st_mode)) {
@ -298,7 +305,7 @@ long add_file_stat(char *filepath)
nftw(filepath, &get_directory_size, 15, FTW_PHYS); nftw(filepath, &get_directory_size, 15, FTW_PHYS);
bytes = total_dir_size; bytes = total_dir_size;
} }
/* max 25 chars due to long, space, suffix and nul */ /* max 25 chars due to long, space, suffix and null */
char *size = memalloc(25 * sizeof(char)); char *size = memalloc(25 * sizeof(char));
int unit = 0; int unit = 0;
const char* units[] = {" B", "KiB", "MiB", "GiB", "TiB", "PiB"}; const char* units[] = {" B", "KiB", "MiB", "GiB", "TiB", "PiB"};
@ -309,22 +316,32 @@ long add_file_stat(char *filepath)
/* 4 sig fig, limiting characters to have better format */ /* 4 sig fig, limiting characters to have better format */
sprintf(size, "%-6.4g %-3s", bytes, units[unit]); sprintf(size, "%-6.4g %-3s", bytes, units[unit]);
/* get file type */ /* get file type and color */
char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */ char *type = memalloc(4 * sizeof(char)); /* 4 chars for type */
int color;
if (S_ISDIR(file_stat.st_mode)) { if (S_ISDIR(file_stat.st_mode)) {
strcpy(type, "DIR"); /* directory */ strcpy(type, "DIR"); /* directory type */
color = 5; /* blue color */
} else if (S_ISREG(file_stat.st_mode)) { } else if (S_ISREG(file_stat.st_mode)) {
strcpy(type, "REG"); /* regular file */ strcpy(type, "REG"); /* regular file */
color = 8; /* white color */
} else if (S_ISLNK(file_stat.st_mode)) { } else if (S_ISLNK(file_stat.st_mode)) {
strcpy(type, "LNK"); /* symbolic link */ strcpy(type, "LNK"); /* symbolic link */
color = 3; /* green color */
} else if (S_ISCHR(file_stat.st_mode)) { } else if (S_ISCHR(file_stat.st_mode)) {
strcpy(type, "CHR"); /* character device */ strcpy(type, "CHR"); /* character device */
color = 8; /* white color */
} else if (S_ISSOCK(file_stat.st_mode)) { } else if (S_ISSOCK(file_stat.st_mode)) {
strcpy(type, "SOC"); /* socket */ strcpy(type, "SOC"); /* socket */
color = 8; /* white color */
} else if (S_ISBLK(file_stat.st_mode)) { } else if (S_ISBLK(file_stat.st_mode)) {
strcpy(type, "BLK"); /* block device */ strcpy(type, "BLK"); /* block device */
color = 4; /* yellow color */
} else if (S_ISFIFO(file_stat.st_mode)) { } else if (S_ISFIFO(file_stat.st_mode)) {
strcpy(type, "FIF"); /* FIFO */ strcpy(type, "FIF"); /* FIFO */
color = 8; /* white color */
} else {
color = 8; /* white color */
} }
/* don't know how to handle socket, block device, character device and FIFO */ /* don't know how to handle socket, block device, character device and FIFO */
@ -335,7 +352,7 @@ long add_file_stat(char *filepath)
free(time); free(time);
free(size); free(size);
long index = add_file(filepath, total_stat, type); long index = add_file(filepath, total_stat, type, color);
free(total_stat); free(total_stat);
free(type); free(type);
@ -367,7 +384,7 @@ void highlight_current_line()
for (long i = overflow; i < range; i++) { for (long i = overflow; i < range; i++) {
if ((overflow == 0 && i == current_selection) || (overflow != 0 && i == current_selection)) { if ((overflow == 0 && i == current_selection) || (overflow != 0 && i == current_selection)) {
wattron(directory_content, A_REVERSE); wattron(directory_content, A_REVERSE);
wattron(directory_content, COLOR_PAIR(1)); wattron(directory_content, COLOR_PAIR(5));
/* update the panel */ /* update the panel */
wclear(panel); wclear(panel);
@ -375,12 +392,18 @@ void highlight_current_line()
} }
/* print the actual filename and stats */ /* print the actual filename and stats */
char *line = get_line(i); char *line = get_line(i);
int color = get_color(i);
/* print the whole directory with colors */
wattron(directory_content, COLOR_PAIR(color));
if (overflow > 0) if (overflow > 0)
mvwprintw(directory_content, line_count, 0, "%s", line); mvwprintw(directory_content, line_count, 0, "%s", line);
else else
mvwprintw(directory_content, i, 0, "%s", line); mvwprintw(directory_content, i, 0, "%s", line);
wattroff(directory_content, A_REVERSE); wattroff(directory_content, A_REVERSE);
wattroff(directory_content, COLOR_PAIR(1)); wattroff(directory_content, COLOR_PAIR(color));
wattroff(directory_content, COLOR_PAIR(5));
free(line); free(line);
line_count++; line_count++;
} }
@ -482,9 +505,9 @@ void draw_border_title(WINDOW *window, bool active)
{ {
/* turn on color depends on active */ /* turn on color depends on active */
if (active) { if (active) {
wattron(window, COLOR_PAIR(2)); wattron(window, COLOR_PAIR(7));
} else { } else {
wattron(window, COLOR_PAIR(1)); wattron(window, COLOR_PAIR(5));
} }
/* draw top border */ /* draw top border */
mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */ mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */
@ -503,9 +526,9 @@ void draw_border_title(WINDOW *window, bool active)
/* turn color off after turning it on */ /* turn color off after turning it on */
if (active) { if (active) {
wattroff(window, COLOR_PAIR(2)); wattroff(window, COLOR_PAIR(7));
} else { } else {
wattroff(window, COLOR_PAIR(1)); wattroff(window, COLOR_PAIR(5));
} }
wrefresh(window); /* Refresh the window to see the colored border and title */ wrefresh(window); /* Refresh the window to see the colored border and title */
} }

38
file.c
View file

@ -9,7 +9,7 @@ typedef struct file {
char *path; char *path;
char *stats; char *stats;
char *type; char *type;
/* put some more useful stat here */ int color;
struct file *next; struct file *next;
} file; } file;
@ -65,20 +65,24 @@ void clear_marked()
} }
} }
long add_file(char *filepath, char *stats, char *type) long add_file(char *filepath, char *stats, char *type, int color)
{ {
file *current = files; file *current = files;
file *new_file = memalloc(sizeof(file)); file *new_file = memalloc(sizeof(file));
char *buf = strdup(filepath); char *buf = strdup(filepath);
char *buf2 = strdup(stats); char *buf2 = strdup(stats);
char *buf3 = strdup(type); char *buf3 = strdup(type);
if (buf == NULL || buf2 == NULL || buf3 == NULL) { int buf4 = color;
if (buf == NULL || buf2 == NULL || buf3 == NULL)
perror("ccc"); perror("ccc");
}
new_file->path = buf; new_file->path = buf;
new_file->stats = buf2; new_file->stats = buf2;
new_file->type = buf3; new_file->type = buf3;
new_file->color = buf4;
new_file->next = NULL; new_file->next = NULL;
if (current == NULL) { if (current == NULL) {
files = new_file; files = new_file;
return 0; return 0;
@ -121,6 +125,20 @@ char *get_filepath(long index)
} }
} }
int get_color(long index)
{
file *file = get_file(index);
if (file != NULL) {
int color = file->color;
if (!color) {
perror("ccc");
}
return color;
} else {
return 8; /* white */
}
}
/* /*
* Construct a formatted line for display * Construct a formatted line for display
*/ */
@ -129,14 +147,16 @@ char *get_line(long index)
file *file = get_file(index); file *file = get_file(index);
if (file != NULL) { if (file != NULL) {
char *name = strdup(file->path); char *name = strdup(file->path);
name = basename(name);
char *stats = strdup(file->stats); char *stats = strdup(file->stats);
if (name == NULL || stats == NULL) { size_t length = strlen(name) + strlen(stats) + 2; /* one for space and one for null */
perror("ccc");
}
size_t length = strlen(name) + strlen(stats) + 2; /* one for space and one for nul */
char *line = memalloc(length * sizeof(char)); char *line = memalloc(length * sizeof(char));
name = basename(name);
if (name == NULL || stats == NULL)
perror("ccc");
snprintf(line, length, "%s %s", stats, name); snprintf(line, length, "%s %s", stats, name);
return line; return line;
} else { } else {
return NULL; return NULL;

5
file.h
View file

@ -5,7 +5,7 @@ typedef struct file {
char *path; char *path;
char *stats; char *stats;
char *type; char *type;
// put some more useful stat here int color;
struct file *next; struct file *next;
} file; } file;
@ -13,9 +13,10 @@ long files_len();
long marked_len(); long marked_len();
void clear_files(); void clear_files();
void clear_marked(); void clear_marked();
long add_file(char *filename, char *time, char *type); long add_file(char *filepath, char *stats, char *type, int color);
file *get_file(long index); file *get_file(long index);
char *get_filepath(long index); char *get_filepath(long index);
int get_color(long index);
char *get_line(long index); char *get_line(long index);
#endif #endif