Merge pull request #10 from piotr-marendowski/feature

Added bottom panel, other smaller changes
This commit is contained in:
Night Kaly 2024-03-10 21:39:25 +00:00 committed by GitHub
commit cd2366fab1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 104 additions and 58 deletions

View file

@ -7,7 +7,7 @@ SRC = ccc.c util.c file.c
# Flags # Flags
LDFLAGS = $(shell pkg-config --libs ncurses) LDFLAGS = $(shell pkg-config --libs ncurses)
CFLAGS = -march=native -mtune=native -O3 -pipe -O3 -s -std=c11 -W -pedantic $(shell pkg-config --cflags ncurses) -Wall -Wextra # -Werror CFLAGS = -march=native -mtune=native -O3 -pipe -O3 -s -std=c99 -W -pedantic $(shell pkg-config --cflags ncurses) -Wall -Wextra # -Werror
CC=cc CC=cc
CONF = config.h CONF = config.h
DEFCONF = config.def.h DEFCONF = config.def.h

View file

@ -1,10 +1,9 @@
# ccc # ccc
`ccc` is a rewrite of [fff](https://github.com/piotr-marendowski/fff) in C aiming for [sucklessness](https://suckless.org/philosophy/) and speed. `ccc` is a rewrite of [fff](https://github.com/piotr-marendowski/fff) in C aiming for usefulness and speed.
The fact that it is written in C makes it more versatile and rapid, enabling us to add features that were previously ruled out due to time complexity. You may call it a `soft fork`. The fact that it is written in C makes it more versatile and rapid, enabling us to add features that were previously ruled out due to time complexity. You may call it a `soft fork`.
## Features ## Features
Consider this project incomplete and WIP! Consider this project incomplete and WIP!

133
ccc.c
View file

@ -10,6 +10,7 @@
#include "util.h" #include "util.h"
#define ESC 0x1B /* \e or \033 */ #define ESC 0x1B /* \e or \033 */
#define PH 1 /* panel height */
typedef struct { typedef struct {
WINDOW *window; WINDOW *window;
@ -18,9 +19,8 @@ typedef struct {
int x; int x;
} WIN_STRUCT; } WIN_STRUCT;
/* functions' definitions */ /* functions' definitions */
void list_cwd_files(); void list_files(char *path);
void highlight_current_line(); void highlight_current_line();
void show_file_content(); void show_file_content();
void init_windows(); void init_windows();
@ -28,14 +28,17 @@ void draw_border_title(WINDOW *window, bool active);
/* global variables */ /* global variables */
unsigned int focus = 0; unsigned int focus = 0;
int current_selection = 0; long current_selection = 0;
int half_width; int half_width;
WIN_STRUCT windows[2]; WIN_STRUCT windows[3];
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
char cwd[PATH_MAX]; char cwd[PATH_MAX];
if (argc > 1 && strcmp(argv[1], "-h") == 0)
die("Usage: ccc filename");
/* check if it is interactive shell */ /* check if it is interactive shell */
if (!isatty(STDIN_FILENO)) if (!isatty(STDIN_FILENO))
die("ccc: No tty detected. ccc requires an interactive shell to run.\n"); die("ccc: No tty detected. ccc requires an interactive shell to run.\n");
@ -43,8 +46,11 @@ int main(int argc, char** argv)
/* set window name */ /* set window name */
printf("%c]2;ccc: %s%c", ESC, getcwd(cwd, sizeof(cwd)), ESC); printf("%c]2;ccc: %s%c", ESC, getcwd(cwd, sizeof(cwd)), ESC);
/* initialize screen, don't print special chars,
* make ctrl + c work, don't show cursor */
initscr(); initscr();
noecho(); noecho();
cbreak();
curs_set(0); curs_set(0);
/* check terminal has colors */ /* check terminal has colors */
@ -61,43 +67,62 @@ int main(int argc, char** argv)
refresh(); refresh();
half_width = COLS / 2; half_width = COLS / 2;
init_windows(); init_windows();
list_cwd_files(); list_files(getcwd(cwd, sizeof(cwd)));
highlight_current_line(); highlight_current_line();
int ch; int ch, second;
while (1) { while (1) {
if (COLS < 80 || LINES < 24) { if (COLS < 80 || LINES < 24) {
endwin(); endwin();
die("ccc: Terminal size needs to be at least 80x24\n"); die("ccc: Terminal size needs to be at least 80x24\n");
} }
ch = getch(); ch = getch();
if (ch == 'q')
break;
switch (ch) { switch (ch) {
case 'q':
endwin();
return 0;
case '.': case '.':
list_cwd_files(); list_files(getcwd(cwd, sizeof(cwd)));
break; break;
/* go back */
case 'h': case 'h':
break;
/* enter directory/open a file */
case 'l': case 'l':
if (focus == 0) focus++; if (focus == 0) focus++;
else if (focus == 1) focus--; else if (focus == 1) focus--;
break; break;
/* go up */
case 'k': case 'k':
if (current_selection > 0) { if (current_selection > 0)
current_selection--; current_selection--;
}
highlight_current_line(); highlight_current_line();
break; break;
/* go down */
case 'j': case 'j':
if (current_selection < files_len() - 1) { if (current_selection < files_len() - 1)
current_selection++; current_selection++;
}
highlight_current_line(); highlight_current_line();
break; break;
/* jump to the bottom */
case 'G': case 'G':
current_selection = files_len() - 1; current_selection = files_len() - 1;
highlight_current_line(); highlight_current_line();
break; break;
/* jump to the top */
case 'g':
second = getch();
switch (second) {
case 'g':
current_selection = 0;
highlight_current_line();
break;
default:
break;
}
break;
case 27: /* esc */ case 27: /* esc */
break; break;
case KEY_RESIZE: case KEY_RESIZE:
@ -106,25 +131,27 @@ int main(int argc, char** argv)
} }
init_windows(); init_windows();
break; break;
default:
break;
} }
} }
endwin(); /* End curses */ endwin();
return 0; return 0;
} }
/* /*
* read current directory and list all files to window 0 * Read the provided directory and list all files to window 0
* ep->d_name -> filename
*/ */
void list_cwd_files() void list_files(char *path)
{ {
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
DIR *dp; DIR *dp;
struct dirent *ep; struct dirent *ep;
dp = opendir(cwd); dp = opendir(path);
if (dp != NULL) { if (dp != NULL) {
int count = 0; int count = 0;
while ((ep = readdir(dp)) != NULL) { while ((ep = readdir(dp)) != NULL) {
char *filename = strdup(ep->d_name); char *filename = strdup(ep->d_name);
if (filename == NULL) { if (filename == NULL) {
@ -133,7 +160,7 @@ void list_cwd_files()
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name); fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* can't be strncmp as that filter out dotfiles */ /* can't be strncmp as that would filter out the dotfiles */
if (strcmp(filename, ".") && strcmp(filename, "..")) { if (strcmp(filename, ".") && strcmp(filename, "..")) {
add_file(filename); add_file(filename);
mvwprintw(windows[0].window, count + 1, 1, "%s", filename); mvwprintw(windows[0].window, count + 1, 1, "%s", filename);
@ -148,27 +175,38 @@ void list_cwd_files()
} }
/* /*
* highlight current line by reversing the color * Highlight current line by reversing the color
*/ */
void highlight_current_line() void highlight_current_line()
{ {
char cwd[PATH_MAX];
char *filename;
for (long i = 0; i < files_len(); i++) { for (long i = 0; i < files_len(); i++) {
if (i == current_selection) { if (i == current_selection) {
wattron(windows[0].window, A_REVERSE); wattron(windows[0].window, A_REVERSE);
wattron(windows[0].window, COLOR_PAIR(1)); wattron(windows[0].window, COLOR_PAIR(1));
/* update the panel */
wclear(windows[2].window);
wprintw(windows[2].window, "(%ld/%ld) %s", i + 1, files_len(),
getcwd(cwd, sizeof(cwd)));
} }
char *name = get_filename(i); /* print the actual filename */
mvwprintw(windows[0].window, i + 1, 1, "%s", name); /* print actual file name */ filename = get_filename(i);
mvwprintw(windows[0].window, i + 1, 1, "%s", filename);
wattroff(windows[0].window, A_REVERSE); wattroff(windows[0].window, A_REVERSE);
wattroff(windows[0].window, COLOR_PAIR(1)); wattroff(windows[0].window, COLOR_PAIR(1));
} }
wrefresh(windows[0].window); /* refresh to see the changes */ wrefresh(windows[0].window);
show_file_content(); /* show file content every time cursor changes */ wrefresh(windows[2].window);
/* show file content every time cursor changes */
show_file_content();
} }
/* /*
* get file content into buffer and show it to preview window * Get file content into buffer and show it to preview window
*/ */
void show_file_content() void show_file_content()
{ {
@ -176,33 +214,40 @@ void show_file_content()
if (file) { if (file) {
wclear(windows[1].window); wclear(windows[1].window);
draw_border_title(windows[1].window, true); draw_border_title(windows[1].window, true);
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
/* check if file isn't empty */
long length = ftell(file); long length = ftell(file);
if (length != 0) {
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
char a[100];
char *buffer = memalloc(length * sizeof(char)); char *buffer = memalloc(length * sizeof(char));
fread(buffer, 1, length, file); fread(buffer, 1, length, file);
fclose(file);
mvwprintw(windows[1].window, 1, 1, "%s", buffer); mvwprintw(windows[1].window, 1, 1, "%s", buffer);
wrefresh(windows[1].window); wrefresh(windows[1].window);
} else {
wclear(windows[1].window);
}
fclose(file);
} }
} }
void init_windows() void init_windows()
{ {
/* /*-----------------------------+
* ------------------------------/ | | |
* | directory(0)/ preview(1) / | | |
* | / / | directory (0) | preview (1) |
* | / / | | |
* | / / | | |
* | / / | | |
* |-----------------------------/ +==========panel (2)==========*/
*/
/* create windows */ /* lines, cols, y, x */
WINDOW *directory = newwin(LINES, half_width, 0, 0 ); WINDOW *directory = newwin(LINES, half_width, 0, 0 );
WINDOW *preview = newwin(LINES, half_width, 0, half_width); WINDOW *preview = newwin(LINES, half_width, 0, half_width);
WINDOW *panel = newwin(PH, COLS, LINES - PH, 0 );
/* draw border around windows */ /* draw border around windows */
draw_border_title(directory, true); draw_border_title(directory, true);
@ -211,10 +256,11 @@ void init_windows()
/* window location y, x */ /* window location y, x */
windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 }; windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 };
windows[1] = (WIN_STRUCT) { preview, 1, 0, half_width }; windows[1] = (WIN_STRUCT) { preview, 1, 0, half_width };
windows[2] = (WIN_STRUCT) { panel, 2, LINES - PH, 0 };
} }
/* /*
* Draw the border of the window depends on it is active or not * Draw the border of the window depending if it's active or not
*/ */
void draw_border_title(WINDOW *window, bool active) void draw_border_title(WINDOW *window, bool active)
{ {
@ -233,10 +279,11 @@ void draw_border_title(WINDOW *window, bool active)
mvwvline(window, 1, 0, ACS_VLINE, LINES - 2); /* left vertical line */ mvwvline(window, 1, 0, ACS_VLINE, LINES - 2); /* left vertical line */
mvwvline(window, 1, half_width - 1, ACS_VLINE, LINES - 2); /* right vertical line */ mvwvline(window, 1, half_width - 1, ACS_VLINE, LINES - 2); /* right vertical line */
/* draw bottom border */ /* draw bottom border
mvwaddch(window, LINES - 1, 0, ACS_LLCORNER); /* lower left corner */ * make space for the panel */
mvwhline(window, LINES - 1, 1, ACS_HLINE, half_width - 2); /* bottom horizontal line */ mvwaddch(window, LINES - PH - 1, 0, ACS_LLCORNER); /* lower left corner */
mvwaddch(window, LINES - 1, half_width - 1, ACS_LRCORNER); /* lower right 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 */
/* turn color off after turning it on */ /* turn color off after turning it on */
if (active) { if (active) {