Compare commits

..

No commits in common. "fc83b825e9e42ccf84e4cce63510ca4df6ec1c96" and "fdafdf4b99a2be92124df14eb49e2164f7b4202a" have entirely different histories.

2 changed files with 75 additions and 55 deletions

95
ccc.c
View file

@ -34,7 +34,7 @@ void list_files(void);
void show_file_content(void); void show_file_content(void);
void edit_file(void); void edit_file(void);
void toggle_executable(void); void toggle_executable(void);
void replace_home(char *str); char *replace_home(char *str);
int write_last_d(void); int write_last_d(void);
int sort_compare(const void *a, const void *b); int sort_compare(const void *a, const void *b);
void sort_files(void); void sort_files(void);
@ -55,6 +55,10 @@ unsigned int focus = 0;
long sel_file = 0; long sel_file = 0;
bool file_picker = false; bool file_picker = false;
bool to_open_file = false; bool to_open_file = false;
bool dirs_size = DIRS_SIZE;
bool show_hidden = SHOW_HIDDEN;
bool file_details = SHOW_DETAILS;
bool show_icons = SHOW_ICONS;
char *argv_cp; char *argv_cp;
char *cwd; char *cwd;
char *p_cwd; /* previous cwd */ char *p_cwd; /* previous cwd */
@ -195,9 +199,9 @@ int main(int argc, char **argv)
} else if (c_file.type == REG) { } else if (c_file.type == REG) {
/* Write opened file to a file for file pickers */ /* Write opened file to a file for file pickers */
if (file_picker) { if (file_picker) {
char opened_file_path[PATH_MAX]; char *opened_file_path = memalloc(PATH_MAX);
strcpy(opened_file_path, "~/.cache/ccc/opened_file"); strcpy(opened_file_path, "~/.cache/ccc/opened_file");
replace_home(opened_file_path); opened_file_path = replace_home(opened_file_path);
FILE *opened_file = fopen(opened_file_path, "w+"); FILE *opened_file = fopen(opened_file_path, "w+");
fprintf(opened_file, "%s\n", c_file.path); fprintf(opened_file, "%s\n", c_file.path);
fclose(opened_file); fclose(opened_file);
@ -211,8 +215,8 @@ int main(int argc, char **argv)
/* jump up */ /* jump up */
case CTRLU: case CTRLU:
if ((sel_file - jump_num) > 0) if ((sel_file - JUMP_NUM) > 0)
sel_file -= jump_num; sel_file -= JUMP_NUM;
else else
sel_file = 0; sel_file = 0;
@ -230,8 +234,8 @@ int main(int argc, char **argv)
/* jump down */ /* jump down */
case CTRLD: case CTRLD:
if ((sel_file + jump_num) < (files->length - 1)) if ((sel_file + JUMP_NUM) < (files->length - 1))
sel_file += jump_num; sel_file += JUMP_NUM;
else else
sel_file = (files->length - 1); sel_file = (files->length - 1);
@ -315,7 +319,7 @@ int main(int argc, char **argv)
/* toggle file details */ /* toggle file details */
case 'i': case 'i':
show_details = !show_details; file_details = !file_details;
change_dir(cwd, 0, 0); change_dir(cwd, 0, 0);
break; break;
@ -404,7 +408,7 @@ void handle_sigwinch(int ignore)
cleanup(); cleanup();
die("ccc: Terminal size needs to be at least 80x24"); die("ccc: Terminal size needs to be at least 80x24");
} }
half_width = cols / 2 + window_offset; half_width = cols / 2 + WINDOW_OFFSET;
list_files(); list_files();
} }
@ -450,16 +454,22 @@ char *check_trash_dir(void)
{ {
char *path = memalloc(PATH_MAX); char *path = memalloc(PATH_MAX);
char *trash_dir;
#ifdef TRASH_DIR
trash_dir = TRASH_DIR;
strcpy(path, trash_dir);
#endif
/* check if there is trash_dir */ /* check if there is trash_dir */
if (!strcmp(trash_dir, "")) { if (trash_dir == NULL) {
wpprintw("Trash directory not defined"); wpprintw("Trash directory not defined");
return NULL; return NULL;
} else { } else {
strcpy(path, trash_dir);
/* if trash_dir has ~ then make it $HOME */ /* if trash_dir has ~ then make it $HOME */
/* use path as trash_dir */ /* use path as trash_dir */
if (path[0] == '~') if (trash_dir[0] == '~') {
replace_home(path); path = replace_home(path);
}
/* if has access to trash_dir */ /* if has access to trash_dir */
if (access(path, F_OK) != 0) { if (access(path, F_OK) != 0) {
@ -673,10 +683,10 @@ void add_file_stat(char *filename, char *path, int ftype)
bytes = total_dir_size; bytes = total_dir_size;
} }
} }
/* 4 before decimal + 1 dot + decimal_place (after decimal) + /* 4 before decimal + 1 dot + DECIMAL_PLACES (after decimal) +
unit length (1 for K, 3 for KiB, taking units[1] as B never changes) + 1 space + 1 null */ unit length (1 for K, 3 for KiB, taking units[1] as B never changes) + 1 space + 1 null */
static const char* units[] = {"B", "K", "M", "G", "T", "P"}; static const char* units[] = {"B", "K", "M", "G", "T", "P"};
int size_size = 4 + 1 + decimal_place + strlen(units[1]) + 1 + 1; int size_size = 4 + 1 + DECIMAL_PLACES + strlen(units[1]) + 1 + 1;
char *size = memalloc(size_size); char *size = memalloc(size_size);
int unit = 0; int unit = 0;
while (bytes > 1024) { while (bytes > 1024) {
@ -687,7 +697,7 @@ void add_file_stat(char *filename, char *path, int ftype)
if (bytes == (unsigned int) bytes) { if (bytes == (unsigned int) bytes) {
sprintf(size, "%d%s", (unsigned int) bytes, units[unit]); sprintf(size, "%d%s", (unsigned int) bytes, units[unit]);
} else { } else {
sprintf(size, "%.*f%s", decimal_place, bytes, units[unit]); sprintf(size, "%.*f%s", DECIMAL_PLACES, bytes, units[unit]);
} }
/* get file mode string */ /* get file mode string */
char *mode_str = get_file_mode(file_stat.st_mode); char *mode_str = get_file_mode(file_stat.st_mode);
@ -785,7 +795,7 @@ void list_files(void)
} }
} }
/* print the actual filename and stats */ /* print the actual filename and stats */
char *line = get_line(files, i, show_details, show_icons); char *line = get_line(files, i, file_details, show_icons);
int color = files->items[i].color; int color = files->items[i].color;
/* check is file marked for action */ /* check is file marked for action */
bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1; bool is_marked = arraylist_search(marked, files->items[i].path, false) != -1;
@ -978,12 +988,15 @@ void show_file_content(void)
*/ */
void edit_file(void) void edit_file(void)
{ {
if (editor == NULL) { #ifdef EDITOR
editor = getenv("EDITOR"); char *editor = EDITOR;
#else
char *editor = getenv("EDITOR");
#endif
if (editor == NULL) { if (editor == NULL) {
wpprintw("$EDITOR not defined"); wpprintw("$EDITOR not defined");
return; return;
}
} else { } else {
char *filename = files->items[sel_file].path; char *filename = files->items[sel_file].path;
@ -1019,34 +1032,35 @@ void toggle_executable(void)
} }
void replace_home(char *str) char *replace_home(char *str)
{ {
char *home = getenv("HOME"); char *home = getenv("HOME");
if (home == NULL) { if (home == NULL) {
wpprintw("$HOME not defined"); wpprintw("$HOME not defined");
return; return str;
} }
int len = strlen(str) + strlen(home) + 1; char *newstr = memalloc(strlen(str) + strlen(home));
char newstr[len];
/* replace ~ with home */ /* replace ~ with home */
snprintf(newstr, len, "%s%s", home, str + 1); snprintf(newstr, strlen(str) + strlen(home), "%s%s", home, str + 1);
strcpy(str, newstr); free(str);
return newstr;
} }
int write_last_d(void) int write_last_d(void)
{ {
if (!strcmp(last_d, "")) { #ifdef LAST_D
strcpy(last_d, getenv("CCC_LAST_D")); char *last_d = estrdup(LAST_D);
if (!strcmp(last_d, "")) { #else
char *last_d = getenv("CCC_LAST_D");
#endif
if (last_d == NULL) {
wpprintw("$CCC_LAST_D not defined (Press enter to continue)"); wpprintw("$CCC_LAST_D not defined (Press enter to continue)");
return -1; return -1;
}
} else { } else {
if (last_d[0] == '~') if (last_d[0] == '~') {
replace_home(last_d); last_d = replace_home(last_d);
}
char last_ddup[PATH_MAX]; char *last_ddup = estrdup(last_d);
strcpy(last_ddup, last_d);
char *last_d_dir = strrchr(last_ddup, '/'); char *last_d_dir = strrchr(last_ddup, '/');
if (last_d_dir != NULL) { if (last_d_dir != NULL) {
@ -1060,6 +1074,8 @@ int write_last_d(void)
} }
fwrite(cwd, strlen(cwd), sizeof(char), last_d_file); fwrite(cwd, strlen(cwd), sizeof(char), last_d_file);
fclose(last_d_file); fclose(last_d_file);
free(last_ddup);
free(last_d);
} }
return 0; return 0;
} }
@ -1106,11 +1122,12 @@ char *get_panel_string(char *prompt)
buf[buflen] = '\0'; buf[buflen] = '\0';
} }
} }
char *input = memalloc(PATH_MAX);
if (buf[0] == '~') if (input[0] == '~') {
replace_home(buf); input = replace_home(input);
}
return buf; return input;
} }
void rename_file(void) void rename_file(void)

