Rework get_line and add marked file color

This commit is contained in:
Night Kaly 2024-11-17 22:24:31 +00:00
parent fe24d3ffbc
commit 548572536b
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
3 changed files with 23 additions and 23 deletions

18
ccc.c
View file

@ -624,7 +624,7 @@ void add_file_stat(char *filename, char *path, int ftype)
}
int type;
char *icon_str = memalloc(8);
char icon_str[5];
filename[strlen(filename)] = '\0';
/* handle file without extension
@ -639,14 +639,14 @@ void add_file_stat(char *filename, char *path, int ftype)
if (!ext_icon)
memcpy(icon_str, "", 4);
else
memcpy(icon_str, ext_icon->icon, 5);
memcpy(icon_str, ext_icon->icon, 4);
int color = DEF_COLOR;
if (S_ISDIR(file_stat.st_mode)) {
type = DRY; /* dir */
color = DIR_COLOR;
memcpy(icon_str, "󰉋", 5);
memcpy(icon_str, "󰉋", 4);
} else if (S_ISREG(file_stat.st_mode)) {
type = REG; /* regular file */
color = REG_COLOR;
@ -723,16 +723,16 @@ void add_file_stat(char *filename, char *path, int ftype)
mode_str[7] = (file_stat.st_mode & S_IROTH) ? 'r' : '-';
mode_str[8] = (file_stat.st_mode & S_IWOTH) ? 'w' : '-';
mode_str[9] = (file_stat.st_mode & S_IXOTH) ? 'x' : '-';
mode_str[10] = '\0';
mode_str[10] = 0;
if (mode_str[0] == '-' && (mode_str[3] == 'x' || mode_str[6] == 'x' || mode_str[9] == 'x')) {
color = EXE_COLOR;
}
/* mode_str(11) + time(17) + size_size + 2 spaces + 1 null */
size_t stat_size = 11 + time_size + size_size + 3;
/* mode_str + time(17) + size_size + 2 spaces + 1 null */
size_t stat_size = 11 + 17 + size_size + 3;
char *total_stat = memalloc(stat_size);
snprintf(total_stat, stat_size, "%s %s %-*s", mode_str, time, size_size, size);
sprintf(total_stat, "%s %s %-*s", mode_str, time, size_size, size);
/* DIR if color is blue */
if (color == 34)
@ -799,8 +799,8 @@ void list_files(void)
/* check is file marked for action */
int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
move_cursor(i + 1, 1);
if (is_marked) color = 32;
bprintf("\033[30m\033[%dm%s\033[0m\n",
if (is_marked) color = MAR_COLOR;
bprintf("\033[30m\033[%dm%s\033[m\n",
is_selected ? color + 10 : color, line);
free(line);

26
file.c
View file

@ -23,8 +23,6 @@ void arraylist_free(ArrayList *list)
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);
@ -72,7 +70,8 @@ void arraylist_remove(ArrayList *list, long index)
*/
void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int type, char *icon, int color, int marked, int force)
{
file new_file = { name, path, type, stats, icon, color };
file new_file = { name, path, type, stats, color };
strcpy(new_file.icon, icon);
if (list->capacity != list->length) {
if (marked) {
@ -106,26 +105,27 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, int typ
*/
char *get_line(ArrayList *list, long index, int detail, int icons)
{
file file = list->items[index];
file f = list->items[index];
size_t name_len = strlen(file.name);
size_t length;
size_t length = strlen(f.name) + 1;
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 */
length += strlen(f.stats) + 1; /* 1 for space */
}
if (icons) {
length += 5; /* 4 for icon, 1 for space */
}
char *line = memalloc(length);
line[0] = '\0';
if (detail) {
strcat(line, file.stats);
strcat(line, f.stats);
strcat(line, " ");
}
if (icons) {
strcat(line, file.icon);
strcat(line, f.icon);
strcat(line, " ");
}
strcat(line, file.name);
strcat(line, f.name);
line[length] = '\0';
return line;
}

2
file.h
View file

@ -18,8 +18,8 @@ typedef struct {
char *path; /* absolute path */
int type;
char *stats;
char *icon;
int color;
char icon[5];
} file;
typedef struct {