Merge pull request #12 from piotr-marendowski/feature

Feature
This commit is contained in:
Piotr Marendowski 2024-03-12 22:04:01 +00:00 committed by GitHub
commit 0884c735c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 167 additions and 95 deletions

View file

@ -6,10 +6,13 @@ The fact that it is written in C makes it more versatile and rapid, enabling us
## Features ## Features
- Vim-like key binding
- File Preview
Consider this project incomplete and WIP! Consider this project incomplete and WIP!
| Feature | Ported | Dropped | Added | | Feature | Ported | Dropped | Added |
|---------|:------:|:-------:|:-----:| |--------------------------------|:------:|:-------:|:-----:|
| Standard movement | X | | | | Standard movement | X | | |
| Advanced movement (jumps) | X | | | | Advanced movement (jumps) | X | | |
| Searching | | | | | Searching | | | |
@ -17,16 +20,13 @@ Consider this project incomplete and WIP!
| Sorting | | | | | Sorting | | | |
| Marking and marking operations | | | | | Marking and marking operations | | | |
| Other operations on files | | | | | Other operations on files | | | |
| File details | | | | | File details | X | | |
| Image previews | | | | | Image previews | | | |
| Help | | | | | Help | | | |
| History | | | | | History | | | |
| Bookmarks | | | | | Bookmarks | | | |
| Bulk rename | | | | | Bulk rename | | | |
| Workspaces | | | | | Workspaces | | | |
| Workspaces | | | |
## Installation
### Dependencies ### Dependencies
@ -37,8 +37,10 @@ Consider this project incomplete and WIP!
### Building ### Building
You may need to run these with elevated privilages. You will need to run these with elevated privilages.
``` ```sh
make install $ git clone https://github.com/piotr-marendowski/ccc
$ make
$ sudo make install
``` ```

183
ccc.c
View file

