-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumns.go
More file actions
466 lines (416 loc) · 13.6 KB
/
Copy pathcolumns.go
File metadata and controls
466 lines (416 loc) · 13.6 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
// Nerd Font icons for individual git status characters.
// See https://www.nerdfonts.com/cheat-sheet for the full icon list.
//
// Git's porcelain status is two characters: [index][worktree].
// The index (staged) character is colored green, worktree (unstaged) is red,
// matching git's own color convention.
var nerdFontCharMap = map[byte]string{
'M': "\uf040", // nf-fa-pencil (modified)
'A': "\uf471", // nf-oct-diff_added (added)
'D': "\uf014", // nf-fa-trash_o (deleted)
'R': "\uf064", // nf-fa-share (renamed/moved)
'C': "\uf0c5", // nf-fa-copy (copied)
'U': "\uf0e7", // nf-fa-bolt (unmerged)
}
// nerdFontSpecialMap handles statuses that don't follow the 2-char
// [index][worktree] pattern, or where the two characters don't represent
// separate staged/unstaged states.
var nerdFontSpecialMap = map[string]string{
"I": "\uf070", // nf-fa-eye_slash (ignored)
"*": "\ue5fb", // nf-custom-folder_git (.git directory)
"??": "\uf128", // nf-fa-question (untracked — not a staged/unstaged split)
"UU": "\uf0e7", // nf-fa-bolt (unmerged, both modified)
"AA": "\uf0e7", // nf-fa-bolt (unmerged, both added)
"DD": "\uf0e7", // nf-fa-bolt (unmerged, both deleted)
"AU": "\uf0e7", // nf-fa-bolt (unmerged, added by us)
"UA": "\uf0e7", // nf-fa-bolt (unmerged, added by them)
"DU": "\uf0e7", // nf-fa-bolt (unmerged, deleted by us)
"UD": "\uf0e7", // nf-fa-bolt (unmerged, deleted by them)
}
// statusToNerdFont converts a git status string to a colored Nerd Font icon
// string. For 2-character statuses, the index (first) character is shown in
// green and the worktree (second) character in red, preserving the
// staged-vs-unstaged distinction. For compound statuses (comma-separated),
// each part is converted independently.
func statusToNerdFont(status string) string {
if status == "" {
return ""
}
// Handle compound statuses like "M , D"
parts := strings.Split(status, ",")
var result strings.Builder
for _, part := range parts {
result.WriteString(statusPartToNerdFont(part))
}
return result.String()
}
// statusPartToNerdFont converts a single status code to nerdfont icons.
// All returned strings include a trailing space to visually separate the
// status icons from the adjacent diff column.
func statusPartToNerdFont(status string) string {
// Check special statuses first
if icon, ok := nerdFontSpecialMap[status]; ok {
return icon + " "
}
// Standard 2-char status: [index][worktree]
if len(status) == 2 {
idx, wt := status[0], status[1]
idxIcon, hasIdx := nerdFontCharMap[idx]
wtIcon, hasWt := nerdFontCharMap[wt]
if hasIdx || hasWt {
var result strings.Builder
// Index (staged) character — green
if hasIdx {
result.WriteString(GREEN)
result.WriteString(idxIcon)
result.WriteString(RESET)
}
// Separate icons with a space to prevent terminal
// rendering glitches with adjacent PUA glyphs
if hasIdx && hasWt {
result.WriteByte(' ')
}
// Worktree (unstaged) character — red
if hasWt {
result.WriteString(RED)
result.WriteString(wtIcon)
result.WriteString(RESET)
}
// Trailing space to visually separate from the next column
result.WriteByte(' ')
return result.String()
}
}
// Fallback: return the original text for unknown statuses
return status
}
// nerdFontStatusWidth returns the visible width of a nerdfont status string,
// i.e. the number of icon characters that statusToNerdFont will produce.
func nerdFontStatusWidth(status string) int {
return width(statusToNerdFont(status))
}
// Column represents a displayable column
type Column string
const (
ColStatus Column = "status"
ColDiff Column = "diff"
ColFilename Column = "filename"
ColShorthash Column = "shorthash"
ColHash Column = "hash"
ColDate Column = "date"
ColAuthor Column = "author"
ColEmail Column = "email"
ColNumstat Column = "numstat"
ColCommitMessage Column = "commitmessage"
)
// AllColumns returns the default column order
func AllColumns() []Column {
return []Column{
ColStatus,
ColDiff,
ColFilename,
ColShorthash,
ColDate,
ColAuthor,
ColCommitMessage,
}
}
// ValidColumns returns a map of all valid column names
func ValidColumns() map[string]Column {
return map[string]Column{
"status": ColStatus,
"diff": ColDiff,
"filename": ColFilename,
"shorthash": ColShorthash,
"hash": ColHash,
"date": ColDate,
"author": ColAuthor,
"email": ColEmail,
"numstat": ColNumstat,
"commitmessage": ColCommitMessage,
}
}
// calculateColumnWidths computes the maximum width needed for each column
func calculateColumnWidths(files []*File, columns []Column, rctx *RenderContext) map[Column]int {
widths := make(map[Column]int)
for _, col := range columns {
maxWidth := 0
for _, file := range files {
var w int
switch col {
case ColStatus:
if rctx.NerdFont {
w = nerdFontStatusWidth(file.status)
} else {
w = width(file.status)
}
case ColDiff:
w = width(file.diffStat)
case ColFilename:
w = width(file.Name())
case ColShorthash:
w = width(file.shortHash)
case ColHash:
w = width(file.hash)
case ColDate:
w = width(file.lastModified)
case ColAuthor:
w = width(file.author)
case ColEmail:
w = width(file.authorEmail)
case ColNumstat:
if file.diffSum != nil {
w = width(fmt.Sprintf("+%d/-%d", file.diffSum.plus, file.diffSum.minus))
}
case ColCommitMessage:
w = width(file.message)
}
if w > maxWidth {
maxWidth = w
}
}
widths[col] = maxWidth
}
return widths
}
// renderStatus renders the status column
func renderStatus(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
if maxWidth > 0 {
var statusStr string
if rctx.NerdFont {
statusStr = statusToNerdFont(file.status)
} else {
statusStr = file.status
}
truncated := truncateToWidth(statusStr, maxWidth)
visibleWidth := width(truncated)
// Right-align: pad with spaces on the left
for i := 0; i < maxWidth-visibleWidth; i++ {
must(fmt.Fprintf(out, " "))
}
must(fmt.Fprintf(out, "%s", truncated))
// No right padding needed for status since it's right-aligned
}
}
// renderDiff renders the diff graph column
func renderDiff(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
if maxWidth > 0 {
truncated := truncateToWidth(file.diffStat, maxWidth)
must(fmt.Fprintf(out, "%s", truncated))
if !isLast {
for i := 0; i < maxWidth-width(truncated); i++ {
must(fmt.Fprintf(out, " "))
}
}
}
}
// renderFilename renders the filename column with colors and hyperlinks
func renderFilename(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
truncatedName := truncateToWidth(file.Name(), maxWidth)
truncatedWidth := width(truncatedName)
if file.isDeleted {
must(fmt.Fprintf(out, "%s%s", RED, STRIKEOUT))
} else if file.isDir {
must(fmt.Fprintf(out, "%s", BLUE))
} else if file.isExe {
must(fmt.Fprintf(out, "%s", GREEN))
}
// link the file name to the file's location (but not for deleted files)
if file.isDeleted {
must(fmt.Fprintf(out, "%s", truncatedName))
} else {
fileURL := fmt.Sprintf("file://%s%s", must(os.Hostname()), filepath.Join(rctx.Dir, file.Name()))
must(fmt.Fprintf(out, "%s", link(fileURL, truncatedName)))
}
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-truncatedWidth; i++ {
must(fmt.Fprintf(out, " "))
}
}
// reset color for dir/exe but not deleted (strikethrough continues)
if file.isDir || file.isExe {
must(fmt.Fprintf(out, "%s", RESET))
}
}
// renderShorthash renders the short commit hash
func renderShorthash(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
if maxWidth > 0 {
shortHash := file.shortHash
truncated := truncateToWidth(shortHash, maxWidth)
truncatedWidth := width(truncated)
var color string
if rctx.MonoHash {
color = CYAN
} else {
color = hashToColor(file.hash)
}
if len(rctx.GithubURL) > 0 && shortHash != "" {
commitURL := fmt.Sprintf("%s/commit/%s", rctx.GithubURL, file.hash)
must(fmt.Fprintf(out, "%s%s%s", color, link(commitURL, truncated), RESET))
} else {
must(fmt.Fprintf(out, "%s%s%s", color, truncated, RESET))
}
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-truncatedWidth; i++ {
must(fmt.Fprintf(out, " "))
}
}
}
}
// renderHash renders the full commit hash
func renderHash(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
if maxWidth > 0 {
var color string
if rctx.MonoHash {
color = CYAN
} else {
color = hashToColor(file.hash)
}
truncated := truncateToWidth(file.hash, maxWidth)
truncatedWidth := width(truncated)
if len(rctx.GithubURL) > 0 && file.hash != "" {
commitURL := fmt.Sprintf("%s/commit/%s", rctx.GithubURL, file.hash)
must(fmt.Fprintf(out, "%s%s%s", color, link(commitURL, truncated), RESET))
} else {
must(fmt.Fprintf(out, "%s%s%s", color, truncated, RESET))
}
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-truncatedWidth; i++ {
must(fmt.Fprintf(out, " "))
}
}
}
}
// renderDate renders the date column
func renderDate(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
truncated := truncateToWidth(file.lastModified, maxWidth)
must(fmt.Fprintf(out, "%s", truncated))
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-width(truncated); i++ {
must(fmt.Fprintf(out, " "))
}
}
}
// renderAuthor renders the author column with hyperlinks
func renderAuthor(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
authorWidth := min(width(file.author), maxWidth)
// Truncate author string if needed, being careful with multi-byte characters
truncatedAuthor := truncateToWidth(file.author, authorWidth)
if len(rctx.GithubURL) > 0 {
authorLink := fmt.Sprintf("%s/commits?author=%s", rctx.GithubURL, file.authorEmail)
if file.isDeleted {
must(fmt.Fprintf(out, "%s", link(authorLink, truncatedAuthor)))
} else {
must(fmt.Fprintf(out, "%s%s%s", YELLOW, link(authorLink, truncatedAuthor), RESET))
}
} else {
if file.isDeleted {
must(fmt.Fprintf(out, "%s", truncatedAuthor))
} else {
must(fmt.Fprintf(out, "%s%s%s", YELLOW, truncatedAuthor, RESET))
}
}
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-width(truncatedAuthor); i++ {
must(fmt.Fprintf(out, " "))
}
}
}
// renderEmail renders the author email column
func renderEmail(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
// Calculate how many characters we can display
emailWidth := min(width(file.authorEmail), maxWidth)
// Truncate email string if needed
truncatedEmail := truncateToWidth(file.authorEmail, emailWidth)
must(fmt.Fprintf(out, "%s", truncatedEmail))
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-width(truncatedEmail); i++ {
must(fmt.Fprintf(out, " "))
}
}
}
// renderNumstat renders the numeric diffstat
func renderNumstat(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
if file.diffSum != nil {
numstat := fmt.Sprintf("+%d/-%d", file.diffSum.plus, file.diffSum.minus)
numstatWidth := min(width(numstat), maxWidth)
truncatedNumstat := truncateToWidth(numstat, numstatWidth)
must(fmt.Fprintf(out, "%s", truncatedNumstat))
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-width(truncatedNumstat); i++ {
must(fmt.Fprintf(out, " "))
}
}
} else if !isLast {
// pad spaces if no diffSum (unless this is the last column)
for range maxWidth {
must(fmt.Fprintf(out, " "))
}
}
}
// renderCommitMessage renders the commit message with issue linkification
func renderCommitMessage(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
// Calculate how many characters we can display
messageWidth := min(width(file.message), maxWidth)
// Truncate message string if needed
truncatedMessage := truncateToWidth(file.message, messageWidth)
if file.isDeleted {
if len(rctx.GithubURL) > 0 {
must(fmt.Fprintf(out, "%s", linkify(truncatedMessage, rctx.GithubURL, file.hash)))
} else {
must(fmt.Fprintf(out, "%s", truncatedMessage))
}
} else if len(rctx.GithubURL) > 0 {
must(fmt.Fprintf(out, "%s", linkify(truncatedMessage, rctx.GithubURL, file.hash)))
} else {
must(fmt.Fprintf(out, "%s", truncatedMessage))
}
// pad spaces to the right up to maxWidth (unless this is the last column)
if !isLast {
for i := 0; i < maxWidth-width(truncatedMessage); i++ {
must(fmt.Fprintf(out, " "))
}
}
}
// getColumnRenderer returns the renderer function for a column
func getColumnRenderer(col Column) func(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {
switch col {
case ColStatus:
return renderStatus
case ColDiff:
return renderDiff
case ColFilename:
return renderFilename
case ColShorthash:
return renderShorthash
case ColHash:
return renderHash
case ColDate:
return renderDate
case ColAuthor:
return renderAuthor
case ColEmail:
return renderEmail
case ColNumstat:
return renderNumstat
case ColCommitMessage:
return renderCommitMessage
default:
return func(out io.Writer, file *File, maxWidth int, rctx *RenderContext, isLast bool) {}
}
}