ui: file list (not completed)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "fs_file_list.h"
|
||||
#include "../../file_processor.h"
|
||||
#include "progress_bar.h"
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
@@ -7,35 +6,60 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void reset_file_list(ui_fs_file_list_t *fl_ui);
|
||||
|
||||
static void fl_add(fl_item_t **f_start, fl_item_t **f_current,
|
||||
struct dirent *dent) {
|
||||
fl_item_t *c_item = malloc(sizeof(fl_item_t));
|
||||
c_item->name = dent->d_name;
|
||||
c_item->description = NULL;
|
||||
c_item->owner = NULL;
|
||||
c_item->size = 0;
|
||||
struct fl_args {
|
||||
char *name;
|
||||
u_char d_type;
|
||||
};
|
||||
|
||||
static void fl_add(fs_fl_item_t **f_start, fs_fl_item_t **f_current,
|
||||
struct fl_args *fargs) {
|
||||
fs_fl_item_t *c_item = malloc(sizeof(fs_fl_item_t));
|
||||
c_item->name = malloc(strlen(fargs->name) + 1);
|
||||
strcpy(c_item->name, fargs->name);
|
||||
c_item->next = NULL;
|
||||
c_item->d_type = fargs->d_type;
|
||||
if (*f_start == NULL) {
|
||||
c_item->prev = NULL;
|
||||
*f_start = c_item;
|
||||
*f_current = c_item;
|
||||
} else {
|
||||
c_item->prev = *f_current;
|
||||
(*f_current)->next = c_item;
|
||||
*f_current = c_item;
|
||||
}
|
||||
}
|
||||
|
||||
fl_item_t *get_files_from_fs(char *path) {
|
||||
/* TODO: separate files from directories, mark directories, filter empty fields */
|
||||
static void fl_clear(fs_fl_item_t **start, fs_fl_item_t **arg_current) {
|
||||
if (*start == NULL)
|
||||
return;
|
||||
fs_fl_item_t *next, *current;
|
||||
current = *start;
|
||||
|
||||
do {
|
||||
next = current->next;
|
||||
free(current->name);
|
||||
free(current);
|
||||
} while ((current = next) != NULL);
|
||||
*start = NULL;
|
||||
*arg_current = NULL;
|
||||
}
|
||||
|
||||
fs_fl_item_t *get_files_from_fs(char *path) {
|
||||
DIR *dir;
|
||||
struct dirent *dent;
|
||||
char name[64];
|
||||
struct fl_args f_args;
|
||||
|
||||
fl_item_t *l_start = NULL, *l_current = NULL;
|
||||
fs_fl_item_t *d_start = NULL, *d_current = NULL; /* directories */
|
||||
fs_fl_item_t *f_start = NULL, *f_current = NULL; /* files */
|
||||
|
||||
dir = opendir(path);
|
||||
if (!dir) {
|
||||
@@ -43,39 +67,60 @@ fl_item_t *get_files_from_fs(char *path) {
|
||||
errno = 1;
|
||||
}
|
||||
while ((dent = readdir(dir)) != NULL) {
|
||||
fl_add(&l_start, &l_current, dent);
|
||||
if ((f_args.d_type = dent->d_type) == DT_DIR) {
|
||||
if (!strcmp(dent->d_name, "."))
|
||||
continue;
|
||||
sprintf(name, "/%s", dent->d_name);
|
||||
f_args.name = name;
|
||||
fl_add(&d_start, &d_current, &f_args);
|
||||
} else {
|
||||
f_args.name = dent->d_name;
|
||||
sprintf(name, "%s", dent->d_name);
|
||||
fl_add(&f_start, &f_current, &f_args);
|
||||
}
|
||||
}
|
||||
if (d_current != NULL) d_current->next = f_start;
|
||||
if (f_start != NULL) f_start->prev = d_current;
|
||||
return d_start;
|
||||
}
|
||||
|
||||
void select_item(ui_fs_file_list_t *fui) {
|
||||
char d_name[64] = "./";
|
||||
if (fui->current->d_type == DT_DIR) {
|
||||
if (!strcmp("/..", fui->current->name)) {
|
||||
/* TODO: slice the last "/abc" */
|
||||
} else {
|
||||
strcat(d_name, fui->current->name + 1); /* skip "/" in the name */
|
||||
}
|
||||
fui->start = get_files_from_fs(d_name);
|
||||
fui->current = fui->start;
|
||||
draw_fs_file_list(fui);
|
||||
}
|
||||
return l_start;
|
||||
}
|
||||
|
||||
void fs_file_list_cb(callback_args_t *args) {
|
||||
ui_fs_file_list_t *fui = args->element;
|
||||
int32_t key = *((int32_t *)args->data);
|
||||
int32_t q_len = 0;
|
||||
fl_item_t *f_item;
|
||||
fs_fl_item_t *f_item;
|
||||
ui_progress_bar_t *pb;
|
||||
switch (key) {
|
||||
case KEY_DOWN:
|
||||
if (fui->current_idx < fui->current_count - 1) {
|
||||
if (fui->current->next != NULL) {
|
||||
fui->current = fui->current->next;
|
||||
fui->current_idx++;
|
||||
draw_fs_file_list(fui);
|
||||
} else {
|
||||
fl_clear(fui->start, fui->current);
|
||||
reset_file_list(fui);
|
||||
}
|
||||
break;
|
||||
case KEY_UP:
|
||||
if (fui->current_idx > 0) {
|
||||
if (fui->current->prev != NULL) {
|
||||
fui->current = fui->current->prev;
|
||||
fui->current_idx--;
|
||||
draw_fs_file_list(fui);
|
||||
} else {
|
||||
fl_clear(fui->start, fui->current);
|
||||
reset_fs_file_list(fui);
|
||||
fui->activate_last = true;
|
||||
}
|
||||
break;
|
||||
case '\n':
|
||||
/* here goes file select */
|
||||
select_item(fui);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -85,22 +130,16 @@ ui_fs_file_list_t *init_fs_file_list(WINDOW **win, widget_t *w_parent) {
|
||||
WINDOW *win_par = *(w_parent->w_parent->parent_win);
|
||||
init_widget(&(fl_ui->w), w_parent, win, "");
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->current_count = 0;
|
||||
fl_ui->activate_last = false;
|
||||
fl_ui->info_win = NULL;
|
||||
fl_ui->start = malloc(sizeof(fl_item_t *)); /* For compatibility reasons */
|
||||
*(fl_ui->start) = get_files_from_fs("./");
|
||||
fl_ui->start = get_files_from_fs("./");
|
||||
fl_ui->current = fl_ui->start;
|
||||
fl_ui->w.callback = fs_file_list_cb;
|
||||
fl_ui->w.x = getmaxx(win_par) / 10 * 8;
|
||||
fl_ui->w.y = getmaxy(win_par) / 10 * 8;
|
||||
return fl_ui;
|
||||
}
|
||||
|
||||
void reset_fs_file_list(ui_fs_file_list_t *fl_ui) {
|
||||
fl_ui->current_idx = 0;
|
||||
fl_ui->current_count = 0;
|
||||
fl_ui->activate_last = false;
|
||||
}
|
||||
void reset_fs_file_list(ui_fs_file_list_t *fl_ui) { fl_ui->current_idx = 0; }
|
||||
|
||||
void draw_fs_file_list(ui_fs_file_list_t *fl_ui) {
|
||||
int32_t sz_y, sz_x;
|
||||
@@ -111,39 +150,31 @@ void draw_fs_file_list(ui_fs_file_list_t *fl_ui) {
|
||||
int32_t sz_x_f = sz_x - 2; // actual size (without box)
|
||||
p_y = 1;
|
||||
p_x = 1;
|
||||
fl_item_t *el = *(fl_ui->start);
|
||||
fl_item_t *active_el = *(fl_ui->start);
|
||||
int32_t cur_el_idx = 0;
|
||||
|
||||
if (fl_ui->activate_last) {
|
||||
fl_ui->current_idx = fl_ui->current_count - 1;
|
||||
fl_ui->activate_last = false;
|
||||
}
|
||||
fs_fl_item_t *el = fl_ui->start;
|
||||
fs_fl_item_t *active_el = fl_ui->start;
|
||||
|
||||
if (fl_ui->current_idx + 1 >= sz_y_f) {
|
||||
p_y -= fl_ui->current_idx - sz_y_f + 2;
|
||||
}
|
||||
|
||||
wattrset(win, A_REVERSE); /* Match with window background */
|
||||
wattrset(win, A_REVERSE); /* Match with modal background */
|
||||
|
||||
do {
|
||||
if (p_y < 1) {
|
||||
p_y++;
|
||||
cur_el_idx++;
|
||||
continue;
|
||||
}
|
||||
if (cur_el_idx == fl_ui->current_idx) {
|
||||
if (el == fl_ui->current) {
|
||||
wattrset(win, COLOR_PAIR(3) | A_BOLD | A_REVERSE);
|
||||
active_el = el;
|
||||
}
|
||||
p_x = 1;
|
||||
mvwprintw(win, p_y, p_x, "%s%n", el->name, &p_x);
|
||||
mvwprintw(win, p_y, p_x + 1, "%*s", sz_x_f - p_x, "");
|
||||
if (cur_el_idx == fl_ui->current_idx) {
|
||||
if (el == fl_ui->current) {
|
||||
wattroff(win, COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
p_y++;
|
||||
cur_el_idx++;
|
||||
} while ((el = el->next) != NULL && p_y < sz_y_f);
|
||||
|
||||
p_x = 1;
|
||||
@@ -158,7 +189,6 @@ void draw_fs_file_list(ui_fs_file_list_t *fl_ui) {
|
||||
}
|
||||
|
||||
void destroy_fs_file_list(ui_fs_file_list_t *fui) {
|
||||
fl_clear(fui->start, fui->current);
|
||||
free(fui->start);
|
||||
fl_clear(&(fui->start), &(fui->current));
|
||||
free(fui);
|
||||
}
|
||||
Reference in New Issue
Block a user