once again switched to complex data types, basic file coloring implemented
This commit is contained in:
60
backend.c
60
backend.c
@ -1,75 +1,69 @@
|
||||
#define _GNU_SOURCE
|
||||
#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;
|
||||
|
||||
|
||||
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);
|
||||
unsigned long entry_count = 0;
|
||||
if (dir) {
|
||||
unsigned long entry_count = 0;
|
||||
unsigned long max_length;
|
||||
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 {
|
||||
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;
|
||||
dir_content[i].file_name_width = strlen(entry[i]->d_name);
|
||||
dir_content[i].file_name = malloc(dir_content[i].file_name_width * sizeof(char));
|
||||
dir_content[i].file_name = entry[i]->d_name;
|
||||
dir_content[i].file_type = entry[i]->d_type;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < dir_length_width[0]; i++) {
|
||||
|
||||
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++) {
|
||||
for (i = 0; i < *dir_file_count; 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]);
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user