once again switched to complex data types, basic file coloring implemented

This commit is contained in:
nova 2025-05-08 16:03:16 +02:00
parent 08913786de
commit 37d5531aa7
5 changed files with 100 additions and 103 deletions

View File

@ -1,75 +1,69 @@
#define _GNU_SOURCE
#include <curses.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include "defines.h"
#include "sorting.h"
extern unsigned int settings;
extern unsigned int file_modifiers;
extern unsigned int color_count;
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width){
unsigned long get_dir_size(char *path){
DIR *dir = opendir(path);
if (dir) {
unsigned long entry_count = 0;
unsigned long max_length;
if (dir) {
struct dirent *entry;
while ((entry=readdir(dir))) {
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
unsigned int current_length = 0;
unsigned int i = 0;
for (; entry->d_name[i] != '\0'; i++) {
current_length++;
}
if (current_length > max_length) {
/*dynamic filename length to save on memory*/
max_length = current_length;
}
entry_count++;
} else {
}
}
dir_length_width[0] = entry_count;
dir_length_width[1] = max_length;
}
closedir(dir);
return entry_count;
}
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content){
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
struct dirent **entry;
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
dir_length_width[0] = scandir(path, &entry, NULL, alphasort);
scandir(path, &entry, NULL, alphasort);
} else {
dir_length_width[0] = scandir(path, &entry, skip_hidden_files, alphasort);
scandir(path, &entry, skip_hidden_files, alphasort);
}
unsigned long i = 0;
for (i = 0; i < dir_length_width[0]; i++ ) {
for (i = 0; i < *dir_file_count; i++ ) {
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
} else {
unsigned long j = 0;
for (; entry[i]->d_name[j] != '\0'; j++) {
dir_content[i * dir_length_width[1] + j] = entry[i]->d_name[j];
}
dir_width[i] = j;
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 = entry[i]->d_name;
dir_content[i].file_type = entry[i]->d_type;
}
}
for (i = 0; i < dir_length_width[0]; i++) {
for (i = 0; i < *dir_file_count; i++) {
free(entry[i]);
}
free(entry);
}
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *file_width, char *dir_content){
void print_dir(WINDOW *win, unsigned long *dir_file_count, file *dir_content){
unsigned long i = 0;
for (i = 0; i<dir_length_width[0]; i++) {
for (i = 0; i < *dir_file_count; i++) {
unsigned long j = 0;
for (j = 0; j < file_width[i]; j++){
mvwprintw(win, i, j, "%c", dir_content[i * dir_length_width[1] + j]);
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);
wattroff(win, COLOR_PAIR(dir_content[i].file_type));
}
}
}

View File

@ -2,6 +2,6 @@
#include "backend.c"
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width);
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content);
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *width, char *dir_content);
unsigned long get_dir_size(char *path);
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
void print_dir(WINDOW *win, unsigned long *dir_file_count, file *dir_content);

View File

@ -4,6 +4,8 @@
#define STATUS_UPDATE_SCREEN_0 4
#define STATUS_UPDATE_SCREEN_RESIZE 8
#define SETTINGS_HAS_COLOR 1
#define FILE_MODIFIERS_HIDDEN_FILES 1
#define FILE_MODIFIERS_SORT_BITMASK 126 /* 00000000000000000000000001111110*/
#define FILE_MODIFIERS_SORT_ALPHABETIC 2
@ -13,3 +15,16 @@
#define FILE_MODIFIERS_SORT_RANDOM 32
#define FILE_MODIFIERS_SORT_REVERSE 64
/*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/
#ifndef GUARD
#define GUARD
/* complex types are good actually */
typedef struct File {
char *file_name;
unsigned char file_type;
unsigned long file_name_width;
unsigned long file_size_bytes;
} file;
#endif

View File

