-
Notifications
You must be signed in to change notification settings - Fork 17
/
fmt.go
125 lines (107 loc) · 3.23 KB
/
fmt.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
// fmt.go for sutff to format output in the stdoutt/terminal
package main
import (
"fmt"
"os"
"sort"
"strings"
"unicode/utf8"
isatty "github.com/mattn/go-isatty"
c "github.com/mitchellh/colorstring"
)
const (
dashesNumber = 2
)
var (
terminalWidth = 80
columnSize = 39 // characters in the filename column
)
// Defines Terminal Coloring Theme
var ColorScheme c.Colorize
// BlankScheme color scheme just deletes all the coloring tags
// this colorScheme is applied before fetching the string length
// so that color tags (or color decoding escape symbols )
// do not screw up column alignment and so on
var BlankScheme c.Colorize
func init() {
ColorScheme = c.Colorize{
Colors: map[string]string{
"DEFAULT": c.DefaultColors["default"],
"FILENAME": c.DefaultColors["light_green"],
"META": c.DefaultColors["red"],
"DESCRIPTION": c.DefaultColors["light_yellow"],
"HR": c.DefaultColors["light_cyan"],
"NUMBER": c.DefaultColors["light_red"],
},
Reset: true,
Disable: !isatty.IsTerminal(os.Stdout.Fd()),
}
BlankScheme = ColorScheme
BlankScheme.Disable = true
}
func render() {
SetColumnSize()
Traverse()
renderSummary()
}
func renderSummary() {
printHR()
printCentered(fmt.Sprintf(ColorScheme.Color("[DEFAULT]lsp \"[NUMBER]%s[DEFAULT]\""), presentPath(mode.absolutePath)) + fmt.Sprintf(ColorScheme.Color(", [NUMBER]%v[DEFAULT] files, [NUMBER]%v[DEFAULT] directories"), len(FileList), len(Trie.Ch["dirs"].Fls)))
for _, cm := range mode.comments {
printCentered(cm)
}
}
func renderFiles(fls []*FileInfo) {
switch {
case mode.size:
sort.Sort(sizeSort(fls))
case mode.time:
sort.Sort(timeSort(fls))
default:
sort.Sort(alphabeticSort(fls))
}
for _, fl := range fls {
if !fl.hidden {
PrintColumns(fl.f.Name(), fl.Description())
}
}
}
// PrintColumns prints two-column table row, nicely formatted and shortened if needed
func PrintColumns(filename, description string) {
maxFileNameSize := columnSize - 6
if utf8.RuneCountInString(filename) > maxFileNameSize {
filename = string([]rune(filename)[0:maxFileNameSize]) + "[META][...]"
}
indentSize := columnSize - utf8.RuneCountInString(BlankScheme.Color(filename))
if !mode.pyramid {
fmt.Printf(ColorScheme.Color(fmt.Sprintf("[FILENAME]%s", filename)))
fmt.Printf(strings.Repeat(" ", indentSize))
} else {
fmt.Printf(strings.Repeat(" ", indentSize))
fmt.Printf(ColorScheme.Color(fmt.Sprintf("[FILENAME]%s", filename)))
}
// central dividing space
fmt.Printf(" ")
fmt.Printf(ColorScheme.Color(fmt.Sprintf("[DESCRIPTION]%s\n", description)))
}
func printHeader(o string) {
length := utf8.RuneCountInString(o)
sideburns := (6+2*columnSize-length)/2 - dashesNumber
if sideburns < 0 {
sideburns = 0
}
fmt.Printf(strings.Repeat(" ", sideburns))
fmt.Printf(ColorScheme.Color("[DESCRIPTION]" + strings.Repeat("-", dashesNumber) + o + strings.Repeat("-", dashesNumber) + "[DEFAULT]\n"))
}
func printCentered(o string) {
length := utf8.RuneCountInString(o)
sideburns := (6 + 2*columnSize - length) / 2
if sideburns < 0 {
sideburns = 0
}
fmt.Printf(strings.Repeat(" ", sideburns))
fmt.Printf(ColorScheme.Color(o + "\n"))
}
func printHR() {
fmt.Printf(ColorScheme.Color("[HR]" + strings.Repeat("-", terminalWidth) + "\n"))
}