2024-03-09 18:37:04 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <linux/limits.h>
|
2024-03-09 19:24:38 +01:00
|
|
|
#include <dirent.h> /* directories etc. */
|
2024-03-11 22:54:14 +01:00
|
|
|
#include <sys/stat.h>
|
2024-03-13 20:37:26 +01:00
|
|
|
#include <errno.h>
|
2024-03-11 22:54:14 +01:00
|
|
|
#include <time.h>
|
2024-03-09 18:37:04 +01:00
|
|
|
#include <ncurses.h>
|
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
#include "file.h"
|
2024-03-09 18:37:04 +01:00
|
|
|
#include "util.h"
|
2024-03-13 20:19:22 +01:00
|
|
|
#include "config.h"
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-09 18:37:04 +01:00
|
|
|
typedef struct {
|
|
|
|
WINDOW *window;
|
|
|
|
int location;
|
|
|
|
int y;
|
|
|
|
int x;
|
|
|
|
} WIN_STRUCT;
|
|
|
|
|
2024-03-09 19:24:38 +01:00
|
|
|
/* functions' definitions */
|
2024-03-12 21:59:36 +01:00
|
|
|
void list_files(const char *path);
|
2024-03-13 11:18:35 +01:00
|
|
|
long long get_directory_size(const char *path);
|
|
|
|
long add_file_stat(char *filename);
|
2024-03-09 18:37:04 +01:00
|
|
|
void highlight_current_line();
|
|
|
|
void show_file_content();
|
2024-03-11 16:57:41 +01:00
|
|
|
void edit_file();
|
2024-03-09 18:37:04 +01:00
|
|
|
void init_windows();
|
|
|
|
void draw_border_title(WINDOW *window, bool active);
|
|
|
|
|
2024-03-09 19:24:38 +01:00
|
|
|
/* global variables */
|
2024-03-09 18:37:04 +01:00
|
|
|
unsigned int focus = 0;
|
2024-03-10 21:23:15 +01:00
|
|
|
long current_selection = 0;
|
2024-03-12 21:59:36 +01:00
|
|
|
char *cwd;
|
2024-03-09 19:39:29 +01:00
|
|
|
int half_width;
|
2024-03-12 21:59:36 +01:00
|
|
|
WIN_STRUCT windows[5];
|
|
|
|
WINDOW *directory_border;
|
|
|
|
WINDOW *directory_content;
|
|
|
|
WINDOW *preview_border;
|
|
|
|
WINDOW *preview_content;
|
|
|
|
WINDOW *panel;
|
2024-03-09 18:37:04 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2024-03-10 22:38:26 +01:00
|
|
|
if (argc > 1 && strcmp(argv[1], "-h") == 0)
|
2024-03-10 21:23:15 +01:00
|
|
|
die("Usage: ccc filename");
|
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
/* check if it is interactive shell */
|
2024-03-09 18:37:04 +01:00
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
die("ccc: No tty detected. ccc requires an interactive shell to run.\n");
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* initialize screen, don't print special chars,
|
|
|
|
* make ctrl + c work, don't show cursor */
|
2024-03-09 18:37:04 +01:00
|
|
|
initscr();
|
|
|
|
noecho();
|
2024-03-10 21:23:15 +01:00
|
|
|
cbreak();
|
2024-03-09 18:37:04 +01:00
|
|
|
curs_set(0);
|
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
/* check terminal has colors */
|
|
|
|
if (!has_colors()) {
|
2024-03-09 18:37:04 +01:00
|
|
|
endwin();
|
|
|
|
die("ccc: Color is not supported in your terminal.\n");
|
2024-03-10 11:54:08 +01:00
|
|
|
} else {
|
2024-03-13 11:18:35 +01:00
|
|
|
use_default_colors();
|
2024-03-09 19:24:38 +01:00
|
|
|
start_color();
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
init_pair(1, COLOR_BLUE, -1); /* foreground, background */
|
|
|
|
init_pair(2, COLOR_CYAN, -1); /* active color */
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-09 19:39:29 +01:00
|
|
|
half_width = COLS / 2;
|
2024-03-09 18:37:04 +01:00
|
|
|
init_windows();
|
2024-03-12 21:59:36 +01:00
|
|
|
refresh();
|
|
|
|
|
|
|
|
cwd = memalloc(PATH_MAX * sizeof(char));
|
|
|
|
getcwd(cwd, PATH_MAX);
|
|
|
|
|
|
|
|
list_files(cwd);
|
2024-03-09 18:37:04 +01:00
|
|
|
highlight_current_line();
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
/* set window name */
|
|
|
|
printf("%c]2;ccc: %s%c", ESC, cwd, ESC);
|
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
int ch, second;
|
2024-03-10 11:54:08 +01:00
|
|
|
while (1) {
|
|
|
|
if (COLS < 80 || LINES < 24) {
|
2024-03-09 18:37:04 +01:00
|
|
|
endwin();
|
|
|
|
die("ccc: Terminal size needs to be at least 80x24\n");
|
|
|
|
}
|
|
|
|
ch = getch();
|
2024-03-10 11:54:08 +01:00
|
|
|
switch (ch) {
|
2024-03-13 20:19:22 +01:00
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
/* quit */
|
2024-03-10 21:23:15 +01:00
|
|
|
case 'q':
|
|
|
|
endwin();
|
|
|
|
return 0;
|
2024-03-12 21:59:36 +01:00
|
|
|
|
|
|
|
/* reload */
|
2024-03-09 18:37:04 +01:00
|
|
|
case '.':
|
2024-03-12 21:59:36 +01:00
|
|
|
clear_files();
|
|
|
|
list_files(cwd);
|
|
|
|
current_selection = 0;
|
|
|
|
highlight_current_line();
|
2024-03-09 18:37:04 +01:00
|
|
|
break;
|
2024-03-12 21:59:36 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* go back */
|
2024-03-09 18:37:04 +01:00
|
|
|
case 'h':
|
2024-03-12 21:59:36 +01:00
|
|
|
clear_files();
|
|
|
|
/* get parent directory */
|
|
|
|
char *last_slash = strrchr(cwd, '/');
|
|
|
|
if (last_slash != NULL) {
|
|
|
|
*last_slash = '\0';
|
|
|
|
list_files(cwd);
|
|
|
|
current_selection = 0;
|
|
|
|
highlight_current_line();
|
|
|
|
}
|
2024-03-10 21:23:15 +01:00
|
|
|
break;
|
2024-03-12 21:59:36 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* enter directory/open a file */
|
2024-03-12 21:59:36 +01:00
|
|
|
case 'l':;
|
2024-03-13 11:18:35 +01:00
|
|
|
file *file = get_file(current_selection);
|
|
|
|
if (file != NULL) {
|
2024-03-12 21:59:36 +01:00
|
|
|
/* check if it is directory or regular file */
|
2024-03-13 11:18:35 +01:00
|
|
|
if (strncmp(file->type, "DIR", 3) == 0) {
|
2024-03-12 21:59:36 +01:00
|
|
|
/* change cwd to directory */
|
2024-03-13 11:18:35 +01:00
|
|
|
strcpy(cwd, file->path);
|
2024-03-12 21:59:36 +01:00
|
|
|
clear_files();
|
2024-03-13 11:18:35 +01:00
|
|
|
list_files(cwd);
|
2024-03-12 21:59:36 +01:00
|
|
|
current_selection = 0;
|
|
|
|
highlight_current_line();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/*
|
2024-03-09 18:37:04 +01:00
|
|
|
if (focus == 0) focus++;
|
|
|
|
else if (focus == 1) focus--;
|
|
|
|
break;
|
2024-03-12 21:59:36 +01:00
|
|
|
*/
|
2024-03-12 22:01:11 +01:00
|
|
|
|
2024-03-13 20:46:25 +01:00
|
|
|
/* jump up (ctrl u)*/
|
2024-03-11 21:24:54 +01:00
|
|
|
case '\x15':
|
|
|
|
if ((current_selection - JUMP_NUM) > 0)
|
|
|
|
current_selection -= JUMP_NUM;
|
|
|
|
else
|
|
|
|
current_selection = 0;
|
|
|
|
|
|
|
|
highlight_current_line();
|
|
|
|
break;
|
2024-03-12 22:01:11 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* go up */
|
2024-03-09 18:37:04 +01:00
|
|
|
case 'k':
|
2024-03-10 21:23:15 +01:00
|
|
|
if (current_selection > 0)
|
2024-03-09 18:37:04 +01:00
|
|
|
current_selection--;
|
2024-03-10 21:23:15 +01:00
|
|
|
|
2024-03-11 21:24:54 +01:00
|
|
|
highlight_current_line();
|
|
|
|
break;
|
2024-03-13 16:51:52 +01:00
|
|
|
|
2024-03-13 20:46:25 +01:00
|
|
|
/* jump down (ctrl d)*/
|
2024-03-11 21:24:54 +01:00
|
|
|
case '\x04':
|
|
|
|
if ((current_selection + JUMP_NUM) < (files_len() - 1))
|
|
|
|
current_selection += JUMP_NUM;
|
|
|
|
else
|
|
|
|
current_selection = (files_len() - 1);
|
|
|
|
|
2024-03-09 18:37:04 +01:00
|
|
|
highlight_current_line();
|
|
|
|
break;
|
2024-03-13 16:51:52 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* go down */
|
2024-03-09 18:37:04 +01:00
|
|
|
case 'j':
|
2024-03-11 21:24:54 +01:00
|
|
|
if (current_selection < (files_len() - 1))
|
2024-03-09 18:37:04 +01:00
|
|
|
current_selection++;
|
2024-03-10 21:23:15 +01:00
|
|
|
|
2024-03-09 18:37:04 +01:00
|
|
|
highlight_current_line();
|
|
|
|
break;
|
2024-03-13 16:51:52 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* jump to the bottom */
|
2024-03-09 20:52:22 +01:00
|
|
|
case 'G':
|
2024-03-11 21:24:54 +01:00
|
|
|
current_selection = (files_len() - 1);
|
2024-03-09 20:52:22 +01:00
|
|
|
highlight_current_line();
|
|
|
|
break;
|
2024-03-13 16:51:52 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* jump to the top */
|
|
|
|
case 'g':
|
|
|
|
second = getch();
|
|
|
|
switch (second) {
|
|
|
|
case 'g':
|
|
|
|
current_selection = 0;
|
|
|
|
highlight_current_line();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2024-03-11 16:57:41 +01:00
|
|
|
case 'e':
|
|
|
|
edit_file();
|
|
|
|
break;
|
2024-03-10 11:54:08 +01:00
|
|
|
case 27: /* esc */
|
2024-03-09 18:37:04 +01:00
|
|
|
break;
|
|
|
|
case KEY_RESIZE:
|
2024-03-10 11:54:08 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2024-03-09 18:37:04 +01:00
|
|
|
delwin(windows[i].window);
|
|
|
|
}
|
2024-03-13 11:18:35 +01:00
|
|
|
endwin();
|
2024-03-09 18:37:04 +01:00
|
|
|
init_windows();
|
|
|
|
break;
|
2024-03-10 21:23:15 +01:00
|
|
|
default:
|
|
|
|
break;
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-13 11:18:35 +01:00
|
|
|
clear_files();
|
|
|
|
clear_marked();
|
2024-03-10 21:23:15 +01:00
|
|
|
endwin();
|
2024-03-09 18:37:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
/*
|
2024-03-10 21:23:15 +01:00
|
|
|
* Read the provided directory and list all files to window 0
|
|
|
|
* ep->d_name -> filename
|
2024-03-09 20:14:26 +01:00
|
|
|
*/
|
2024-03-12 21:59:36 +01:00
|
|
|
void list_files(const char *path)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *ep;
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
draw_border_title(directory_border, true);
|
2024-03-13 11:18:35 +01:00
|
|
|
if ((dp = opendir(path)) != NULL) {
|
2024-03-09 18:37:04 +01:00
|
|
|
int count = 0;
|
2024-03-12 21:59:36 +01:00
|
|
|
/* clear directory window to ready for printing */
|
|
|
|
wclear(directory_content);
|
2024-03-10 21:23:15 +01:00
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
2024-03-12 21:59:36 +01:00
|
|
|
char *filename = memalloc(PATH_MAX * sizeof(char));
|
|
|
|
/* make filename be basename of selected item just to pass check */
|
|
|
|
filename[0] = '\0';
|
|
|
|
strcat(filename, ep->d_name);
|
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* can't be strncmp as that would filter out the dotfiles */
|
2024-03-10 11:54:08 +01:00
|
|
|
if (strcmp(filename, ".") && strcmp(filename, "..")) {
|
2024-03-12 21:59:36 +01:00
|
|
|
/* construct full file path */
|
|
|
|
filename[0] = '\0';
|
|
|
|
strcat(filename, cwd);
|
|
|
|
strcat(filename, "/");
|
|
|
|
strcat(filename, ep->d_name); /* add file name */
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
long index = add_file_stat(filename);
|
2024-03-11 22:54:14 +01:00
|
|
|
char *line = get_line(index);
|
2024-03-12 21:59:36 +01:00
|
|
|
|
|
|
|
mvwprintw(directory_content, count, 0, "%s", line);
|
2024-03-11 22:54:14 +01:00
|
|
|
free(line);
|
2024-03-09 18:37:04 +01:00
|
|
|
count++;
|
|
|
|
}
|
2024-03-11 22:54:14 +01:00
|
|
|
free(filename);
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
closedir(dp);
|
2024-03-12 21:59:36 +01:00
|
|
|
wrefresh(directory_content);
|
2024-03-10 11:54:08 +01:00
|
|
|
} else {
|
2024-03-09 18:37:04 +01:00
|
|
|
perror("ccc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
long long get_directory_size(const char *path)
|
|
|
|
{
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *ep;
|
|
|
|
struct stat statbuf;
|
|
|
|
long long total_size = 0;
|
|
|
|
|
|
|
|
if ((dp = opendir(path)) != NULL) {
|
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
|
|
|
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-13 19:40:39 +01:00
|
|
|
/* build full path of entry */
|
2024-03-13 16:51:52 +01:00
|
|
|
char full_path[PATH_MAX];
|
2024-03-13 11:18:35 +01:00
|
|
|
snprintf(full_path, sizeof(full_path), "%s/%s", path, ep->d_name);
|
|
|
|
|
|
|
|
if (lstat(full_path, &statbuf) == -1) {
|
|
|
|
perror("lstat");
|
|
|
|
closedir(dp);
|
|
|
|
return -1;
|
|
|
|
}
|
2024-03-13 19:40:39 +01:00
|
|
|
/* recursively calculate its size if it is directory */
|
2024-03-13 11:18:35 +01:00
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
total_size += get_directory_size(full_path);
|
|
|
|
} else {
|
2024-03-13 19:40:39 +01:00
|
|
|
/* else add the size of the file to the total */
|
2024-03-13 11:18:35 +01:00
|
|
|
total_size += statbuf.st_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
} else {
|
|
|
|
perror("ccc");
|
2024-03-13 16:51:52 +01:00
|
|
|
return -1;
|
2024-03-13 11:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
2024-03-11 22:54:14 +01:00
|
|
|
/*
|
2024-03-13 11:18:35 +01:00
|
|
|
* Get file's last modified time, size, type
|
|
|
|
* Add that file into list
|
2024-03-11 22:54:14 +01:00
|
|
|
*/
|
2024-03-13 11:18:35 +01:00
|
|
|
long add_file_stat(char *filepath)
|
2024-03-12 21:59:36 +01:00
|
|
|
{
|
2024-03-11 22:54:14 +01:00
|
|
|
struct stat file_stat;
|
2024-03-13 20:37:26 +01:00
|
|
|
if (stat(filepath, &file_stat) == -1) {
|
|
|
|
/* can't be triggered ? */
|
|
|
|
if (errno == EACCES) {
|
|
|
|
return add_file(filepath, "", "");
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 22:54:14 +01:00
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
/* get last modified time */
|
2024-03-11 22:54:14 +01:00
|
|
|
char *time = memalloc(20 * sizeof(char));
|
2024-03-12 21:59:36 +01:00
|
|
|
/* format last modified time to a string */
|
|
|
|
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
|
2024-03-11 22:54:14 +01:00
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
|
|
|
|
/* get file size */
|
2024-03-11 22:54:14 +01:00
|
|
|
double bytes = file_stat.st_size;
|
2024-03-13 11:18:35 +01:00
|
|
|
if (S_ISDIR(file_stat.st_mode)) {
|
|
|
|
bytes = (double) get_directory_size(filepath);
|
|
|
|
}
|
|
|
|
/* max 25 chars due to long, space, suffix and nul */
|
|
|
|
char *size = memalloc(25 * sizeof(char));
|
2024-03-11 22:54:14 +01:00
|
|
|
int unit = 0;
|
|
|
|
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
|
|
|
|
while (bytes > 1024) {
|
|
|
|
bytes /= 1024;
|
|
|
|
unit++;
|
|
|
|
}
|
|
|
|
sprintf(size, "%.*f%s", unit, bytes, units[unit]);
|
|
|
|
|
2024-03-13 11:18:35 +01:00
|
|
|
/* get file type */
|
|
|
|
char *type = memalloc(4 * sizeof(char)); /* 3 chars for type */
|
|
|
|
if (S_ISDIR(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "DIR"); /* directory */
|
|
|
|
} else if (S_ISREG(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "REG"); /* regular file */
|
|
|
|
} else if (S_ISLNK(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "LNK"); /* symbolic link */
|
2024-03-13 19:28:04 +01:00
|
|
|
} else if (S_ISCHR(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "CHR"); /* character device */
|
|
|
|
} else if (S_ISSOCK(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "SOC"); /* socket */
|
|
|
|
} else if (S_ISBLK(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "BLK"); /* block device */
|
|
|
|
} else if (S_ISFIFO(file_stat.st_mode)) {
|
|
|
|
strcpy(type, "FIF"); /* FIFO */
|
|
|
|
}
|
|
|
|
/* don't know how to handle socket, block device, character device and FIFO */
|
2024-03-13 11:18:35 +01:00
|
|
|
|
2024-03-11 22:54:14 +01:00
|
|
|
char *total_stat = memalloc(45 * sizeof(char));
|
|
|
|
snprintf(total_stat, 45, "%-18s %-10s", time, size);
|
2024-03-12 21:59:36 +01:00
|
|
|
total_stat[strlen(total_stat)] = '\0';
|
2024-03-11 22:54:14 +01:00
|
|
|
|
|
|
|
free(time);
|
|
|
|
free(size);
|
2024-03-13 11:18:35 +01:00
|
|
|
|
|
|
|
long index = add_file(filepath, total_stat, type);
|
|
|
|
|
|
|
|
free(total_stat);
|
|
|
|
free(type);
|
|
|
|
return index;
|
2024-03-11 22:54:14 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
/*
|
2024-03-10 21:23:15 +01:00
|
|
|
* Highlight current line by reversing the color
|
2024-03-09 20:14:26 +01:00
|
|
|
*/
|
2024-03-09 18:37:04 +01:00
|
|
|
void highlight_current_line()
|
|
|
|
{
|
2024-03-13 16:51:52 +01:00
|
|
|
long overflow = 0;
|
|
|
|
if (current_selection > LINES - 4) {
|
|
|
|
/* overflown */
|
|
|
|
overflow = current_selection - (LINES - 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate range of files to show */
|
|
|
|
long range = files_len();
|
|
|
|
if (range > LINES - 3) {
|
|
|
|
/* if there is more files than lines available to display*/
|
|
|
|
/* shrink range to avaiable lines to display */
|
|
|
|
/* with overflow to keep number of iterations to be constant */
|
|
|
|
range = LINES - 3 + overflow;
|
|
|
|
}
|
|
|
|
|
|
|
|
wclear(directory_content);
|
|
|
|
long line_count = 0;
|
|
|
|
for (long i = overflow; i < range; i++) {
|
|
|
|
if ((overflow == 0 && i == current_selection) || (overflow != 0 && i == current_selection)) {
|
2024-03-12 21:59:36 +01:00
|
|
|
wattron(directory_content, A_REVERSE);
|
|
|
|
wattron(directory_content, COLOR_PAIR(1));
|
2024-03-10 21:23:15 +01:00
|
|
|
|
|
|
|
/* update the panel */
|
2024-03-12 21:59:36 +01:00
|
|
|
wclear(panel);
|
2024-03-13 16:51:52 +01:00
|
|
|
wprintw(panel, "(%ld/%ld) %s", current_selection + 1, files_len(), cwd);
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
2024-03-11 22:54:14 +01:00
|
|
|
/* print the actual filename and stats */
|
|
|
|
char *line = get_line(i);
|
2024-03-13 16:51:52 +01:00
|
|
|
if (overflow > 0)
|
|
|
|
mvwprintw(directory_content, line_count, 0, "%s", line);
|
|
|
|
else
|
|
|
|
mvwprintw(directory_content, i, 0, "%s", line);
|
2024-03-12 21:59:36 +01:00
|
|
|
wattroff(directory_content, A_REVERSE);
|
|
|
|
wattroff(directory_content, COLOR_PAIR(1));
|
2024-03-11 22:54:14 +01:00
|
|
|
free(line);
|
2024-03-13 16:51:52 +01:00
|
|
|
line_count++;
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
wrefresh(directory_content);
|
|
|
|
wrefresh(panel);
|
2024-03-10 21:23:15 +01:00
|
|
|
/* show file content every time cursor changes */
|
|
|
|
show_file_content();
|
2024-03-13 20:37:26 +01:00
|
|
|
wrefresh(preview_content);
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
/*
|
2024-03-10 21:23:15 +01:00
|
|
|
* Get file content into buffer and show it to preview window
|
2024-03-09 20:14:26 +01:00
|
|
|
*/
|
2024-03-09 18:37:04 +01:00
|
|
|
void show_file_content()
|
|
|
|
{
|
2024-03-13 20:37:26 +01:00
|
|
|
wclear(preview_content);
|
|
|
|
file *current_file = get_file((long) current_selection);
|
|
|
|
if (strncmp(current_file->type, "DIR", 3) == 0) return;
|
|
|
|
FILE *file = fopen(current_file->path, "rb");
|
2024-03-10 11:54:08 +01:00
|
|
|
if (file) {
|
2024-03-12 21:59:36 +01:00
|
|
|
draw_border_title(preview_border, true);
|
2024-03-09 18:37:04 +01:00
|
|
|
fseek(file, 0, SEEK_END);
|
2024-03-10 22:31:33 +01:00
|
|
|
long length = ftell(file);
|
2024-03-11 16:57:41 +01:00
|
|
|
/* check if file isn't empty */
|
|
|
|
if (length != 0 && length < 4096) {
|
|
|
|
fseek(file, 0, SEEK_SET); /* set cursor back to start of file */
|
2024-03-10 21:23:15 +01:00
|
|
|
char *buffer = memalloc(length * sizeof(char));
|
|
|
|
fread(buffer, 1, length, file);
|
2024-03-12 21:59:36 +01:00
|
|
|
mvwprintw(preview_content, 0, 0, "%s", buffer);
|
2024-03-11 22:54:14 +01:00
|
|
|
free(buffer);
|
2024-03-10 21:23:15 +01:00
|
|
|
} else {
|
2024-03-12 21:59:36 +01:00
|
|
|
wclear(preview_content);
|
2024-03-10 21:23:15 +01:00
|
|
|
}
|
2024-03-09 18:37:04 +01:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 16:57:41 +01:00
|
|
|
/*
|
|
|
|
* Opens $EDITOR to edit the file
|
|
|
|
*/
|
2024-03-12 21:59:36 +01:00
|
|
|
void edit_file()
|
|
|
|
{
|
2024-03-11 16:57:41 +01:00
|
|
|
char *editor = getenv("EDITOR");
|
|
|
|
if (editor == NULL) {
|
2024-03-12 21:59:36 +01:00
|
|
|
wclear(panel);
|
|
|
|
wprintw(panel, "Cannot get EDITOR variable, is it defined?");
|
|
|
|
wrefresh(panel);
|
2024-03-11 16:57:41 +01:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
def_prog_mode(); /* save the tty modes */
|
|
|
|
endwin(); /* end curses mode temporarily */
|
2024-03-12 21:59:36 +01:00
|
|
|
char *filename = get_filepath(current_selection);
|
2024-03-11 16:57:41 +01:00
|
|
|
int length = strlen(editor) + strlen(filename) + 2; /* one for space one for nul */
|
|
|
|
char command[length];
|
|
|
|
snprintf(command, length, "%s %s", editor, filename);
|
|
|
|
system(command);
|
|
|
|
reset_prog_mode(); /* return to previous tty mode */
|
|
|
|
refresh(); /* store the screen contents */
|
2024-03-11 22:54:14 +01:00
|
|
|
free(filename);
|
2024-03-11 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 18:37:04 +01:00
|
|
|
void init_windows()
|
|
|
|
{
|
2024-03-11 16:57:41 +01:00
|
|
|
/*------------------------------+
|
2024-03-12 21:59:36 +01:00
|
|
|
|----border(0)--||--border(2)--||
|
|
|
|
|| || ||
|
|
|
|
|| content (1) || content (3) ||
|
|
|
|
|| || ||
|
|
|
|
|| || ||
|
|
|
|
|---------------||-------------||
|
|
|
|
+==========panel (4)===========*/
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-12 21:59:36 +01:00
|
|
|
/* lines, cols, y, x */
|
|
|
|
directory_border = newwin(LINES - PH, half_width, 0, 0 );
|
|
|
|
directory_content = newwin(LINES - PH -2, half_width - 2, 1, 1);
|
|
|
|
preview_border = newwin(LINES - PH, half_width, 0, half_width );
|
|
|
|
preview_content = newwin(LINES - PH - 2, half_width - 2, 1, half_width + 1);
|
|
|
|
panel = newwin(PH, COLS, LINES - PH, 0 );
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* draw border around windows */
|
2024-03-12 21:59:36 +01:00
|
|
|
draw_border_title(directory_border, true);
|
|
|
|
draw_border_title(preview_border, false);
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-13 16:51:52 +01:00
|
|
|
scrollok(directory_content, true);
|
2024-03-11 16:57:41 +01:00
|
|
|
/* window location y, x */
|
2024-03-12 21:59:36 +01:00
|
|
|
windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
|
|
|
windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
|
|
|
|
windows[2] = (WIN_STRUCT) { preview_border, 1, 0, half_width };
|
|
|
|
windows[3] = (WIN_STRUCT) { preview_content, 1, 0, half_width };
|
|
|
|
windows[4] = (WIN_STRUCT) { panel, 2, LINES - PH, 0 };
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-03-10 21:23:15 +01:00
|
|
|
* Draw the border of the window depending if it's active or not
|
2024-03-09 18:37:04 +01:00
|
|
|
*/
|
|
|
|
void draw_border_title(WINDOW *window, bool active)
|
|
|
|
{
|
2024-03-10 11:54:08 +01:00
|
|
|
/* turn on color depends on active */
|
2024-03-09 18:37:04 +01:00
|
|
|
if (active) {
|
|
|
|
wattron(window, COLOR_PAIR(2));
|
|
|
|
} else {
|
|
|
|
wattron(window, COLOR_PAIR(1));
|
|
|
|
}
|
2024-03-10 11:54:08 +01:00
|
|
|
/* draw top border */
|
|
|
|
mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */
|
|
|
|
mvwhline(window, 0, 1, ACS_HLINE, half_width - 2); /* top horizontal line */
|
|
|
|
mvwaddch(window, 0, half_width - 1, ACS_URCORNER); /* upper right corner */
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
/* draw side border */
|
|
|
|
mvwvline(window, 1, 0, ACS_VLINE, LINES - 2); /* left vertical line */
|
|
|
|
mvwvline(window, 1, half_width - 1, ACS_VLINE, LINES - 2); /* right vertical line */
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* draw bottom border
|
|
|
|
* make space for the panel */
|
|
|
|
mvwaddch(window, LINES - PH - 1, 0, ACS_LLCORNER); /* lower left corner */
|
|
|
|
mvwhline(window, LINES - PH - 1, 1, ACS_HLINE, half_width - 2); /* bottom horizontal line */
|
|
|
|
mvwaddch(window, LINES - PH - 1, half_width - 1, ACS_LRCORNER); /* lower right corner */
|
2024-03-09 20:14:26 +01:00
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
/* turn color off after turning it on */
|
2024-03-09 18:37:04 +01:00
|
|
|
if (active) {
|
|
|
|
wattroff(window, COLOR_PAIR(2));
|
|
|
|
} else {
|
|
|
|
wattroff(window, COLOR_PAIR(1));
|
|
|
|
}
|
2024-03-10 11:54:08 +01:00
|
|
|
wrefresh(window); /* Refresh the window to see the colored border and title */
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|