Reduce magic numbers, calculating size_size correctly, adding commtents at config.h, add more icons

This commit is contained in:
Night Kaly 2024-03-30 23:13:14 +00:00
parent 721cbfff35
commit 53dd0e16c5
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE
3 changed files with 29 additions and 17 deletions

28
ccc.c
View file

@ -539,11 +539,14 @@ void add_file_stat(char *filepath, int ftype)
} }
/* get file type and color, 4 chars for the type and icon */ /* get file type and color, 4 chars for the type and icon */
char *type = memalloc(4 * sizeof(char)); size_t type_size = 4 * sizeof(char);
wchar_t *icon_str = memalloc(2 * sizeof(wchar_t)); size_t icon_size = 2 * sizeof(wchar_t);
char *type = memalloc(type_size);
wchar_t *icon_str = memalloc(icon_size);
filepath[strlen(filepath)] = '\0'; filepath[strlen(filepath)] = '\0';
/* find last / in path */ /* find last / in path by finding basename */
char *f_bname = strrchr(filepath, '/'); char *f_bname = strrchr(filepath, '/');
char *ext = NULL; char *ext = NULL;
if (f_bname != NULL) { if (f_bname != NULL) {
@ -564,7 +567,7 @@ void add_file_stat(char *filepath, int ftype)
else else
wcsncpy(icon_str, ext_icon->icon, 2); wcsncpy(icon_str, ext_icon->icon, 2);
} else { } else {
wcsncpy(icon_str, L"0", 2); wcsncpy(icon_str, L"", 2);
} }
int color; int color;
@ -605,9 +608,10 @@ void add_file_stat(char *filepath, int ftype)
} }
/* get last modified time */ /* get last modified time */
char *time = memalloc(17 * sizeof(char)); size_t time_size = 17 * sizeof(char);
char *time = memalloc(time_size);
/* format last modified time to a string */ /* format last modified time to a string */
strftime(time, 17, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); strftime(time, time_size, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
/* get file size */ /* get file size */
double bytes = file_stat.st_size; double bytes = file_stat.st_size;
@ -621,8 +625,8 @@ void add_file_stat(char *filepath, int ftype)
bytes = total_dir_size; bytes = total_dir_size;
} }
} }
/* 3 before decimal + 1 decimal + SIZE_OFFSET (after decimal) + 1 space + 1 null */ /* 4 before decimal + 1 dot + DECIMAL_PLACES (after decimal) + unit length(1 for K, 3 for KiB, taking units[1] as B never changes)+ 1 space + 1 null */
int size_size = 3 + 1 + DECIMAL_PLACES + 1 + 1; int size_size = 4 + 1 + DECIMAL_PLACES + strlen(units[1]) + 1 + 1;
char *size = memalloc(size_size * sizeof(char)); char *size = memalloc(size_size * sizeof(char));
int unit = 0; int unit = 0;
while (bytes > 1024) { while (bytes > 1024) {
@ -638,9 +642,9 @@ void add_file_stat(char *filepath, int ftype)
/* get file mode string */ /* get file mode string */
char *mode_str = get_file_mode(file_stat.st_mode); char *mode_str = get_file_mode(file_stat.st_mode);
/* 11 + 17 + size_size + 1 space + 1 null */ /* mode_str(11) + time(17) + size_size + 2 spaces + 1 null */
int stat_size = 11 + 17 + size_size + 1 + 1; size_t stat_size = 11 * sizeof(char) + time_size + size_size + 3 * sizeof(char);
char *total_stat = memalloc(stat_size * sizeof(char)); char *total_stat = memalloc(stat_size);
snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size); snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size);
arraylist_add(files, filepath, total_stat, type, icon_str, color, false, false); arraylist_add(files, filepath, total_stat, type, icon_str, color, false, false);
@ -659,7 +663,7 @@ void add_file_stat(char *filepath, int ftype)
*/ */
char *get_file_mode(mode_t mode) char *get_file_mode(mode_t mode)
{ {
char *mode_str = memalloc(sizeof(char) * 11); char *mode_str = memalloc(11 * sizeof(char));
mode_str[0] = S_ISDIR(mode) ? 'd' : '-'; /* Check if it's a directory */ mode_str[0] = S_ISDIR(mode) ? 'd' : '-'; /* Check if it's a directory */
mode_str[1] = (mode & S_IRUSR) ? 'r' : '-'; mode_str[1] = (mode & S_IRUSR) ? 'r' : '-';
mode_str[2] = (mode & S_IWUSR) ? 'w' : '-'; mode_str[2] = (mode & S_IWUSR) ? 'w' : '-';

View file

@ -1,5 +1,3 @@
#include "icons.h"
/* Settings */ /* Settings */
#define PH 1 /* panel height */ #define PH 1 /* panel height */
#define JUMP_NUM 14 /* how long ctrl + u/d jump are */ #define JUMP_NUM 14 /* how long ctrl + u/d jump are */
@ -32,7 +30,8 @@ In COLS:
#define SHOW_ICONS true /* show file icons at startup */ #define SHOW_ICONS true /* show file icons at startup */
/* Calculate directories' sizes RECURSIVELY upon entering /* Calculate directories' sizes RECURSIVELY upon entering
`A` keybind at the startup */ `A` keybind at the startup
**VERY EXPENSIVE**, **CAN TAKE UP TO A MINUTE IN ROOT** */
#define DIRS_SIZE false #define DIRS_SIZE false
/* Default text editor */ /* Default text editor */

11
icons.c
View file

@ -44,7 +44,6 @@ void hashtable_init()
strcpy(hpp->name, "hpp"); strcpy(hpp->name, "hpp");
hpp->icon = L"󰰀"; hpp->icon = L"󰰀";
icon *md = memalloc(sizeof(icon)); icon *md = memalloc(sizeof(icon));
strcpy(md->name, "md"); strcpy(md->name, "md");
md->icon = L""; md->icon = L"";
@ -109,6 +108,14 @@ void hashtable_init()
strcpy(lua->name, "lua"); strcpy(lua->name, "lua");
lua->icon = L""; lua->icon = L"";
icon *license = memalloc(sizeof(icon));
strcpy(license->name, "LICENSE");
license->icon = L"";
icon *gitignore = memalloc(sizeof(icon));
strcpy(gitignore->name, "gitignore");
gitignore->icon = L"";
hashtable_add(c); hashtable_add(c);
hashtable_add(h); hashtable_add(h);
hashtable_add(cpp); hashtable_add(cpp);
@ -129,6 +136,8 @@ void hashtable_init()
hashtable_add(rb); hashtable_add(rb);
hashtable_add(iso); hashtable_add(iso);
hashtable_add(lua); hashtable_add(lua);
hashtable_add(license);
hashtable_add(gitignore);
} }
void hashtable_print() void hashtable_print()