Minor refactoring and changes in config.h
This commit is contained in:
parent
5f6c4ee903
commit
b2b682e96c
3 changed files with 59 additions and 55 deletions
|
@ -6,9 +6,6 @@ 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 |
|
||||
|
|
56
ccc.c
56
ccc.c
|
@ -39,7 +39,7 @@ 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];
|
||||
|
@ -241,7 +241,7 @@ int main(int argc, char** argv)
|
|||
|
||||
/* a to toggle between DISK_USAGE and BLOCK SIZE */
|
||||
case 'a':
|
||||
dir_mode = !dir_mode;
|
||||
dirs_size = !dirs_size;
|
||||
clear_files();
|
||||
populate_files(cwd);
|
||||
highlight_current_line();
|
||||
|
@ -292,6 +292,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 +306,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);
|
||||
|
@ -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
|
||||
* 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)
|
||||
{
|
||||
|
@ -402,8 +401,8 @@ long add_file_stat(char *filepath, int ftype)
|
|||
/* get file size */
|
||||
double bytes = file_stat.st_size;
|
||||
|
||||
if (!dir_mode) {
|
||||
/* dir_mode is 0, so disk usage mode, calculate disk usage */
|
||||
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;
|
||||
|
@ -461,8 +460,8 @@ long add_file_stat(char *filepath, int ftype)
|
|||
return index;
|
||||
}
|
||||
/* already marked */
|
||||
return -1;
|
||||
/* -1 does nothing, just function required to return something */
|
||||
return -1;
|
||||
|
||||
}
|
||||
char *total_stat = memalloc(45 * sizeof(char));
|
||||
|
@ -501,9 +500,9 @@ void highlight_current_line()
|
|||
}
|
||||
|
||||
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,13 +515,6 @@ 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) {
|
||||
|
@ -531,9 +523,9 @@ void highlight_current_line()
|
|||
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);
|
||||
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 */
|
||||
|
@ -579,7 +571,10 @@ 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);
|
||||
|
@ -588,7 +583,7 @@ void show_file_content()
|
|||
draw_border_title(preview_border, true);
|
||||
|
||||
int c;
|
||||
/* check binary */
|
||||
/* check if its binary */
|
||||
while ((c = fgetc(file)) != EOF) {
|
||||
if (c == '\0') {
|
||||
mvwprintw(preview_content, 0, 0, "binary");
|
||||
|
@ -617,16 +612,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 */
|
||||
|
|
17
config.h
17
config.h
|
@ -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 ENTER 0xA
|
||||
#define CTRLU 0x15
|
||||
|
@ -9,9 +20,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 */
|
||||
|
|
Loading…
Reference in a new issue