sfm

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

commit 66027260af032e04df0edea4b4295249e6deeacc
parent 23b0e51c6024ab5af4cab2a231c4f526d0a6d60a
Author: afify <hassan@afify.dev>
Date:   Sun, 30 May 2021 02:11:43 +0300

[feat] replace all move function, add delconf

- move delete confirmation variable in config file
- replace:
        mvdwn()
        mvdwns()
        mvup()
        mvups()
        scrdwn()
        scrdwns()
        scrup()
        scrups()
 with: mv_ver(Arg)

Diffstat:
Mconfig.def.h | 86+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msfm.c | 220++++++++++++++++++-------------------------------------------------------------
2 files changed, 91 insertions(+), 215 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -20,10 +20,12 @@ static const Cpair cstatus = { 243, 0 }; static const char *rm_cmd[] = { "rm", "-rf" }; /* delete */ static const char *cp_cmd[] = { "cp", "-r" }; /* copy */ static const char *mv_cmd[] = { "mv" }; /* move */ +static const char delconf[] = "yes"; -static const size_t rm_cmd_len = LEN(rm_cmd); -static const size_t cp_cmd_len = LEN(cp_cmd); -static const size_t mv_cmd_len = LEN(mv_cmd); +static const size_t rm_cmd_len = LEN(rm_cmd); +static const size_t cp_cmd_len = LEN(cp_cmd); +static const size_t mv_cmd_len = LEN(mv_cmd); +static const size_t delconf_len = LEN(delconf); /* bookmarks */ static const char root[] = "/"; @@ -61,48 +63,48 @@ static Rule rules[] = { /* normal keys */ static Key nkeys[] = { - /* keyval function arg */ - { {.ch = 'j'}, mvdwn, {.i = 0} }, - { {.key = TB_KEY_ARROW_DOWN}, mvdwn, {.i = 0} }, - { {.ch = 'k'}, mvup, {.i = 0} }, - { {.key = TB_KEY_ARROW_UP}, mvup, {.i = 0} }, - { {.ch = 'l'}, mvfwd, {.i = 0} }, - { {.key = TB_KEY_ARROW_RIGHT}, mvfwd, {.i = 0} }, - { {.ch = 'h'}, mvbk, {.i = 0} }, - { {.key = TB_KEY_ARROW_LEFT}, mvbk, {.i = 0} }, - { {.ch = 'g'}, mvtop, {.i = 0} }, - { {.ch = 'G'}, mvbtm, {.i = 0} }, - { {.ch = 'M'}, mvmid, {.i = 0} }, - { {.key = TB_KEY_CTRL_U}, scrup, {.i = 0} }, - { {.key = TB_KEY_CTRL_D}, scrdwn, {.i = 0} }, - { {.ch = 'n'}, crnf, {0} }, - { {.ch = 'N'}, crnd, {0} }, - { {.ch = 'd'}, delent, {0} }, - { {.ch = 'x'}, calcdir, {0} }, - { {.ch = '/'}, start_filter, {0} }, - { {.ch = 'q'}, quit, {0} }, - { {.ch = 'v'}, start_vmode, {0} }, - { {.ch = 'y'}, yank, {0} }, - { {.ch = 'p'}, paste, {0} }, - { {.ch = 'P'}, selmv, {0} }, - { {.ch = 'c'}, rname, {0} }, - { {.key = TB_KEY_SPACE}, switch_pane, {0} }, - { {.ch = '\\'}, bkmrk, {.v = root} }, + /* keyval function arg */ + { {.ch = 'j'}, mv_ver, {.i = -1} }, + { {.key = TB_KEY_ARROW_DOWN}, mv_ver, {.i = -1} }, + { {.ch = 'k'}, mv_ver, {.i = +1} }, + { {.key = TB_KEY_ARROW_UP}, mv_ver, {.i = +1} }, + { {.key = TB_KEY_CTRL_U}, mv_ver, {.i = +3} }, + { {.key = TB_KEY_CTRL_D}, mv_ver, {.i = -3} }, + { {.ch = 'l'}, mvfwd, {.i = 0} }, + { {.key = TB_KEY_ARROW_RIGHT}, mvfwd, {.i = 0} }, + { {.ch = 'h'}, mvbk, {.i = 0} }, + { {.key = TB_KEY_ARROW_LEFT}, mvbk, {.i = 0} }, + { {.ch = 'g'}, mvtop, {.i = 0} }, + { {.ch = 'G'}, mvbtm, {.i = 0} }, + { {.ch = 'M'}, mvmid, {.i = 0} }, + { {.ch = 'n'}, crnf, {0} }, + { {.ch = 'N'}, crnd, {0} }, + { {.ch = 'd'}, delent, {0} }, + { {.ch = 'x'}, calcdir, {0} }, + { {.ch = '/'}, start_filter, {0} }, + { {.ch = 'q'}, quit, {0} }, + { {.ch = 'v'}, start_vmode, {0} }, + { {.ch = 'y'}, yank, {0} }, + { {.ch = 'p'}, paste, {0} }, + { {.ch = 'P'}, selmv, {0} }, + { {.ch = 'c'}, rname, {0} }, + { {.key = TB_KEY_SPACE}, switch_pane, {0} }, + { {.ch = '\\'}, bkmrk, {.v = root} }, }; /* visual keys */ static Key vkeys[] = { /* keyval function arg */ - { {.ch = 'j'}, seldwn, {.i = 0} }, - { {.key = TB_KEY_ARROW_DOWN}, seldwn, {.i = 0} }, - { {.ch = 'k'}, selup, {.i = 0} }, - { {.key = TB_KEY_ARROW_UP}, selup, {.i = 0} }, - { {.ch = 'a'}, selall, {.i = 0} }, - { {.ch = 'y'}, selynk, {.i = 0} }, - { {.ch = 'd'}, seldel, {.i = 0} }, - { {.ch = 'q'}, exit_vmode, {0} }, - { {.ch = 'v'}, exit_vmode, {0} }, - { {.key = TB_KEY_ESC}, exit_vmode, {0} }, + { {.ch = 'j'}, seldwn, {.i = -1} }, + { {.key = TB_KEY_ARROW_DOWN}, seldwn, {.i = -1} }, + { {.ch = 'k'}, selup, {.i = +1} }, + { {.key = TB_KEY_ARROW_UP}, selup, {.i = +1} }, + { {.ch = 'a'}, selall, {0} }, + { {.ch = 'y'}, selynk, {0} }, + { {.ch = 'd'}, seldel, {.v = delconf} }, + { {.ch = 'q'}, exit_vmode, {0} }, + { {.ch = 'v'}, exit_vmode, {0} }, + { {.key = TB_KEY_ESC}, exit_vmode, {0} }, }; static const size_t nkeyslen = LEN(nkeys); @@ -112,10 +114,6 @@ static const size_t vkeyslen = LEN(vkeys); static const mode_t ndir_perm = S_IRWXU; static const mode_t nf_perm = S_IRUSR | S_IWUSR; -/* scroll */ -static const int scrmv = 10; /* ctrl+u, ctrl+d movement */ -static const int scrsp = 3; /* space before scroll */ - /* statusbar */ static const char dtfmt[] = "%F %R"; /* date time format */ diff --git a/sfm.c b/sfm.c @@ -135,19 +135,12 @@ static void delent(const Arg *arg); static void calcdir(const Arg *arg); static void crnd(const Arg *arg); static void crnf(const Arg *arg); +static void mv_ver(const Arg *arg); static void mvbk(const Arg *arg); static void mvbtm(const Arg *arg); -static void mvdwn(const Arg *arg); -static void mvdwns(void); static void mvfwd(const Arg *arg); static void mvmid(const Arg *arg); static void mvtop(const Arg *arg); -static void mvup(const Arg *arg); -static void mvups(void); -static void scrdwn(const Arg *arg); -static void scrdwns(const Arg *arg); -static void scrup(const Arg *arg); -static void scrups(const Arg *arg); static void bkmrk(const Arg *arg); static int get_usrinput(char *, size_t, const char *, ...); static int frules(char *); @@ -762,6 +755,49 @@ crnf(const Arg *arg) free(user_input); free(path); } +static void +mv_ver(const Arg *arg) +{ + + if (cpane->dirc < 1) + return; + if (cpane->hdir - cpane->firstrow - arg->i < 1) { /* move to the top */ + if (arg->i > 1) + mvtop(arg); + return; + } + if (cpane->hdir - arg->i > cpane->dirc || /* move to the bottom */ + cpane->hdir - cpane->firstrow - arg->i > scrheight - 1) { + if (arg->i < 1) + mvbtm(arg); + return; + } + + if (cpane->firstrow > 1 && arg->i > 0 && + cpane->hdir < (cpane->firstrow + arg->i)) { /* scroll up */ + cpane->firstrow = cpane->firstrow - arg->i; + rm_hi(cpane, cpane->hdir - 1); + cpane->hdir = cpane->hdir - arg->i; + refresh_pane(cpane); + add_hi(cpane, cpane->hdir - 1); + return; + } + + if (cpane->hdir - cpane->firstrow >= scrheight - 1 && + arg->i < 0) { /* scroll down */ + cpane->firstrow = cpane->firstrow - arg->i; + rm_hi(cpane, cpane->hdir - 1); + cpane->hdir = cpane->hdir - arg->i; + refresh_pane(cpane); + add_hi(cpane, cpane->hdir - 1); + return; + } + + rm_hi(cpane, cpane->hdir - 1); + cpane->hdir = cpane->hdir - arg->i; + add_hi(cpane, cpane->hdir - 1); + print_info(cpane, NULL); +} static void mvbk(const Arg *arg) @@ -805,40 +841,6 @@ mvbtm(const Arg *arg) } static void -mvdwn(const Arg *arg) -{ - if (cpane->dirc < 1) - return; - if (cpane->dirc < scrheight && cpane->hdir < cpane->dirc) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir++; - add_hi(cpane, cpane->hdir - 1); - } else { - mvdwns(); /* scroll */ - } - print_info(cpane, NULL); -} - -static void -mvdwns(void) -{ - int real; - real = cpane->hdir - 1 - cpane->firstrow; - - if (real > scrheight - 3 - scrsp && cpane->hdir + scrsp < cpane->dirc) { - cpane->firstrow++; - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir++; - refresh_pane(cpane); - add_hi(cpane, cpane->hdir - 1); - } else if (cpane->hdir < cpane->dirc) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir++; - add_hi(cpane, cpane->hdir - 1); - } -} - -static void mvfwd(const Arg *arg) { if (cpane->dirc < 1) @@ -905,128 +907,6 @@ mvtop(const Arg *arg) } static void -mvup(const Arg *arg) -{ - if (cpane->dirc < 1) - return; - if (cpane->dirc < scrheight && cpane->hdir > 1) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir--; - add_hi(cpane, cpane->hdir - 1); - } else { - mvups(); /* scroll */ - } - print_info(cpane, NULL); -} - -static void -mvups(void) -{ - size_t real; - real = cpane->hdir - 1 - cpane->firstrow; - - if (cpane->firstrow > 0 && real < 1 + scrsp) { - cpane->firstrow--; - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir--; - refresh_pane(cpane); - add_hi(cpane, cpane->hdir - 1); - } else if (cpane->hdir > 1) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir--; - add_hi(cpane, cpane->hdir - 1); - } -} - -static void -scrdwn(const Arg *arg) -{ - if (cpane->dirc < 1) - return; - if (cpane->dirc < scrheight && cpane->hdir < cpane->dirc) { - if (cpane->hdir < cpane->dirc - scrmv) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir += scrmv; - add_hi(cpane, cpane->hdir - 1); - } else { - mvbtm(arg); - } - } else { - scrdwns(arg); - } - print_info(cpane, NULL); -} - -static void -scrdwns(const Arg *arg) -{ - int real, dynmv; - - real = cpane->hdir - cpane->firstrow; - dynmv = MIN(cpane->dirc - cpane->hdir - cpane->firstrow, scrmv); - - if (real + scrmv + 1 > scrheight && - cpane->hdir + scrsp + scrmv < cpane->dirc) { /* scroll */ - cpane->firstrow += dynmv; - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir += scrmv; - refresh_pane(cpane); - add_hi(cpane, cpane->hdir - 1); - } else { - if (cpane->hdir < cpane->dirc - scrmv - 1) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir += scrmv; - add_hi(cpane, cpane->hdir - 1); - } else { - mvbtm(arg); - } - } -} - -static void -scrup(const Arg *arg) -{ - if (cpane->dirc < 1) - return; - if (cpane->dirc < scrheight && cpane->hdir > 1) { - if (cpane->hdir > scrmv) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir = cpane->hdir - scrmv; - add_hi(cpane, cpane->hdir - 1); - print_info(cpane, NULL); - } else { - mvtop(arg); - } - } else { - scrups(arg); - } -} - -static void -scrups(const Arg *arg) -{ - int real, dynmv; - real = cpane->hdir - cpane->firstrow; - dynmv = MIN(cpane->firstrow, scrmv); - - if (cpane->firstrow > 0 && real < scrmv + scrsp) { - cpane->firstrow -= dynmv; - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir -= scrmv; - refresh_pane(cpane); - add_hi(cpane, cpane->hdir - 1); - } else { - if (cpane->hdir > scrmv + 1) { - rm_hi(cpane, cpane->hdir - 1); - cpane->hdir -= scrmv; - add_hi(cpane, cpane->hdir - 1); - } else { - mvtop(arg); - } - } -} - -static void bkmrk(const Arg *arg) { if (check_dir((char *)arg->v) != 0) { @@ -1333,7 +1213,7 @@ exit_vmode(const Arg *arg) static void selup(const Arg *arg) { - mvup(arg); + mv_ver(arg); print_prompt("-- VISUAL --"); int index = abs(cpane->hdir - sel_indexes[0]); @@ -1352,7 +1232,7 @@ selup(const Arg *arg) static void seldwn(const Arg *arg) { - mvdwn(arg); + mv_ver(arg); print_prompt("-- VISUAL --"); int index = abs(cpane->hdir - sel_indexes[0]); @@ -1450,12 +1330,10 @@ static void seldel(const Arg *arg) { char *inp_conf; - int conf_len = 4; - char conf[] = "yes"; - inp_conf = ecalloc(conf_len, sizeof(char)); - if ((get_usrinput(inp_conf, conf_len, "delete file (yes) ?") < 0) || - (strncmp(inp_conf, conf, conf_len) != 0)) { + inp_conf = ecalloc(delconf_len, sizeof(char)); + if ((get_usrinput(inp_conf, delconf_len, "delete file (yes) ?") < 0) || + (strncmp(inp_conf, delconf, delconf_len) != 0)) { free(inp_conf); return; /* canceled by user or wrong inp_conf */ }