file preview infrastructure & implementation of preview of text files
This commit is contained in:
56
threading.c
56
threading.c
@ -4,8 +4,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <time.h>
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "backend.h"
|
#include "backend.h"
|
||||||
|
#include "file_previews.h"
|
||||||
|
|
||||||
|
|
||||||
pthread_mutex_t mutex_top;
|
pthread_mutex_t mutex_top;
|
||||||
@ -18,6 +20,7 @@ pthread_mutex_t mutex_selection;
|
|||||||
file *rgt_content;
|
file *rgt_content;
|
||||||
file *mid_content;
|
file *mid_content;
|
||||||
file *lft_content;
|
file *lft_content;
|
||||||
|
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
||||||
|
|
||||||
char *top_content; /* current path */
|
char *top_content; /* current path */
|
||||||
|
|
||||||
@ -26,12 +29,15 @@ unsigned long mid_file_count;
|
|||||||
unsigned long lft_file_count;
|
unsigned long lft_file_count;
|
||||||
unsigned long top_width;
|
unsigned long top_width;
|
||||||
|
|
||||||
|
extern file_preview file_previews[];
|
||||||
|
|
||||||
|
|
||||||
extern unsigned int status;
|
extern unsigned int status;
|
||||||
extern unsigned long selected_file_current;
|
extern unsigned long selected_file_current;
|
||||||
extern unsigned long selected_file_last;
|
extern unsigned long selected_file_last;
|
||||||
|
|
||||||
|
void *thread_rgt(void *data);
|
||||||
|
|
||||||
void *thread_mid(void *data){
|
void *thread_mid(void *data){
|
||||||
pthread_mutex_lock(&mutex_mid);
|
pthread_mutex_lock(&mutex_mid);
|
||||||
|
|
||||||
@ -39,8 +45,8 @@ void *thread_mid(void *data){
|
|||||||
char *path;
|
char *path;
|
||||||
if((path=getcwd(NULL, 0)) == NULL) {
|
if((path=getcwd(NULL, 0)) == NULL) {
|
||||||
mid_content = malloc(sizeof(file));
|
mid_content = malloc(sizeof(file));
|
||||||
mid_content[1].file_name_width = sizeof("cannot open directory");
|
mid_content[0].file_name_width = sizeof("cannot open directory");
|
||||||
mid_content[1].file_name = "cannot open directory";
|
mid_content[0].file_name = "cannot open directory";
|
||||||
mid_file_count = 1;
|
mid_file_count = 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
@ -78,8 +84,8 @@ void *thread_lft(void *data){
|
|||||||
char *path;
|
char *path;
|
||||||
if((path=getcwd(NULL, 0)) == NULL) {
|
if((path=getcwd(NULL, 0)) == NULL) {
|
||||||
lft_content = malloc(sizeof(file));
|
lft_content = malloc(sizeof(file));
|
||||||
lft_content[1].file_name_width = sizeof("cannot open directory");
|
lft_content[0].file_name_width = sizeof("cannot open directory");
|
||||||
lft_content[1].file_name = "cannot open directory";
|
lft_content[0].file_name = "cannot open directory";
|
||||||
lft_file_count = 1;
|
lft_file_count = 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
@ -105,32 +111,41 @@ void *thread_rgt(void *data){
|
|||||||
pthread_mutex_lock(&mutex_rgt);
|
pthread_mutex_lock(&mutex_rgt);
|
||||||
|
|
||||||
pthread_mutex_lock(&mutex_mid);
|
pthread_mutex_lock(&mutex_mid);
|
||||||
char *path = mid_content[selected_file_current].file_name;
|
/* TODO(2025-06-13T01:24:43) fix the occasional wrongly coppied path */
|
||||||
|
char *path = malloc(mid_content[selected_file_current].file_name_width);
|
||||||
|
strcpy(path, mid_content[selected_file_current].file_name);
|
||||||
|
unsigned long name_len = strlen(mid_content[selected_file_current].file_name);
|
||||||
|
unsigned long file_size = mid_content[selected_file_current].file_size_bytes;
|
||||||
|
unsigned char file_type = mid_content[selected_file_current].file_type;
|
||||||
|
|
||||||
pthread_mutex_unlock(&mutex_mid);
|
pthread_mutex_unlock(&mutex_mid);
|
||||||
|
|
||||||
if (mid_content[selected_file_current].file_type == FILE_TYPE_DIR || mid_content[selected_file_current].file_type == FILE_TYPE_SYMLINK) {
|
if (file_type == FILE_TYPE_DIR || file_type == FILE_TYPE_SYMLINK) {
|
||||||
free(rgt_content);
|
free(rgt_content);
|
||||||
rgt_file_count = (unsigned long)get_dir_size(path);
|
rgt_file_count = get_dir_size(path);
|
||||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||||
memset(rgt_content, ' ', rgt_file_count * sizeof(file));
|
memset(rgt_content, ' ', rgt_file_count * sizeof(file));
|
||||||
get_dir_content(path, &rgt_file_count, rgt_content);
|
get_dir_content(path, &rgt_file_count, rgt_content);
|
||||||
} else if (mid_content[selected_file_current].file_type == FILE_TYPE_REGULAR) {
|
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
||||||
FILE *fp = fopen(mid_content[selected_file_current].file_name, "r");
|
} else if (file_type == FILE_TYPE_REGULAR || file_type == FILE_TYPE_EXEC) {
|
||||||
unsigned long file_size = ftell(fp);
|
|
||||||
rewind(fp);
|
|
||||||
free(rgt_content);
|
free(rgt_content);
|
||||||
|
|
||||||
rgt_content = malloc(sizeof(file));
|
rgt_content = malloc(sizeof(file));
|
||||||
rgt_content[0].file_name = malloc(file_size);
|
rgt_content[0].file_name = malloc(name_len);
|
||||||
fgets(rgt_content[0].file_name, file_size, fp);
|
rgt_content[0].file_name_width = name_len;
|
||||||
rgt_content[0].file_size_bytes = file_size;
|
rgt_content[0].file_size_bytes = file_size;
|
||||||
rgt_content[0].file_type = FILE_TYPE_REGULAR;
|
/*memcpy(rgt_content[0].file_name, path, sizeof(path)/sizeof(char));*/
|
||||||
rgt_content[0].file_name_width = file_size;
|
strcpy(rgt_content[0].file_name, path);
|
||||||
rgt_content[0].color_pair = 0;
|
rgt_file_count = 1;
|
||||||
rgt_content[0].status = 0;
|
|
||||||
fclose(fp);
|
if (file_type != FILE_TYPE_EXEC) {
|
||||||
|
rgt_content[0].file_type = FILE_TYPE_OPEN_FILE;
|
||||||
|
rgt_content[0].status = FILE_STATUS_HOVER;
|
||||||
|
free(rgt_buffer);
|
||||||
|
rgt_buffer = preview_file(rgt_content[0].file_name, file_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(&mutex_rgt);
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
@ -159,11 +174,12 @@ void *thread_btm(void *data){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void threading_init(){
|
void threading_init(){
|
||||||
rgt_content = malloc(sizeof(char));
|
rgt_content = malloc(sizeof(file));
|
||||||
mid_content = malloc(sizeof(file));
|
mid_content = malloc(sizeof(file));
|
||||||
lft_content = malloc(sizeof(file));
|
lft_content = malloc(sizeof(file));
|
||||||
|
|
||||||
top_content = malloc(sizeof(char));
|
top_content = malloc(sizeof(char));
|
||||||
|
rgt_buffer = malloc(sizeof(char));
|
||||||
|
|
||||||
pthread_mutex_init(&mutex_top, NULL);
|
pthread_mutex_init(&mutex_top, NULL);
|
||||||
pthread_mutex_init(&mutex_mid, NULL);
|
pthread_mutex_init(&mutex_mid, NULL);
|
||||||
|
12
window.c
12
window.c
@ -1,5 +1,6 @@
|
|||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
extern unsigned int status;
|
extern unsigned int status;
|
||||||
@ -12,6 +13,7 @@ extern file *mid_content;
|
|||||||
extern file *lft_content;
|
extern file *lft_content;
|
||||||
extern file *rgt_content;
|
extern file *rgt_content;
|
||||||
extern char *top_content;
|
extern char *top_content;
|
||||||
|
extern char *rgt_buffer;
|
||||||
|
|
||||||
extern unsigned long lft_file_count;
|
extern unsigned long lft_file_count;
|
||||||
extern unsigned long mid_file_count;
|
extern unsigned long mid_file_count;
|
||||||
@ -31,6 +33,7 @@ void window_top(WINDOW *win){
|
|||||||
if (pthread_mutex_trylock(&mutex_top)) {
|
if (pthread_mutex_trylock(&mutex_top)) {
|
||||||
wprintw(win,"loading");
|
wprintw(win,"loading");
|
||||||
status |= STATUS_UPDATE_SCREEN_0;
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
wattron(win, COLOR_PAIR(COLOR_PATH));
|
wattron(win, COLOR_PAIR(COLOR_PATH));
|
||||||
@ -42,6 +45,7 @@ void window_top(WINDOW *win){
|
|||||||
void window_btm(WINDOW *win){
|
void window_btm(WINDOW *win){
|
||||||
werase(win);
|
werase(win);
|
||||||
if (pthread_mutex_trylock(&mutex_btm)) {
|
if (pthread_mutex_trylock(&mutex_btm)) {
|
||||||
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
} else {
|
} else {
|
||||||
pthread_mutex_unlock(&mutex_btm);
|
pthread_mutex_unlock(&mutex_btm);
|
||||||
}
|
}
|
||||||
@ -56,6 +60,7 @@ void window_lft(WINDOW *win){
|
|||||||
if (pthread_mutex_trylock(&mutex_lft)) {
|
if (pthread_mutex_trylock(&mutex_lft)) {
|
||||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||||
status |= STATUS_UPDATE_SCREEN_0;
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
print_dir(win, &lft_file_count, lft_content);
|
print_dir(win, &lft_file_count, lft_content);
|
||||||
@ -72,6 +77,7 @@ void window_mid(WINDOW *win){
|
|||||||
if (pthread_mutex_trylock(&mutex_mid)) {
|
if (pthread_mutex_trylock(&mutex_mid)) {
|
||||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||||
status |= STATUS_UPDATE_SCREEN_0;
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
print_dir(win, &mid_file_count, mid_content);
|
print_dir(win, &mid_file_count, mid_content);
|
||||||
@ -86,10 +92,16 @@ void window_rgt(WINDOW *win){
|
|||||||
unsigned long local_height;
|
unsigned long local_height;
|
||||||
getmaxyx(win, local_height, local_width);
|
getmaxyx(win, local_height, local_width);
|
||||||
if (pthread_mutex_trylock(&mutex_rgt)) {
|
if (pthread_mutex_trylock(&mutex_rgt)) {
|
||||||
|
/* TODO(2025-06-13T00:49:26) fix race condition and use trylock */
|
||||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||||
status |= STATUS_UPDATE_SCREEN_0;
|
status |= STATUS_UPDATE_SCREEN_0;
|
||||||
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
|
} else {
|
||||||
|
if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) {
|
||||||
|
mvwprintw(win, 0, 0, "%s", rgt_buffer);
|
||||||
} else {
|
} else {
|
||||||
print_dir(win, &rgt_file_count, rgt_content);
|
print_dir(win, &rgt_file_count, rgt_content);
|
||||||
|
}
|
||||||
pthread_mutex_unlock(&mutex_rgt);
|
pthread_mutex_unlock(&mutex_rgt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user