summaryrefslogtreecommitdiffstats
path: root/patches/namefirst/mainline.diff
blob: 17f472c295e9b1874f91f6ce473bbf958ddcf0be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Description: Prints filenames first in the detail view.  Prints user/group
#              columns when a directory contains different users/groups.
#
# Author: Luuk van Baal

diff --git a/src/nnn.c b/src/nnn.c
index fe5d650..2d33716 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -382,6 +382,10 @@ typedef struct {
 } session_header_t;
 #endif

+static struct {
+	ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
+} dtls;
+
 /* GLOBALS */

 /* Configuration, contexts */
@@ -1065,10 +1069,12 @@ static char *getpwname(uid_t uid)
 	static char *namecache;

 	if (uidcache != uid) {
+		if (dtls.maxuidln && !dtls.printguid) dtls.printguid = 1;
 		struct passwd *pw = getpwuid(uid);

 		uidcache = uid;
 		namecache = pw ? pw->pw_name : NULL;
+		dtls.uidln = xstrlen(namecache ? namecache : xitoa(uid));
 	}

 	return namecache ? namecache : xitoa(uid);
@@ -1080,10 +1086,12 @@ static char *getgrname(gid_t gid)
 	static char *grpcache;

 	if (gidcache != gid) {
+		if (dtls.maxgidln && !dtls.printguid) dtls.printguid = 1;
 		struct group *gr = getgrgid(gid);

 		gidcache = gid;
 		grpcache = gr ? gr->gr_name : NULL;
+		dtls.gidln = xstrlen(grpcache ? grpcache : xitoa(gid));
 	}

 	return grpcache ? grpcache : xitoa(gid);
@@ -3796,14 +3804,13 @@ static void resetdircolor(int flags)
  * Max supported str length: NAME_MAX;
  */
 #ifdef NOLC
-static char *unescape(const char *str, uint_t maxcols)
+static size_t unescape(const char *str, uint_t maxcols)
 {
 	char * const wbuf = g_buf;
 	char *buf = wbuf;
-
-	xstrsncpy(wbuf, str, maxcols);
+	size_t len = xstrsncpy(wbuf, str, maxcols);
 #else
-static wchar_t *unescape(const char *str, uint_t maxcols)
+static size_t unescape(const char *str, uint_t maxcols)
 {
 	wchar_t * const wbuf = (wchar_t *)g_buf;
 	wchar_t *buf = wbuf;
@@ -3828,7 +3835,7 @@ static wchar_t *unescape(const char *str, uint_t maxcols)
 		++buf;
 	}

-	return wbuf;
+	return len;
 }

 static off_t get_size(off_t size, off_t *pval, int comp)
@@ -4089,33 +4096,7 @@ static uchar_t get_color_pair_name_ind(const struct entry *ent, char *pind, int
 static void printent(const struct entry *ent, uint_t namecols, bool sel)
 {
 	char ind = '\0';
-	int attrs;
-
-	if (cfg.showdetail) {
-		int type = ent->mode & S_IFMT;
-		char perms[6] = {' ', ' ', (char)('0' + ((ent->mode >> 6) & 7)),
-				(char)('0' + ((ent->mode >> 3) & 7)),
-				(char)('0' + (ent->mode & 7)), '\0'};
-
-		addch(' ');
-		attrs = g_state.oldcolor ? (resetdircolor(ent->flags), A_DIM)
-					 : (fcolors[C_MIS] ? COLOR_PAIR(C_MIS) : 0);
-		if (attrs)
-			attron(attrs);
-
-		/* Print details */
-		print_time(&ent->sec);
-
-		printw("%s%9s ", perms, (type == S_IFREG || type == S_IFDIR)
-			? coolsize(cfg.blkorder ? (blkcnt_t)ent->blocks << blk_shift : ent->size)
-			: (type = (uchar_t)get_detail_ind(ent->mode), (char *)&type));
-
-		if (attrs)
-			attroff(attrs);
-	}
-
-	attrs = 0;
-
+	int attrs = 0, namelen;
 	uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);

 	addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
@@ -4140,15 +4121,40 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
 		++namecols;

 #ifndef NOLC
-	addwstr(unescape(ent->name, namecols));
+	addwstr((namelen = unescape(ent->name, namecols), (wchar_t *)g_buf));
 #else
-	addstr(unescape(ent->name, MIN(namecols, ent->nlen) + 1));
+	addstr((namelen = unescape(ent->name, MIN(namecols, ent->nlen) + 1), (char *)g_buf));
 #endif

-	if (attrs)
+	if (!sel && attrs)
 		attroff(attrs);
 	if (ind)
 		addch(ind);
+	if (cfg.showdetail) {
+		int type = ent->mode & S_IFMT;
+		char perms[6] = {(char)('0' + ((ent->mode >> 6) & 7)),
+				(char)('0' + ((ent->mode >> 3) & 7)),
+				(char)('0' + (ent->mode & 7)), ' ', ' ', '\0'}, *size = NULL;
+
+		if (attrs)
+			attron(attrs);
+		if (!g_state.oldcolor && (type == S_IFDIR || (type == S_IFLNK && ent->flags & DIR_OR_DIRLNK)))
+			attroff(A_BOLD);
+		size_t sizelen = (type == S_IFREG || type == S_IFDIR) ? xstrlen(size = coolsize(cfg.blkorder ? ent->blocks << blk_shift : ent->size)) : 1;
+		printw("%*c%*s%s%s", 1 + MIN(namecols, dtls.maxnameln + (size_t)(ind ? 0 : 1)) - namelen, ' ',
+				dtls.maxsizeln - sizelen, "", size ? size : (type = (uchar_t)get_detail_ind(ent->mode), (char *)&type), "  ");
+#ifndef NOUG
+		if (g_state.uidgid && dtls.printguid) {
+			addstr(getpwname(ent->uid));
+			printw("%*c%s", dtls.maxuidln + 1 - dtls.uidln, ' ', getgrname(ent->gid));
+			printw("%*c", dtls.maxgidln + 2 - dtls.gidln, ' ');
+		}
+#endif
+		addstr(perms);
+		print_time(&ent->sec);
+	}
+	if (attrs)
+		attroff(attrs);
 }

 static void savecurctx(char *path, char *curname, int nextctx)
