Merge pull request #10 from piotr-marendowski/feature
Added bottom panel, other smaller changes
This commit is contained in:
commit
cd2366fab1
3 changed files with 104 additions and 58 deletions
2
Makefile
2
Makefile
|
@ -7,7 +7,7 @@ SRC = ccc.c util.c file.c
|
|||
|
||||
# Flags
|
||||
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
|
||||
CONF = config.h
|
||||
DEFCONF = config.def.h
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
# 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`.
|
||||
|
||||
|
||||
## Features
|
||||
|
||||
Consider this project incomplete and WIP!
|
||||
|
|
157
ccc.c
157
ccc.c
|
@ -10,6 +10,7 @@
|
|||
#include "util.h"
|
||||
|
||||
#define ESC 0x1B /* \e or \033 */
|
||||
#define PH 1 /* panel height */
|
||||
|
||||
typedef struct {
|
||||
WINDOW *window;
|
||||
|
@ -18,9 +19,8 @@ typedef struct {
|
|||
int x;
|
||||
} WIN_STRUCT;
|
||||
|
||||
|
||||
/* functions' definitions */
|
||||
void list_cwd_files();
|
||||
void list_files(char *path);
|
||||
void highlight_current_line();
|
||||
void show_file_content();
|
||||
void init_windows();
|
||||
|
@ -28,14 +28,17 @@ void draw_border_title(WINDOW *window, bool active);
|
|||
|
||||
/* global variables */
|
||||
unsigned int focus = 0;
|
||||
int current_selection = 0;
|
||||
long current_selection = 0;
|
||||
int half_width;
|
||||
WIN_STRUCT windows[2];
|
||||
WIN_STRUCT windows[3];
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char cwd[PATH_MAX];
|
||||
|
||||
if (argc > 1 && strcmp(argv[1], "-h") == 0)
|
||||
die("Usage: ccc filename");
|
||||
|
||||
/* check if it is interactive shell */
|
||||
if (!isatty(STDIN_FILENO))
|
||||
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 */
|
||||
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();
|
||||
noecho();
|
||||
cbreak();
|
||||
curs_set(0);
|
||||
|
||||
/* check terminal has colors */
|
||||
|
@ -61,43 +67,62 @@ int main(int argc, char** argv)
|
|||
refresh();
|
||||
half_width = COLS / 2;
|
||||
init_windows();
|
||||
list_cwd_files();
|
||||
list_files(getcwd(cwd, sizeof(cwd)));
|
||||
highlight_current_line();
|
||||
|
||||
int ch;
|
||||
int ch, second;
|
||||
while (1) {
|
||||
if (COLS < 80 || LINES < 24) {
|
||||
endwin();
|
||||
die("ccc: Terminal size needs to be at least 80x24\n");
|
||||
}
|
||||
ch = getch();
|
||||
if (ch == 'q')
|
||||
break;
|
||||
switch (ch) {
|
||||
case 'q':
|
||||
endwin();
|
||||
return 0;
|
||||
case '.':
|
||||
list_cwd_files();
|
||||
list_files(getcwd(cwd, sizeof(cwd)));
|
||||
break;
|
||||
/* go back */
|
||||
case 'h':
|
||||
break;
|
||||
/* enter directory/open a file */
|
||||
case 'l':
|
||||
if (focus == 0) focus++;
|
||||
else if (focus == 1) focus--;
|
||||
break;
|
||||
/* go up */
|
||||
case 'k':
|
||||
if (current_selection > 0) {
|
||||
if (current_selection > 0)
|
||||
current_selection--;
|
||||
}
|
||||
|
||||
highlight_current_line();
|
||||
break;
|
||||
/* go down */
|
||||
case 'j':
|
||||
if (current_selection < files_len() - 1) {
|
||||
if (current_selection < files_len() - 1)
|
||||
current_selection++;
|
||||
}
|
||||
|
||||
highlight_current_line();
|
||||
break;
|
||||
/* jump to the bottom */
|
||||
case 'G':
|
||||
current_selection = files_len() - 1;
|
||||
highlight_current_line();
|
||||
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 */
|
||||
break;
|
||||
case KEY_RESIZE:
|
||||
|
@ -106,25 +131,27 @@ int main(int argc, char** argv)
|
|||
}
|
||||
init_windows();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
endwin(); /* End curses */
|
||||
endwin();
|
||||
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;
|
||||
struct dirent *ep;
|
||||
|
||||
dp = opendir(cwd);
|
||||
dp = opendir(path);
|
||||
if (dp != NULL) {
|
||||
int count = 0;
|
||||
|
||||
while ((ep = readdir(dp)) != NULL) {
|
||||
char *filename = strdup(ep->d_name);
|
||||
if (filename == NULL) {
|
||||
|
@ -133,7 +160,7 @@ void list_cwd_files()
|
|||
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name);
|
||||
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, "..")) {
|
||||
add_file(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()
|
||||
{
|
||||
char cwd[PATH_MAX];
|
||||
char *filename;
|
||||
|
||||
for (long i = 0; i < files_len(); i++) {
|
||||
if (i == current_selection) {
|
||||
wattron(windows[0].window, A_REVERSE);
|
||||
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);
|
||||
mvwprintw(windows[0].window, i + 1, 1, "%s", name); /* print actual file name */
|
||||
/* print the actual filename */
|
||||
filename = get_filename(i);
|
||||
mvwprintw(windows[0].window, i + 1, 1, "%s", filename);
|
||||
wattroff(windows[0].window, A_REVERSE);
|
||||
wattroff(windows[0].window, COLOR_PAIR(1));
|
||||
}
|
||||
|
||||
wrefresh(windows[0].window); /* refresh to see the changes */
|
||||
show_file_content(); /* show file content every time cursor changes */
|
||||
wrefresh(windows[0].window);
|
||||
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()
|
||||
{
|
||||
|
@ -176,45 +214,53 @@ void show_file_content()
|
|||
if (file) {
|
||||
wclear(windows[1].window);
|
||||
draw_border_title(windows[1].window, true);
|
||||
|
||||
fseek(file, 0, SEEK_END);
|
||||
/* check if file isn't empty */
|
||||
long length = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
char a[100];
|
||||
char *buffer = memalloc(length * sizeof(char));
|
||||
fread(buffer, 1, length, file);
|
||||
if (length != 0) {
|
||||
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);
|
||||
}
|
||||
fclose(file);
|
||||
mvwprintw(windows[1].window, 1, 1, "%s", buffer);
|
||||
wrefresh(windows[1].window);
|
||||
}
|
||||
}
|
||||
|
||||
void init_windows()
|
||||
{
|
||||
/*
|
||||
* ------------------------------/
|
||||
* | directory(0)/ preview(1) /
|
||||
* | / /
|
||||
* | / /
|
||||
* | / /
|
||||
* | / /
|
||||
* |-----------------------------/
|
||||
*/
|
||||
/*-----------------------------+
|
||||
| | |
|
||||
| | |
|
||||
| directory (0) | preview (1) |
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
+==========panel (2)==========*/
|
||||
|
||||
/* create windows */
|
||||
WINDOW *directory = newwin(LINES, half_width, 0, 0);
|
||||
WINDOW *preview = newwin(LINES, half_width, 0, half_width);
|
||||
/* 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 );
|
||||
|
||||
/* draw border around windows */
|
||||
draw_border_title(directory, true);
|
||||
draw_border_title(preview, false);
|
||||
/* draw border around windows */
|
||||
draw_border_title(directory, true);
|
||||
draw_border_title(preview, false);
|
||||
|
||||
/* window location y, x */
|
||||
windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 };
|
||||
windows[1] = (WIN_STRUCT) { preview, 1, 0, half_width };
|
||||
/* 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 };
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
|
@ -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, half_width - 1, ACS_VLINE, LINES - 2); /* right vertical line */
|
||||
|
||||
/* draw bottom border */
|
||||
mvwaddch(window, LINES - 1, 0, ACS_LLCORNER); /* lower left corner */
|
||||
mvwhline(window, LINES - 1, 1, ACS_HLINE, half_width - 2); /* bottom horizontal line */
|
||||
mvwaddch(window, LINES - 1, half_width - 1, ACS_LRCORNER); /* lower right corner */
|
||||
/* 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 */
|
||||
|
||||
/* turn color off after turning it on */
|
||||
if (active) {
|
||||
|
|
Loading…
Reference in a new issue