View file

@ -1,8 +1,11 @@
#define PATH_MAX 4096 /* Max length of path */
/* Settings */ /* Settings */
static int panel_height = 1; /* panel height */ #define PH 1 /* panel height */
static int jump_num = 14; /* Length of ctrl + u/d jump */ #define JUMP_NUM 14 /* how long ctrl + u/d jump are */
static int decimal_place = 1; /* Number of decimal places size can be shown */ #define PATH_MAX 4096 /* max length of the path */
#define DECIMAL_PLACES 1 /* how many decimal places show size with */
/* Size units */
static const char* units[] = {"B", "K", "M", "G", "T", "P"};
/* Set width offset for windows: /* Set width offset for windows:
+-------------%-------------+ +-------------%-------------+
@ -16,26 +19,26 @@ In COLS:
0 will make them equal (at the center), 0 will make them equal (at the center),
15 will make files bigger 15 will make files bigger
-15 will make preview bigger */ -15 will make preview bigger */
static int window_offset = -30; #define WINDOW_OFFSET -30
/* Options */ /* Options */
static int show_hidden = 1; /* Show hidden files/dotfiles at startup */ #define SHOW_HIDDEN true /* show hidden files/dotfiles at startup */
static int show_details = 0; /* Show file details at startup */ #define SHOW_DETAILS false /* show file details at startup */
static int show_icons = 1; /* Show file icons at startup */ #define SHOW_ICONS true /* show file icons at startup */
/* Calculate directories' sizes RECURSIVELY upon entering /* Calculate directories' sizes RECURSIVELY upon entering
`A` keybind at the startup `A` keybind at the startup
**VERY EXPENSIVE**, **CAN TAKE UP TO A MINUTE IN ROOT** */ **VERY EXPENSIVE**, **CAN TAKE UP TO A MINUTE IN ROOT** */
static int dirs_size = 0; #define DIRS_SIZE false
/* Default text editor */ /* Default text editor */
static const char *editor = "nvim"; #define EDITOR "nvim"
/* File location to write last directory */ /* File location to write last directory */
static char last_d[PATH_MAX] = "~/.cache/ccc/.ccc_d"; #define LAST_D "~/.cache/ccc/.ccc_d"
/* Will create this directory if doesn't exist! */ /* Will create this directory if doesn't exist! */
static char trash_dir[PATH_MAX] = "~/.cache/ccc/trash/"; #define TRASH_DIR "~/.cache/ccc/trash/"
/* Keybindings */ /* Keybindings */
#define CTRLD 0x04 #define CTRLD 0x04