base layout for multithreading

This commit is contained in:
nova 2025-02-26 22:19:32 +01:00
parent 13190d7ef8
commit 8cf1918b0d
6 changed files with 160 additions and 99 deletions

102
backend.c Normal file
View File

@ -0,0 +1,102 @@
#include <curses.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
extern unsigned long longest_name;
extern unsigned long file_count;
extern char **content_l;
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){
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, char **dir_content, 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) {
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, 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 *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);
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);
} else {
get_dir_size(path, &file_count, &longest_name, 0);
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);
}
} else {
get_dir_size(path, &file_count, &longest_name, 0);
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);
}
sleep(0);
return NULL;
}

8
backend.h Normal file
View File

@ -0,0 +1,8 @@
#include <curses.h>
#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 print_dir(WINDOW *win, char **dir_content);
void *populate_dir(void *dir);

42
main.c
View File

@ -1,29 +1,51 @@
#include <curses.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include "window.h"
unsigned int terminal_height;
unsigned int terminal_width;
unsigned long file_count;
unsigned long longest_name;
char **content_l; //content of parent dir, used in left window
char **content_m; //content of current dir, used in main window
char **content_r; //content of child dir, used in right window
char *path = ".";
int main() {
initscr(); //start ncurses
unsigned int ch;
getmaxyx(stdscr, terminal_height, terminal_width);
WINDOW *win_m = newwin(terminal_height, terminal_width/3, 0, 0);
WINDOW *win_l = newwin(terminal_height, terminal_width/3, terminal_height, (terminal_width/3));
WINDOW *win_r = newwin(terminal_height, terminal_width/3, terminal_height, ((terminal_width/3)*2));
while((ch = wgetch(win_m)) != 'q'){
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));
while((ch = wgetch(winm)) != '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));
pthread_t populate_l;
pthread_t populate_m;
pthread_t populate_r;
pthread_create(&populate_l, NULL, populate_dir, (void*)0); //parent_content slash win_l
pthread_create(&populate_m, NULL, populate_dir, (void*)1); //current_content slash win_m
pthread_create(&populate_r, NULL, populate_dir, (void*)2); //child_content slash win_r
pthread_join(populate_l, NULL);
pthread_join(populate_m, NULL);
pthread_join(populate_r, NULL);
window_left(winl, 0, 0, content_l);
window_main(winm, 0, terminal_width/3, content_m);
window_right(winr, 0, (terminal_width/3)*2, content_r);
wmove(stdscr,0,0);
free(content_l);
free(content_m);
free(content_r);
}

BIN
th

Binary file not shown.

View File

@ -1,78 +1,19 @@
#include "curses.h"
#include "string.h"
#include <stdlib.h>
#include <curses.h>
#include <dirent.h>
#include "backend.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);
}
}
extern unsigned long longest_name;
extern unsigned long file_count;
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x){
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
//WINDOW *win = (window_data)window_data.win;
unsigned int local_width;
unsigned int local_height;
unsigned long file_count = 0;
@ -86,24 +27,15 @@ void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x){
//}}}
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);
print_dir(win, dir_content);
free(*dir_content);
box(win,0,0);
wrefresh(win);
}
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x){
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
DIR *dir = opendir(".");
unsigned int local_width;
@ -118,13 +50,15 @@ void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x){
wclear(win);
//}}}
wmove(win, 0, 0);
wmove(win, 1, 1);
print_dir(win, dir_content);
box(win,0,0);
wrefresh(win);
}
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x){
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
wmove(win, 0, 0);
unsigned int local_width;

View File

@ -1,12 +1,7 @@
#include "curses.h"
#include <curses.h>
#include "window.c"
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);
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
typedef struct window_data {
WINDOW *win;
unsigned int start_y;
unsigned int start_x;
} window_data;