1
0
Fork 0
forked from mirror/nnn
This commit is contained in:
tezlm 2024-03-26 18:36:57 -07:00
parent 48d7976f90
commit 6233c24488
Signed by: tezlm
GPG key ID: 649733FCD94AFBBA
3 changed files with 117 additions and 8 deletions

View file

@ -32,7 +32,6 @@ O_NOSORT := 0 # disable sorting entries on dir load
# User patches
O_COLEMAK := 0 # change key bindings to colemak compatible layout
O_COLEMAK-DH := 0 # change key bindings to colemak-dh compatible layout
O_GITSTATUS := 1 # add git status to detail view
O_NAMEFIRST := 0 # print file name first, add uid and guid to detail view
O_RESTOREPREVIEW := 0 # add preview pipe to close and restore preview pane

120
src/nnn.c
View file

@ -282,6 +282,25 @@
#define VFS_USED 1
#define VFS_SIZE 2
/* Git icons */
#ifdef NERD
#define GIT_ADD ""
#define GIT_DEL ""
#define GIT_IGN ""
#define GIT_MOD ""
#define GIT_NEW ""
#define GIT_NON "-"
#define GIT_UPD "ﮮ"
#else
#define GIT_ADD "A"
#define GIT_DEL "D"
#define GIT_IGN "!"
#define GIT_MOD "M"
#define GIT_NEW "?"
#define GIT_NON "-"
#define GIT_UPD "U"
#endif
/* TYPE DEFINITIONS */
typedef unsigned int uint_t;
typedef unsigned char uchar_t;
@ -306,6 +325,7 @@ typedef struct entry {
uid_t uid; /* 4 bytes */
gid_t gid; /* 4 bytes */
#endif
char git_status[2][5];
} *pEntry;
/* Selection marker */
@ -361,6 +381,7 @@ typedef struct {
uint_t cliopener : 1; /* All-CLI app opener */
uint_t waitedit : 1; /* For ops that can't be detached, used EDITOR */
uint_t rollover : 1; /* Roll over at edges */
uint_t normalgit : 1; /* Show git status in normal mode */
} settings;
/* Non-persistent program-internal states (alphabeical order) */
@ -386,7 +407,7 @@ typedef struct {
uint_t selbm : 1; /* Select a bookmark from bookmarks directory */
uint_t selmode : 1; /* Set when selecting files */
uint_t stayonsel : 1; /* Disable auto-advance on selection */
uint_t trash : 2; /* Trash method 0: rm -rf, 1: trash-cli, 2: gio trash */
uint_t trash : 1; /* Trash method 0: rm -rf, 1: trash-cli, 2: gio trash */
uint_t uidgid : 1; /* Show owner and group info */
uint_t usebsdtar : 1; /* Use bsdtar as default archive utility */
uint_t xprompt : 1; /* Use native prompt instead of readline prompt */
@ -414,7 +435,17 @@ typedef struct {
} session_header_t;
#endif
typedef struct {
char status[2];
char path[PATH_MAX];
} git_status_t;
/* GLOBALS */
struct {
bool show;
size_t len;
git_status_t *statuses;
} git_statuses;
/* Configuration, contexts */
static settings cfg = {
@ -3937,6 +3968,47 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
return -1;
}
static size_t get_git_statuses(const char *path)
{
static char gst[] = "git -c core.quotePath= status -s --no-renames --ignored=matching -unormal . 2>/dev/null";
FILE *fp = popen(gst, "r");
char status[PATH_MAX];
size_t pathindex, i = -1;
git_statuses.show = FALSE;
while (fgets(status, PATH_MAX, fp)) {
pathindex = (status[3] == '"') ? 4 : 3;
if (!cfg.showhidden && status[pathindex] == '.')
continue;
status[xstrlen(status) - pathindex + 2] = '\0';
git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1));
git_statuses.statuses[i].status[0] = status[0];
git_statuses.statuses[i].status[1] = status[1];
mkpath(path, status + pathindex, git_statuses.statuses[i].path);
}
pclose(fp);
return (i + 1);
}
static void set_git_status(char status[][5], uint_t nr)
{
for (int j = 0; j < 2; j++) {
if (status[j][0] == '-')
switch (git_statuses.statuses[nr].status[j]) {
case ' ': xstrsncpy(status[j], GIT_NON, 4); break;
case 'M': xstrsncpy(status[j], GIT_MOD, 4); break;
case 'A': xstrsncpy(status[j], GIT_ADD, 4); break;
case '?': xstrsncpy(status[j], GIT_NEW, 4); break;
case '!': xstrsncpy(status[j], GIT_IGN, 4); break;
case 'D': xstrsncpy(status[j], GIT_DEL, 4); break;
case 'U': xstrsncpy(status[j], GIT_UPD, 4); break;
}
}
if (git_statuses.statuses[nr].status[1] != '!')
git_statuses.show = TRUE;
}
static void resetdircolor(int flags)
{
/* Directories are always shown on top, clear the color when moving to first file */
@ -4270,6 +4342,10 @@ static void printent(int pdents_index, uint_t namecols, bool sel)
uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
if (git_statuses.show && (cfg.showdetail || cfg.normalgit))
printw("%*s%s%s", (cfg.normalgit && !cfg.showdetail) ? 1 : 0, "",
ent->git_status[0], ent->git_status[1]);
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
if (g_state.oldcolor)
@ -5770,6 +5846,11 @@ static int dentfill(char *path, struct entry **ppdents)
attron(COLOR_PAIR(cfg.curctx + 1));
}
char linkpath[PATH_MAX];
if ((git_statuses.len = get_git_statuses(path)))
if (!realpath(path, linkpath))
printwarn(NULL);
#if _POSIX_C_SOURCE >= 200112L
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
@ -5970,6 +6051,29 @@ static int dentfill(char *path, struct entry **ppdents)
#endif
}
if (git_statuses.len) {
char dentpath[PATH_MAX];
size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
dentp->git_status[0][0] = dentp->git_status[1][0] = '-';
dentp->git_status[0][1] = dentp->git_status[1][1] = '\0';
if (dentp->flags & DIR_OR_DIRLNK) {
char prefix[PATH_MAX];
memccpy(prefix, dentpath, '\0', PATH_MAX);
prefix[pathlen - 1] = '/';
for (size_t i = 0; i < git_statuses.len; ++i)
if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen))
set_git_status(dentp->git_status, i);
} else {
for (size_t i = 0; i < git_statuses.len; ++i)
if (!xstrcmp(git_statuses.statuses[i].path, dentpath)) {
set_git_status(dentp->git_status, i);
break;
}
}
}
++ndents;
} while ((dp = readdir(dirp)));
@ -6551,11 +6655,12 @@ static int adjust_cols(int n)
#endif
if (cfg.showdetail) {
/* Fallback to light mode if less than 35 columns */
if (n < 36)
if (n < 38)
cfg.showdetail ^= 1;
else /* 2 more accounted for below */
n -= 32;
}
n -= (git_statuses.show ? 34 : 32);
} else if (cfg.normalgit && git_statuses.show)
n -= 3;
/* 2 columns for preceding space and indicator */
return (n - 2);
@ -8383,6 +8488,7 @@ static void usage(void)
" -F val fifo mode [0:preview 1:explore]\n"
#endif
" -g regex filters\n"
" -G always show git status\n"
" -H show hidden files\n"
" -i show current file info\n"
" -J no auto-advance on selection\n"
@ -8525,6 +8631,7 @@ static void cleanup(void)
fflush(stdout);
}
#endif
free(git_statuses.statuses);
free(selpath);
free(plgpath);
free(cfgpath);
@ -8569,7 +8676,7 @@ int main(int argc, char *argv[])
while ((opt = (env_opts_id > 0
? env_opts[--env_opts_id]
: getopt(argc, argv, "aAb:BcCdDeEfF:gHiJKl:nNop:P:QrRs:St:T:uUVxh"))) != -1) {
: getopt(argc, argv, "aAb:BcCdDeEfF:gGHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) {
switch (opt) {
#ifndef NOFIFO
case 'a':
@ -8623,6 +8730,9 @@ int main(int argc, char *argv[])
cfg.regex = 1;
filterfn = &visible_re;
break;
case 'G':
cfg.normalgit = 1;
break;
case 'H':
cfg.showhidden = 1;
break;

View file

@ -262,10 +262,10 @@ static struct key bindings[] = {
{ CONTROL('J'), SEL_AUTONEXT },
/* Edit in EDITOR */
{ 'y', SEL_EDIT },
/* Run a plugin */
{ ',', SEL_PLUGIN },
/* Show total size of listed selection */
{ 'S', SEL_SELSIZE },
/* Run a plugin */
{ ',', SEL_PLUGIN },
/* Run command */
// broken!
// { '!', SEL_SHELL },