Compare commits
4 Commits
38338df254
...
5a7a4baf9b
Author | SHA1 | Date | |
---|---|---|---|
|
5a7a4baf9b | ||
|
6f7415a913 | ||
|
37d5531aa7 | ||
|
08913786de |
91
backend.c
91
backend.c
@@ -1,75 +1,102 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <curses.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <pthread.h>
|
||||
#include "defines.h"
|
||||
#include "sorting.h"
|
||||
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern unsigned int color_count;
|
||||
extern color *colors;
|
||||
|
||||
|
||||
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width){
|
||||
unsigned long get_dir_size(char *path){
|
||||
DIR *dir = opendir(path);
|
||||
if (dir) {
|
||||
unsigned long entry_count = 0;
|
||||
unsigned long max_length;
|
||||
if (dir) {
|
||||
struct dirent *entry;
|
||||
while ((entry=readdir(dir))) {
|
||||
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
unsigned int current_length = 0;
|
||||
unsigned int i = 0;
|
||||
for (; entry->d_name[i] != '\0'; i++) {
|
||||
current_length++;
|
||||
}
|
||||
if (current_length > max_length) {
|
||||
/*dynamic filename length to save on memory*/
|
||||
max_length = current_length;
|
||||
}
|
||||
entry_count++;
|
||||
} else {
|
||||
}
|
||||
}
|
||||
dir_length_width[0] = entry_count;
|
||||
dir_length_width[1] = max_length;
|
||||
}
|
||||
closedir(dir);
|
||||
return entry_count;
|
||||
|
||||
}
|
||||
|
||||
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content){
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
|
||||
struct dirent **entry;
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
||||
dir_length_width[0] = scandir(path, &entry, NULL, alphasort);
|
||||
scandir(path, &entry, NULL, alphasort);
|
||||
} else {
|
||||
dir_length_width[0] = scandir(path, &entry, skip_hidden_files, alphasort);
|
||||
scandir(path, &entry, skip_hidden_files, alphasort);
|
||||
}
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < dir_length_width[0]; i++ ) {
|
||||
for (i = 0; i < *dir_file_count; i++ ) {
|
||||
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
} else {
|
||||
dir_content[i].file_name_width = strlen(entry[i]->d_name);
|
||||
dir_content[i].file_name = malloc(sizeof(entry[i]->d_name));
|
||||
dir_content[i].file_name = entry[i]->d_name;
|
||||
|
||||
struct stat *file;
|
||||
file = malloc(sizeof(struct stat));
|
||||
memset(file, ' ', sizeof(struct stat));
|
||||
lstat(dir_content[i].file_name, file);
|
||||
|
||||
if (S_ISDIR(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_DIR;
|
||||
} else if (S_ISBLK(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_BLOCK;
|
||||
} else if (S_ISCHR(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_CHARDEV;
|
||||
} else if (S_ISLNK(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_SYMLINK;
|
||||
} else if (S_ISFIFO(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_FIFO;
|
||||
} else if (S_ISSOCK(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_SOCK;
|
||||
} else if (S_ISREG(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_REGULAR;
|
||||
unsigned long j = 0;
|
||||
for (; entry[i]->d_name[j] != '\0'; j++) {
|
||||
dir_content[i * dir_length_width[1] + j] = entry[i]->d_name[j];
|
||||
}
|
||||
dir_width[i] = j;
|
||||
char *extension = strrchr(entry[i]->d_name, '.');
|
||||
if (extension) {
|
||||
for (j = 0; j < color_count; j++) {
|
||||
if (!strcmp(colors[j].file_extension, extension)) {
|
||||
dir_content[i].file_type = colors[j].color_pair;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < dir_length_width[0]; i++) {
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
dir_content[i].file_type = COLOR_REGULAR;
|
||||
}
|
||||
free(file);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < *dir_file_count; i++) {
|
||||
free(entry[i]);
|
||||
}
|
||||
free(entry);
|
||||
|
||||
}
|
||||
|
||||
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *file_width, char *dir_content){
|
||||
void print_dir(WINDOW *win, unsigned long *dir_file_count, file *dir_content){
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i<dir_length_width[0]; i++) {
|
||||
unsigned long j = 0;
|
||||
for (j = 0; j < file_width[i]; j++){
|
||||
mvwprintw(win, i, j, "%c", dir_content[i * dir_length_width[1] + j]);
|
||||
for (i = 0; i < *dir_file_count; i++) {
|
||||
wattron(win, COLOR_PAIR(dir_content[i].file_type));
|
||||
mvwprintw(win, i, 0, "%d", dir_content[i].file_type);
|
||||
mvwaddstr(win, i, 3,dir_content[i].file_name);
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].file_type));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -2,6 +2,6 @@
|
||||
#include "backend.c"
|
||||
|
||||
|
||||
void get_dir_size(char *path, unsigned long *dir_length_width, unsigned long *dir_width);
|
||||
void get_dir_content(char *path, unsigned long *dir_length_width, unsigned long *dir_width, char *dir_content);
|
||||
void print_dir(WINDOW *win, unsigned long *dir_length_width, unsigned long *width, char *dir_content);
|
||||
unsigned long get_dir_size(char *path);
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
||||
void print_dir(WINDOW *win, unsigned long *dir_file_count, file *dir_content);
|
||||
|
34
defines.h
34
defines.h
@@ -3,12 +3,8 @@
|
||||
#define STATUS_UPDATE_SCREEN_MASK 12 /* 1100*/
|
||||
#define STATUS_UPDATE_SCREEN_0 4
|
||||
#define STATUS_UPDATE_SCREEN_RESIZE 8
|
||||
#define STATUS_LOCK_MASK 496
|
||||
#define STATUS_LOCK_TOP 16
|
||||
#define STATUS_LOCK_BTM 32
|
||||
#define STATUS_LOCK_LFT 64
|
||||
#define STATUS_LOCK_MID 128
|
||||
#define STATUS_LOCK_RGT 256
|
||||
|
||||
#define SETTINGS_HAS_COLOR 1
|
||||
|
||||
#define FILE_MODIFIERS_HIDDEN_FILES 1
|
||||
#define FILE_MODIFIERS_SORT_BITMASK 126 /* 00000000000000000000000001111110*/
|
||||
@@ -19,3 +15,29 @@
|
||||
#define FILE_MODIFIERS_SORT_RANDOM 32
|
||||
#define FILE_MODIFIERS_SORT_REVERSE 64
|
||||
/*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/
|
||||
|
||||
#define COLOR_REGULAR 0
|
||||
#define COLOR_DIR 1
|
||||
#define COLOR_BLOCK 2
|
||||
#define COLOR_CHARDEV 3
|
||||
#define COLOR_SYMLINK 4
|
||||
#define COLOR_FIFO 5
|
||||
#define COLOR_SOCK 6
|
||||
#define COLOR_PATH
|
||||
|
||||
#ifndef GUARD
|
||||
#define GUARD
|
||||
|
||||
/* complex types are good actually */
|
||||
typedef struct File {
|
||||
char *file_name;
|
||||
unsigned char file_type;
|
||||
unsigned long file_name_width;
|
||||
unsigned long file_size_bytes;
|
||||
} file;
|
||||
typedef struct Color {
|
||||
char *file_extension;
|
||||
short color_pair;
|
||||
} color;
|
||||
|
||||
#endif
|
||||
|
6
main.c
6
main.c
@@ -7,6 +7,7 @@
|
||||
#include "window.h"
|
||||
#include "interactions.h"
|
||||
#include "defines.h"
|
||||
#include "colors.h"
|
||||
|
||||
unsigned int terminal_height;
|
||||
unsigned int terminal_width;
|
||||
@@ -75,6 +76,7 @@ int main() {
|
||||
render_pass(win_t, win_b, win_l, win_m, win_r);
|
||||
|
||||
}
|
||||
/*
|
||||
threading_free();
|
||||
|
||||
pthread_join(thread_l, NULL);
|
||||
@@ -82,6 +84,7 @@ int main() {
|
||||
pthread_join(thread_m, NULL);
|
||||
pthread_join(thread_t, NULL);
|
||||
pthread_join(thread_b, NULL);
|
||||
*/
|
||||
|
||||
curs_set(1);
|
||||
endwin();
|
||||
@@ -93,7 +96,6 @@ int main() {
|
||||
void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WINDOW *win_r){
|
||||
|
||||
if ((status & STATUS_UPDATE_SCREEN_MASK) & STATUS_UPDATE_SCREEN_RESIZE) {
|
||||
status |= STATUS_LOCK_MASK;
|
||||
|
||||
/*TODO: check if deallocation of window and reallocation is faster than this or not */
|
||||
|
||||
@@ -116,7 +118,6 @@ void render_pass(WINDOW *win_t, WINDOW *win_b, WINDOW *win_l, WINDOW *win_m, WIN
|
||||
mvwin(win_r, 1, ((terminal_width/3)*2));
|
||||
|
||||
|
||||
status &= ~STATUS_LOCK_MASK;
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
|
||||
@@ -147,6 +148,7 @@ void init() {
|
||||
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||
|
||||
threading_init(); /* found in threading.c */
|
||||
colors_init();
|
||||
|
||||
|
||||
}
|
||||
|
92
threading.c
92
threading.c
@@ -15,19 +15,16 @@ pthread_mutex_t mutex_rgt;
|
||||
|
||||
/* contains entire directory as 2d array
|
||||
* may be changed in future to only include parts of the dir (currently includes entire dir) */
|
||||
char *mid_content;
|
||||
char *lft_content;
|
||||
file *rgt_content;
|
||||
file *mid_content;
|
||||
file *lft_content;
|
||||
char *top_content; /* current path */
|
||||
/* index 0 = file_count
|
||||
* index 1 = longest_name */
|
||||
unsigned long *mid_length_width;
|
||||
unsigned long *lft_length_width;
|
||||
unsigned long *top_length_width;
|
||||
/* this array exists so that a file with a 3 char long name wont spend the same amount of time printing as a file with 200 chars
|
||||
* should both exist in the same directory
|
||||
* currently unused */
|
||||
unsigned long *mid_file_name_width;
|
||||
unsigned long *lft_file_name_width;
|
||||
|
||||
unsigned long rgt_file_count;
|
||||
unsigned long mid_file_count;
|
||||
unsigned long lft_file_count;
|
||||
unsigned long top_width;
|
||||
|
||||
|
||||
|
||||
extern unsigned int status;
|
||||
@@ -36,24 +33,20 @@ void *thread_mid(void *data){
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
|
||||
free(mid_content);
|
||||
free(mid_file_name_width);
|
||||
free(mid_length_width);
|
||||
mid_length_width = malloc(sizeof(char)*2);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
mid_content = malloc(sizeof("cannot open directory"));
|
||||
mid_length_width[0] = 1;
|
||||
mid_length_width[1] = sizeof("cannot open directory");
|
||||
mid_content = "cannot open directory";
|
||||
mid_content = malloc(sizeof(file));
|
||||
mid_content[1].file_name_width = sizeof("cannot open directory");
|
||||
mid_content[1].file_name = "cannot open directory";
|
||||
mid_file_count = 1;
|
||||
} else {
|
||||
|
||||
|
||||
get_dir_size(path, mid_length_width, mid_file_name_width);
|
||||
mid_file_name_width = malloc(mid_length_width[0] * sizeof(unsigned long));
|
||||
mid_content = malloc(mid_length_width[0] * mid_length_width[1] * sizeof(char));
|
||||
memset(mid_content, ' ', mid_length_width[0] * mid_length_width[1] * sizeof(char));
|
||||
get_dir_content(path, mid_length_width, mid_file_name_width, mid_content);
|
||||
mid_file_count = (unsigned long)get_dir_size(path);
|
||||
mid_content = malloc(mid_file_count * sizeof(file));
|
||||
memset(mid_content, ' ', mid_file_count * sizeof(file));
|
||||
get_dir_content(path, &mid_file_count, mid_content);
|
||||
|
||||
}
|
||||
free(path);
|
||||
@@ -64,19 +57,15 @@ void *thread_lft(void *data){
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
|
||||
free(lft_content);
|
||||
free(lft_file_name_width);
|
||||
free(lft_length_width);
|
||||
lft_length_width = malloc(sizeof(char)*2);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
lft_content = malloc(sizeof("cannot open directory"));
|
||||
lft_length_width[0] = 1;
|
||||
lft_length_width[1] = sizeof("cannot open directory");
|
||||
lft_content = "cannot open directory";
|
||||
lft_content = malloc(sizeof(file));
|
||||
lft_content[1].file_name_width = sizeof("cannot open directory");
|
||||
lft_content[1].file_name = "cannot open directory";
|
||||
lft_file_count = 1;
|
||||
} else {
|
||||
|
||||
|
||||
char *parent ;
|
||||
if((parent = malloc(strlen(path)+strlen("/..")+1)) != NULL){
|
||||
parent[0] = '\0'; /* ensures empty string */
|
||||
@@ -84,16 +73,18 @@ void *thread_lft(void *data){
|
||||
strcat(parent, "/..");
|
||||
}
|
||||
|
||||
get_dir_size(parent, lft_length_width, lft_file_name_width);
|
||||
lft_file_name_width = malloc(lft_length_width[0] * sizeof(unsigned long));
|
||||
lft_content = malloc(lft_length_width[0] * lft_length_width[1] * sizeof(char));
|
||||
memset(lft_content, ' ', lft_length_width[0] * lft_length_width[1] * sizeof(char));
|
||||
get_dir_content(parent, lft_length_width, lft_file_name_width, lft_content);
|
||||
free (parent);
|
||||
lft_file_count = (unsigned long)get_dir_size(parent);
|
||||
lft_content = malloc(lft_file_count * sizeof(file));
|
||||
memset(lft_content, ' ', lft_file_count * sizeof(file));
|
||||
get_dir_content(parent, &lft_file_count, lft_content);
|
||||
free(parent);
|
||||
|
||||
}
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
pthread_exit(NULL);
|
||||
|
||||
|
||||
}
|
||||
void *thread_rgt(void *data){
|
||||
|
||||
@@ -103,19 +94,15 @@ void *thread_rgt(void *data){
|
||||
void *thread_top(void *data){
|
||||
pthread_mutex_lock(&mutex_top);
|
||||
free(top_content);
|
||||
free(top_length_width);
|
||||
top_length_width = malloc(sizeof(char)*2);
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
top_content = malloc(sizeof("cannot open directory"));
|
||||
top_length_width[0] = 1;
|
||||
top_length_width[1] = sizeof("cannot open directory");
|
||||
top_width = sizeof("cannot open directory");
|
||||
top_content = "cannot open directory";
|
||||
} else {
|
||||
top_content = getcwd(NULL, 0);
|
||||
top_length_width[0] = 1;
|
||||
top_length_width[1] = strlen(top_content);
|
||||
top_width = strlen(top_content);
|
||||
}
|
||||
|
||||
free(path);
|
||||
@@ -128,24 +115,23 @@ void *thread_btm(void *data){
|
||||
}
|
||||
|
||||
void threading_init(){
|
||||
mid_content = malloc(sizeof(char));
|
||||
mid_file_name_width = malloc(sizeof(char));
|
||||
mid_length_width = malloc(sizeof(char));
|
||||
lft_content = malloc(sizeof(char));
|
||||
lft_file_name_width = malloc(sizeof(char));
|
||||
lft_length_width = malloc(sizeof(char));
|
||||
top_length_width = malloc(sizeof(char));
|
||||
rgt_content = malloc(sizeof(char));
|
||||
mid_content = malloc(sizeof(file));
|
||||
lft_content = malloc(sizeof(file));
|
||||
|
||||
top_content = malloc(sizeof(char));
|
||||
|
||||
|
||||
|
||||
pthread_mutex_init(&mutex_top, NULL);
|
||||
pthread_mutex_init(&mutex_mid, NULL);
|
||||
pthread_mutex_init(&mutex_lft, NULL);
|
||||
}
|
||||
void threading_free(){
|
||||
free(rgt_content);
|
||||
free(mid_content);
|
||||
free(mid_file_name_width);
|
||||
free(mid_length_width);
|
||||
free(lft_content);
|
||||
free(top_content);
|
||||
|
||||
pthread_mutex_destroy(&mutex_top);
|
||||
pthread_mutex_destroy(&mutex_mid);
|
||||
|
32
window.c
32
window.c
@@ -4,16 +4,15 @@
|
||||
|
||||
extern unsigned int status;
|
||||
|
||||
extern char *mid_content;
|
||||
extern unsigned long *mid_width;
|
||||
extern unsigned long *mid_length_width;
|
||||
extern unsigned long *mid_file_name_width;
|
||||
extern file *mid_content;
|
||||
extern file *lft_content;
|
||||
extern file *rgt_content;
|
||||
extern char *top_content;
|
||||
extern unsigned long *top_length_width;
|
||||
extern char *lft_content;
|
||||
extern unsigned long *lft_width;
|
||||
extern unsigned long *lft_length_width;
|
||||
extern unsigned long *lft_file_name_width;
|
||||
|
||||
extern unsigned long lft_file_count;
|
||||
extern unsigned long mid_file_count;
|
||||
extern unsigned long rgt_file_count;
|
||||
extern unsigned long top_width;
|
||||
|
||||
extern pthread_mutex_t mutex_top;
|
||||
extern pthread_mutex_t mutex_btm;
|
||||
@@ -30,7 +29,7 @@ void window_top(WINDOW *win){
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
|
||||
} else {
|
||||
for (i = 0; i<top_length_width[1]; i++) {
|
||||
for (i = 0; i < top_width; i++) {
|
||||
mvwprintw(win, 0, i, "%c", top_content[i]);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_top);
|
||||
@@ -55,8 +54,7 @@ void window_lft(WINDOW *win){
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", lft_length_width[0], lft_length_width[1]);
|
||||
print_dir(win, lft_length_width, lft_file_name_width, lft_content);
|
||||
print_dir(win, &lft_file_count, lft_content);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
}
|
||||
}
|
||||
@@ -72,8 +70,7 @@ void window_mid(WINDOW *win){
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", mid_length_width[0], mid_length_width[1]);
|
||||
print_dir(win, mid_length_width, mid_file_name_width, mid_content);
|
||||
print_dir(win, &mid_file_count, mid_content);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
}
|
||||
}
|
||||
@@ -81,8 +78,15 @@ void window_rgt(WINDOW *win){
|
||||
werase(win);
|
||||
box(win, 0, 0);
|
||||
|
||||
unsigned long local_width;
|
||||
unsigned long local_height;
|
||||
getmaxyx(win, local_height, local_width);
|
||||
if (pthread_mutex_trylock(&mutex_rgt)) {
|
||||
} else {
|
||||
/*
|
||||
pthread_mutex_unlock(&mutex_rgt);
|
||||
mvwprintw(win, local_height/2, local_width/2, "%ld %ld", rgt_length_width[0], rgt_length_width[1]);
|
||||
print_dir(win, rgt_length_width, rgt_file_name_width, rgt_content);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user