namefirst.diff (7360B)
1 # Description: Add git status column to detail mode. Provides additional 2 # command line flag -G which will render the git status 3 # column also in normal mode. Vim plugin users may consider 4 # adding the -G flag to their command override. 5 # Compatibility patch for the namefirst patch. 6 # 7 # Authors: Luuk van Baal 8 9 diff --git a/src/nnn.c b/src/nnn.c 10 index af586056..9ebfb203 100644 11 --- a/src/nnn.c 12 +++ b/src/nnn.c 13 @@ -262,6 +262,25 @@ 14 #define FREE 0 15 #define CAPACITY 1 16 17 +/* Git icons */ 18 +#ifdef NERD 19 +#define GIT_ADD "" 20 +#define GIT_DEL "" 21 +#define GIT_IGN "" 22 +#define GIT_MOD "" 23 +#define GIT_NEW "" 24 +#define GIT_NON "-" 25 +#define GIT_UPD "ﮮ" 26 +#else 27 +#define GIT_ADD "A" 28 +#define GIT_DEL "D" 29 +#define GIT_IGN "!" 30 +#define GIT_MOD "M" 31 +#define GIT_NEW "?" 32 +#define GIT_NON "-" 33 +#define GIT_UPD "U" 34 +#endif 35 + 36 /* TYPE DEFINITIONS */ 37 typedef unsigned int uint_t; 38 typedef unsigned char uchar_t; 39 @@ -286,6 +305,7 @@ typedef struct entry { 40 uid_t uid; /* 4 bytes */ 41 gid_t gid; /* 4 bytes */ 42 #endif 43 + char git_status[2][5]; 44 } *pEntry; 45 46 /* Selection marker */ 47 @@ -342,6 +362,7 @@ typedef struct { 48 uint_t cliopener : 1; /* All-CLI app opener */ 49 uint_t waitedit : 1; /* For ops that can't be detached, used EDITOR */ 50 uint_t rollover : 1; /* Roll over at edges */ 51 + uint_t normalgit : 1; /* Show git status in normal mode */ 52 } settings; 53 54 /* Non-persistent program-internal states (alphabeical order) */ 55 @@ -395,7 +416,17 @@ static struct { 56 ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid; 57 } dtls; 58 59 +typedef struct { 60 + char status[2]; 61 + char path[PATH_MAX]; 62 +} git_status_t; 63 + 64 /* GLOBALS */ 65 +struct { 66 + bool show; 67 + size_t len; 68 + git_status_t *statuses; 69 +} git_statuses; 70 71 /* Configuration, contexts */ 72 static settings cfg = { 73 @@ -426,6 +457,7 @@ static settings cfg = { 74 0, /* cliopener */ 75 0, /* waitedit */ 76 1, /* rollover */ 77 + 0, /* normalgit */ 78 }; 79 80 static context g_ctx[CTX_MAX] __attribute__ ((aligned)); 81 @@ -3847,6 +3879,56 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id) 82 return -1; 83 } 84 85 +static size_t get_git_statuses(const char *path) 86 +{ 87 + static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null"; 88 + char workdir[PATH_MAX], *ret; 89 + FILE *fp = popen(gitrev, "r"); 90 + 91 + git_statuses.show = FALSE; 92 + ret = fgets(workdir, PATH_MAX, fp); 93 + pclose(fp); 94 + if (!ret) 95 + return 0; 96 + 97 + static char gitstat[] = "git -c core.quotePath= status --porcelain --no-renames --ignored=matching -u "; 98 + char pathspec[PATH_MAX], status[PATH_MAX]; 99 + size_t i = -1; 100 + workdir[xstrlen(workdir) - 1] = '\0'; 101 + snprintf(pathspec, PATH_MAX, "%s\"%s\"%s 2>/dev/null", gitstat, path, cfg.showhidden ? "" : "/*"); 102 + fp = popen(pathspec, "r"); 103 + 104 + while (fgets(status, PATH_MAX, fp)) { 105 + size_t pathindex = (status[3] == '"') ? 4 : 3; 106 + status[xstrlen(status) - pathindex + 2] = '\0'; 107 + git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1)); 108 + git_statuses.statuses[i].status[0] = status[0]; 109 + git_statuses.statuses[i].status[1] = status[1]; 110 + mkpath(workdir, status + pathindex, git_statuses.statuses[i].path); 111 + } 112 + 113 + pclose(fp); 114 + return (i + 1); 115 +} 116 + 117 +static void set_git_status(char status[][5], uint_t nr) 118 +{ 119 + for (int j = 0; j < 2; j++) { 120 + if (status[j][0] == '-') 121 + switch (git_statuses.statuses[nr].status[j]) { 122 + case ' ': xstrsncpy(status[j], GIT_NON, 4); break; 123 + case 'M': xstrsncpy(status[j], GIT_MOD, 4); break; 124 + case 'A': xstrsncpy(status[j], GIT_ADD, 4); break; 125 + case '?': xstrsncpy(status[j], GIT_NEW, 4); break; 126 + case '!': xstrsncpy(status[j], GIT_IGN, 4); break; 127 + case 'D': xstrsncpy(status[j], GIT_DEL, 4); break; 128 + case 'U': xstrsncpy(status[j], GIT_UPD, 4); break; 129 + } 130 + } 131 + if (git_statuses.statuses[nr].status[1] != '!') 132 + git_statuses.show = TRUE; 133 +} 134 + 135 static void resetdircolor(int flags) 136 { 137 /* Directories are always shown on top, clear the color when moving to first file */ 138 @@ -4157,6 +4239,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel) 139 int attrs = 0, namelen; 140 uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs); 141 142 + if (git_statuses.show && (cfg.showdetail || cfg.normalgit)) 143 + printw(" %s%s", ent->git_status[0], ent->git_status[1]); 144 + 145 addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' '); 146 147 if (g_state.oldcolor) 148 @@ -5623,6 +5708,11 @@ static int dentfill(char *path, struct entry **ppdents) 149 attron(COLOR_PAIR(cfg.curctx + 1)); 150 } 151 152 + char linkpath[PATH_MAX]; 153 + if ((git_statuses.len = get_git_statuses(path))) 154 + if (!realpath(path, linkpath)) 155 + printwarn(NULL); 156 + 157 #if _POSIX_C_SOURCE >= 200112L 158 posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); 159 #endif 160 @@ -5821,6 +5911,29 @@ static int dentfill(char *path, struct entry **ppdents) 161 #endif 162 } 163 164 + if (git_statuses.len) { 165 + char dentpath[PATH_MAX]; 166 + size_t pathlen = mkpath(linkpath, dentp->name, dentpath); 167 + dentp->git_status[0][0] = dentp->git_status[1][0] = '-'; 168 + dentp->git_status[0][1] = dentp->git_status[1][1] = '\0'; 169 + 170 + if (dentp->flags & DIR_OR_DIRLNK) { 171 + char prefix[PATH_MAX]; 172 + memccpy(prefix, dentpath, '\0', PATH_MAX); 173 + prefix[pathlen - 1] = '/'; 174 + 175 + for (size_t i = 0; i < git_statuses.len; ++i) 176 + if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) 177 + set_git_status(dentp->git_status, i); 178 + } else { 179 + for (size_t i = 0; i < git_statuses.len; ++i) 180 + if (!xstrcmp(git_statuses.statuses[i].path, dentpath)) { 181 + set_git_status(dentp->git_status, i); 182 + break; 183 + } 184 + } 185 + } 186 + 187 ++ndents; 188 } while ((dp = readdir(dirp))); 189 190 @@ -6357,7 +6470,8 @@ static int adjust_cols(int n) 191 cfg.showdetail ^= 1; 192 else /* 2 more accounted for below */ 193 n -= (dtls.maxentln - 2 - dtls.maxnameln); 194 - } 195 + } else if (cfg.normalgit && git_statuses.show) 196 + n -= 3; 197 198 /* 2 columns for preceding space and indicator */ 199 return (n - 2); 200 @@ -6512,7 +6626,7 @@ static void redraw(char *path) 201 } 202 #endif 203 } 204 - dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 29) : 26); 205 + dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 3) : 0) + (git_statuses.show ? 29 : 26); 206 } 207 208 ncols = adjust_cols(ncols); 209 @@ -8132,6 +8246,7 @@ static void usage(void) 210 " -F val fifo mode [0:preview 1:explore]\n" 211 #endif 212 " -g regex filters\n" 213 + " -G always show git status\n" 214 " -H show hidden files\n" 215 " -i show current file info\n" 216 " -J no auto-proceed on select\n" 217 @@ -8272,6 +8387,7 @@ static void cleanup(void) 218 free(hostname); 219 } 220 #endif 221 + free(git_statuses.statuses); 222 free(selpath); 223 free(plgpath); 224 free(cfgpath); 225 @@ -8316,7 +8432,7 @@ int main(int argc, char *argv[]) 226 227 while ((opt = (env_opts_id > 0 228 ? env_opts[--env_opts_id] 229 - : getopt(argc, argv, "aAb:cCdDeEfF:gHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) { 230 + : getopt(argc, argv, "aAb:cCdDeEfF:gGHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) { 231 switch (opt) { 232 #ifndef NOFIFO 233 case 'a': 234 @@ -8367,6 +8483,9 @@ int main(int argc, char *argv[]) 235 cfg.regex = 1; 236 filterfn = &visible_re; 237 break; 238 + case 'G': 239 + cfg.normalgit = 1; 240 + break; 241 case 'H': 242 cfg.showhidden = 1; 243 break;