start of a major rewrite
This commit is contained in:
125
dir.c
125
dir.c
@@ -12,8 +12,7 @@
|
||||
#include "config.h"
|
||||
|
||||
|
||||
extern file *mid_content;
|
||||
extern unsigned long mid_file_count;
|
||||
extern dir mid_dir;
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern unsigned int color_count;
|
||||
@@ -48,85 +47,87 @@ unsigned long get_dir_size(char *path){
|
||||
|
||||
}
|
||||
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
|
||||
void get_dir_content(char *path, dir *dir){
|
||||
struct dirent **entry = NULL;
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
||||
scandir(path, &entry, skip_dot, NULL);
|
||||
} else {
|
||||
scandir(path, &entry, skip_hidden_files, NULL);
|
||||
}
|
||||
|
||||
char *full_path = NULL;
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < *dir_file_count; i++ ) {
|
||||
for (i = 0; i < dir->file_count; i++ ) {
|
||||
dir->file_list[i].file_name = NULL;
|
||||
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
} else {
|
||||
dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1);
|
||||
memcpy(dir_content[i].file_name, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
dir->file_list[i].file_name = malloc(strlen(entry[i]->d_name)+1);
|
||||
memcpy(dir->file_list[i].file_name, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
dir->file_list[i].status = 0;
|
||||
|
||||
|
||||
struct stat *file;
|
||||
file = malloc(sizeof(struct stat));
|
||||
|
||||
/* using the full path allows using the same function for all windows */
|
||||
char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
|
||||
full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
|
||||
memcpy(full_path, path, strlen(path));
|
||||
memcpy(full_path + strlen(path) + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
full_path[strlen(path)] = '/';
|
||||
|
||||
lstat(full_path, file);
|
||||
|
||||
dir_content[i].file_size = file->st_size;
|
||||
dir_content[i].permissions = file->st_mode;
|
||||
dir->file_list[i].file_size = file->st_size;
|
||||
dir->file_list[i].permissions = file->st_mode;
|
||||
|
||||
if (S_ISLNK(file->st_mode)) {
|
||||
stat(full_path, file);
|
||||
if (S_ISDIR(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_DIR | FILE_TYPE_SYMLINK;
|
||||
dir_content[i].color_pair = COLOR_SYMLINK;
|
||||
dir_content[i].file_size = get_dir_size(full_path);
|
||||
dir->file_list[i].file_type = FILE_TYPE_DIR | FILE_TYPE_SYMLINK;
|
||||
dir->file_list[i].color_pair = COLOR_SYMLINK;
|
||||
dir->file_list[i].file_size = get_dir_size(full_path);
|
||||
} else {
|
||||
dir_content[i].file_type = FILE_TYPE_REGULAR | FILE_TYPE_SYMLINK;
|
||||
dir_content[i].color_pair = COLOR_SYMLINK;
|
||||
dir->file_list[i].file_type = FILE_TYPE_REGULAR | FILE_TYPE_SYMLINK;
|
||||
dir->file_list[i].color_pair = COLOR_SYMLINK;
|
||||
}
|
||||
} else if (S_ISDIR(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_DIR;
|
||||
dir_content[i].color_pair = COLOR_DIR;
|
||||
dir_content[i].file_size = get_dir_size(full_path);
|
||||
dir->file_list[i].file_type = FILE_TYPE_DIR;
|
||||
dir->file_list[i].color_pair = COLOR_DIR;
|
||||
dir->file_list[i].file_size = get_dir_size(full_path);
|
||||
} else if (file->st_mode & S_IXUSR) {
|
||||
dir_content[i].file_type = FILE_TYPE_EXEC;
|
||||
dir_content[i].color_pair = COLOR_EXEC;
|
||||
dir->file_list[i].file_type = FILE_TYPE_EXEC;
|
||||
dir->file_list[i].color_pair = COLOR_EXEC;
|
||||
} else if (S_ISREG(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_REGULAR;
|
||||
dir_content[i].color_pair = COLOR_REGULAR;
|
||||
dir->file_list[i].file_type = FILE_TYPE_REGULAR;
|
||||
dir->file_list[i].color_pair = COLOR_REGULAR;
|
||||
unsigned long j = 0;
|
||||
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].color_pair = colors[j].color_pair;
|
||||
dir->file_list[i].color_pair = colors[j].color_pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (S_ISBLK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_BLOCK;
|
||||
dir_content[i].color_pair = COLOR_BLOCK;
|
||||
dir->file_list[i].file_type = FILE_TYPE_BLOCK;
|
||||
dir->file_list[i].color_pair = COLOR_BLOCK;
|
||||
} else if (S_ISCHR(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_CHARDEV;
|
||||
dir->file_list[i].file_type = COLOR_CHARDEV;
|
||||
} else if (S_ISFIFO(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_FIFO;
|
||||
dir_content[i].color_pair = COLOR_FIFO;
|
||||
dir->file_list[i].file_type = FILE_TYPE_FIFO;
|
||||
dir->file_list[i].color_pair = COLOR_FIFO;
|
||||
} else if (S_ISSOCK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_SOCK;
|
||||
dir_content[i].color_pair = COLOR_SOCK;
|
||||
dir->file_list[i].file_type = FILE_TYPE_SOCK;
|
||||
dir->file_list[i].color_pair = COLOR_SOCK;
|
||||
} else {
|
||||
dir_content[i].file_type = COLOR_REGULAR;
|
||||
dir_content[i].color_pair = COLOR_REGULAR;
|
||||
dir->file_list[i].file_type = COLOR_REGULAR;
|
||||
dir->file_list[i].color_pair = COLOR_REGULAR;
|
||||
unsigned long j = 0;
|
||||
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].color_pair = colors[j].color_pair;
|
||||
dir->file_list[i].color_pair = colors[j].color_pair;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -135,16 +136,19 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
||||
free(full_path);
|
||||
free(file);
|
||||
free(entry[i]);
|
||||
full_path = NULL;
|
||||
file = NULL;
|
||||
entry[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
qsort(dir_content, *dir_file_count, sizeof(file), order_func);
|
||||
qsort(dir->file_list, dir->file_count, sizeof(file), order_func);
|
||||
|
||||
free(entry);
|
||||
|
||||
}
|
||||
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content){
|
||||
void print_dir(WINDOW *win, char print_info, dir *dir){
|
||||
/* i am not proud of this function */
|
||||
unsigned long line_width = getmaxx(win);
|
||||
|
||||
@@ -161,22 +165,23 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
#else
|
||||
long offset_front = 2;
|
||||
#endif
|
||||
long selected_file_current = dir->current_file - dir->file_list;
|
||||
|
||||
long offset_index = 2; /* only used for the index of the file itself */
|
||||
if (print_info) {
|
||||
if (*dir_file_count > 9) {
|
||||
if (dir->file_count > 9) {
|
||||
|
||||
#if SETTINGS_LINE_NUMBERS != 0
|
||||
unsigned long dir_file_count_ = *dir_file_count;
|
||||
unsigned long dir_file_count_ = dir->file_count;
|
||||
while(dir_file_count_ > 9) {
|
||||
offset_front++;
|
||||
dir_file_count_ /= 10;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (selected_file_current > (terminal_height/3)*2 && *dir_file_count > terminal_height - 2) {
|
||||
if (selected_file_current + (terminal_height/3) >= *dir_file_count) {
|
||||
offset_vertical = *dir_file_count - terminal_height+2;
|
||||
if (selected_file_current > (terminal_height/3)*2 && dir->file_count > terminal_height - 2) {
|
||||
if (selected_file_current + (terminal_height/3) >= dir->file_count) {
|
||||
offset_vertical = dir->file_count - terminal_height+2;
|
||||
} else {
|
||||
offset_vertical = selected_file_current - (terminal_height/3)*2;
|
||||
}
|
||||
@@ -184,12 +189,12 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
} else {
|
||||
offset_front = 0;
|
||||
}
|
||||
for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) {
|
||||
for (i = offset_vertical; i < dir->file_count && i < (terminal_height + offset_vertical); i++) {
|
||||
|
||||
|
||||
|
||||
if (print_info) {
|
||||
file_size = dir_content[i].file_size;
|
||||
file_size = dir->file_list[i].file_size;
|
||||
char size_index = -1;
|
||||
|
||||
do {
|
||||
@@ -199,8 +204,8 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
} while (file_size > 1 && size_index < size_unit_count);
|
||||
size_char = size_unit[(unsigned)size_index];
|
||||
|
||||
if (dir_content[i].file_type &= FILE_TYPE_DIR) {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
|
||||
if (dir->file_list[i].file_type &= FILE_TYPE_DIR) {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%ld", dir->file_list[i].file_size) + 1);
|
||||
} else if (size_char =='B') {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
|
||||
} else {
|
||||
@@ -210,17 +215,17 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
offset_back = line_width;
|
||||
}
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
if (dir->file_list[i].status & FILE_STATUS_SELECTED) {
|
||||
is_selected = 1;
|
||||
} else {
|
||||
is_selected = 0;
|
||||
}
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
if (dir->file_list[i].status & FILE_STATUS_SELECTED) {
|
||||
wattron(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
wattron(win, COLOR_PAIR(dir->file_list[i].color_pair));
|
||||
}
|
||||
if (dir_content[i].status & FILE_STATUS_HOVER) {
|
||||
if (&dir->file_list[i] == dir->current_file) {
|
||||
wattron(win, A_REVERSE);
|
||||
unsigned long bg = 0;
|
||||
for (bg = 0; bg < line_width; bg++) {
|
||||
@@ -265,29 +270,29 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
#endif
|
||||
|
||||
|
||||
if (dir_content[i].file_type &= FILE_TYPE_DIR) {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size);
|
||||
if (dir->file_list[i].file_type &= FILE_TYPE_DIR) {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir->file_list[i].file_size);
|
||||
}else if (size_char =='B') {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char);
|
||||
} else {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.2lf %c", printed_size, size_char);
|
||||
}
|
||||
}
|
||||
char *extension = strrchr(dir_content[i].file_name, '.');
|
||||
char *extension = strrchr(dir->file_list[i].file_name, '.');
|
||||
|
||||
unsigned long printable_size = offset_back-offset_front-is_selected-2;
|
||||
|
||||
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, dir_content[i].file_name, printable_size);
|
||||
if (extension && printable_size <= strlen(dir_content[i].file_name)) {
|
||||
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, dir->file_list[i].file_name, printable_size);
|
||||
if (extension && printable_size <= strlen(dir->file_list[i].file_name)) {
|
||||
mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-1, extension, strlen(extension));
|
||||
mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-2, "~", 1);
|
||||
}
|
||||
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
if (dir->file_list[i].status & FILE_STATUS_SELECTED) {
|
||||
wattroff(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
wattroff(win, COLOR_PAIR(dir->file_list[i].color_pair));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -296,6 +301,7 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
char update_selected_file(){
|
||||
char ret = -1; /* -1 on empty or inaccessible file, 0 on unchanged file, 1 on changed file */
|
||||
|
||||
/*
|
||||
if (selected_file_current >= mid_file_count) {
|
||||
selected_file_current = mid_file_count-1;
|
||||
}
|
||||
@@ -305,18 +311,22 @@ char update_selected_file(){
|
||||
} else {
|
||||
ret = 0;
|
||||
}
|
||||
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
|
||||
mid_dir.current_dir.status |= FILE_STATUS_HOVER;
|
||||
selected_file_last = selected_file_current;
|
||||
*/
|
||||
return ret;
|
||||
}
|
||||
void dir_set_selected_file_current(unsigned long selected_file_current){
|
||||
/*
|
||||
if (mid_content->file_name) {
|
||||
current_dir->selected_file_current = selected_file_current;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
unsigned long dir_get_selected_file_current(){
|
||||
|
||||
/*
|
||||
current_dir = visited_dirs;
|
||||
if (mid_content->file_name[0] == '\0') {
|
||||
return 0;
|
||||
@@ -339,6 +349,7 @@ unsigned long dir_get_selected_file_current(){
|
||||
free(path);
|
||||
return current_dir->selected_file_current;
|
||||
}
|
||||
*/
|
||||
}
|
||||
void dir_init(){
|
||||
visited_dirs = malloc(sizeof(linked_dir));
|
||||
@@ -350,6 +361,7 @@ void dir_init(){
|
||||
}
|
||||
|
||||
void recursive_delete(file current_file){
|
||||
/*
|
||||
if (S_ISLNK(current_file.permissions)) {
|
||||
remove(current_file.file_name);
|
||||
} else if (current_file.file_type & FILE_TYPE_DIR ) {
|
||||
@@ -379,4 +391,5 @@ void recursive_delete(file current_file){
|
||||
} else {
|
||||
remove(current_file.file_name);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user