Minor refactoring and changes in config.h

This commit is contained in:
Piotr Marendowski 2024-03-16 21:37:56 +01:00
parent 5f6c4ee903
commit b2b682e96c
3 changed files with 59 additions and 55 deletions

View file

@ -6,9 +6,6 @@ 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 |

56
ccc.c
View file

@ -39,7 +39,7 @@ 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]; WIN_STRUCT windows[5];
@ -241,7 +241,7 @@ int main(int argc, char** argv)
/* a to toggle between DISK_USAGE and BLOCK SIZE */ /* a to toggle between DISK_USAGE and BLOCK SIZE */
case 'a': case 'a':
dir_mode = !dir_mode; dirs_size = !dirs_size;
clear_files(); clear_files();
populate_files(cwd); populate_files(cwd);
highlight_current_line(); highlight_current_line();
@ -292,6 +292,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 +306,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);
@ -382,7 +381,7 @@ 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: 0 = normal file, 1 = marked
*/ */
long add_file_stat(char *filepath, int ftype) long add_file_stat(char *filepath, int ftype)
{ {
@ -402,8 +401,8 @@ long add_file_stat(char *filepath, int ftype)
/* get file size */ /* get file size */
double bytes = file_stat.st_size; double bytes = file_stat.st_size;
if (!dir_mode) { if (dirs_size) {
/* dir_mode is 0, so disk usage mode, calculate disk usage */ /* dirs_size is true, so calculate disk usage */
if (S_ISDIR(file_stat.st_mode)) { if (S_ISDIR(file_stat.st_mode)) {
/* at most 15 fd opened */ /* at most 15 fd opened */
total_dir_size = 0; total_dir_size = 0;
@ -461,8 +460,8 @@ long add_file_stat(char *filepath, int ftype)
return index; return index;
} }
/* already marked */ /* already marked */
return -1;
/* -1 does nothing, just function required to return something */ /* -1 does nothing, just function required to return something */
return -1;
} }
char *total_stat = memalloc(45 * sizeof(char)); char *total_stat = memalloc(45 * sizeof(char));
@ -501,9 +500,9 @@ void highlight_current_line()
} }
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,13 +515,6 @@ 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) {
@ -531,9 +523,9 @@ void highlight_current_line()
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 */
@ -579,7 +571,10 @@ 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);
@ -588,7 +583,7 @@ void show_file_content()
draw_border_title(preview_border, true); draw_border_title(preview_border, true);
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 +612,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 */

View file

@ -1,3 +1,14 @@
/* Settings */
#define PH 1 /* panel height */
#define JUMP_NUM 14 /* how long ctrl + u/d jump are */
/* Calculate directories' sizes upon entering? */
#define DIRS_SIZE false
/* 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 +20,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 */