| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
"text/template" |
| 9 |
|
| 10 |
_ "embed" |
| 11 |
) |
| 12 |
|
| 13 |
//go:embed etc/ruler.default |
| 14 |
var gDefaultRuler string |
| 15 |
|
| 16 |
type statData struct { |
| 17 |
Path string |
| 18 |
Name string |
| 19 |
Extension string |
| 20 |
Size int64 |
| 21 |
DirSize int64 |
| 22 |
DirCount int |
| 23 |
Permissions string |
| 24 |
ModTime string |
| 25 |
AccessTime string |
| 26 |
BirthTime string |
| 27 |
ChangeTime string |
| 28 |
LinkCount string |
| 29 |
User string |
| 30 |
Group string |
| 31 |
Target string |
| 32 |
CustomInfo string |
| 33 |
} |
| 34 |
|
| 35 |
type rulerData struct { |
| 36 |
SPACER string |
| 37 |
Message string |
| 38 |
Keys string |
| 39 |
Progress []string |
| 40 |
Copy []string |
| 41 |
Cut []string |
| 42 |
Select []string |
| 43 |
Visual []string |
| 44 |
Index int |
| 45 |
Total int |
| 46 |
Hidden int |
| 47 |
All int |
| 48 |
LinePercentage string |
| 49 |
ScrollPercentage string |
| 50 |
Filter []string |
| 51 |
Mode string |
| 52 |
Options map[string]string |
| 53 |
UserOptions map[string]string |
| 54 |
Stat *statData |
| 55 |
} |
| 56 |
|
| 57 |
func parseRuler(path string) (*template.Template, error) { |
| 58 |
funcs := template.FuncMap{ |
| 59 |
"df": func() string { return diskFree(".") }, |
| 60 |
"env": os.Getenv, |
| 61 |
"humanize": humanize, |
| 62 |
"join": strings.Join, |
| 63 |
"lower": strings.ToLower, |
| 64 |
"substr": func(s string, start, length int) string { return string([]rune(s)[start : start+length]) }, |
| 65 |
"upper": strings.ToUpper, |
| 66 |
} |
| 67 |
|
| 68 |
if path == "" { |
| 69 |
return template.New("ruler").Funcs(funcs).Parse(gDefaultRuler) |
| 70 |
} |
| 71 |
|
| 72 |
return template.New(filepath.Base(path)).Funcs(funcs).ParseFiles(path) |
| 73 |
} |
| 74 |
|
| 75 |
func renderRuler(ruler *template.Template, data rulerData, width int) (string, string, error) { |
| 76 |
var b strings.Builder |
| 77 |
if err := ruler.Execute(&b, data); err != nil { |
| 78 |
return "", "", err |
| 79 |
} |
| 80 |
|
| 81 |
s := strings.NewReplacer("\r", "", "\n", "").Replace(b.String()) |
| 82 |
sections := strings.Split(s, "\x1f") |
| 83 |
|
| 84 |
if len(sections) == 1 { |
| 85 |
return s, "", nil |
| 86 |
} |
| 87 |
|
| 88 |
wtot := 0 |
| 89 |
for _, section := range sections { |
| 90 |
wtot += printLength(section) |
| 91 |
} |
| 92 |
|
| 93 |
if wtot > width { |
| 94 |
return sections[0], strings.Join(sections[1:], ""), nil |
| 95 |
} |
| 96 |
|
| 97 |
wspacer := max(width-wtot, 0) / (len(sections) - 1) |
| 98 |
wspacerLast := max(width-wtot-wspacer*(len(sections)-2), 0) |
| 99 |
|
| 100 |
b.Reset() |
| 101 |
for i, section := range sections { |
| 102 |
switch i { |
| 103 |
case 0: |
| 104 |
b.WriteString(section) |
| 105 |
case len(sections) - 1: |
| 106 |
fmt.Fprintf(&b, "%*s%s", wspacerLast, "", section) |
| 107 |
default: |
| 108 |
fmt.Fprintf(&b, "%*s%s", wspacer, "", section) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return b.String(), "", nil |
| 113 |
} |