th/window.c
2025-02-26 14:02:29 +01:00

146 lines
3.6 KiB
C

#include "curses.h"
#include "string.h"
#include <stdlib.h>
#include <dirent.h>
extern unsigned int terminal_height;
extern unsigned int terminal_width;
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name, char show_hidden) {
DIR *dir = opendir(path);
if (dir) {
unsigned long index = 0;
struct dirent *entry;
while ( entry=readdir(dir) ) {
if (entry->d_name[0] != '.' && !show_hidden) {
index++;
if ((unsigned long)sizeof(entry->d_name) > *longest_name) {
*longest_name = sizeof(entry->d_name);
}
} else if (show_hidden){
index++;
if ((unsigned long*)sizeof(entry->d_name) > (unsigned long*)longest_name) {
longest_name = (unsigned long*)sizeof(entry->d_name);
}
}
}
*file_count = index;
}
closedir(dir);
}
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, char *dir_content[file_count], char show_hidden){
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) {
// for (unsigned long i = 0; i < sizeof(entry->d_name)/sizeof(char); i++) {
// if ((entry->d_name[i]) != '\0'){
// dir_content[index][i] = entry->d_name[i];
// } else {
// break;
// }
// }
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;
}
}
index++;
}
}
}
closedir(dir);
}
void print_dir(WINDOW *win, unsigned long file_count, unsigned long longest_name, 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);
wmove(win, i, 1);
}
}
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x){
unsigned int local_width;
unsigned int local_height;
unsigned long file_count = 0;
unsigned long longest_name = 0;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
getmaxyx(win, local_height, local_width);
mvwin(win, start_y, start_x);
wclear(win);
//}}}
wmove(win, 1, 1);
wprintw(win, "meow");
get_dir_size(".", &file_count, &longest_name, 0);
char **dir_content;
dir_content = calloc(file_count, sizeof(*dir_content));
for (unsigned long i = 0; i<file_count; i++) {
dir_content[i] = calloc(longest_name, sizeof(*dir_content[i]));
}
get_dir_content(".", file_count, longest_name, dir_content, 0);
print_dir(win, file_count, longest_name, dir_content);
free(*dir_content);
box(win,0,0);
wrefresh(win);
}
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x){
DIR *dir = opendir(".");
unsigned int local_width;
unsigned int local_height;
unsigned long file_count = 0;
unsigned long longest_name = 0;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
getmaxyx(win, local_height, local_width);
mvwin(win, start_y, start_x);
wclear(win);
//}}}
wmove(win, 0, 0);
box(win,0,0);
wrefresh(win);
}
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x){
wmove(win, 0, 0);
unsigned int local_width;
unsigned int local_height;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
getmaxyx(win, local_height, local_width);
mvwin(win, start_y, start_x);
wclear(win);
//}}}
wmove(win, local_height/2, local_width/2);
wprintw(win, "%d,%d", local_height, local_width);
box(win,0,0);
wrefresh(win);
}