refractoring & hidden files functionality

This commit is contained in:
nova 2025-02-26 22:42:51 +01:00
parent 8cf1918b0d
commit c927039b68
5 changed files with 20 additions and 27 deletions

View File

@ -4,6 +4,7 @@
#include <stdlib.h>
#include <unistd.h>
extern unsigned int settings;
extern unsigned long longest_name;
extern unsigned long file_count;
extern char **content_l;
@ -11,18 +12,18 @@ extern char **content_m;
extern char **content_r;
extern char *path;
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name, char show_hidden){
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
DIR *dir = opendir(path);
if (dir) {
unsigned long index = 0;
struct dirent *entry;
while ( entry=readdir(dir) ) {
if (entry->d_name[0] != '.' && !show_hidden) {
if (entry->d_name[0] != '.' && !(settings & 1)) { //bit 0 = show_hidden_files
index++;
if ((unsigned long)sizeof(entry->d_name) > *longest_name) {
*longest_name = sizeof(entry->d_name);
}
} else if (show_hidden){
} else if (settings & 1){ //bit 0 = show_hidden_files
index++;
if ((unsigned long*)sizeof(entry->d_name) > (unsigned long*)longest_name) {
longest_name = (unsigned long*)sizeof(entry->d_name);
@ -34,24 +35,19 @@ void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_
closedir(dir);
}
void get_dir_content(char *path, char **dir_content, char show_hidden){
void get_dir_content(char *path, char **dir_content){
DIR *dir = opendir(path);
char content[file_count][longest_name];
memset(content,0,sizeof(content));
if (dir) {
int index = 0;
struct dirent *entry;
while ( entry=readdir(dir) ) {
if (entry->d_name[0] != '.' && !show_hidden) {
while ( entry=readdir(dir) ) {
if (entry->d_name[0] != '.' && !(settings & 1)) { //bit 0 = show_hidden_files
strcpy(dir_content[index], entry->d_name);
index++;
} else if (show_hidden){
for (unsigned long i = 0; i < sizeof(entry->d_name)/sizeof(char); i++) {
if ((entry->d_name[i]) != '\0'){
} else {
break;
}
}
} else if (settings & 1){ //bit 0 = show_hidden_files
strcpy(dir_content[index], entry->d_name);
index++;
}
}
@ -60,43 +56,39 @@ void get_dir_content(char *path, char **dir_content, char show_hidden){
}
void print_dir(WINDOW *win, char **dir_content){
char str[longest_name];
for (unsigned long i = 0; i < (unsigned long)file_count; i++ ){
strcpy(str, dir_content[i]);
wprintw(win, "%s",str);
wprintw(win, "%s",dir_content[i]);
wmove(win, i, 1);
}
}
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
char wh = (char)which;
if (wh) {
if (wh==1) {
get_dir_size(path, &file_count, &longest_name, 0);
get_dir_size(path, &file_count, &longest_name);
content_m = calloc(file_count, sizeof(*content_m));
for (unsigned long i = 0; i<file_count; i++) {
content_m[i] = calloc(longest_name, sizeof(content_m[i]));
}
get_dir_content(path, content_m, 0);
get_dir_content(path, content_m);
} else {
get_dir_size(path, &file_count, &longest_name, 0);
get_dir_size(path, &file_count, &longest_name);
content_r = calloc(file_count, sizeof(*content_r));
for (unsigned long i = 0; i<file_count; i++) {
content_r[i] = calloc(longest_name, sizeof(content_r[i]));
}
get_dir_content(path, content_r, 0);
get_dir_content(path, content_r);
}
} else {
get_dir_size(path, &file_count, &longest_name, 0);
get_dir_size(path, &file_count, &longest_name);
content_l = calloc(file_count, sizeof(*content_l));
for (unsigned long i = 0; i<file_count; i++) {
content_l[i] = calloc(longest_name, sizeof(content_l));
}
get_dir_content(path, content_l, 0);
get_dir_content(path, content_l);
}
sleep(0);
return NULL;
}

View File

@ -2,7 +2,7 @@
#include "backend.c"
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name, char show_hidden);
void get_dir_content(char *path, char **dir_content, char show_hidden);
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name);
void get_dir_content(char *path, char **dir_content);
void print_dir(WINDOW *win, char **dir_content);
void *populate_dir(void *dir);

2
main.c
View File

@ -6,6 +6,7 @@
unsigned int terminal_height;
unsigned int terminal_width;
unsigned int settings;
unsigned long file_count;
unsigned long longest_name;
char **content_l; //content of parent dir, used in left window
@ -22,6 +23,7 @@ int main() {
WINDOW *winl = newwin(terminal_height, terminal_width/3, terminal_height, (terminal_width/3));
WINDOW *winm = newwin(terminal_height, terminal_width/3, 0, 0);
WINDOW *winr = newwin(terminal_height, terminal_width/3, terminal_height, ((terminal_width/3)*2));
settings = 1; //bit 0 = show_hidden_files
while((ch = wgetch(winm)) != 'q'){

BIN
th

Binary file not shown.

View File

@ -37,7 +37,6 @@ void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char *
}
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
DIR *dir = opendir(".");
unsigned int local_width;
unsigned int local_height;
unsigned long file_count = 0;