Use ansi instead of ncurses and add directory preview

This commit is contained in:
Night Kaly 2024-10-31 23:19:58 +00:00
parent 85de46dfe5
commit 12205feacd
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
6 changed files with 158 additions and 147 deletions

View file

@ -19,14 +19,11 @@ In COLS:
0 will make them equal (at the center),
15 will make files bigger
-15 will make preview bigger */
#define WINDOW_OFFSET -20
#define WINDOW_OFFSET -65
/* Options */
#define DRAW_BORDERS false /* draw borders around windows */
#define DRAW_PREVIEW true /* draw file preview */
#define SHOW_HIDDEN true /* show hidden files/dotfiles at startup */
#define SHOW_DETAILS true /* show file details at startup */
#define SHOW_DETAILS false /* show file details at startup */
#define SHOW_ICONS true /* show file icons at startup */
/* Calculate directories' sizes RECURSIVELY upon entering
@ -54,4 +51,26 @@ In COLS:
#define UP 0x103
#define LEFT 0x104
#define RIGHT 0x105
#define BACKSPACE 0x107
#define RESIZE 278
/* Colros */
#define GREEN "166;227;161"
#define BLUE "137;180;250"
#define PINK "245;194;231"
#define RED "243;139;168"
#define YELLOW "249;226;175"
#define LAVENDER "180;190;254"
#define WHITE "205;214;244"
enum keys {
BACKSPACE = 127,
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};

176
file.c
View file

