-
Notifications
You must be signed in to change notification settings - Fork 25
/
ls-go.go
702 lines (631 loc) · 18.2 KB
/
ls-go.go
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package main
import (
"fmt"
"io/ioutil"
"math"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/acarl005/textcol"
colorable "github.com/mattn/go-colorable"
"github.com/willf/pad"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
const VERSION = "1.0.1"
// Wraps the file stat info and string to be printed.
type DisplayItem struct {
display string
info os.FileInfo
basename string
ext string
link *LinkInfo
}
func (item DisplayItem) Filename() string {
return item.basename + "." + item.ext
}
func (item DisplayItem) IsHidden() bool {
return item.basename == "" || item.basename[0] == '.'
}
// Wraps link stat info and whether the link points to valid file.
type LinkInfo struct {
path string
info os.FileInfo
broken bool
}
var (
// True is a helper varable to help make pointers to `true`.
True = true
// Prefixes for metric units.
sizeUnits = []string{"B", "K", "M", "G", "T", "P", "E"}
// Uses the "reference time"
// https://golang.org/pkg/time/#Time.Format
dateFormat = "02.Jan'06"
timeFormat = "15:04"
// Keeps track of execution time.
start int64
// Write to this to allow ANSI color codes to be compatible on Windows.
stdout = colorable.NewColorableStdout()
)
func main() {
textcol.Output = stdout
start = time.Now().UnixNano()
// auto-generate help text for the command with -h
kingpin.CommandLine.HelpFlag.Short('h')
// parse the arguments and populate the struct
kingpin.Parse()
argsPostParse()
if *args.version {
fmt.Println("v" + VERSION)
return
}
generateColors()
// separate the directories from the regular files
dirs := []string{}
files := []os.FileInfo{}
for _, pathStr := range *args.paths {
fileStat, err := os.Stat(pathStr)
if err != nil && strings.Contains(err.Error(), "no such file or directory") {
printErrorHeader(err, prettifyPath(pathStr))
continue
} else {
check(err)
}
if fileStat.IsDir() {
dirs = append(dirs, pathStr)
} else {
files = append(files, fileStat)
}
}
// list files first
if len(files) > 0 {
pwd := os.Getenv("PWD")
listFiles(pwd, &files, true)
}
// then list the contents of each directory
for i, dir := range dirs {
// print a blank line between directories, but not before the first one
if i > 0 {
fmt.Fprintln(stdout, "")
}
listDir(dir)
}
}
func listDir(pathStr string) {
items, err := ioutil.ReadDir(pathStr)
// if we couldn't read the folder, print a "header" with error message and use error-looking colors
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "permission denied") {
printErrorHeader(err, prettifyPath(pathStr))
return
}
check(err)
}
// filter by the regexp if one was passed
if len(*args.find) > 0 {
filteredItems := []os.FileInfo{}
for _, fileInfo := range items {
re, err := regexp.Compile(*args.find)
check(err)
if re.MatchString(fileInfo.Name()) {
filteredItems = append(filteredItems, fileInfo)
}
}
items = filteredItems
}
if !(len(*args.find) > 0 && len(items) == 0) &&
!(len(*args.paths) == 1 && (*args.paths)[0] == "." && !*args.recurse) {
printFolderHeader(pathStr)
}
if len(items) > 0 {
listFiles(pathStr, &items, false)
}
if *args.recurse {
for _, item := range items {
if item.IsDir() && (item.Name()[0] != '.' || *args.all) {
fmt.Fprintln(stdout, "") // put a blank line between directories
listDir(path.Join(pathStr, item.Name()))
}
}
}
}
func listFiles(parentDir string, items *[]os.FileInfo, forceDotfiles bool) {
absPath, err := filepath.Abs(parentDir)
check(err)
// collect all the contents here
files := []*DisplayItem{}
dirs := []*DisplayItem{}
// to help with formatting, we need to know the length of the longest name to add appropriate padding
longestOwnerName := 0
longestGroupName := 0
if *args.owner {
for _, fileInfo := range *items {
owner, group := getOwnerAndGroup(&fileInfo)
longestOwnerName = max(longestOwnerName, len(owner))
longestGroupName = max(longestGroupName, len(group))
}
}
for _, fileInfo := range *items {
// if this is a dotfile (hidden file)
if fileInfo.Name()[0] == '.' {
// we can skip everything with this file if we aren't using the `all` option
if !*args.all && !forceDotfiles {
continue
}
}
basename, ext := splitExt(fileInfo.Name())
displayItem := DisplayItem{
info: fileInfo,
ext: ext,
basename: basename,
}
// read some info about linked file if this item is a symlink
if fileInfo.Mode()&os.ModeSymlink != 0 {
getLinkInfo(&displayItem, absPath)
}
if fileInfo.IsDir() || (fileInfo.Mode()&os.ModeSymlink != 0 &&
displayItem.link.info != nil &&
displayItem.link.info.IsDir()) {
if *args.files {
continue
} else {
dirs = append(dirs, &displayItem)
}
} else {
if *args.dirs {
continue
} else {
files = append(files, &displayItem)
}
}
owner, group := getOwnerAndGroup(&fileInfo)
ownerColor, groupColor := getOwnerAndGroupColors(owner, group)
if *args.perms {
displayItem.display += permString(fileInfo, ownerColor, groupColor)
}
if *args.owner {
paddedOwner := pad.Right(owner, longestOwnerName, " ")
ownerInfo := []string{Reset + ownerColor + paddedOwner}
if !*args.nogroup {
paddedGroup := pad.Right(group, longestGroupName, " ")
ownerInfo = append(ownerInfo, groupColor+paddedGroup)
}
ownerInfo = append(ownerInfo, Reset)
displayItem.display += strings.Join(ownerInfo, " ")
}
if *args.bytes {
if fileInfo.Mode()&os.ModeDevice != 0 {
displayItem.display += deviceNumbers(path.Join(absPath, fileInfo.Name()))
} else {
displayItem.display += sizeString(fileInfo.Size())
}
}
if *args.mdate {
displayItem.display += timeString(fileInfo.ModTime())
}
displayItem.display += nameString(&displayItem)
if *args.links && fileInfo.Mode()&os.ModeSymlink != 0 {
displayItem.display += linkString(&displayItem, absPath)
}
}
if *args.sortTime {
sort.Sort(ByTime(dirs))
sort.Sort(ByTime(files))
if *args.backwards {
reverse(dirs)
reverse(files)
}
}
if *args.sortSize {
sort.Sort(BySize(files))
if *args.backwards {
reverse(files)
}
}
if *args.sortKind {
sort.Sort(ByKind(files))
if *args.backwards {
reverse(files)
}
}
// combine the items together again after sorting
allItems := append(dirs, files...)
// if using "long" display, just print one item per line
if *args.bytes || *args.mdate || *args.owner || *args.perms || *args.long {
for _, item := range allItems {
fmt.Fprintln(stdout, item.display)
}
} else {
// but if not, try to format in columns, link `ls` would
strs := []string{}
for _, item := range allItems {
strs = append(strs, item.display)
}
textcol.PrintColumns(&strs, 2)
}
if *args.stats {
printStats(len(files), len(dirs))
}
}
func getLinkInfo(item *DisplayItem, absPath string) {
fullPath := path.Join(absPath, item.info.Name())
linkPath, err1 := os.Readlink(fullPath)
check(err1)
linkFullPath := linkPath
if linkPath[0] != '/' {
linkFullPath = path.Join(absPath, linkPath)
}
linkInfo, err2 := os.Stat(linkFullPath)
if *args.linkRel {
linkRel, _ := filepath.Rel(absPath, linkPath)
if linkRel != "" && len(linkRel) <= len(linkPath) {
// i prefer the look of these relative paths prepended with ./
if linkRel[0] != '.' {
linkPath = "./" + linkRel
} else {
linkPath = linkRel
}
}
}
link := LinkInfo{
path: linkPath,
}
item.link = &link
if linkInfo != nil {
link.info = linkInfo
} else if strings.Contains(err2.Error(), "no such file or directory") {
link.broken = true
} else if !strings.Contains(err2.Error(), "permission denied") {
check(err2)
}
}
func nameString(item *DisplayItem) string {
mode := item.info.Mode()
name := item.info.Name()
if mode&os.ModeDir != 0 {
return dirString(item)
} else if mode&os.ModeSymlink != 0 {
if !item.link.broken && item.link.info.IsDir() {
color := ConfigColor["link"]["nameDir"]
if *args.nerdfont {
var linkIcon string
if item.link.broken {
linkIcon = otherIcons["brokenLink"]
} else {
linkIcon = otherIcons["linkDir"]
}
return color + linkIcon + " " + name + " " + Reset
} else if *args.icons {
return color + "🔗 " + name + " " + Reset
} else {
return color + " " + name + " " + Reset
}
} else {
color := ConfigColor["link"]["name"]
if *args.nerdfont {
var linkIcon string
if item.link.broken {
linkIcon = otherIcons["brokenLink"]
} else {
linkIcon = otherIcons["link"]
}
return color + linkIcon + " " + name + " " + Reset
} else if *args.icons {
return color + "🔗 " + name + " " + Reset
} else {
return color + " " + name + " " + Reset
}
}
} else if mode&os.ModeDevice != 0 {
color := ConfigColor["device"]["name"]
if *args.nerdfont {
return color + otherIcons["device"] + " " + name + " " + Reset
} else if *args.icons {
return color + "💽 " + name + " " + Reset
} else {
return color + " " + name + " " + Reset
}
} else if mode&os.ModeNamedPipe != 0 {
color := ConfigColor["pipe"]["name"]
if *args.nerdfont {
return color + otherIcons["pipe"] + " " + name + " " + Reset
} else if *args.icons {
return color + "🛢 " + name + " " + Reset
} else {
return color + " " + name + " " + Reset
}
} else if mode&os.ModeSocket != 0 {
color := ConfigColor["socket"]["name"]
if *args.nerdfont {
return color + otherIcons["socket"] + " " + name + " " + Reset
} else if *args.icons {
return color + "🔌 " + name + " " + Reset
} else {
return color + " " + name + " " + Reset
}
}
return fileString(item)
}
func linkString(item *DisplayItem, absPath string) string {
colors := ConfigColor["link"]
displayStrings := []string{}
if item.link.info == nil && item.link.broken {
displayStrings = append(displayStrings, colors["broken"]+"►", item.link.path+Reset)
} else if item.link.info != nil {
linkname, linkext := splitExt(item.link.path)
displayItem := DisplayItem{
info: item.link.info,
basename: linkname,
ext: linkext,
}
arrowColor := colors["arrow"]
if displayItem.info.IsDir() {
arrowColor = colors["arrowDir"]
}
displayStrings = append(displayStrings, arrowColor+"►", nameString(&displayItem))
} else {
displayStrings = append(displayStrings, item.link.path)
}
return strings.Join(displayStrings, " ")
}
func fileString(item *DisplayItem) string {
key := strings.ToLower(item.ext)
// figure out which color to choose
colors := FileColor["_default"]
alias, hasAlias := FileAliases[key]
if hasAlias {
key = alias
}
betterColor, hasBetterColor := FileColor[key]
if hasBetterColor {
colors = betterColor
}
ext := item.ext
if ext != "" {
ext = "." + ext
}
mainColor := colors.light
accentColor := colors.dark
if *args.light && colors.themeSwitch {
mainColor = colors.dark
accentColor = colors.light
}
// in some cases files have icons if front
// if nerd font enabled, then it'll be a file-specific icon, or if its an executable script, a little shell icon
// if the regular --icons flag is used instead, then it will show a ">_" only if the file is executable
icon := ""
executable := isExecutableScript(item)
if *args.nerdfont {
if executable {
icon = mainColor + getIconForFile("", "shell") + " "
} else {
icon = mainColor + getIconForFile(item.basename, item.ext) + " "
}
} else if *args.icons {
if executable {
icon = BgGray(1) + NamedFg(BrightGreen) + ">_" + Reset + " "
}
} else {
icon = " "
}
displayStrings := []string{icon}
if item.IsHidden() {
displayStrings = append(displayStrings, accentColor, item.basename, ext, Reset)
} else {
displayStrings = append(displayStrings, mainColor, item.basename, accentColor, ext, Reset)
}
return strings.Join(displayStrings, "")
}
// check for executable permissions
func isExecutableScript(item *DisplayItem) bool {
if item.info.Mode()&0111 != 0 && item.info.Mode().IsRegular() {
return true
}
return false
}
func dirString(item *DisplayItem) string {
colors := ConfigColor["dir"]
if item.basename == "" {
colors = ConfigColor[".dir"]
}
displayStrings := []string{colors["name"]}
icon := ""
if *args.icons {
displayStrings = append(displayStrings, "📂 ")
} else if *args.nerdfont {
icon = getIconForFolder(item.info.Name()) + " "
displayStrings = append(displayStrings, icon)
} else {
displayStrings = append(displayStrings, " ")
}
ext := item.ext
if ext != "" {
ext = "." + ext
}
displayStrings = append(displayStrings, item.basename, colors["ext"], ext, " ", Reset)
return strings.Join(displayStrings, "")
}
func rwxString(mode os.FileMode, i uint, color string) string {
bits := mode >> (i * 3)
coloredStrings := []string{color}
if bits&4 != 0 {
coloredStrings = append(coloredStrings, "r")
} else {
coloredStrings = append(coloredStrings, "-")
}
if bits&2 != 0 {
coloredStrings = append(coloredStrings, "w")
} else {
coloredStrings = append(coloredStrings, "-")
}
if i == 0 && mode&os.ModeSticky != 0 {
if bits&1 != 0 {
coloredStrings = append(coloredStrings, "t")
} else {
coloredStrings = append(coloredStrings, "T")
}
} else {
if bits&1 != 0 {
coloredStrings = append(coloredStrings, "x")
} else {
coloredStrings = append(coloredStrings, "-")
}
}
return strings.Join(coloredStrings, "")
}
// generates the permissions string, ya know like "drwxr-xr-x" and stuff like that
func permString(info os.FileInfo, ownerColor string, groupColor string) string {
defaultColor := PermsColor["other"]["_default"]
// info.Mode().String() does not produce the same output as `ls`, so we must build that string manually
mode := info.Mode()
// this "type" is not the file extension, but type as far as the OS is concerned
filetype := "-"
if mode&os.ModeDir != 0 {
filetype = "d"
} else if mode&os.ModeSymlink != 0 {
filetype = "l"
} else if mode&os.ModeDevice != 0 {
if mode&os.ModeCharDevice == 0 {
filetype = "b" // block device
} else {
filetype = "c" // character device
}
} else if mode&os.ModeNamedPipe != 0 {
filetype = "p"
} else if mode&os.ModeSocket != 0 {
filetype = "s"
}
coloredStrings := []string{defaultColor, filetype, " "}
coloredStrings = append(coloredStrings, rwxString(mode, 2, ownerColor))
coloredStrings = append(coloredStrings, rwxString(mode, 1, groupColor))
coloredStrings = append(coloredStrings, rwxString(mode, 0, defaultColor), Reset, " ")
return strings.Join(coloredStrings, "")
}
// Convert an integer number of bytes to a human-readable string using metric units with IEC binary
// prefixes, e.g. 10240 becomes "10 KiB", but we only show 1-letter units like "K".
func sizeString(size int64) string {
sizeFloat := float64(size)
for i, unit := range sizeUnits {
base := math.Pow(1024, float64(i))
if sizeFloat < base*1024 {
var sizeStr string
if i == 0 {
sizeStr = strconv.FormatInt(size, 10)
} else {
value := sizeFloat / base
if value < 1000 {
sizeStr = fmt.Sprintf("%.2f", value)
} else {
sizeStr = fmt.Sprintf("%.1f", value)
}
}
return SizeColor[unit] + pad.Left(sizeStr, 6, " ") + unit + " " + Reset
}
}
return strconv.Itoa(int(size))
}
func timeString(modtime time.Time) string {
dateStr := modtime.Format(dateFormat)
timeStr := modtime.Format(timeFormat)
hour, err := strconv.Atoi(timeStr[0:2])
check(err)
// Generate a color based on the hour of the day. darkest at midnight and lightest at noon.
timeColor := 14 - int(8*math.Cos(math.Pi*float64(hour)/12))
colored := []string{FgGray(22) + dateStr, FgGray(timeColor) + timeStr, Reset}
return strings.Join(colored, " ")
}
// When we list out any subdirectories, print those paths conspicuously above the contents. This helps with
// visual separation.
func printFolderHeader(pathStr string) {
colors := ConfigColor["folderHeader"]
headerString := colors["arrow"] + "►" + colors["main"] + " "
prettyPath := prettifyPath(pathStr)
if prettyPath == "/" {
headerString += "/"
} else {
folders := strings.Split(prettyPath, "/")
coloredFolders := make([]string, 0, len(folders))
for i, folder := range folders {
// Use different color for the last folder in the path.
if i == len(folders)-1 {
coloredFolders = append(coloredFolders, colors["lastFolder"]+folder)
} else {
coloredFolders = append(coloredFolders, colors["main"]+folder)
}
}
headerString += strings.Join(coloredFolders, colors["slash"]+"/")
}
fmt.Fprintln(stdout, headerString+" "+Reset)
}
func printErrorHeader(err error, pathStr string) {
fmt.Fprintln(stdout, ConfigColor["folderHeader"]["error"]+"► "+pathStr+Reset)
fmt.Fprintln(stdout, err.Error())
}
func prettifyPath(pathStr string) string {
prettyPath, err := filepath.Abs(pathStr)
check(err)
pwd := os.Getenv("PWD")
home := os.Getenv("HOME")
if strings.HasPrefix(prettyPath, pwd) {
prettyPath = "." + prettyPath[len(pwd):]
} else if strings.HasPrefix(prettyPath, home) {
prettyPath = "~" + prettyPath[len(home):]
}
return prettyPath
}
func getOwnerAndGroupColors(owner string, group string) (string, string) {
if owner == os.Getenv("USER") {
owner = "_self"
}
ownerColor := PermsColor["user"][owner]
if ownerColor == "" {
ownerColor = PermsColor["user"]["_default"]
}
groupColor := PermsColor["group"][group]
if groupColor == "" {
groupColor = PermsColor["group"]["_default"]
}
return ownerColor, groupColor
}
func printStats(numFiles, numDirs int) {
colors := ConfigColor["stats"]
end := time.Now().UnixNano()
microSeconds := (end - start) / int64(time.Microsecond)
milliSeconds := float64(microSeconds) / 1000
statStrings := []string{
colors["text"],
colors["number"] + strconv.Itoa(numDirs),
colors["text"] + "dirs",
colors["number"] + strconv.Itoa(numFiles),
colors["text"] + "files",
colors["ms"] + fmt.Sprintf("%.2f", milliSeconds),
colors["text"] + "ms",
Reset,
}
fmt.Fprintln(stdout, strings.Join(statStrings, " "))
}
func splitExt(filename string) (basepath, ext string) {
basename := filepath.Base(filename)
ext = filepath.Ext(filename)
basepath = strings.TrimSuffix(basename, ext)
if len(ext) > 0 {
ext = ext[1:]
}
return
}
// Go doesn't provide a `Max` function for ints like it does for floats (wtf?)
func max(a int, b int) int {
if a > b {
return a
}
return b
}
func check(err error) {
if err != nil {
panic(err)
}
}