@ -15,19 +15,16 @@ pthread_mutex_t mutex_rgt;
/* contains entire directory as 2d array
* may be changed in future to only include parts of the dir (currently includes entire dir) */
char *mid_content;
char *lft_content;
file *rgt_content;
file *mid_content;
file *lft_content;
char *top_content; /* current path */
/* index 0 = file_count
* index 1 = longest_name */
unsigned long *mid_length_width;
unsigned long *lft_length_width;
unsigned long *top_length_width;
/* this array exists so that a file with a 3 char long name wont spend the same amount of time printing as a file with 200 chars
* should both exist in the same directory
* currently unused */
unsigned long *mid_file_name_width;
unsigned long *lft_file_name_width;
unsigned long rgt_file_count;
unsigned long mid_file_count;
unsigned long lft_file_count;
unsigned long top_width;
extern unsigned int status;
@ -36,24 +33,19 @@ void *thread_mid(void *data){
pthread_mutex_lock(&mutex_mid);
free(mid_content);
free(mid_file_name_width);
free(mid_length_width);
mid_length_width = malloc(sizeof(char)*2);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof("cannot open directory"));
mid_length_width[0] = 1;
mid_length_width[1] = sizeof("cannot open directory");
mid_content = "cannot open directory";
mid_content = malloc(sizeof(file));
mid_content[1].file_name_width = sizeof("cannot open directory");
mid_content[1].file_name = "cannot open directory";
mid_file_count = 1;
} else {
get_dir_size(path, mid_length_width, mid_file_name_width);
mid_file_name_width = malloc(mid_length_width[0] * sizeof(unsigned long));
mid_content = malloc(mid_length_width[0] * mid_length_width[1] * sizeof(char));
memset(mid_content, ' ', mid_length_width[0] * mid_length_width[1] * sizeof(char));
get_dir_content(path, mid_length_width, mid_file_name_width, mid_content);
mid_file_count = (unsigned long)get_dir_size(path);
mid_content = malloc(mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
}
free(path);
@ -64,19 +56,15 @@ void *thread_lft(void *data){
pthread_mutex_lock(&mutex_lft);
free(lft_content);
free(lft_file_name_width);
free(lft_length_width);
lft_length_width = malloc(sizeof(char)*2);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
lft_content = malloc(sizeof("cannot open directory"));
lft_length_width[0] = 1;
lft_length_width[1] = sizeof("cannot open directory");
lft_content = "cannot open directory";
lft_content = malloc(sizeof(file));
lft_content[1].file_name_width = sizeof("cannot open directory");
lft_content[1].file_name = "cannot open directory";
lft_file_count = 1;
} else {
char *parent ;
if((parent = malloc(strlen(path)+strlen("/..")+1)) != NULL){
parent[0] = '\0'; /* ensures empty string */
@ -84,16 +72,17 @@ void *thread_lft(void *data){
strcat(parent, "/..");
}
get_dir_size(parent, lft_length_width, lft_file_name_width);
lft_file_name_width = malloc(lft_length_width[0] * sizeof(unsigned long));
lft_content = malloc(lft_length_width[0] * lft_length_width[1] * sizeof(char));
memset(lft_content, ' ', lft_length_width[0] * lft_length_width[1] * sizeof(char));
get_dir_content(parent, lft_length_width, lft_file_name_width, lft_content);
lft_file_count = (unsigned long)get_dir_size(parent);
lft_content = malloc(lft_file_count * sizeof(file));
get_dir_content(parent, &lft_file_count, lft_content);
free(parent);
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(NULL);
}
void *thread_rgt(void *data){
@ -103,19 +92,15 @@ void *thread_rgt(void *data){
void *thread_top(void *data){
pthread_mutex_lock(&mutex_top);
free(top_content);
free(top_length_width);
top_length_width = malloc(sizeof(char)*2);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
top_content = malloc(sizeof("cannot open directory"));
top_length_width[0] = 1;
top_length_width[1] = sizeof("cannot open directory");
top_width = sizeof("cannot open directory");
top_content = "cannot open directory";
} else {
top_content = getcwd(NULL, 0);
top_length_width[0] = 1;
top_length_width[1] = strlen(top_content);
top_width = strlen(top_content);
}
free(path);
@ -128,24 +113,23 @@ void *thread_btm(void *data){
}
void threading_init(){
mid_content = malloc(sizeof(char));
mid_file_name_width = malloc(sizeof(char));
mid_length_width = malloc(sizeof(char));
lft_content = malloc(sizeof(char));
lft_file_name_width = malloc(sizeof(char));
lft_length_width = malloc(sizeof(char));
top_length_width = malloc(sizeof(char));
rgt_content = malloc(sizeof(char));
mid_content = malloc(sizeof(file));
lft_content = malloc(sizeof(file));
top_content = malloc(sizeof(char));
pthread_mutex_init(&mutex_top, NULL);
pthread_mutex_init(&mutex_mid, NULL);
pthread_mutex_init(&mutex_lft, NULL);
}
void threading_free(){
free(rgt_content);
free(mid_content);
free(mid_file_name_width);
free(mid_length_width);
free(lft_content);
free(top_content);
pthread_mutex_destroy(&mutex_top);
pthread_mutex_destroy(&mutex_mid);

View File

@ -4,16 +4,15 @@
extern unsigned int status;
extern char *mid_content;
extern unsigned long *mid_width;
extern unsigned long *mid_length_width;
extern unsigned long *mid_file_name_width;
extern file *mid_content;
extern file *lft_content;
extern file *rgt_content;
extern char *top_content;
extern unsigned long *top_length_width;
extern char *lft_content;
extern unsigned long *lft_width;
extern unsigned long *lft_length_width;
extern unsigned long *lft_file_name_width;
extern unsigned long lft_file_count;
extern unsigned long mid_file_count;
extern unsigned long rgt_file_count;
extern unsigned long top_width;
extern pthread_mutex_t mutex_top;
extern pthread_mutex_t mutex_btm;
@ -30,7 +29,7 @@ void window_top(WINDOW *win){
status |= STATUS_UPDATE_SCREEN_0;
} else {
for (i = 0; i<top_length_width[1]; i++) {
for (i = 0; i < top_width; i++) {
mvwprintw(win, 0, i, "%c", top_content[i]);
}
pthread_mutex_unlock(&mutex_top);
@ -55,8 +54,7 @@ void window_lft(WINDOW *win){
status |= STATUS_UPDATE_SCREEN_0;
} else {
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", lft_length_width[0], lft_length_width[1]);
print_dir(win, lft_length_width, lft_file_name_width, lft_content);
print_dir(win, &lft_file_count, lft_content);
pthread_mutex_unlock(&mutex_lft);
}
}
@ -72,8 +70,7 @@ void window_mid(WINDOW *win){
status |= STATUS_UPDATE_SCREEN_0;
} else {
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", mid_length_width[0], mid_length_width[1]);
print_dir(win, mid_length_width, mid_file_name_width, mid_content);
print_dir(win, &mid_file_count, mid_content);
pthread_mutex_unlock(&mutex_mid);
}
}
@ -81,8 +78,15 @@ void window_rgt(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_rgt)) {
} else {
/*
pthread_mutex_unlock(&mutex_rgt);
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", rgt_length_width[0], rgt_length_width[1]);
print_dir(win, rgt_length_width, rgt_file_name_width, rgt_content);
*/
}
}