commit
5859c61af7
7 changed files with 370 additions and 196 deletions
2
Makefile
2
Makefile
|
@ -12,7 +12,7 @@ MANDIR = $(PREFIX)/share/man/man1
|
||||||
|
|
||||||
# Flags
|
# Flags
|
||||||
LDFLAGS = $(shell pkg-config --libs ncurses)
|
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)
|
SRC = ccc.c util.c file.c $(CONF)
|
||||||
|
|
||||||
|
|
76
README.md
76
README.md
|
@ -6,41 +6,75 @@ 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 of fff | Ported | Dropped |
|
||||||
|--------------------------------|:------:|:-------:|:-----:|
|
|--------------------------------|:------:|:-------:|
|
||||||
| Standard movement | X | | |
|
| Standard movement | X | |
|
||||||
| Advanced movement (jumps) | X | | |
|
| Advanced movement (jumps) | X | |
|
||||||
| Searching | | | |
|
| File details | X | |
|
||||||
| File preview | | | X |
|
| Searching for files | | |
|
||||||
| Sorting | | | |
|
| Sorting | | |
|
||||||
| Marking and marking operations | | | |
|
| Marking and marking operations | | |
|
||||||
| Other operations on files | | | |
|
| Other operations on files | | |
|
||||||
| File details | X | | |
|
| Image previews | | |
|
||||||
| Image previews | | | |
|
| Help | | |
|
||||||
| Help | | | |
|
| History | | |
|
||||||
| History | | | |
|
| Bookmarks | | |
|
||||||
| Bookmarks | | | |
|
| Bulk rename | | |
|
||||||
| Bulk rename | | | |
|
|
||||||
| Workspaces | | | |
|
#### Features added that are not in [fff](https://github.com/piotr-marendowski/fff):
|
||||||
|
|
||||||
|
- File preview (without highlighting)
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
|
|
||||||
- gcc
|
- gcc
|
||||||
|
- ncurses
|
||||||
- make
|
- make
|
||||||
- pkg-config
|
- pkg-config
|
||||||
- ncurses
|
|
||||||
|
|
||||||
### Building
|
### Building
|
||||||
|
|
||||||
You will need to run these with elevated privilages.
|
You will need to run these with elevated privilages.
|
||||||
|
|
||||||
```sh
|
```
|
||||||
$ git clone https://github.com/piotr-marendowski/ccc
|
$ git clone https://github.com/piotr-marendowski/ccc
|
||||||
$ make
|
$ make
|
||||||
$ sudo make install
|
$ 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
47
ccc.1
|
@ -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
326
ccc.c
|
@ -16,17 +16,10 @@
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
WINDOW *window;
|
|
||||||
int location;
|
|
||||||
int y;
|
|
||||||
int x;
|
|
||||||
} WIN_STRUCT;
|
|
||||||
|
|
||||||
/* functions' definitions */
|
/* functions' definitions */
|
||||||
void change_dir(const char *buf);
|
void change_dir(const char *buf, int selection);
|
||||||
int mkdir_p(const char *destdir);
|
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);
|
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
|
||||||
long add_file_stat(char *filepath, int ftype);
|
long add_file_stat(char *filepath, int ftype);
|
||||||
void highlight_current_line();
|
void highlight_current_line();
|
||||||
|
@ -39,10 +32,9 @@ 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;
|
||||||
bool dir_mode = BLOCK_SIZE;
|
bool dirs_size = DIRS_SIZE;
|
||||||
char *cwd;
|
char *cwd;
|
||||||
int half_width;
|
int half_width;
|
||||||
WIN_STRUCT windows[5];
|
|
||||||
WINDOW *directory_border;
|
WINDOW *directory_border;
|
||||||
WINDOW *directory_content;
|
WINDOW *directory_content;
|
||||||
WINDOW *preview_border;
|
WINDOW *preview_border;
|
||||||
|
@ -95,12 +87,9 @@ int main(int argc, char** argv)
|
||||||
cwd = memalloc(PATH_MAX * sizeof(char));
|
cwd = memalloc(PATH_MAX * sizeof(char));
|
||||||
getcwd(cwd, PATH_MAX);
|
getcwd(cwd, PATH_MAX);
|
||||||
|
|
||||||
populate_files(cwd);
|
populate_files(cwd, 0);
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
|
|
||||||
/* set window name */
|
|
||||||
printf("%c]2;ccc: %s%c", ESC, cwd, ESC);
|
|
||||||
|
|
||||||
int ch, ch2;
|
int ch, ch2;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (COLS < 80 || LINES < 24) {
|
if (COLS < 80 || LINES < 24) {
|
||||||
|
@ -117,7 +106,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
/* reload using z */
|
/* reload using z */
|
||||||
case 'z':
|
case 'z':
|
||||||
change_dir(cwd);
|
change_dir(cwd, 0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* go back by backspace or h or left arrow */
|
/* go back by backspace or h or left arrow */
|
||||||
|
@ -128,7 +117,7 @@ int main(int argc, char** argv)
|
||||||
char *last_slash = strrchr(cwd, '/');
|
char *last_slash = strrchr(cwd, '/');
|
||||||
if (last_slash != NULL) {
|
if (last_slash != NULL) {
|
||||||
*last_slash = '\0';
|
*last_slash = '\0';
|
||||||
change_dir(cwd);
|
change_dir(cwd, 0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -138,20 +127,15 @@ int main(int argc, char** argv)
|
||||||
case 'l':;
|
case 'l':;
|
||||||
file *file = get_file(current_selection);
|
file *file = get_file(current_selection);
|
||||||
if (file != NULL) {
|
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) {
|
if (strncmp(file->type, "DIR", 3) == 0) {
|
||||||
/* change cwd to directory */
|
/* change cwd to directory */
|
||||||
change_dir(file->path);
|
change_dir(file->path, 0);
|
||||||
} else if (strncmp(file->type, "REG", 3) == 0) {
|
} else if (strncmp(file->type, "REG", 3) == 0) {
|
||||||
edit_file();
|
edit_file();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/*
|
|
||||||
if (focus == 0) focus++;
|
|
||||||
else if (focus == 1) focus--;
|
|
||||||
break;
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* jump up (ctrl u) */
|
/* jump up (ctrl u) */
|
||||||
case CTRLU:
|
case CTRLU:
|
||||||
|
@ -210,19 +194,23 @@ int main(int argc, char** argv)
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* ~ to go home */
|
/* '~' go to $HOME */
|
||||||
case TILDE:;
|
case TILDE:;
|
||||||
char *home = getenv("HOME");
|
char *home = getenv("HOME");
|
||||||
if (home == NULL) {
|
if (home == NULL) {
|
||||||
wpprintw("$HOME is not defined");
|
wpprintw("$HOME is not defined");
|
||||||
} else {
|
} else {
|
||||||
change_dir(home);
|
change_dir(home, 0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* t to go to trash dir */
|
/* go to the trash dir */
|
||||||
case 't':;
|
case 't':;
|
||||||
|
#ifdef TRASH_DIR
|
||||||
|
char *trash_dir = TRASH_DIR;
|
||||||
|
#else
|
||||||
char *trash_dir = getenv("CCC_TRASH");
|
char *trash_dir = getenv("CCC_TRASH");
|
||||||
|
#endif
|
||||||
if (trash_dir == NULL) {
|
if (trash_dir == NULL) {
|
||||||
wpprintw("$CCC_TRASH is not defined");
|
wpprintw("$CCC_TRASH is not defined");
|
||||||
} else {
|
} else {
|
||||||
|
@ -231,36 +219,80 @@ int main(int argc, char** argv)
|
||||||
if (mkdir_p(trash_dir) == -1) {
|
if (mkdir_p(trash_dir) == -1) {
|
||||||
switch (errno) {
|
switch (errno) {
|
||||||
case EACCES:
|
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;
|
break;
|
||||||
|
|
||||||
/* a to toggle between DISK_USAGE and BLOCK SIZE */
|
/* show directories' sizes */
|
||||||
case 'a':
|
case 'A':
|
||||||
dir_mode = !dir_mode;
|
dirs_size = !dirs_size;
|
||||||
clear_files();
|
clear_files();
|
||||||
populate_files(cwd);
|
populate_files(cwd, 0);
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* mark files by space */
|
/* mark one file */
|
||||||
case SPACE:;
|
case SPACE:
|
||||||
add_file_stat(get_filepath(current_selection), 1);
|
add_file_stat(get_filepath(current_selection), 1);
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
break;
|
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 */
|
/* escape */
|
||||||
case ESC:
|
case ESC:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case KEY_RESIZE:
|
case KEY_RESIZE:
|
||||||
for (int i = 0; i < 2; i++) {
|
delwin(directory_border);
|
||||||
delwin(windows[i].window);
|
delwin(directory_content);
|
||||||
}
|
delwin(preview_border);
|
||||||
|
delwin(preview_content);
|
||||||
|
delwin(panel);
|
||||||
endwin();
|
endwin();
|
||||||
init_windows();
|
init_windows();
|
||||||
break;
|
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);
|
strcpy(cwd, buf);
|
||||||
clear_files();
|
clear_files();
|
||||||
populate_files(cwd);
|
populate_files(cwd, 0);
|
||||||
current_selection = 0;
|
current_selection = selection;
|
||||||
highlight_current_line();
|
highlight_current_line();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -292,6 +324,9 @@ void change_dir(const char *buf)
|
||||||
int mkdir_p(const char *destdir)
|
int mkdir_p(const char *destdir)
|
||||||
{
|
{
|
||||||
char *path = memalloc(PATH_MAX * sizeof(char));
|
char *path = memalloc(PATH_MAX * sizeof(char));
|
||||||
|
char *token = strtok(path, "/");
|
||||||
|
char dir_path[PATH_MAX] = "";
|
||||||
|
|
||||||
strcpy(path, destdir);
|
strcpy(path, destdir);
|
||||||
if (destdir[0] == '~') {
|
if (destdir[0] == '~') {
|
||||||
char *home = getenv("HOME");
|
char *home = getenv("HOME");
|
||||||
|
@ -303,13 +338,9 @@ int mkdir_p(const char *destdir)
|
||||||
memmove(path + strlen(home), path + 1, strlen(path));
|
memmove(path + strlen(home), path + 1, strlen(path));
|
||||||
memcpy(path, home, strlen(home));
|
memcpy(path, home, strlen(home));
|
||||||
}
|
}
|
||||||
char *token = strtok(path, "/");
|
/* fix first / not appearing in the string */
|
||||||
char dir_path[PATH_MAX] = "";
|
if (path[0] == '/')
|
||||||
|
|
||||||
/* fix first / is not appearing in string */
|
|
||||||
if (path[0] == '/') {
|
|
||||||
strcat(dir_path, "/");
|
strcat(dir_path, "/");
|
||||||
}
|
|
||||||
|
|
||||||
while (token != NULL) {
|
while (token != NULL) {
|
||||||
strcat(dir_path, token);
|
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
|
* 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
|
* ep->d_name -> filename
|
||||||
*/
|
*/
|
||||||
void populate_files(const char *path)
|
void populate_files(const char *path, int ftype)
|
||||||
{
|
{
|
||||||
DIR *dp;
|
DIR *dp;
|
||||||
struct dirent *ep;
|
struct dirent *ep;
|
||||||
|
|
||||||
|
#if DRAW_BORDERS
|
||||||
draw_border_title(directory_border, true);
|
draw_border_title(directory_border, true);
|
||||||
|
#endif
|
||||||
if ((dp = opendir(path)) != NULL) {
|
if ((dp = opendir(path)) != NULL) {
|
||||||
/* clear directory window to ready for printing */
|
/* clear directory window to ready for printing */
|
||||||
wclear(directory_content);
|
wclear(directory_content);
|
||||||
|
@ -362,7 +396,7 @@ void populate_files(const char *path)
|
||||||
strcat(filename, "/");
|
strcat(filename, "/");
|
||||||
strcat(filename, ep->d_name); /* add filename */
|
strcat(filename, ep->d_name); /* add filename */
|
||||||
|
|
||||||
add_file_stat(filename, 0);
|
add_file_stat(filename, ftype);
|
||||||
}
|
}
|
||||||
free(filename);
|
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
|
* Get file's last modified time, size, type
|
||||||
* Add that file into list
|
* 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)
|
long add_file_stat(char *filepath, int ftype)
|
||||||
{
|
{
|
||||||
struct stat file_stat;
|
struct stat file_stat;
|
||||||
if (stat(filepath, &file_stat) == -1) {
|
if (stat(filepath, &file_stat) == -1) {
|
||||||
/* can't be triggered? */
|
/* can't be triggered? */
|
||||||
if (errno == EACCES) {
|
if (errno == EACCES)
|
||||||
return add_file(filepath, "", "", 8);
|
return add_file(filepath, "", "", 8);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* get last modified time */
|
/* get file type and color, 4 chars for the type */
|
||||||
char *time = memalloc(20 * sizeof(char));
|
char *type = memalloc(4 * 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 */
|
|
||||||
int color;
|
int color;
|
||||||
if (S_ISDIR(file_stat.st_mode)) {
|
if (S_ISDIR(file_stat.st_mode)) {
|
||||||
strcpy(type, "DIR"); /* directory type */
|
strcpy(type, "DIR"); /* directory type */
|
||||||
|
@ -449,31 +454,57 @@ long add_file_stat(char *filepath, int ftype)
|
||||||
} else {
|
} else {
|
||||||
color = 8; /* white color */
|
color = 8; /* white color */
|
||||||
}
|
}
|
||||||
/* don't know how to handle socket, block device, character device and FIFO */
|
|
||||||
|
|
||||||
if (ftype == 1) {
|
/* if file is to be marked */
|
||||||
long index = add_marked(filepath, type);
|
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(type);
|
||||||
free(size);
|
|
||||||
free(time);
|
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
/* just marked */
|
return index; /* just marked */
|
||||||
return index;
|
} 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));
|
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';
|
total_stat[strlen(total_stat)] = '\0';
|
||||||
|
|
||||||
free(time);
|
|
||||||
free(size);
|
|
||||||
|
|
||||||
long index = add_file(filepath, total_stat, type, color);
|
long index = add_file(filepath, total_stat, type, color);
|
||||||
|
|
||||||
|
free(time);
|
||||||
|
free(size);
|
||||||
free(total_stat);
|
free(total_stat);
|
||||||
free(type);
|
free(type);
|
||||||
return index;
|
return index;
|
||||||
|
@ -495,15 +526,17 @@ void highlight_current_line()
|
||||||
long range = files_len();
|
long range = files_len();
|
||||||
/* not highlight if no files in directory */
|
/* not highlight if no files in directory */
|
||||||
if (range == 0) {
|
if (range == 0) {
|
||||||
|
#if DRAW_PREVIEW
|
||||||
wprintw(preview_content, "empty directory");
|
wprintw(preview_content, "empty directory");
|
||||||
wrefresh(preview_content);
|
wrefresh(preview_content);
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (range > LINES - 3) {
|
if (range > LINES - 3) {
|
||||||
/* if there is more files than lines available to display*/
|
/* if there are more files than lines available to display
|
||||||
/* shrink range to avaiable lines to display */
|
* shrink range to avaiable lines to display with
|
||||||
/* with overflow to keep number of iterations to be constant */
|
* overflow to keep the number of iterations to be constant */
|
||||||
range = LINES - 3 + overflow;
|
range = LINES - 3 + overflow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,24 +549,17 @@ void highlight_current_line()
|
||||||
/* update the panel */
|
/* update the panel */
|
||||||
wclear(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 */
|
/* check for marked files */
|
||||||
long num_marked = marked_len();
|
long num_marked = marked_len();
|
||||||
if (num_marked > 0) {
|
if (num_marked > 0) {
|
||||||
/* Determine length of formatted string */
|
/* 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));
|
char *selected = memalloc((m_len + 1) * sizeof(char));
|
||||||
|
|
||||||
snprintf(selected, m_len + 1, "(%ld selected)", num_marked);
|
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);
|
wprintw(panel, "(%ld/%ld) %s %s", current_selection + 1, files_len(), selected, cwd);
|
||||||
} else {
|
} 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 */
|
/* print the actual filename and stats */
|
||||||
|
@ -568,7 +594,9 @@ void highlight_current_line()
|
||||||
wrefresh(directory_content);
|
wrefresh(directory_content);
|
||||||
wrefresh(panel);
|
wrefresh(panel);
|
||||||
/* show file content every time cursor changes */
|
/* show file content every time cursor changes */
|
||||||
|
#if DRAW_PREVIEW
|
||||||
show_file_content();
|
show_file_content();
|
||||||
|
#endif
|
||||||
wrefresh(preview_content);
|
wrefresh(preview_content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -579,16 +607,21 @@ void show_file_content()
|
||||||
{
|
{
|
||||||
wclear(preview_content);
|
wclear(preview_content);
|
||||||
file *current_file = get_file((long) current_selection);
|
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");
|
FILE *file = fopen(current_file->path, "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
mvwprintw(preview_content, 0, 0, "Unable to read %s", current_file->path);
|
mvwprintw(preview_content, 0, 0, "Unable to read %s", current_file->path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#if DRAW_BORDERS
|
||||||
draw_border_title(preview_border, true);
|
draw_border_title(preview_border, true);
|
||||||
|
#endif
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
/* check binary */
|
/* check if its binary */
|
||||||
while ((c = fgetc(file)) != EOF) {
|
while ((c = fgetc(file)) != EOF) {
|
||||||
if (c == '\0') {
|
if (c == '\0') {
|
||||||
mvwprintw(preview_content, 0, 0, "binary");
|
mvwprintw(preview_content, 0, 0, "binary");
|
||||||
|
@ -617,16 +650,23 @@ void show_file_content()
|
||||||
*/
|
*/
|
||||||
void edit_file()
|
void edit_file()
|
||||||
{
|
{
|
||||||
|
#ifdef EDITOR
|
||||||
|
char *editor = EDITOR;
|
||||||
|
#else
|
||||||
char *editor = getenv("EDITOR");
|
char *editor = getenv("EDITOR");
|
||||||
|
#endif
|
||||||
|
|
||||||
if (editor == NULL) {
|
if (editor == NULL) {
|
||||||
wpprintw("Cannot get EDITOR variable, is it defined?");
|
wpprintw("Cannot get EDITOR variable, is it defined?");
|
||||||
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_filepath(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 null */
|
||||||
char command[length];
|
char command[length];
|
||||||
|
|
||||||
snprintf(command, length, "%s %s", editor, filename);
|
snprintf(command, length, "%s %s", editor, filename);
|
||||||
system(command);
|
system(command);
|
||||||
reset_prog_mode(); /* return to previous tty mode */
|
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)
|
void wpprintw(const char *line)
|
||||||
{
|
{
|
||||||
|
@ -647,40 +687,60 @@ void wpprintw(const char *line)
|
||||||
|
|
||||||
void init_windows()
|
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)--||
|
|----border(0)--||--border(2)--||
|
||||||
|| || ||
|
|| || ||
|
||||||
|| content (1) || content (3) ||
|
|| content (1) || content (3) ||
|
||||||
|| || ||
|
|| (directory) || (preview) ||
|
||||||
|| || ||
|
|| || ||
|
||||||
|---------------||-------------||
|
|---------------||-------------||
|
||||||
+==========panel (4)===========*/
|
+==========panel (4)===========*/
|
||||||
|
|
||||||
/* lines, cols, y, x */
|
/* 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 );
|
panel = newwin(PH, COLS, LINES - PH, 0 );
|
||||||
|
|
||||||
/* draw border around windows */
|
/* 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(directory_border, true);
|
||||||
draw_border_title(preview_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);
|
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)
|
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 */
|
/* turn on color depends on active */
|
||||||
if (active) {
|
if (active) {
|
||||||
wattron(window, COLOR_PAIR(7));
|
wattron(window, COLOR_PAIR(7));
|
||||||
|
@ -689,18 +749,18 @@ void draw_border_title(WINDOW *window, bool active)
|
||||||
}
|
}
|
||||||
/* draw top border */
|
/* draw top border */
|
||||||
mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */
|
mvwaddch(window, 0, 0, ACS_ULCORNER); /* upper left corner */
|
||||||
mvwhline(window, 0, 1, ACS_HLINE, half_width - 2); /* top horizontal line */
|
mvwhline(window, 0, 1, ACS_HLINE, width - 2); /* top horizontal line */
|
||||||
mvwaddch(window, 0, half_width - 1, ACS_URCORNER); /* upper right corner */
|
mvwaddch(window, 0, width - 1, ACS_URCORNER); /* upper right corner */
|
||||||
|
|
||||||
/* draw side border */
|
/* draw side border */
|
||||||
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, width - 1, ACS_VLINE, LINES - 2); /* right vertical line */
|
||||||
|
|
||||||
/* draw bottom border
|
/* draw bottom border
|
||||||
* make space for the panel */
|
* make space for the panel */
|
||||||
mvwaddch(window, LINES - PH - 1, 0, ACS_LLCORNER); /* lower left corner */
|
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 */
|
mvwhline(window, LINES - PH - 1, 1, ACS_HLINE, width - 2); /* bottom horizontal line */
|
||||||
mvwaddch(window, LINES - PH - 1, half_width - 1, ACS_LRCORNER); /* lower right corner */
|
mvwaddch(window, LINES - PH - 1, 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) {
|
||||||
|
|
34
config.h
34
config.h
|
@ -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 CTRLD 0x04
|
||||||
#define ENTER 0xA
|
#define ENTER 0xA
|
||||||
#define CTRLU 0x15
|
#define CTRLU 0x15
|
||||||
|
@ -9,9 +37,3 @@
|
||||||
#define LEFT 0x104
|
#define LEFT 0x104
|
||||||
#define RIGHT 0x105
|
#define RIGHT 0x105
|
||||||
#define BACKSPACE 0x107
|
#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
13
file.c
|
@ -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 *current = marked;
|
||||||
file *new_file = memalloc(sizeof(file));
|
file *new_file = memalloc(sizeof(file));
|
||||||
|
@ -153,18 +156,26 @@ long add_marked(char *filepath, char *type)
|
||||||
long index = 1;
|
long index = 1;
|
||||||
while (current->next != NULL) {
|
while (current->next != NULL) {
|
||||||
if (strcmp(current->path, new_file->path) == 0) {
|
if (strcmp(current->path, new_file->path) == 0) {
|
||||||
|
if (force) {
|
||||||
|
return index;
|
||||||
|
} else {
|
||||||
remove_marked(current);
|
remove_marked(current);
|
||||||
free_file(new_file);
|
free_file(new_file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
if (strcmp(current->path, new_file->path) == 0){
|
if (strcmp(current->path, new_file->path) == 0){
|
||||||
|
if (force) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
remove_marked(current);
|
remove_marked(current);
|
||||||
free_file(new_file);
|
free_file(new_file);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
current->next = new_file;
|
current->next = new_file;
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
2
file.h
2
file.h
|
@ -16,7 +16,7 @@ void clear_files();
|
||||||
void clear_marked();
|
void clear_marked();
|
||||||
long add_file(char *filepath, char *stats, char *type, int color);
|
long add_file(char *filepath, char *stats, char *type, int color);
|
||||||
void remove_marked(file *marked_file);
|
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);
|
file *get_marked(long index);
|
||||||
bool in_marked(char *path);
|
bool in_marked(char *path);
|
||||||
file *get_file(long index);
|
file *get_file(long index);
|
||||||
|
|
Loading…
Reference in a new issue