@@ -6140,18 +6146,6 @@ static void statusbar(char *path)
 		tocursor();
 }

-static inline void markhovered(void)
-{
-	if (cfg.showdetail && ndents) { /* Reversed block for hovered entry */
-		tocursor();
-#ifdef ICONS_ENABLED
-		addstr(MD_ARROW_FORWARD);
-#else
-		addch(' ' | A_REVERSE);
-#endif
-	}
-}
-
 static int adjust_cols(int n)
 {
 	/* Calculate the number of cols available to print entry name */
@@ -6159,11 +6153,10 @@ static int adjust_cols(int n)
 	n -= (g_state.oldcolor ? 0 : 1 + xstrlen(ICON_PADDING_LEFT) + xstrlen(ICON_PADDING_RIGHT));
 #endif
 	if (cfg.showdetail) {
-		/* Fallback to light mode if less than 35 columns */
-		if (n < 36)
+		if (n < (dtls.maxentln + 1 - dtls.maxnameln))
 			cfg.showdetail ^= 1;
 		else /* 2 more accounted for below */
-			n -= 32;
+			n -= (dtls.maxentln - 2 - dtls.maxnameln);
 	}

 	/* 2 columns for preceding space and indicator */
@@ -6200,8 +6193,6 @@ static void draw_line(int ncols)
 	/* Must reset e.g. no files in dir */
 	if (dir)
 		attroff(COLOR_PAIR(cfg.curctx + 1) | A_BOLD);
-
-	markhovered();
 }

 static void redraw(char *path)
@@ -6309,6 +6300,21 @@ static void redraw(char *path)

 	onscreen = MIN(onscreen + curscroll, ndents);

+	if (cfg.showdetail) {
+		ushort_t lenbuf = dtls.maxnameln = dtls.maxsizeln = dtls.maxuidln = dtls.maxgidln = dtls.printguid = 0;
+		for (i = curscroll; i < onscreen; ++i) {
+			if ((lenbuf = pdents[i].nlen - 1) > dtls.maxnameln) dtls.maxnameln = lenbuf;
+			if ((lenbuf = xstrlen(coolsize(cfg.blkorder ? pdents[i].blocks << blk_shift : pdents[i].size))) > dtls.maxsizeln) dtls.maxsizeln = lenbuf;
+#ifndef NOUG
+			if (g_state.uidgid) {
+				if ((getpwname(pdents[i].uid), dtls.uidln) > dtls.maxuidln) dtls.maxuidln = dtls.uidln;
+				if ((getgrname(pdents[i].gid), dtls.gidln) > dtls.maxgidln) dtls.maxgidln = dtls.gidln;
+			}
+#endif
+		}
+		dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 29) : 26);
+	}
+
 	ncols = adjust_cols(ncols);

 	int len = scanselforpath(path, FALSE);
@@ -6339,7 +6345,7 @@ static void redraw(char *path)
 #endif
 	}

-	markhovered();
+	statusbar(path);
 }

 static bool cdprep(char *lastdir, char *lastname, char *path, char *newpath)