mirror of
https://gittea.dev/nova/th.git
synced 2025-10-21 18:30:15 -04:00
101 lines
2.9 KiB
C
101 lines
2.9 KiB
C
#include <curses.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <dirent.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include "defines.h"
|
|
#include "sorting.h"
|
|
|
|
extern unsigned int settings;
|
|
extern unsigned int file_modifiers;
|
|
extern file_data *content_l;
|
|
extern file_data *content_m;
|
|
extern file_data *content_r;
|
|
extern char *path;
|
|
|
|
unsigned long file_count_l;
|
|
unsigned long file_count_m;
|
|
unsigned long file_count_r;
|
|
|
|
|
|
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
|
|
DIR *dir = opendir(path);
|
|
*longest_name = 256; //magic number originates out of readdir(), unless i implement my own name size function, thisll do
|
|
unsigned long index = 0;
|
|
if (dir) {
|
|
struct dirent *entry;
|
|
while ( (entry=readdir(dir)) ) {
|
|
if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
|
index++;
|
|
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES){
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
*file_count = index;
|
|
closedir(dir);
|
|
}
|
|
|
|
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, file_data *dir_content){
|
|
DIR *dir = opendir(path);
|
|
if (dir) {
|
|
unsigned long index = 0;
|
|
struct dirent *entry;
|
|
while ( (entry=readdir(dir)) ) {
|
|
if (entry->d_name[0] != '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
|
dir_content[index].file_name = entry->d_name;
|
|
dir_content[index].file_type = entry->d_type;
|
|
index++;
|
|
} else if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
|
dir_content[index].file_name = entry->d_name;
|
|
dir_content[index].file_type = entry->d_type;
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
closedir(dir);
|
|
}
|
|
|
|
void print_dir(WINDOW *win, unsigned long file_count, file_data *dir_content){
|
|
for (unsigned long i = 0; i < file_count; i++ ){ //skip index 0 as it is used for metadata like file count
|
|
if (dir_content[i].file_name) {
|
|
wprintw(win, "%s",dir_content[i].file_name);
|
|
wmove(win, i, 1);
|
|
} else {
|
|
wprintw(win, "NULL");
|
|
wmove(win, i, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
|
|
char wh = (char)which;
|
|
unsigned long longest_name = 0;
|
|
|
|
if (wh) {
|
|
if (wh == 1) {
|
|
free(content_m);
|
|
get_dir_size(path, &file_count_m, &longest_name);
|
|
content_m = (file_data*)calloc(file_count_m, sizeof(file_data));
|
|
get_dir_content(path, file_count_m, longest_name, content_m);
|
|
sort_dir(&file_count_m, &longest_name, content_m);
|
|
} else {
|
|
free(content_r);
|
|
get_dir_size(path, &file_count_r, &longest_name);
|
|
content_r = calloc(file_count_r, sizeof(file_data));
|
|
get_dir_content(path, file_count_r, longest_name, content_r);
|
|
sort_dir(&file_count_m, &longest_name, content_r);
|
|
}
|
|
} else {
|
|
free(content_l);
|
|
get_dir_size(path, &file_count_l, &longest_name);
|
|
content_l = calloc(file_count_l, sizeof(file_data));
|
|
get_dir_content(path, file_count_l, longest_name, content_l);
|
|
sort_dir(&file_count_m, &longest_name, content_l);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|