sfm

simple file manager
git clone git://git.afify.dev/sfm
Log | Files | Refs | README | LICENSE

commit e86127b62bf7685b52ca5a45452f0103a503544d
parent 8983e49cb7e79478c75e66b4a52635b8bc41eb3d
Author: afify <hassan@afify.dev>
Date:   Wed,  1 Jul 2020 09:44:10 +0300

[feat] add bookmarks, fix config color var

* add new feature bookmarks
* add static const to colors and unicode box shapes

Diffstat:
Mconfig.def.h | 61++++++++++++++++++++++++++++++++++---------------------------
Msfm.c | 77++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
2 files changed, 84 insertions(+), 54 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -6,36 +6,43 @@ #include "termbox.h" /* colors */ -uint16_t dir_b = TB_DEFAULT; -uint16_t dir_f = 33; -uint16_t file_b = TB_DEFAULT; -uint16_t file_f = TB_DEFAULT; -uint16_t frame_b = TB_DEFAULT; -uint16_t frame_f = TB_DEFAULT; -uint16_t other_b = TB_DEFAULT; -uint16_t other_f = 3; -uint16_t pane_l_b = TB_DEFAULT; -uint16_t pane_l_f = 166; -uint16_t pane_r_b = TB_DEFAULT; -uint16_t pane_r_f = 5; -uint16_t status_b = 234; -uint16_t status_f = TB_DEFAULT; +static const uint16_t dir_b = TB_DEFAULT; +static const uint16_t dir_f = 33; +static const uint16_t file_b = TB_DEFAULT; +static const uint16_t file_f = TB_DEFAULT; +static const uint16_t frame_b = TB_DEFAULT; +static const uint16_t frame_f = TB_DEFAULT; +static const uint16_t other_b = TB_DEFAULT; +static const uint16_t other_f = 3; +static const uint16_t pane_l_b = TB_DEFAULT; +static const uint16_t pane_l_f = 166; +static const uint16_t pane_r_b = TB_DEFAULT; +static const uint16_t pane_r_f = 5; +static const uint16_t status_b = 234; +static const uint16_t status_f = TB_DEFAULT; + +/* bookmarks */ +static Bookmark bmarks[] = { + {'\\', "/root"}, + {'e', "/etc"}, + {'u', "/usr/local/bin"}, +}; /* openwith */ -char audio[] = "mpv"; -char html[] = "firefox"; -char images[] = "sxiv"; -char pdf[] = "mupdf"; -char videos[] = "mpv"; +static char audio[] = "mpv"; +static char html[] = "firefox"; +static char images[] = "sxiv"; +static char pdf[] = "mupdf"; +static char videos[] = "mpv"; /* unicode chars */ -uint32_t u_cne = 0x2510; -uint32_t u_cnw = 0x250C; -uint32_t u_cse = 0x2518; -uint32_t u_csw = 0x2514; -uint32_t u_hl = 0x2500; -uint32_t u_mn = 0x252C; -uint32_t u_ms = 0x2534; -uint32_t u_vl = 0x2502; +static const uint32_t u_cne = 0x2510; +static const uint32_t u_cnw = 0x250C; +static const uint32_t u_cse = 0x2518; +static const uint32_t u_csw = 0x2514; +static const uint32_t u_hl = 0x2500; +static const uint32_t u_mn = 0x252C; +static const uint32_t u_ms = 0x2534; +static const uint32_t u_vl = 0x2502; #endif /* CONFIG_H */ diff --git a/sfm.c b/sfm.c @@ -17,7 +17,6 @@ #include <pwd.h> #include <grp.h> -#include "config.h" #include "termbox.h" #include "util.h" @@ -43,6 +42,13 @@ typedef struct { uint16_t dir_fg; } Pane; +typedef struct { + char key; + char path[MAX_P]; +} Bookmark; + +#include "config.h" + /* function declarations */ static void print_tb(const char*, int, int, uint16_t, uint16_t); static void printf_tb(int, int, uint16_t, uint16_t, const char*, ...); @@ -198,22 +204,22 @@ static char * get_extentions(char *str) { char *ext; - char *dot; + char dot; size_t counter, len, i; - dot = "."; + dot = '.'; counter = 0; len = strlen(str); for (i = len-1; i > 0; i--) { - if (str[i] == *dot) { + if (str[i] == dot) { break; } else { counter++; } } - ext = ecalloc(counter, sizeof(char)); + ext = ecalloc(counter+1, sizeof(char)); strncpy(ext, &str[len-counter], counter); return ext; } @@ -242,15 +248,15 @@ static char * get_parent(char *dir) { char *parent; - char *dot; + char dot; size_t counter, len, i; - dot = "/"; + dot = '/'; counter = 0; len = strlen(dir); for (i = len-1; i > 0; i--) { - if (dir[i] == *dot) { + if (dir[i] == dot) { break; } else { counter++; @@ -585,6 +591,23 @@ get_memory_usage(void) return myusage.ru_maxrss; } +static int +findbm(char event) +{ + size_t i; + + for (i = 0; i < LEN(bmarks); i++) { + if (event == bmarks[i].key) { + if (check_dir(bmarks[i].path) != 0) { + print_error("%s", strerror(errno)); + return -1; + } + return i; + } + } + return -1; +} + static void print_col(Entry *entry, size_t hdir, size_t x, size_t y, int dyn_y, int width) { @@ -735,24 +758,20 @@ static void press(struct tb_event *ev, Pane *cpane, Pane *opane) { char *parent; - int isdir; - int ret; + int isdir, ret, b; clear_error(); - switch (ev->ch) { - case 'j': + if (ev->ch == 'j') { if (cpane->hdir < cpane->dirc) { cpane->hdir++; (void)listdir(cpane); } - break; - case 'k': + } else if (ev->ch == 'k') { if (cpane->hdir > 1) { cpane->hdir--; (void)listdir(cpane); } - break; - case 'h': + } else if (ev->ch == 'h') { parent = get_parent(cpane->dirn); if (check_dir(parent) < 0) { /* failed to open directory */ print_error("%s", strerror(errno)); @@ -764,9 +783,7 @@ press(struct tb_event *ev, Pane *cpane, Pane *opane) parent_row = 1; } free(parent); - - break; - case 'l': + } else if (ev->ch == 'l') { isdir = check_dir(cpane->high_dir); switch (isdir) { case 0: @@ -795,21 +812,27 @@ press(struct tb_event *ev, Pane *cpane, Pane *opane) /* failed to open directory */ print_error("%s", strerror(errno)); } - - break; - case 'g': + } else if (ev->ch == 'g') { cpane->hdir = 1; (void)listdir(cpane); - break; - case 'G': + } else if (ev->ch == 'G') { cpane->hdir = cpane->dirc; (void)listdir(cpane); - break; - case 'M': + } else if (ev->ch == 'M') { cpane->hdir = (cpane->dirc/2); (void)listdir(cpane); - break; + } else { + /* bookmarks */ + b = findbm((char)ev->ch); + if (b < 0) + return; + + strcpy(cpane->dirn, bmarks[b].path); + clear_pane(cpane->dirx); + cpane->hdir = 1; + (void)listdir(cpane); } + } static void