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-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-09 19:39:29 +01:00
|
|
|
#define ESC 0x1B /* \e or \033 */
|
2024-03-10 21:23:15 +01:00
|
|
|
#define PH 1 /* panel height */
|
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-10 21:23:15 +01:00
|
|
|
void list_files(char *path);
|
2024-03-09 18:37:04 +01:00
|
|
|
void highlight_current_line();
|
|
|
|
void show_file_content();
|
|
|
|
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-09 19:39:29 +01:00
|
|
|
int half_width;
|
2024-03-10 21:23:15 +01:00
|
|
|
WIN_STRUCT windows[3];
|
2024-03-09 18:37:04 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2024-03-09 19:39:29 +01:00
|
|
|
char cwd[PATH_MAX];
|
|
|
|
|
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-09 19:39:29 +01:00
|
|
|
/* set window name */
|
|
|
|
printf("%c]2;ccc: %s%c", ESC, getcwd(cwd, sizeof(cwd)), ESC);
|
|
|
|
|
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-09 19:24:38 +01:00
|
|
|
start_color();
|
|
|
|
}
|
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */
|
|
|
|
init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-09 18:37:04 +01:00
|
|
|
refresh();
|
2024-03-09 19:39:29 +01:00
|
|
|
half_width = COLS / 2;
|
2024-03-09 18:37:04 +01:00
|
|
|
init_windows();
|
2024-03-10 21:23:15 +01:00
|
|
|
list_files(getcwd(cwd, sizeof(cwd)));
|
2024-03-09 18:37:04 +01:00
|
|
|
highlight_current_line();
|
2024-03-09 19:24:38 +01:00
|
|
|
|
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-10 21:23:15 +01:00
|
|
|
case 'q':
|
|
|
|
endwin();
|
|
|
|
return 0;
|
2024-03-09 18:37:04 +01:00
|
|
|
case '.':
|
2024-03-10 21:23:15 +01:00
|
|
|
list_files(getcwd(cwd, sizeof(cwd)));
|
2024-03-09 18:37:04 +01:00
|
|
|
break;
|
2024-03-10 21:23:15 +01:00
|
|
|
/* go back */
|
2024-03-09 18:37:04 +01:00
|
|
|
case 'h':
|
2024-03-10 21:23:15 +01:00
|
|
|
break;
|
|
|
|
/* enter directory/open a file */
|
2024-03-09 18:37:04 +01:00
|
|
|
case 'l':
|
|
|
|
if (focus == 0) focus++;
|
|
|
|
else if (focus == 1) focus--;
|
|
|
|
break;
|
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-09 18:37:04 +01:00
|
|
|
highlight_current_line();
|
|
|
|
break;
|
2024-03-10 21:23:15 +01:00
|
|
|
/* go down */
|
2024-03-09 18:37:04 +01:00
|
|
|
case 'j':
|
2024-03-10 21:23:15 +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-10 21:23:15 +01:00
|
|
|
/* jump to the bottom */
|
2024-03-09 20:52:22 +01:00
|
|
|
case 'G':
|
|
|
|
current_selection = files_len() - 1;
|
|
|
|
highlight_current_line();
|
|
|
|
break;
|
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-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);
|
|
|
|
}
|
|
|
|
init_windows();
|
|
|
|
break;
|
2024-03-10 21:23:15 +01:00
|
|
|
default:
|
|
|
|
break;
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
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-10 21:23:15 +01:00
|
|
|
void list_files(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-10 21:23:15 +01:00
|
|
|
dp = opendir(path);
|
2024-03-10 11:54:08 +01:00
|
|
|
if (dp != NULL) {
|
2024-03-09 18:37:04 +01:00
|
|
|
int count = 0;
|
2024-03-10 21:23:15 +01:00
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
while ((ep = readdir(dp)) != NULL) {
|
2024-03-09 18:37:04 +01:00
|
|
|
char *filename = strdup(ep->d_name);
|
2024-03-10 11:54:08 +01:00
|
|
|
if (filename == NULL) {
|
|
|
|
/* memory allocation failed */
|
2024-03-09 18:37:04 +01:00
|
|
|
perror("ccc");
|
2024-03-09 20:18:19 +01:00
|
|
|
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name);
|
2024-03-09 19:39:29 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
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-09 20:14:26 +01:00
|
|
|
add_file(filename);
|
2024-03-09 18:37:04 +01:00
|
|
|
mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
wrefresh(windows[0].window);
|
2024-03-10 11:54:08 +01:00
|
|
|
} else {
|
2024-03-09 18:37:04 +01:00
|
|
|
perror("ccc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10 21:23:15 +01:00
|
|
|
char cwd[PATH_MAX];
|
|
|
|
char *filename;
|
|
|
|
|
2024-03-10 11:54:08 +01:00
|
|
|
for (long i = 0; i < files_len(); i++) {
|
|
|
|
if (i == current_selection) {
|
2024-03-09 18:37:04 +01:00
|
|
|
wattron(windows[0].window, A_REVERSE);
|
|
|
|
wattron(windows[0].window, COLOR_PAIR(1));
|
2024-03-10 21:23:15 +01:00
|
|
|
|
|
|
|
/* update the panel */
|
|
|
|
wclear(windows[2].window);
|
|
|
|
wprintw(windows[2].window, "(%ld/%ld) %s", i + 1, files_len(),
|
|
|
|
getcwd(cwd, sizeof(cwd)));
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
2024-03-10 21:23:15 +01:00
|
|
|
/* print the actual filename */
|
|
|
|
filename = get_filename(i);
|
|
|
|
mvwprintw(windows[0].window, i + 1, 1, "%s", filename);
|
2024-03-09 18:37:04 +01:00
|
|
|
wattroff(windows[0].window, A_REVERSE);
|
|
|
|
wattroff(windows[0].window, COLOR_PAIR(1));
|
|
|
|
}
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
wrefresh(windows[0].window);
|
|
|
|
wrefresh(windows[2].window);
|
|
|
|
/* show file content every time cursor changes */
|
|
|
|
show_file_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-09 20:46:15 +01:00
|
|
|
FILE *file = fopen(get_filename((long) current_selection), "rb");
|
2024-03-10 11:54:08 +01:00
|
|
|
if (file) {
|
2024-03-09 20:52:22 +01:00
|
|
|
wclear(windows[1].window);
|
2024-03-09 20:55:32 +01:00
|
|
|
draw_border_title(windows[1].window, true);
|
2024-03-10 21:23:15 +01:00
|
|
|
|
2024-03-09 18:37:04 +01:00
|
|
|
fseek(file, 0, SEEK_END);
|
2024-03-10 21:23:15 +01:00
|
|
|
/* check if file isn't empty */
|
2024-03-10 22:31:33 +01:00
|
|
|
long length = ftell(file);
|
|
|
|
if (length != 0) {
|
2024-03-10 21:23:15 +01:00
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
|
|
|
char *buffer = memalloc(length * sizeof(char));
|
|
|
|
fread(buffer, 1, length, file);
|
|
|
|
|
|
|
|
mvwprintw(windows[1].window, 1, 1, "%s", buffer);
|
|
|
|
wrefresh(windows[1].window);
|
|
|
|
} else {
|
|
|
|
wclear(windows[1].window);
|
|
|
|
}
|
2024-03-09 18:37:04 +01:00
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_windows()
|
|
|
|
{
|
2024-03-10 21:23:15 +01:00
|
|
|
/*-----------------------------+
|
|
|
|
| | |
|
|
|
|
| | |
|
|
|
|
| directory (0) | preview (1) |
|
|
|
|
| | |
|
|
|
|
| | |
|
|
|
|
| | |
|
|
|
|
+==========panel (2)==========*/
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* lines, cols, y, x */
|
|
|
|
WINDOW *directory = newwin(LINES, half_width, 0, 0 );
|
|
|
|
WINDOW *preview = newwin(LINES, half_width, 0, half_width);
|
|
|
|
WINDOW *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-10 22:31:33 +01:00
|
|
|
draw_border_title(directory, true);
|
2024-03-10 21:23:15 +01:00
|
|
|
draw_border_title(preview, false);
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-10 21:23:15 +01:00
|
|
|
/* window location y, x */
|
|
|
|
windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 };
|
|
|
|
windows[1] = (WIN_STRUCT) { preview, 1, 0, half_width };
|
|
|
|
windows[2] = (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
|
|
|
}
|