@ -1,39 +1,35 @@
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <libgen.h>
#include <wchar.h>
#include "util.h"
#include "file.h"
ArrayList *arraylist_init(size_t capacity)
{
ArrayList *list = memalloc(sizeof(ArrayList));
list->length = 0;
list->capacity = capacity;
list->items = memalloc(capacity * sizeof(file));
ArrayList *list = memalloc(sizeof(ArrayList));
list->length = 0;
list->capacity = capacity;
list->items = memalloc(capacity * sizeof(file));
return list;
return list;
}
void arraylist_free(ArrayList *list)
{
for (size_t i = 0; i < list->length; i++) {
if (list->items[i].name != NULL)
free(list->items[i].name);
if (list->items[i].path != NULL)
free(list->items[i].path);
if (list->items[i].type != NULL)
free(list->items[i].type);
if (list->items[i].stats != NULL)
free(list->items[i].stats);
if (list->items[i].icon != NULL)
free(list->items[i].icon);
}
for (size_t i = 0; i < list->length; i++) {
if (list->items[i].name != NULL)
free(list->items[i].name);
if (list->items[i].path != NULL)
free(list->items[i].path);
if (list->items[i].stats != NULL)
free(list->items[i].stats);
if (list->items[i].icon != NULL)
free(list->items[i].icon);
}
free(list->items);
free(list);
free(list->items);
free(list);
}
/*
@ -42,70 +38,69 @@ void arraylist_free(ArrayList *list)
*/
long arraylist_search(ArrayList *list, char *filepath, bool bname)
{
for (long i = 0; i < list->length; i++) {
if (!bname && strcmp(list->items[i].path, filepath) == 0) {
return i;
}
if (bname) {
if (strcmp(list->items[i].name, filepath) == 0) {
return i;
}
}
}
return -1;
for (long i = 0; i < list->length; i++) {
if (!bname && strcmp(list->items[i].path, filepath) == 0) {
return i;
}
if (bname) {
if (strcmp(list->items[i].name, filepath) == 0) {
return i;
}
}
}
return -1;
}
void arraylist_remove(ArrayList *list, long index)
{
if (index >= list->length)
return;
if (index >= list->length)
return;
/* marked stuff doesn't work with this
free(list->items[index].name);
free(list->items[index].path);
free(list->items[index].type);
free(list->items[index].stats);
free(list->items[index].icon);
free(list->items[index].name);
free(list->items[index].path);
free(list->items[index].stats);
free(list->items[index].icon);
*/
for (long i = index; i < list->length - 1; i++)
list->items[i] = list->items[i + 1];
for (long i = index; i < list->length - 1; i++)
list->items[i] = list->items[i + 1];
list->length--;
list->length--;
}
/*
* Force will not remove duplicate marked files, instead it just skip adding
*/
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *type, wchar_t *icon, int color, bool marked, bool force)
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, char color[12], bool marked, bool force)
{
/* name, path, stats, type, icon, color */
file new_file = { name, path, type, stats, icon, color };
file new_file = { name, path, type, stats, icon };
memcpy(new_file.color, color, 12);
if (list->capacity != list->length) {
if (marked) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->items[i].path, new_file.path) == 0) {
if (!force)
arraylist_remove(list, i);
return;
}
}
}
list->items[list->length] = new_file;
} else {
int new_cap = list->capacity * 2;
file *new_items = memalloc(new_cap * sizeof(file));
file *old_items = list->items;
list->capacity = new_cap;
list->items = new_items;
if (list->capacity != list->length) {
if (marked) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->items[i].path, new_file.path) == 0) {
if (!force)
arraylist_remove(list, i);
return;
}
}
}
list->items[list->length] = new_file;
} else {
int new_cap = list->capacity * 2;
file *new_items = memalloc(new_cap * sizeof(file));
file *old_items = list->items;
list->capacity = new_cap;
list->items = new_items;
for (int i = 0; i < list->length; i++)
new_items[i] = old_items[i];
for (int i = 0; i < list->length; i++)
new_items[i] = old_items[i];
free(old_items);
list->items[list->length] = new_file;
}
list->length++;
free(old_items);
list->items[list->length] = new_file;
}
list->length++;
}
/*
@ -113,30 +108,29 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *t
*/
char *get_line(ArrayList *list, long index, bool detail, bool icons)
{
file file = list->items[index];
file file = list->items[index];
size_t name_len = strlen(file.name);
size_t length;
if (detail) {
length = name_len + strlen(file.stats) + 7; /* 4 for icon, 2 for space and 1 for null */
} else {
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
}
size_t name_len = strlen(file.name);
size_t length;
if (detail) {
length = name_len + strlen(file.stats) + 7; /* 4 for icon, 2 for space and 1 for null */
} else {
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
}
char *line = memalloc(length * sizeof(char));
line[0] = '\0';
if (detail) {
strcat(line, file.stats);
strcat(line, " ");
}
if (icons) {
char *tmp = memalloc(8 * sizeof(char));
snprintf(tmp, 8, "%ls", file.icon);
strcat(line, tmp);
strcat(line, " ");
free(tmp);
}
strcat(line, file.name);
char *line = memalloc(length);
line[0] = '\0';
if (detail) {
strcat(line, file.stats);
strcat(line, " ");
}
if (icons) {
char *tmp = memalloc(10);
snprintf(tmp, 10, "%s ", file.icon);
strcat(line, tmp);
free(tmp);
}
strcat(line, file.name);
return line;
return line;
}

31
file.h
View file

@ -2,27 +2,38 @@
#define FILE_H_
#include <stdio.h>
#include <stdbool.h>
enum ftypes {
REG,
DRY, /* DIR is taken */
LNK,
CHR,
SOC,
BLK,
FIF
};
typedef struct file {
char *name; /* basename */
char *path; /* absolute path */
char *type;
char *stats;
wchar_t *icon;
int color;
char *name; /* basename */
char *path; /* absolute path */
int type;
char *stats;
char *icon;
char color[12];
} file;
typedef struct ArrayList {
size_t length;
size_t capacity;
file *items;
size_t length;
size_t capacity;
file *items;
} ArrayList;
ArrayList *arraylist_init(size_t capacity);
void arraylist_free(ArrayList *list);
long arraylist_search(ArrayList *list, char *filepath, bool bname);
void arraylist_remove(ArrayList *list, long index);
void arraylist_add(ArrayList *list, char *filename, char *path, char *stats, char *type, wchar_t *icon, int color, bool marked, bool force);
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, char color[12], bool marked, bool force);
char *get_line(ArrayList *list, long index, bool detail, bool icons);
#endif

47
icons.c
View file

