working file printing

This commit is contained in:
nova 2025-02-26 14:01:23 +01:00
parent bac8f5e564
commit 56bc1e4314
4 changed files with 99 additions and 30 deletions

7
main.c
View File

@ -1,4 +1,6 @@
#include <curses.h>
#include <pthread.h>
#include <unistd.h>
#include "window.h"
unsigned int terminal_height;
@ -14,9 +16,14 @@ int main() {
WINDOW *win_r = newwin(terminal_height, terminal_width/3, terminal_height, ((terminal_width/3)*2));
while((ch = wgetch(win_m)) != 'q'){
getmaxyx(stdscr, terminal_height, terminal_width);
pthread_t main_window_thread;
pthread_t left_window_thread;
pthread_t right_window_thread;
window_left(win_l,0,0);
window_main(win_m,0,(terminal_width/3));
window_right(win_r,0,((terminal_width/3)*2));
wmove(stdscr,0,0);
}

BIN
th Executable file

Binary file not shown.

116
window.c
View File

@ -1,14 +1,82 @@
#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;
DIR *path = opendir(".");
unsigned long file_count = 0;
unsigned long longest_name = 0;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
@ -17,30 +85,31 @@ void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x){
wclear(win);
//}}}
wmove(win, 0, 0);
if (path == NULL) {
wprintw(win, "Unable to read directory");
} else {
int index = 0;
struct dirent *entry;
while ( entry=readdir(path) ) {
if (entry->d_name[0] != '.') {
wprintw(win, "%s", entry->d_name);
wmove(win, index, 1);
index++;
}
}
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]));
}
closedir(path);
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;
DIR *path = opendir("..");
unsigned long file_count = 0;
unsigned long longest_name = 0;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
@ -50,20 +119,6 @@ void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x){
//}}}
wmove(win, 0, 0);
if (path == NULL) {
wprintw(win, "Unable to read directory");
} else {
int index = 0;
struct dirent *entry;
while ( entry=readdir(path) ) {
if (entry->d_name[0] != '.') {
wprintw(win, "%s", entry->d_name);
wmove(win, index, 1);
index++;
}
}
}
closedir(path);
box(win,0,0);
@ -71,6 +126,7 @@ void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x){
}
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;

View File

@ -4,3 +4,9 @@
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x);
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x);
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x);
typedef struct window_data {
WINDOW *win;
unsigned int start_y;
unsigned int start_x;
} window_data;