forked from gokcehan/lf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruler.go
More file actions
113 lines (97 loc) · 2.41 KB
/
ruler.go
File metadata and controls
113 lines (97 loc) · 2.41 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
_ "embed"
)
//go:embed etc/ruler.default
var gDefaultRuler string
type statData struct {
Path string
Name string
Extension string
Size int64
DirSize int64
DirCount int
Permissions string
ModTime string
AccessTime string
BirthTime string
ChangeTime string
LinkCount string
User string
Group string
Target string
CustomInfo string
}
type rulerData struct {
SPACER string
Message string
Keys string
Progress []string
Copy []string
Cut []string
Select []string
Visual []string
Index int
Total int
Hidden int
All int
LinePercentage string
ScrollPercentage string
Filter []string
Mode string
Options map[string]string
UserOptions map[string]string
Stat *statData
}
func parseRuler(path string) (*template.Template, error) {
funcs := template.FuncMap{
"df": func() string { return diskFree(".") },
"env": os.Getenv,
"humanize": humanize,
"join": strings.Join,
"lower": strings.ToLower,
"substr": func(s string, start, length int) string { return string([]rune(s)[start : start+length]) },
"upper": strings.ToUpper,
}
if path == "" {
return template.New("ruler").Funcs(funcs).Parse(gDefaultRuler)
}
return template.New(filepath.Base(path)).Funcs(funcs).ParseFiles(path)
}
func renderRuler(ruler *template.Template, data rulerData, width int) (string, string, error) {
var b strings.Builder
if err := ruler.Execute(&b, data); err != nil {
return "", "", err
}
s := strings.NewReplacer("\r", "", "\n", "").Replace(b.String())
sections := strings.Split(s, "\x1f")
if len(sections) == 1 {
return s, "", nil
}
wtot := 0
for _, section := range sections {
wtot += printLength(section)
}
if wtot > width {
return sections[0], strings.Join(sections[1:], ""), nil
}
wspacer := max(width-wtot, 0) / (len(sections) - 1)
wspacerLast := max(width-wtot-wspacer*(len(sections)-2), 0)
b.Reset()
for i, section := range sections {
switch i {
case 0:
b.WriteString(section)
case len(sections) - 1:
fmt.Fprintf(&b, "%*s%s", wspacerLast, "", section)
default:
fmt.Fprintf(&b, "%*s%s", wspacer, "", section)
}
}
return b.String(), "", nil
}