@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <wchar.h>
#include "icons.h"
#include "util.h"
@ -30,91 +29,91 @@ void hashtable_init()
icon *c = memalloc(sizeof(icon));
strcpy(c->name, "c");
c->icon = L"";
c->icon = "";
icon *h = memalloc(sizeof(icon));
strcpy(h->name, "h");
h->icon = L"";
h->icon = "";
icon *cpp = memalloc(sizeof(icon));
strcpy(cpp->name, "cpp");
cpp->icon = L"";
cpp->icon = "";
icon *hpp = memalloc(sizeof(icon));
strcpy(hpp->name, "hpp");
hpp->icon = L"󰰀";
hpp->icon = "󰰀";
icon *md = memalloc(sizeof(icon));
strcpy(md->name, "md");
md->icon = L"";
md->icon = "";
icon *py = memalloc(sizeof(icon));
strcpy(py->name, "py");
py->icon = L"";
py->icon = "";
icon *java = memalloc(sizeof(icon));
strcpy(java->name, "java");
java->icon = L"";
java->icon = "";
icon *json = memalloc(sizeof(icon));
strcpy(json->name, "json");
json->icon = L"";
json->icon = "";
icon *js = memalloc(sizeof(icon));
strcpy(js->name, "js");
js->icon = L"";
js->icon = "";
icon *html = memalloc(sizeof(icon));
strcpy(html->name, "html");
html->icon = L"";
html->icon = "";
icon *rs = memalloc(sizeof(icon));
strcpy(rs->name, "rs");
rs->icon = L"";
rs->icon = "";
icon *sh = memalloc(sizeof(icon));
strcpy(sh->name, "sh");
sh->icon = L"";
sh->icon = "";
icon *go = memalloc(sizeof(icon));
strcpy(go->name, "go");
go->icon = L"";
go->icon = "";
icon *r = memalloc(sizeof(icon));
strcpy(r->name, "r");
r->icon = L"";
r->icon = "";
icon *diff = memalloc(sizeof(icon));
strcpy(diff->name, "diff");
diff->icon = L"";
diff->icon = "";
icon *hs = memalloc(sizeof(icon));
strcpy(hs->name, "hs");
hs->icon = L"";
hs->icon = "";
icon *log = memalloc(sizeof(icon));
strcpy(log->name, "log");
log->icon = L"󱀂";
log->icon = "󱀂";
icon *rb = memalloc(sizeof(icon));
strcpy(rb->name, "rb");
rb->icon = L"";
rb->icon = "";
icon *iso = memalloc(sizeof(icon));
strcpy(iso->name, "iso");
iso->icon = L"󰻂";
iso->icon = "󰻂";
icon *lua = memalloc(sizeof(icon));
strcpy(lua->name, "lua");
lua->icon = L"";
lua->icon = "";
icon *license = memalloc(sizeof(icon));
strcpy(license->name, "LICENSE");
license->icon = L"";
license->icon = "";
icon *gitignore = memalloc(sizeof(icon));
strcpy(gitignore->name, "gitignore");
gitignore->icon = L"";
gitignore->icon = "";
hashtable_add(c);
hashtable_add(h);
@ -148,7 +147,7 @@ void hashtable_print()
if (hash_table[i] == NULL) {
printf("%i. ---\n", i);
} else {
printf("%i. | Name %s | Icon %ls\n", i, hash_table[i]->name, hash_table[i]->icon);
printf("%i. | Name %s | Icon %s\n", i, hash_table[i]->name, hash_table[i]->icon);
}
}
}

View file

@ -2,14 +2,13 @@
#define ICONS_H_
#include <stdbool.h>
#include <wchar.h>
#define MAX_NAME 30
#define TABLE_SIZE 100
typedef struct {
char name[MAX_NAME];
wchar_t *icon;
char *icon;
} icon;
unsigned int hash(char *name);

17
util.c
View file

@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
void die(char *reason)
{
@ -21,22 +20,12 @@ void *memalloc(size_t size)
void *estrdup(void *ptr)
{
void *duped = strdup(ptr);
if (!duped) {
void *dup = strdup(ptr);
if (!dup) {
perror("ccc");
exit(EXIT_FAILURE);
}
return duped;
}
void *ewcsdup(void *ptr)
{
void *duped = wcsdup(ptr);
if (!duped) {
perror("ccc");
exit(EXIT_FAILURE);
}
return duped;
return dup;
}
void *rememalloc(void *ptr, size_t size)