Merge pull request #17 from piotr-marendowski/feature

Feature
This commit is contained in:
Piotr Marendowski 2024-03-19 18:41:55 +00:00 committed by GitHub
commit 5859c61af7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 370 additions and 196 deletions

View file

@ -12,7 +12,7 @@ MANDIR = $(PREFIX)/share/man/man1
# Flags
LDFLAGS = $(shell pkg-config --libs ncurses)
CFLAGS = -march=native -mtune=native -O3 -pipe -s -std=c99 -pedantic $(shell pkg-config --cflags ncurses) -Wall # -Wextra -Werror
CFLAGS = -O3 -pipe -s -std=c99 -pedantic -Wall $(shell pkg-config --cflags ncurses)
SRC = ccc.c util.c file.c $(CONF)

View file

@ -6,41 +6,75 @@ The fact that it is written in C makes it more versatile and rapid, enabling us
## Features
- Vim-like key binding
- File Preview
Consider this project incomplete and WIP!
| Feature | Ported | Dropped | Added |
|--------------------------------|:------:|:-------:|:-----:|
| Standard movement | X | | |
| Advanced movement (jumps) | X | | |
| Searching | | | |
| File preview | | | X |
| Sorting | | | |
| Marking and marking operations | | | |
| Other operations on files | | | |
| File details | X | | |
| Image previews | | | |
| Help | | | |
| History | | | |
| Bookmarks | | | |
| Bulk rename | | | |
| Workspaces | | | |
| Feature of fff | Ported | Dropped |
|--------------------------------|:------:|:-------:|
| Standard movement | X | |
| Advanced movement (jumps) | X | |
| File details | X | |
| Searching for files | | |
| Sorting | | |
| Marking and marking operations | | |
| Other operations on files | | |
| Image previews | | |
| Help | | |
| History | | |
| Bookmarks | | |
| Bulk rename | | |
#### Features added that are not in [fff](https://github.com/piotr-marendowski/fff):
- File preview (without highlighting)
## Installation
### Dependencies
- gcc
- ncurses
- make
- pkg-config
- ncurses
### Building
You will need to run these with elevated privilages.
```sh
```
$ git clone https://github.com/piotr-marendowski/ccc
$ make
$ sudo make install
```
## Usage
```
j: scroll down
k: scroll up
h: go to parent dir
l: go to child dir
down: scroll down
up: scroll up
left: go to parent dir
right: go to child dir
enter: go to child dir/open file
backspace: go to parent dir
g: go to top
G: go to bottom
t: go to trash
~: go to home
z: refresh current dir
space: mark file
a: mark all files in directory
q: exit
```
## License
This project has GNU GPL v.3 license.

47
ccc.1
View file

@ -0,0 +1,47 @@
.
.TH CCC "1" "March 2024" "ccc" "User Commands"
.SH NAME
ccc \- Fast TUI file manager written in C, using ncurses.
.SH SYNOPSIS
.B ccc
.SH DESCRIPTION
ccc is a rewrite of fff file manager 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.
.PP
.SH "Usage"
.
.nf
j: scroll down
k: scroll up
h: go to parent dir
l: go to child dir
down: scroll down
up: scroll up
left: go to parent dir
right: go to child dir
enter: go to child dir/open file
backspace: go to parent dir
g: go to top
G: go to bottom
t: go to trash
~: go to home
z: refresh current dir
space: mark file
a: mark all files in directory
q: exit
.
.fi
.
.SH "Customization"
.
.nf
Various settings can be changed in config.h file located in the program's directory.
.
.fi

326
ccc.c
View file

@ -16,17 +16,10 @@
#include "util.h"
#include "config.h"
typedef struct {
WINDOW *window;
int location;
int y;
int x;
} WIN_STRUCT;
/* functions' definitions */
void change_dir(const char *buf);
void change_dir(const char *buf, int selection);
int mkdir_p(const char *destdir);
void populate_files(const char *path);
void populate_files(const char *path, int ftype);
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
long add_file_stat(char *filepath, int ftype);
void highlight_current_line();
@ -39,10 +32,9 @@ void draw_border_title(WINDOW *window, bool active);
/* global variables */
unsigned int focus = 0;
long current_selection = 0;
bool dir_mode = BLOCK_SIZE;
bool dirs_size = DIRS_SIZE;
char *cwd;
int half_width;
WIN_STRUCT windows[5];
WINDOW *directory_border;
WINDOW *directory_content;
WINDOW *preview_border;
@ -95,12 +87,9 @@ int main(int argc, char** argv)
cwd = memalloc(PATH_MAX * sizeof(char));
getcwd(cwd, PATH_MAX);
populate_files(cwd);
populate_files(cwd, 0);
highlight_current_line();
/* set window name */
printf("%c]2;ccc: %s%c", ESC, cwd, ESC);
int ch, ch2;
while (1) {
if (COLS < 80 || LINES < 24) {
@ -117,7 +106,7 @@ int main(int argc, char** argv)
/* reload using z */
case 'z':
change_dir(cwd);
change_dir(cwd, 0);
break;
/* go back by backspace or h or left arrow */
@ -128,7 +117,7 @@ int main(int argc, char** argv)
char *last_slash = strrchr(cwd, '/');
if (last_slash != NULL) {
*last_slash = '\0';
change_dir(cwd);
change_dir(cwd, 0);
}
break;
@ -138,20 +127,15 @@ int main(int argc, char** argv)
case 'l':;
file *file = get_file(current_selection);
if (file != NULL) {
/* check if it is directory or regular file */
/* check if it is directory or a regular file */
if (strncmp(file->type, "DIR", 3) == 0) {
/* change cwd to directory */
change_dir(file->path);
change_dir(file->path, 0);
} else if (strncmp(file->type, "REG", 3) == 0) {
edit_file();
}
}
break;
/*
if (focus == 0) focus++;
else if (focus == 1) focus--;
break;
*/
/* jump up (ctrl u) */
case CTRLU:
@ -210,19 +194,23 @@ int main(int argc, char** argv)
}
break;
/* ~ to go home */
/* '~' go to $HOME */
case TILDE:;
char *home = getenv("HOME");
if (home == NULL) {
wpprintw("$HOME is not defined");
} else {
change_dir(home);
change_dir(home, 0);
}
break;
/* t to go to trash dir */
/* go to the trash dir */
case 't':;
#ifdef TRASH_DIR
char *trash_dir = TRASH_DIR;
#else
char *trash_dir = getenv("CCC_TRASH");
#endif
if (trash_dir == NULL) {
wpprintw("$CCC_TRASH is not defined");
} else {
@ -231,36 +219,80 @@ int main(int argc, char** argv)
if (mkdir_p(trash_dir) == -1) {
switch (errno) {
case EACCES:
wpprintw("Parent directory does not allow write permission or one of directory does not allow search access");
wpprintw("Parent directory does not allow write permission or one of directories does not allow search access");
}
}
}
change_dir(trash_dir);
change_dir(trash_dir, 0);
}
break;
/* a to toggle between DISK_USAGE and BLOCK SIZE */
case 'a':
dir_mode = !dir_mode;
/* show directories' sizes */
case 'A':
dirs_size = !dirs_size;
clear_files();
populate_files(cwd);
populate_files(cwd, 0);
highlight_current_line();
break;
/* mark files by space */
case SPACE:;
/* mark one file */
case SPACE:
add_file_stat(get_filepath(current_selection), 1);
highlight_current_line();
break;
/* mark all files in directory */
case 'a':
populate_files(cwd, 2);
change_dir(cwd, current_selection); /* reload current dir */
break;
/* mark actions: */
/* delete */
case 'd':;
if (marked_len()) {
;
}
break;
/* move */
case 'm':;
if (marked_len()) {
;
}
break;
/* copy */
case 'c':;
if (marked_len()) {
;
}
break;
/* symbolic link */
case 's':;
if (marked_len()) {
;
}
break;
/* bulk rename */
case 'b':;
if (marked_len()) {
;
}
break;
/* escape */
case ESC:
break;
case KEY_RESIZE:
for (int i = 0; i < 2; i++) {
delwin(windows[i].window);
}
delwin(directory_border);
delwin(directory_content);
delwin(preview_border);
delwin(preview_content);
delwin(panel);
endwin();
init_windows();
break;
@ -275,14 +307,14 @@ int main(int argc, char** argv)
}
/*
* Change directory in window
* Change directory in window with selection
*/
void change_dir(const char *buf)
void change_dir(const char *buf, int selection)
{
strcpy(cwd, buf);
clear_files();
populate_files(cwd);
current_selection = 0;
populate_files(cwd, 0);
current_selection = selection;
highlight_current_line();
}
@ -292,6 +324,9 @@ void change_dir(const char *buf)
int mkdir_p(const char *destdir)
{
char *path = memalloc(PATH_MAX * sizeof(char));
char *token = strtok(path, "/");
char dir_path[PATH_MAX] = "";
strcpy(path, destdir);
if (destdir[0] == '~') {
char *home = getenv("HOME");
@ -303,13 +338,9 @@ int mkdir_p(const char *destdir)
memmove(path + strlen(home), path + 1, strlen(path));
memcpy(path, home, strlen(home));
}
char *token = strtok(path, "/");
char dir_path[PATH_MAX] = "";
/* fix first / is not appearing in string */
if (path[0] == '/') {
/* fix first / not appearing in the string */
if (path[0] == '/')
strcat(dir_path, "/");
}
while (token != NULL) {
strcat(dir_path, token);
@ -336,14 +367,17 @@ int mkdir_p(const char *destdir)
/*
* Read the provided directory and add all files in directory to linked list
* ftype: normal files = 0, marked = 1, marking ALL = 2
* ep->d_name -> filename
*/
void populate_files(const char *path)
void populate_files(const char *path, int ftype)
{
DIR *dp;
struct dirent *ep;
#if DRAW_BORDERS
draw_border_title(directory_border, true);
#endif
if ((dp = opendir(path)) != NULL) {
/* clear directory window to ready for printing */
wclear(directory_content);
@ -362,7 +396,7 @@ void populate_files(const char *path)
strcat(filename, "/");
strcat(filename, ep->d_name); /* add filename */
add_file_stat(filename, 0);
add_file_stat(filename, ftype);
}
free(filename);
}
@ -382,48 +416,19 @@ int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, s
/*
* Get file's last modified time, size, type
* Add that file into list
* ftype: 0->dir files, 0->marked
* ftype: normal file = 0, normal marked = 1, marking ALL = 2
*/
long add_file_stat(char *filepath, int ftype)
{
struct stat file_stat;
if (stat(filepath, &file_stat) == -1) {
/* can't be triggered? */
if (errno == EACCES) {
if (errno == EACCES)
return add_file(filepath, "", "", 8);
}
}
/* get last modified time */
char *time = memalloc(20 * sizeof(char));
/* format last modified time to a string */
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
/* get file size */
double bytes = file_stat.st_size;
if (!dir_mode) {
/* dir_mode is 0, so disk usage mode, calculate disk usage */
if (S_ISDIR(file_stat.st_mode)) {
/* at most 15 fd opened */
total_dir_size = 0;
nftw(filepath, &get_directory_size, 15, FTW_PHYS);
bytes = total_dir_size;
}
}
/* max 25 chars due to long, space, suffix and null */
char *size = memalloc(25 * sizeof(char));
int unit = 0;
const char* units[] = {" B", "KiB", "MiB", "GiB", "TiB", "PiB"};
while (bytes > 1024) {
bytes /= 1024;
unit++;
}
/* 4 sig fig, limiting characters to have better format */
sprintf(size, "%-6.4g %-3s", bytes, units[unit]);
/* get file type and color */
char *type = memalloc(4 * sizeof(char)); /* 4 chars for type */
/* get file type and color, 4 chars for the type */
char *type = memalloc(4 * sizeof(char));
int color;
if (S_ISDIR(file_stat.st_mode)) {
strcpy(type, "DIR"); /* directory type */
@ -449,31 +454,57 @@ long add_file_stat(char *filepath, int ftype)
} else {
color = 8; /* white color */
}
/* don't know how to handle socket, block device, character device and FIFO */
if (ftype == 1) {
long index = add_marked(filepath, type);
/* if file is to be marked */
if (ftype == 1 || ftype == 2) {
/* force if user is marking all files */
bool force = ftype == 2 ? true : false;
long index = add_marked(filepath, type, force);
/* free type and return without allocating more stuff */
free(type);
free(size);
free(time);
if (index != -1) {
/* just marked */
return index;
return index; /* just marked */
} else {
return -1; /* already marked */
}
}
/* already marked */
return -1;
/* -1 does nothing, just function required to return something */
/* get last modified time */
char *time = memalloc(20 * sizeof(char));
/* format last modified time to a string */
strftime(time, 20, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
/* get file size */
double bytes = file_stat.st_size;
if (dirs_size) {
/* dirs_size is true, so calculate disk usage */
if (S_ISDIR(file_stat.st_mode)) {
/* at most 15 fd opened */
total_dir_size = 0;
nftw(filepath, &get_directory_size, 15, FTW_PHYS);
bytes = total_dir_size;
}
}
/* max 25 chars due to long, space, suffix and null */
char *size = memalloc(25 * sizeof(char));
int unit = 0;
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB"};
while (bytes > 1024) {
bytes /= 1024;
unit++;
}
/* display sizes */
sprintf(size, "%.3g%s", bytes, units[unit]);
char *total_stat = memalloc(45 * sizeof(char));
snprintf(total_stat, 45, "%-18s %-10s", time, size);
snprintf(total_stat, 45, "%-18s %-8s", time, size);
total_stat[strlen(total_stat)] = '\0';
free(time);
free(size);
long index = add_file(filepath, total_stat, type, color);
free(time);
free(size);
free(total_stat);
free(type);
return index;
@ -495,15 +526,17 @@ void highlight_current_line()
long range = files_len();
/* not highlight if no files in directory */
if (range == 0) {
#if DRAW_PREVIEW
wprintw(preview_content, "empty directory");
wrefresh(preview_content);
#endif
return;
}
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 */
/* if there are more files than lines available to display
* shrink range to avaiable lines to display with
* overflow to keep the number of iterations to be constant */
range = LINES - 3 + overflow;
}
@ -516,24 +549,17 @@ void highlight_current_line()
/* update the panel */
wclear(panel);
/* showing dir_mode requires 26 characters */
char *dir_mode_line = memalloc(27 * sizeof(char));
if (dir_mode)
strncpy(dir_mode_line, "Directory Mode: Block Size", 27);
else
strncpy(dir_mode_line, "Directory Mode: Disk Usage", 27);
/* check for marked files */
long num_marked = marked_len();
if (num_marked > 0) {
/* Determine length of formatted string */
int m_len = snprintf(NULL, 0, "(%ld selected)", num_marked);
int m_len = snprintf(NULL, 0, "[%ld] selected", num_marked);
char *selected = memalloc((m_len + 1) * sizeof(char));
snprintf(selected, m_len + 1, "(%ld selected)", num_marked);
wprintw(panel, "(%ld/%ld) %s %s %s", current_selection + 1, files_len(), selected, cwd, dir_mode_line);
snprintf(selected, m_len + 1, "[%ld] selected", num_marked);
wprintw(panel, "(%ld/%ld) %s %s", current_selection + 1, files_len(), selected, cwd);
} else {
wprintw(panel, "(%ld/%ld) %s %s", current_selection + 1, files_len(), cwd, dir_mode_line);
wprintw(panel, "(%ld/%ld) %s", current_selection + 1, files_len(), cwd);
}
}
/* print the actual filename and stats */
@ -568,7 +594,9 @@ void highlight_current_line()
wrefresh(directory_content);
wrefresh(panel);
/* show file content every time cursor changes */
#if DRAW_PREVIEW
show_file_content();
#endif
wrefresh(preview_content);
}
@ -579,16 +607,21 @@ void show_file_content()
{
wclear(preview_content);
file *current_file = get_file((long) current_selection);
if (strncmp(current_file->type, "DIR", 3) == 0) return;
if (strncmp(current_file->type, "DIR", 3) == 0)
return;
FILE *file = fopen(current_file->path, "r");
if (file == NULL) {
mvwprintw(preview_content, 0, 0, "Unable to read %s", current_file->path);
return;
}
#if DRAW_BORDERS
draw_border_title(preview_border, true);
#endif
int c;
/* check binary */
/* check if its binary */
while ((c = fgetc(file)) != EOF) {
if (c == '\0') {
mvwprintw(preview_content, 0, 0, "binary");
@ -617,16 +650,23 @@ void show_file_content()
*/
void edit_file()
{
#ifdef EDITOR
char *editor = EDITOR;
#else
char *editor = getenv("EDITOR");
#endif
if (editor == NULL) {
wpprintw("Cannot get EDITOR variable, is it defined?");
return;
} else {
def_prog_mode(); /* save the tty modes */
endwin(); /* end curses mode temporarily */
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 null */
char command[length];
snprintf(command, length, "%s %s", editor, filename);
system(command);
reset_prog_mode(); /* return to previous tty mode */
@ -636,7 +676,7 @@ void edit_file()
}
/*
* Print line to panel
* Print line to the panel
*/
void wpprintw(const char *line)
{
@ -647,40 +687,60 @@ void wpprintw(const char *line)
void init_windows()
{
/* offset for width of the content 1 and 2 */
int width_left = half_width;
int width_right = half_width;
#ifdef WINDOW_OFFSET
width_left += WINDOW_OFFSET;
width_right -= WINDOW_OFFSET;
#endif
/*------------------------------+
|----border(0)--||--border(2)--||
|| || ||
|| content (1) || content (3) ||
|| || ||
|| (directory) || (preview) ||
|| || ||
|---------------||-------------||
+==========panel (4)===========*/
/* 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 );
/* draw border around windows */
#if DRAW_BORDERS
directory_border = newwin(LINES - PH, width_left, 0, 0 );
directory_content = newwin(LINES - PH - 2, width_left - 2, 1, 1 );
preview_border = newwin(LINES - PH, width_right, 0, width_left );
preview_content = newwin(LINES - PH - 2, width_right - 2, 1, width_left + 1);
draw_border_title(directory_border, true);
draw_border_title(preview_border, true);
#else
/* if there are no borders, then draw content in their places */
directory_border = newwin(0, 0, COLS, LINES );
preview_border = newwin(0, 0, COLS, LINES );
/* -1 for the one space to the left */
directory_content = newwin(LINES - PH - 1, width_left, 0, 1 );
preview_content = newwin(LINES - PH, width_right, 0, width_left );
#endif
scrollok(directory_content, true);
/* window location y, x */
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 };
}
/*
* Draw the border of the window depending if it's active or not
* Draw the border of the window depending if it's active or not,
*/
void draw_border_title(WINDOW *window, bool active)
{
/* check if the window is directory of preview */
int width = half_width;
if (window == directory_border) { /* left */
width += WINDOW_OFFSET;
} else if (window == preview_border) { /* right */
width -= WINDOW_OFFSET;
}
/* turn on color depends on active */
if (active) {
wattron(window, COLOR_PAIR(7));
@ -689,18 +749,18 @@ void draw_border_title(WINDOW *window, bool active)
}
/* 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 */
mvwhline(window, 0, 1, ACS_HLINE, width - 2); /* top horizontal line */
mvwaddch(window, 0, width - 1, ACS_URCORNER); /* upper right corner */
/* 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 */
mvwvline(window, 1, width - 1, ACS_VLINE, LINES - 2); /* right vertical line */
/* 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 */
mvwhline(window, LINES - PH - 1, 1, ACS_HLINE, width - 2); /* bottom horizontal line */
mvwaddch(window, LINES - PH - 1, width - 1, ACS_LRCORNER); /* lower right corner */
/* turn color off after turning it on */
if (active) {

View file

@ -1,3 +1,31 @@
/* Settings */
#define PH 1 /* panel height */
#define JUMP_NUM 14 /* how long ctrl + u/d jump are */
/* Calculate directories' sizes RECURSIVELY upon entering? */
#define DIRS_SIZE false
#define DRAW_BORDERS true /* Draw borders around windows? */
#define DRAW_PREVIEW true /* Draw file preview? */
/* set width offset for windows:
+-------------%-------------+
| % |
| files % preview |
| % |
+=============%=============+
set where the % line between them resides
In COLS:
0 will make them equal (at the center),
15 will make files bigger
-15 will make preview bigger */
#define WINDOW_OFFSET 0
/* Default text editor */
#define EDITOR "nvim"
/* Keybindings */
#define CTRLD 0x04
#define ENTER 0xA
#define CTRLU 0x15
@ -9,9 +37,3 @@
#define LEFT 0x104
#define RIGHT 0x105
#define BACKSPACE 0x107
#define PH 1 /* panel height */
#define JUMP_NUM 14 /* how long ctrl + u/d jump are */
#define BLOCK_SIZE true
#define DISK_USAGE false
static const char *editor = "nvim"; /* default text editor */

13
file.c
View file

@ -131,7 +131,10 @@ void remove_marked(file *marked_file)
}
}
long add_marked(char *filepath, char *type)
/*
* force will not remove duplicate marked files, instead it just skip adding
*/
long add_marked(char *filepath, char *type, bool force)
{
file *current = marked;
file *new_file = memalloc(sizeof(file));
@ -153,18 +156,26 @@ long add_marked(char *filepath, char *type)
long index = 1;
while (current->next != NULL) {
if (strcmp(current->path, new_file->path) == 0) {
if (force) {
return index;
} else {
remove_marked(current);
free_file(new_file);
return -1;
}
}
current = current->next;
index++;
}
if (strcmp(current->path, new_file->path) == 0){
if (force) {
return 0;
} else {
remove_marked(current);
free_file(new_file);
return -1;
}
}
current->next = new_file;
return index;
}

2
file.h
View file

@ -16,7 +16,7 @@ void clear_files();
void clear_marked();
long add_file(char *filepath, char *stats, char *type, int color);
void remove_marked(file *marked_file);
long add_marked(char *filepath, char *type);
long add_marked(char *filepath, char *type, bool force);
file *get_marked(long index);
bool in_marked(char *path);
file *get_file(long index);