if file has more lines than preview window, rest of lines will not be printed

This commit is contained in:
Night Kaly 2024-03-15 14:02:52 +00:00
parent c1387bcee1
commit e7640b5768
No known key found for this signature in database
GPG key ID: 8E829D3381CFEBBE

30
ccc.c
View file

@ -440,6 +440,9 @@ long add_file_stat(char *filepath, int ftype)
if (ftype == 1) { if (ftype == 1) {
long index = add_marked(filepath, type); long index = add_marked(filepath, type);
if (index == -1) {
}
free(type); free(type);
free(size); free(size);
free(time); free(time);
@ -539,16 +542,30 @@ void show_file_content()
wclear(preview_content); wclear(preview_content);
file *current_file = get_file((long) current_selection); file *current_file = get_file((long) current_selection);
if (strncmp(current_file->type, "DIR", 3) == 0) return; if (strncmp(current_file->type, "DIR", 3) == 0) return;
FILE *file = fopen(current_file->path, "rb"); FILE *file = fopen(current_file->path, "r");
if (file) { if (file == NULL) {
mvwprintw(preview_content, 0, 0, "Unable to read %s", current_file->path);
return;
}
draw_border_title(preview_border, true); draw_border_title(preview_border, true);
int c;
/* check binary */
while ((c=fgetc(file)) != EOF) {
if (c == '\0') {
mvwprintw(preview_content, 0, 0, "binary");
return;
}
}
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
long length = ftell(file); long length = ftell(file);
/* check if file isn't empty */ /* check if file isn't empty */
if (length != 0 && length < 4096) { if (length != 0) {
fseek(file, 0, SEEK_SET); /* set cursor back to start of file */ fseek(file, 0, SEEK_SET); /* set cursor back to start of file */
char *buffer = memalloc(length * sizeof(char)); int max_length = (LINES - 3) * (COLS / 2 - 2);
fread(buffer, 1, length, file); char *buffer = memalloc(max_length * sizeof(char));
fread(buffer, 1, max_length, file);
mvwprintw(preview_content, 0, 0, "%s", buffer); mvwprintw(preview_content, 0, 0, "%s", buffer);
free(buffer); free(buffer);
} else { } else {
@ -556,7 +573,6 @@ void show_file_content()
} }
fclose(file); fclose(file);
} }
}
/* /*
* Opens $EDITOR to edit the file * Opens $EDITOR to edit the file
@ -611,7 +627,7 @@ void init_windows()
/* draw border around windows */ /* draw border around windows */
draw_border_title(directory_border, true); draw_border_title(directory_border, true);
draw_border_title(preview_border, false); draw_border_title(preview_border, true);
scrollok(directory_content, true); scrollok(directory_content, true);
/* window location y, x */ /* window location y, x */