Fix line wrapping and padding between dir and preview and fix config.h

This commit is contained in:
Night Kaly 2024-11-21 14:28:15 +00:00
parent 51e74eef22
commit 07aab26f1c
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
2 changed files with 25 additions and 18 deletions

41
ccc.c
View file

@ -829,17 +829,11 @@ void add_file_stat(char *filename, char *path, int ftype)
*/ */
void list_files(void) void list_files(void)
{ {
long overflow = 0;
if (sel_file > rows - 4) {
/* overflown */
overflow = sel_file - (rows - 4);
}
/* calculate range of files to show */ /* calculate range of files to show */
long range = files->length; long range = files->length;
/* not highlight if no files in directory */ /* not highlight if no files in directory */
if (range == 0 && errno == 0) { if (range == 0 && errno == 0) {
for (int i = 0; i < rows; i++) { for (int i = 0; i < rows - 1; i++) {
move_cursor(i + 1, 1); move_cursor(i + 1, 1);
bprintf("\033[K"); bprintf("\033[K");
} }
@ -848,16 +842,22 @@ void list_files(void)
return; return;
} }
if (range > rows - 3) { long overflow = 0;
if (sel_file > rows - 2) {
/* overflown */
overflow = sel_file - (rows - 2);
}
if (range > rows - 1) {
/* if there are more files than rows available to display /* if there are more files than rows available to display
* shrink range to avaiable rows to display with * shrink range to avaiable rows to display with
* overflow to keep the number of iterations to be constant */ * overflow to keep the number of iterations to be constant */
range = rows - 3 + overflow; range = rows - 1 + overflow;
} }
move_cursor(1, 1); move_cursor(1, 1);
bprintf("\033[2J"); bprintf("\033[2J");
int max_flen = 0;
for (long i = overflow; i < range; i++) { for (long i = overflow; i < range; i++) {
int is_selected = 0; int is_selected = 0;
if ((overflow == 0 && i == sel_file) || if ((overflow == 0 && i == sel_file) ||
@ -879,15 +879,20 @@ void list_files(void)
/* print the actual filename and stats */ /* print the actual filename and stats */
char *line = get_line(files, i, show_details, show_icons); char *line = get_line(files, i, show_details, show_icons);
int color = files->items[i].color; int color = files->items[i].color;
int width = strlen(line);
if (width > max_flen) {
max_flen = width;
}
/* check is file marked for action */ /* check is file marked for action */
int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1; int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
move_cursor(i + 1, 1); move_cursor(i - overflow + 1, 1);
if (is_marked) color = MAR_COLOR; if (is_marked) color = MAR_COLOR;
bprintf("\033[30m\033[%dm%s\033[m\n", bprintf("\033[30m\033[%dm%s\033[m\n",
is_selected ? color + 10 : color, line); is_selected ? color + 10 : color, line);
free(line); free(line);
} }
half_width = max_flen;
/* show file content every time cursor changes */ /* show file content every time cursor changes */
show_file_content(); show_file_content();
@ -904,7 +909,7 @@ void show_file_content(void)
if (current_file.type == DRY) { if (current_file.type == DRY) {
ArrayList *files_visit; ArrayList *files_visit;
populate_files(current_file.name, 0, &files_visit); populate_files(current_file.name, 0, &files_visit);
for (long i = 0; i < files_visit->length; i++) { for (long i = 0; i < files_visit->length && i < rows - 1; i++) {
char *line = get_line(files_visit, i, 0, show_icons); char *line = get_line(files_visit, i, 0, show_icons);
int color = files_visit->items[i].color; int color = files_visit->items[i].color;
move_cursor(i + 1, half_width); move_cursor(i + 1, half_width);
@ -935,7 +940,6 @@ void show_file_content(void)
int pid = fork(); int pid = fork();
if (pid == 0) { if (pid == 0) {
/* Child */ /* Child */
move_cursor(1, half_width);
close(pipe_fd[0]); close(pipe_fd[0]);
dup2(pipe_fd[1], STDOUT_FILENO); dup2(pipe_fd[1], STDOUT_FILENO);
dup2(pipe_fd[1], STDERR_FILENO); dup2(pipe_fd[1], STDERR_FILENO);
@ -953,16 +957,13 @@ void show_file_content(void)
size_t buflen = strlen(buffer); size_t buflen = strlen(buffer);
if (buffer[0] == '\0' || strspn(buffer, " \t") == buflen) { if (buffer[0] == '\0' || strspn(buffer, " \t") == buflen) {
move_cursor(row++, half_width); move_cursor(row++, half_width);
putchar('\n'); bprintf("\n");
continue; continue;
} }
move_cursor(row++, half_width); move_cursor(row++, half_width);
/* Visible length */ /* Visible length */
size_t len = 0; size_t len = 0;
for (size_t i = 0; i < buflen; i++) { for (size_t i = 0; i < buflen; i++) {
if ((len + 1) % (cols - half_width) == 0) {
move_cursor(row++, half_width);
}
if (buffer[i] == '\033' && buffer[i + 1] == '[') { if (buffer[i] == '\033' && buffer[i + 1] == '[') {
char *end = strpbrk(buffer + i, "ABCDEFGHJKfhlmus"); char *end = strpbrk(buffer + i, "ABCDEFGHJKfhlmus");
if (end) { if (end) {
@ -979,12 +980,18 @@ void show_file_content(void)
i += length - 1; i += length - 1;
continue; continue;
} }
} else {
bprintf("%c", buffer[i]);
} }
bprintf("%c", buffer[i]);
} else { } else {
bprintf("%c", buffer[i]); bprintf("%c", buffer[i]);
len++; len++;
} }
if (len && (len) % (cols - half_width + 1) == 0) {
if (row + 1 < rows) {
move_cursor(row++, half_width);
}
}
} }
} }
fclose(stream); fclose(stream);

View file

@ -52,7 +52,7 @@ static char last_d[PATH_MAX] = "~/.cache/ccc/.ccc_d";
/* Will create this directory if doesn't exist! */ /* Will create this directory if doesn't exist! */
static char trash_dir[PATH_MAX] = "~/.cache/ccc/trash/"; static char trash_dir[PATH_MAX] = "~/.cache/ccc/trash/";
key keybindings[] = { static Key keybindings[] = {
{'q', ACT_QUIT}, {'q', ACT_QUIT},
{'z', ACT_RELOAD}, {'z', ACT_RELOAD},
{BACKSPACE, ACT_BACK}, {BACKSPACE, ACT_BACK},