@ -23,7 +23,7 @@ typedef struct {
} WIN_STRUCT; } WIN_STRUCT;
/* functions' definitions */ /* functions' definitions */
void list_files(char *path); void list_files(const char *path);
char *get_file_stat(char *filename); char *get_file_stat(char *filename);
void highlight_current_line(); void highlight_current_line();
void show_file_content(); void show_file_content();
@ -34,13 +34,17 @@ void draw_border_title(WINDOW *window, bool active);
/* global variables */ /* global variables */
unsigned int focus = 0; unsigned int focus = 0;
long current_selection = 0; long current_selection = 0;
char *cwd;
int half_width; int half_width;
WIN_STRUCT windows[4]; WIN_STRUCT windows[5];
WINDOW *directory_border;
WINDOW *directory_content;
WINDOW *preview_border;
WINDOW *preview_content;
WINDOW *panel;
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
char cwd[PATH_MAX];
if (argc > 1 && strcmp(argv[1], "-h") == 0) if (argc > 1 && strcmp(argv[1], "-h") == 0)
die("Usage: ccc filename"); die("Usage: ccc filename");
@ -48,9 +52,6 @@ int main(int argc, char** argv)
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");
/* set window name */
printf("%c]2;ccc: %s%c", ESC, getcwd(cwd, sizeof(cwd)), ESC);
/* initialize screen, don't print special chars, /* initialize screen, don't print special chars,
* make ctrl + c work, don't show cursor */ * make ctrl + c work, don't show cursor */
initscr(); initscr();
@ -69,12 +70,19 @@ int main(int argc, char** argv)
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */ init_pair(1, COLOR_BLUE, COLOR_BLACK); /* foreground, background */
init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */ init_pair(2, COLOR_CYAN, COLOR_BLACK); /* active color */
refresh();
half_width = COLS / 2; half_width = COLS / 2;
init_windows(); init_windows();
list_files(getcwd(cwd, sizeof(cwd))); refresh();
cwd = memalloc(PATH_MAX * sizeof(char));
getcwd(cwd, PATH_MAX);
list_files(cwd);
highlight_current_line(); highlight_current_line();
/* set window name */
printf("%c]2;ccc: %s%c", ESC, cwd, ESC);
int ch, second; int ch, second;
while (1) { while (1) {
if (COLS < 80 || LINES < 24) { if (COLS < 80 || LINES < 24) {
@ -83,20 +91,56 @@ int main(int argc, char** argv)
} }
ch = getch(); ch = getch();
switch (ch) { switch (ch) {
/* quit */
case 'q': case 'q':
endwin(); endwin();
return 0; return 0;
/* reload */
case '.': case '.':
list_files(getcwd(cwd, sizeof(cwd))); clear_files();
list_files(cwd);
current_selection = 0;
highlight_current_line();
break; break;
/* go back */ /* go back */
case 'h': case 'h':
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();
}
break; break;
/* enter directory/open a file */ /* enter directory/open a file */
case 'l': case 'l':;
char *filename = get_filepath(current_selection);
if (filename != NULL) {
struct stat file_stat;
stat(filename, &file_stat);
/* check if it is directory or regular file */
if (S_ISDIR(file_stat.st_mode)) {
/* change cwd to directory */
strcpy(cwd, filename);
clear_files();
list_files(filename);
current_selection = 0;
highlight_current_line();
}
}
break;
/*
if (focus == 0) focus++; if (focus == 0) focus++;
else if (focus == 1) focus--; else if (focus == 1) focus--;
break; break;
*/
/* jump up */ /* jump up */
case '\x15': case '\x15':
if ((current_selection - JUMP_NUM) > 0) if ((current_selection - JUMP_NUM) > 0)
@ -106,6 +150,7 @@ int main(int argc, char** argv)
highlight_current_line(); highlight_current_line();
break; break;
/* go up */ /* go up */
case 'k': case 'k':
if (current_selection > 0) if (current_selection > 0)
@ -169,29 +214,38 @@ int main(int argc, char** argv)
* Read the provided directory and list all files to window 0 * Read the provided directory and list all files to window 0
* ep->d_name -> filename * ep->d_name -> filename
*/ */
void list_files(char *path) void list_files(const char *path)
{ {
DIR *dp; DIR *dp;
struct dirent *ep; struct dirent *ep;
draw_border_title(directory_border, true);
dp = opendir(path); dp = opendir(path);
if (dp != NULL) { if (dp != NULL) {
int count = 0; int count = 0;
/* clear directory window to ready for printing */
wclear(directory_content);
while ((ep = readdir(dp)) != NULL) { while ((ep = readdir(dp)) != NULL) {
char *filename = strdup(ep->d_name); char *filename = memalloc(PATH_MAX * sizeof(char));
if (filename == NULL) { /* make filename be basename of selected item just to pass check */
/* memory allocation failed */ filename[0] = '\0';
perror("ccc"); strcat(filename, ep->d_name);
fprintf(stderr, "ccc: Cannot read filename %s.", ep->d_name);
exit(EXIT_FAILURE);
}
/* can't be strncmp as that would filter out the dotfiles */ /* can't be strncmp as that would filter out the dotfiles */
if (strcmp(filename, ".") && strcmp(filename, "..")) { if (strcmp(filename, ".") && strcmp(filename, "..")) {
/* construct full file path */
filename[0] = '\0';
strcat(filename, cwd);
strcat(filename, "/");
strcat(filename, ep->d_name); /* add file name */
char *stats = get_file_stat(filename); char *stats = get_file_stat(filename);
long index = add_file(filename, stats); long index = add_file(filename, stats);
char *line = get_line(index); char *line = get_line(index);
mvwprintw(windows[0].window, count + 1, 1, "%s", line);
mvwprintw(directory_content, count, 0, "%s", line);
free(stats); free(stats);
free(line); free(line);
count++; count++;
@ -199,7 +253,7 @@ void list_files(char *path)
free(filename); free(filename);
} }
closedir(dp); closedir(dp);
wrefresh(windows[0].window); wrefresh(directory_content);
} else { } else {
perror("ccc"); perror("ccc");
} }
@ -208,12 +262,14 @@ void list_files(char *path)
/* /*
* Get file's last modified time and size * Get file's last modified time and size
*/ */
char *get_file_stat(char *filename) { char *get_file_stat(char *filename)
{
struct stat file_stat; struct stat file_stat;
stat(filename, &file_stat); stat(filename, &file_stat);
char *time = memalloc(20 * sizeof(char)); char *time = memalloc(20 * sizeof(char));
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime)); /* format last modified time to a string */ /* format last modified time to a string */
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
double bytes = file_stat.st_size; double bytes = file_stat.st_size;
char *size = memalloc(25 * sizeof(char)); /* max 25 chars due to long, space, suffix and nul */ char *size = memalloc(25 * sizeof(char)); /* max 25 chars due to long, space, suffix and nul */
@ -227,6 +283,7 @@ char *get_file_stat(char *filename) {
char *total_stat = memalloc(45 * sizeof(char)); char *total_stat = memalloc(45 * sizeof(char));
snprintf(total_stat, 45, "%-18s %-10s", time, size); snprintf(total_stat, 45, "%-18s %-10s", time, size);
total_stat[strlen(total_stat)] = '\0';
free(time); free(time);
free(size); free(size);
@ -238,29 +295,26 @@ char *get_file_stat(char *filename) {
*/ */
void highlight_current_line() void highlight_current_line()
{ {
char cwd[PATH_MAX];
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(directory_content, A_REVERSE);
wattron(windows[0].window, COLOR_PAIR(1)); wattron(directory_content, COLOR_PAIR(1));
/* update the panel */ /* update the panel */
wclear(windows[3].window); wclear(panel);
wprintw(windows[3].window, "(%ld/%ld) %s", i + 1, files_len(), wprintw(panel, "(%ld/%ld) %s", i + 1, files_len(), cwd);
getcwd(cwd, sizeof(cwd)));
} }
/* print the actual filename and stats */ /* print the actual filename and stats */
char *line = get_line(i); char *line = get_line(i);
mvwprintw(windows[0].window, i + 1, 1, "%s", line); mvwprintw(directory_content, i, 0, "%s", line);
wattroff(windows[0].window, A_REVERSE); wattroff(directory_content, A_REVERSE);
wattroff(windows[0].window, COLOR_PAIR(1)); wattroff(directory_content, COLOR_PAIR(1));
free(line); free(line);
} }
wrefresh(windows[0].window); wrefresh(directory_content);
wrefresh(windows[3].window); wrefresh(panel);
/* show file content every time cursor changes */ /* show file content every time cursor changes */
show_file_content(); show_file_content();
} }
@ -270,10 +324,10 @@ void highlight_current_line()
*/ */
void show_file_content() void show_file_content()
{ {
FILE *file = fopen(get_filename((long) current_selection), "rb"); FILE *file = fopen(get_filepath((long) current_selection), "rb");
if (file) { if (file) {
wclear(windows[2].window); wclear(preview_content);
draw_border_title(windows[1].window, true); draw_border_title(preview_border, true);
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
long length = ftell(file); long length = ftell(file);
@ -282,11 +336,11 @@ void show_file_content()
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)); char *buffer = memalloc(length * sizeof(char));
fread(buffer, 1, length, file); fread(buffer, 1, length, file);
mvwprintw(windows[2].window, 0, 0, "%s", buffer); mvwprintw(preview_content, 0, 0, "%s", buffer);
wrefresh(windows[2].window); wrefresh(preview_content);
free(buffer); free(buffer);
} else { } else {
wclear(windows[2].window); wclear(preview_content);
} }
fclose(file); fclose(file);
} }
@ -295,17 +349,18 @@ void show_file_content()
/* /*
* Opens $EDITOR to edit the file * Opens $EDITOR to edit the file
*/ */
void edit_file() { void edit_file()
{
char *editor = getenv("EDITOR"); char *editor = getenv("EDITOR");
if (editor == NULL) { if (editor == NULL) {
wclear(windows[3].window); wclear(panel);
wprintw(windows[3].window, "Cannot get EDITOR variable, is it defined?"); wprintw(panel, "Cannot get EDITOR variable, is it defined?");
wrefresh(windows[3].window); wrefresh(panel);
return; return;
} else { } else {
def_prog_mode(); /* save the tty modes */ def_prog_mode(); /* save the tty modes */
endwin(); /* end curses mode temporarily */ endwin(); /* end curses mode temporarily */
char *filename = get_filename(current_selection); char *filename = get_filepath(current_selection);
int length = strlen(editor) + strlen(filename) + 2; /* one for space one for nul */ int length = strlen(editor) + strlen(filename) + 2; /* one for space one for nul */
char command[length]; char command[length];
snprintf(command, length, "%s %s", editor, filename); snprintf(command, length, "%s %s", editor, filename);
@ -319,29 +374,31 @@ void edit_file() {
void init_windows() void init_windows()
{ {
/*------------------------------+ /*------------------------------+
| ||-------------|| |----border(0)--||--border(2)--||
| || || || || ||
| directory (0) || preview (1)|| || content (1) || content (3) ||
| || || || || ||
| || || || || ||
| ||-------------|| |---------------||-------------||
+==========panel (2)===========*/ +==========panel (4)===========*/
/* lines, cols, y, x */ /* lines, cols, y, x */
WINDOW *directory = newwin(LINES - PH, half_width, 0, 0 ); directory_border = newwin(LINES - PH, half_width, 0, 0 );
WINDOW *preview_border = newwin(LINES - PH, half_width, 0, half_width ); directory_content = newwin(LINES - PH -2, half_width - 2, 1, 1);
WINDOW *preview_content = newwin(LINES - PH - 2, half_width - 2, 1, half_width + 1); preview_border = newwin(LINES - PH, half_width, 0, half_width );
WINDOW *panel = newwin(PH, COLS, LINES - PH, 0 ); preview_content = newwin(LINES - PH - 2, half_width - 2, 1, half_width + 1);
panel = newwin(PH, COLS, LINES - PH, 0 );
/* draw border around windows */ /* draw border around windows */
draw_border_title(directory, true); draw_border_title(directory_border, true);
draw_border_title(preview_content, false); draw_border_title(preview_border, false);
/* window location y, x */ /* window location y, x */
windows[0] = (WIN_STRUCT) { directory, 0, 0, 0 }; windows[0] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
windows[1] = (WIN_STRUCT) { preview_border, 1, 0, half_width }; windows[1] = (WIN_STRUCT) { directory_border, 0, 0, 0 };
windows[2] = (WIN_STRUCT) { preview_content, 1, 0, half_width }; windows[2] = (WIN_STRUCT) { preview_border, 1, 0, half_width };
windows[3] = (WIN_STRUCT) { panel, 2, LINES - PH, 0 }; windows[3] = (WIN_STRUCT) { preview_content, 1, 0, half_width };
windows[4] = (WIN_STRUCT) { panel, 2, LINES - PH, 0 };
} }
/* /*

26
file.c
View file

@ -1,11 +1,12 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <libgen.h>
#include "util.h" #include "util.h"
/* files in a link list data structure */ /* files in a link list data structure */
typedef struct file { typedef struct file {
char *name; char *path;
char *stats; char *stats;
/* put some more useful stat here */ /* put some more useful stat here */
struct file *next; struct file *next;
@ -27,16 +28,26 @@ long files_len()
return count; return count;
} }
long add_file(char *filename, char *stats) void clear_files()
{
file *tmp;
while (files != NULL) {
tmp = files;
files = files->next;
free(tmp);
}
}
long add_file(char *filepath, char *stats)
{ {
file *current = files; file *current = files;
file *new_file = memalloc(sizeof(file)); file *new_file = memalloc(sizeof(file));
char *buf = strdup(filename); char *buf = strdup(filepath);
char *buf2 = strdup(stats); char *buf2 = strdup(stats);
if (buf == NULL || buf2 == NULL) { if (buf == NULL || buf2 == NULL) {
perror("ccc"); perror("ccc");
} }
new_file->name = buf; new_file->path = buf;
new_file->stats = buf2; new_file->stats = buf2;
new_file->next = NULL; new_file->next = NULL;
if (current == NULL) { if (current == NULL) {
@ -67,11 +78,11 @@ file *get_file(long index)
return current; return current;
} }
char *get_filename(long index) char *get_filepath(long index)
{ {
file *file = get_file(index); file *file = get_file(index);
if (file != NULL) { if (file != NULL) {
char *name = strdup(file->name); char *name = strdup(file->path);
if (!name) { if (!name) {
perror("ccc"); perror("ccc");
} }
@ -85,7 +96,8 @@ char *get_line(long index)
{ {
file *file = get_file(index); file *file = get_file(index);
if (file != NULL) { if (file != NULL) {
char *name = strdup(file->name); char *name = strdup(file->path);
name = basename(name);
char *stats = strdup(file->stats); char *stats = strdup(file->stats);
if (name == NULL || stats == NULL) { if (name == NULL || stats == NULL) {
perror("ccc"); perror("ccc");

5
file.h
View file

@ -2,16 +2,17 @@
#define FILE_H_ #define FILE_H_
typedef struct file { typedef struct file {
char *name; char *path;
char *stats; char *stats;
// put some more useful stat here // put some more useful stat here
struct file *next; struct file *next;
} file; } file;
long files_len(); long files_len();
void clear_files();
long add_file(char *filename, char *time); long add_file(char *filename, char *time);
file *get_file(long index); file *get_file(long index);
char *get_filename(long index); char *get_filepath(long index);
char *get_line(long index); char *get_line(long index);
#endif #endif