Add ruler file for customizing the ruler (#2186)
* Add ruler file for customizing the ruler * Update `CHANGELOG.md` * Show ruler errors instead of logging them * Allow multiple sections * Show syntax errors for ruler file * Add line percentage * Add lower/upper functions * Add back truncation * Use fmt options --------- Co-authored-by: CatsDeservePets <145048791+CatsDeservePets@users.noreply.github.com>
This commit is contained in:
1 parent
5d93764c79
commit
7b6b6da100
11 files changed
+497
-2
No files matched your search
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||
|
||||
- A new command `cmd-menu-discard` is added to allow exiting the completion menu with completions discarded (#2146).
|
||||
- The `lf_mode` environment variable will now be set to `compmenu` if the completion menu is active (#2146).
|
||||
- A `ruler` config file is added as an alternate method for customizing the ruler (#2186). This is intended to eventually replace the existing `rulerfmt`/`statfmt` options and must be enabled using the new `rulerfile` option. **This feature is currently experimental.**
|
||||
|
||||
### Fixed
|
||||
|
||||
|
||||
@@ -299,6 +299,7 @@ The following options can be used to customize the behavior of lf:
|
||||
relativenumber bool (default false)
|
||||
reverse bool (default false)
|
||||
roundbox bool (default false)
|
||||
rulerfile bool (default false)
|
||||
rulerfmt string (default " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;36m %v \033[0m| \033[7;34m %f \033[0m| %i/%t")
|
||||
scrolloff int (default 0)
|
||||
searchmethod string (default 'text')
|
||||
@@ -447,6 +448,12 @@ The icons file should be located at:
|
||||
Unix /etc/lf/icons ~/.config/lf/icons
|
||||
Windows C:\ProgramData\lf\icons C:\Users\<user>\AppData\Roaming\lf\icons
|
||||
|
||||
The ruler file should be located at:
|
||||
|
||||
OS system-wide user-specific
|
||||
Unix /etc/lf/ruler ~/.config/lf/ruler
|
||||
Windows C:\ProgramData\lf\ruler C:\Users\<user>\AppData\Roaming\lf\ruler
|
||||
|
||||
The selection file should be located at:
|
||||
|
||||
Unix ~/.local/share/lf/files
|
||||
@@ -1089,6 +1096,11 @@ Reverse the direction of sort.
|
||||
|
||||
Draw rounded outer corners when the `drawbox` option is enabled.
|
||||
|
||||
## rulerfile (bool) (default false)
|
||||
|
||||
Use the ruler file instead of the `rulerfmt` and `statfmt` options when drawing the ruler at the bottom.
|
||||
Refer to the [RULER section](https://github.com/gokcehan/lf/blob/master/doc.md#ruler) for more information about how the ruler file works.
|
||||
|
||||
## rulerfmt (string) (default ` %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;36m %v \033[0m| \033[7;34m %f \033[0m| %i/%t`)
|
||||
|
||||
Format string of the ruler shown in the bottom right corner.
|
||||
@@ -2187,3 +2199,52 @@ https://github.com/gokcehan/lf/blob/master/etc/icons.example
|
||||
|
||||
A sample colored icons file can be found at
|
||||
https://github.com/gokcehan/lf/blob/master/etc/icons_colored.example
|
||||
|
||||
# RULER
|
||||
|
||||
The ruler can be configured using a ruler file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)).
|
||||
The contents of the ruler file should be a Go template which is then rendered to create the actual output (refer to https://pkg.go.dev/text/template for more details on the syntax).
|
||||
This feature is currently experimental and must be enabled via the `rulerfile` option.
|
||||
|
||||
The following data fields are exported:
|
||||
|
||||
.Message string Includes internal messages, errors, and messages generated by the `echo`/`echomsg`/`echoerr` commands
|
||||
.Keys string Keys pressed by the user
|
||||
.Progress []string Progress indicators for copied, moved and deleted files
|
||||
.Copy []string List of files in the clipboard to be copied
|
||||
.Cut []string List of files in the clipboard to be moved
|
||||
.Select []string Selection list
|
||||
.Visual []string Visual selection
|
||||
.Index int Index of the cursor
|
||||
.Total int Number of visible files in the current directory
|
||||
.Hidden int Number of hidden files in the current directory
|
||||
.LinePercentage string Line percentage (analagous to `%p` for the `statusline` option in Vim)
|
||||
.ScrollPercentage string Scroll percentage (analagous to `%P` for the `statusline` option in Vim)
|
||||
.Filter []string Filter currently being applied
|
||||
.Mode string Current mode ("NORMAL" for Normal mode, and "VISUAL" for Visual mode)
|
||||
.Options map[string]string The value of options (e.g. `{{.Options.hidden}}`)
|
||||
.UserOptions map[string]string The value of user-defined options (e.g. `{{.UserOptions.foo}}`)
|
||||
.Stat.Path string Path of the current file
|
||||
.Stat.Name string Name of the current file
|
||||
.Stat.Size uint64 Size of the current file
|
||||
.Stat.Permissions string Permissions of the current file
|
||||
.Stat.ModTime string Last modified time of the current file (formatted based on the `timefmt` option)
|
||||
.Stat.LinkCount string Number of hard links for the current file
|
||||
.Stat.User string User of the current file
|
||||
.Stat.Group string Group of the current file
|
||||
.Stat.Target string Target if the current file is a symbolic link, otherwise a blank string
|
||||
|
||||
The following functions are exported:
|
||||
|
||||
df func() string Get an indicator representing the amount of free disk space available
|
||||
env func(string) string Get the value of an environment variable
|
||||
humanize func(uint64) string Express a file size in a human-readable format
|
||||
join func([]string, string) string Join a string array by a separator
|
||||
lower func(string) string Convert a string to lowercase
|
||||
substr func(string, int, int) string Get a substring based on starting index and length
|
||||
upper func(string) string Convert a string to uppercase
|
||||
|
||||
The special identifier `{{.SPACER}}` can be used to divide the ruler into sections that are spaced evenly from each other.
|
||||
|
||||
The default ruler file can be found at
|
||||
https://github.com/gokcehan/lf/blob/master/etc/ruler.default
|
||||
@@ -311,6 +311,7 @@ The following options can be used to customize the behavior of lf:
|
||||
relativenumber bool (default false)
|
||||
reverse bool (default false)
|
||||
roundbox bool (default false)
|
||||
rulerfile bool (default false)
|
||||
rulerfmt string (default " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;36m %v \033[0m| \033[7;34m %f \033[0m| %i/%t")
|
||||
scrolloff int (default 0)
|
||||
searchmethod string (default 'text')
|
||||
@@ -461,6 +462,12 @@ The icons file should be located at:
|
||||
Unix /etc/lf/icons ~/.config/lf/icons
|
||||
Windows C:\ProgramData\lf\icons C:\Users\<user>\AppData\Roaming\lf\icons
|
||||
|
||||
The ruler file should be located at:
|
||||
|
||||
OS system-wide user-specific
|
||||
Unix /etc/lf/ruler ~/.config/lf/ruler
|
||||
Windows C:\ProgramData\lf\ruler C:\Users\<user>\AppData\Roaming\lf\ruler
|
||||
|
||||
The selection file should be located at:
|
||||
|
||||
Unix ~/.local/share/lf/files
|
||||
@@ -1181,6 +1188,12 @@ roundbox (bool) (default false)
|
||||
|
||||
Draw rounded outer corners when the drawbox option is enabled.
|
||||
|
||||
rulerfile (bool) (default false)
|
||||
|
||||
Use the ruler file instead of the rulerfmt and statfmt options when
|
||||
drawing the ruler at the bottom. Refer to the RULER section for more
|
||||
information about how the ruler file works.
|
||||
|
||||
rulerfmt (string) (default %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;36m %v \033[0m| \033[7;34m %f \033[0m| %i/%t)
|
||||
|
||||
Format string of the ruler shown in the bottom right corner.
|
||||
@@ -2451,3 +2464,56 @@ https://github.com/gokcehan/lf/blob/master/etc/icons.example
|
||||
|
||||
A sample colored icons file can be found at
|
||||
https://github.com/gokcehan/lf/blob/master/etc/icons_colored.example
|
||||
|
||||
RULER
|
||||
|
||||
The ruler can be configured using a ruler file (refer to the
|
||||
CONFIGURATION section). The contents of the ruler file should be a Go
|
||||
template which is then rendered to create the actual output (refer to
|
||||
https://pkg.go.dev/text/template for more details on the syntax). This
|
||||
feature is currently experimental and must be enabled via the rulerfile
|
||||
option.
|
||||
|
||||
The following data fields are exported:
|
||||
|
||||
.Message string Includes internal messages, errors, and messages generated by the `echo`/`echomsg`/`echoerr` commands
|
||||
.Keys string Keys pressed by the user
|
||||
.Progress []string Progress indicators for copied, moved and deleted files
|
||||
.Copy []string List of files in the clipboard to be copied
|
||||
.Cut []string List of files in the clipboard to be moved
|
||||
.Select []string Selection list
|
||||
.Visual []string Visual selection
|
||||
.Index int Index of the cursor
|
||||
.Total int Number of visible files in the current directory
|
||||
.Hidden int Number of hidden files in the current directory
|
||||
.LinePercentage string Line percentage (analagous to `%p` for the `statusline` option in Vim)
|
||||
.ScrollPercentage string Scroll percentage (analagous to `%P` for the `statusline` option in Vim)
|
||||
.Filter []string Filter currently being applied
|
||||
.Mode string Current mode ("NORMAL" for Normal mode, and "VISUAL" for Visual mode)
|
||||
.Options map[string]string The value of options (e.g. `{{.Options.hidden}}`)
|
||||
.UserOptions map[string]string The value of user-defined options (e.g. `{{.UserOptions.foo}}`)
|
||||
.Stat.Path string Path of the current file
|
||||
.Stat.Name string Name of the current file
|
||||
.Stat.Size uint64 Size of the current file
|
||||
.Stat.Permissions string Permissions of the current file
|
||||
.Stat.ModTime string Last modified time of the current file (formatted based on the `timefmt` option)
|
||||
.Stat.LinkCount string Number of hard links for the current file
|
||||
.Stat.User string User of the current file
|
||||
.Stat.Group string Group of the current file
|
||||
.Stat.Target string Target if the current file is a symbolic link, otherwise a blank string
|
||||
|
||||
The following functions are exported:
|
||||
|
||||
df func() string Get an indicator representing the amount of free disk space available
|
||||
env func(string) string Get the value of an environment variable
|
||||
humanize func(uint64) string Express a file size in a human-readable format
|
||||
join func([]string, string) string Join a string array by a separator
|
||||
lower func(string) string Convert a string to lowercase
|
||||
substr func(string, int, int) string Get a substring based on starting index and length
|
||||
upper func(string) string Convert a string to uppercase
|
||||
|
||||
The special identifier {{.SPACER}} can be used to divide the ruler into
|
||||
sections that are spaced evenly from each other.
|
||||
|
||||
The default ruler file can be found at
|
||||
https://github.com/gokcehan/lf/blob/master/etc/ruler.default
|
||||
@@ -0,0 +1,20 @@
|
||||
{{with .Message -}}
|
||||
{{. -}}
|
||||
{{else with .Stat -}}
|
||||
{{.Permissions | printf "\033[36m%s\033[0m" -}}
|
||||
{{with .LinkCount}} {{.}}{{end -}}
|
||||
{{with .User}} {{.}}{{end -}}
|
||||
{{with .Group}} {{.}}{{end -}}
|
||||
{{.Size | humanize | printf " %5s" -}}
|
||||
{{.ModTime | printf " %s" -}}
|
||||
{{with .Target}} -> {{.}}{{end -}}
|
||||
{{end -}}
|
||||
{{.SPACER -}}
|
||||
{{with .Keys}} {{.}}{{end -}}
|
||||
{{with .Progress}} {{join . " "}}{{end -}}
|
||||
{{with .Copy}} {{len . | printf "%s %d \033[0m" $.Options.copyfmt}}{{end -}}
|
||||
{{with .Cut}} {{len . | printf "%s %d \033[0m" $.Options.cutfmt}}{{end -}}
|
||||
{{with .Select}} {{len . | printf "%s %d \033[0m" $.Options.selectfmt}}{{end -}}
|
||||
{{with .Visual}} {{len . | printf "%s %d \033[0m" $.Options.visualfmt}}{{end -}}
|
||||
{{with .Filter}} {{join . " " | printf "\033[7;34m %s \033[0m"}}{{end -}}
|
||||
{{printf " %d/%d" .Index .Total}}
|
||||
@@ -160,6 +160,8 @@ func (e *setExpr) eval(app *app, args []string) {
|
||||
}
|
||||
case "roundbox", "noroundbox", "roundbox!":
|
||||
err = applyBoolOpt(&gOpts.roundbox, e)
|
||||
case "rulerfile", "norulerfile", "rulerfile!":
|
||||
err = applyBoolOpt(&gOpts.rulerfile, e)
|
||||
case "showbinds", "noshowbinds", "showbinds!":
|
||||
err = applyBoolOpt(&gOpts.showbinds, e)
|
||||
// DEPRECATED: remove after r38 is released
|
||||
|
||||
@@ -322,6 +322,7 @@ ratios []int (default \(aq1:2:3\(aq)
|
||||
relativenumber bool (default false)
|
||||
reverse bool (default false)
|
||||
roundbox bool (default false)
|
||||
rulerfile bool (default false)
|
||||
rulerfmt string (default \(dq %a| %p| \(rs033[7;31m %m \(rs033[0m| \(rs033[7;33m %c \(rs033[0m| \(rs033[7;35m %s \(rs033[0m| \(rs033[7;36m %v \(rs033[0m| \(rs033[7;34m %f \(rs033[0m| %i/%t\(dq)
|
||||
scrolloff int (default 0)
|
||||
searchmethod string (default \(aqtext\(aq)
|
||||
@@ -488,6 +489,14 @@ Unix /etc/lf/icons \(ti/.config/lf/icons
|
||||
Windows C:\(rsProgramData\(rslf\(rsicons C:\(rsUsers\(rs<user>\(rsAppData\(rsRoaming\(rslf\(rsicons
|
||||
.EE
|
||||
.PP
|
||||
The ruler file should be located at:
|
||||
.IP
|
||||
.EX
|
||||
OS system\-wide user\-specific
|
||||
Unix /etc/lf/ruler \(ti/.config/lf/ruler
|
||||
Windows C:\(rsProgramData\(rslf\(rsruler C:\(rsUsers\(rs<user>\(rsAppData\(rsRoaming\(rslf\(rsruler
|
||||
.EE
|
||||
.PP
|
||||
The selection file should be located at:
|
||||
.IP
|
||||
.EX
|
||||
@@ -1040,6 +1049,14 @@ Reverse the direction of sort.
|
||||
.SS roundbox (bool) (default false)
|
||||
Draw rounded outer corners when the \f[CR]drawbox\f[R] option is
|
||||
enabled.
|
||||
.SS rulerfile (bool) (default false)
|
||||
Use the ruler file instead of the \f[CR]rulerfmt\f[R] and
|
||||
\f[CR]statfmt\f[R] options when drawing the ruler at the bottom.
|
||||
Refer to the \c
|
||||
.UR https://github.com/gokcehan/lf/blob/master/doc.md#ruler
|
||||
RULER section
|
||||
.UE \c
|
||||
\ for more information about how the ruler file works.
|
||||
.SS rulerfmt (string) (default \f[CR] %a| %p| \(rs033[7;31m %m \(rs033[0m| \(rs033[7;33m %c \(rs033[0m| \(rs033[7;35m %s \(rs033[0m| \(rs033[7;36m %v \(rs033[0m| \(rs033[7;34m %f \(rs033[0m| %i/%t\f[R])
|
||||
Format string of the ruler shown in the bottom right corner.
|
||||
.PP
|
||||
@@ -2475,3 +2492,65 @@ A sample icons file can be found at \c
|
||||
A sample colored icons file can be found at \c
|
||||
.UR https://github.com/gokcehan/lf/blob/master/etc/icons_colored.example
|
||||
.UE \c
|
||||
.SH RULER
|
||||
The ruler can be configured using a ruler file (refer to the \c
|
||||
.UR https://github.com/gokcehan/lf/blob/master/doc.md#configuration
|
||||
CONFIGURATION section
|
||||
.UE \c
|
||||
).
|
||||
The contents of the ruler file should be a Go template which is then
|
||||
rendered to create the actual output (refer to \c
|
||||
.UR https://pkg.go.dev/text/template
|
||||
.UE \c
|
||||
\ for more details on the syntax).
|
||||
This feature is currently experimental and must be enabled via the
|
||||
\f[CR]rulerfile\f[R] option.
|
||||
.PP
|
||||
The following data fields are exported:
|
||||
.IP
|
||||
.EX
|
||||
\&.Message string Includes internal messages, errors, and messages generated by the \(gaecho\(ga/\(gaechomsg\(ga/\(gaechoerr\(ga commands
|
||||
\&.Keys string Keys pressed by the user
|
||||
\&.Progress []string Progress indicators for copied, moved and deleted files
|
||||
\&.Copy []string List of files in the clipboard to be copied
|
||||
\&.Cut []string List of files in the clipboard to be moved
|
||||
\&.Select []string Selection list
|
||||
\&.Visual []string Visual selection
|
||||
\&.Index int Index of the cursor
|
||||
\&.Total int Number of visible files in the current directory
|
||||
\&.Hidden int Number of hidden files in the current directory
|
||||
\&.LinePercentage string Line percentage (analagous to \(ga%p\(ga for the \(gastatusline\(ga option in Vim)
|
||||
\&.ScrollPercentage string Scroll percentage (analagous to \(ga%P\(ga for the \(gastatusline\(ga option in Vim)
|
||||
\&.Filter []string Filter currently being applied
|
||||
\&.Mode string Current mode (\(dqNORMAL\(dq for Normal mode, and \(dqVISUAL\(dq for Visual mode)
|
||||
\&.Options map[string]string The value of options (e.g. \(ga{{.Options.hidden}}\(ga)
|
||||
\&.UserOptions map[string]string The value of user\-defined options (e.g. \(ga{{.UserOptions.foo}}\(ga)
|
||||
\&.Stat.Path string Path of the current file
|
||||
\&.Stat.Name string Name of the current file
|
||||
\&.Stat.Size uint64 Size of the current file
|
||||
\&.Stat.Permissions string Permissions of the current file
|
||||
\&.Stat.ModTime string Last modified time of the current file (formatted based on the \(gatimefmt\(ga option)
|
||||
\&.Stat.LinkCount string Number of hard links for the current file
|
||||
\&.Stat.User string User of the current file
|
||||
\&.Stat.Group string Group of the current file
|
||||
\&.Stat.Target string Target if the current file is a symbolic link, otherwise a blank string
|
||||
.EE
|
||||
.PP
|
||||
The following functions are exported:
|
||||
.IP
|
||||
.EX
|
||||
df func() string Get an indicator representing the amount of free disk space available
|
||||
env func(string) string Get the value of an environment variable
|
||||
humanize func(uint64) string Express a file size in a human\-readable format
|
||||
join func([]string, string) string Join a string array by a separator
|
||||
lower func(string) string Convert a string to lowercase
|
||||
substr func(string, int, int) string Get a substring based on starting index and length
|
||||
upper func(string) string Convert a string to uppercase
|
||||
.EE
|
||||
.PP
|
||||
The special identifier \f[CR]{{.SPACER}}\f[R] can be used to divide the
|
||||
ruler into sections that are spaced evenly from each other.
|
||||
.PP
|
||||
The default ruler file can be found at \c
|
||||
.UR https://github.com/gokcehan/lf/blob/master/etc/ruler.default
|
||||
.UE \c
|
||||
@@ -90,6 +90,7 @@ var gOpts struct {
|
||||
relativenumber bool
|
||||
reverse bool
|
||||
roundbox bool
|
||||
rulerfile bool
|
||||
rulerfmt string
|
||||
scrolloff int
|
||||
searchmethod searchMethod
|
||||
@@ -250,6 +251,7 @@ func init() {
|
||||
gOpts.relativenumber = false
|
||||
gOpts.reverse = false
|
||||
gOpts.roundbox = false
|
||||
gOpts.rulerfile = false
|
||||
gOpts.rulerfmt = " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;36m %v \033[0m| \033[7;34m %f \033[0m| %i/%t"
|
||||
gOpts.scrolloff = 0
|
||||
gOpts.searchmethod = textSearch
|
||||
|
||||
@@ -38,6 +38,7 @@ var (
|
||||
gConfigPaths []string
|
||||
gColorsPaths []string
|
||||
gIconsPaths []string
|
||||
gRulerPaths []string
|
||||
gFilesPath string
|
||||
gMarksPath string
|
||||
gTagsPath string
|
||||
@@ -105,6 +106,11 @@ func init() {
|
||||
filepath.Join(config, "lf", "icons"),
|
||||
}
|
||||
|
||||
gRulerPaths = []string{
|
||||
filepath.Join("/etc", "lf", "ruler"),
|
||||
filepath.Join(config, "lf", "ruler"),
|
||||
}
|
||||
|
||||
data := cmp.Or(
|
||||
os.Getenv("LF_DATA_HOME"),
|
||||
os.Getenv("XDG_DATA_HOME"),
|
||||
|
||||
@@ -36,6 +36,7 @@ var (
|
||||
gConfigPaths []string
|
||||
gColorsPaths []string
|
||||
gIconsPaths []string
|
||||
gRulerPaths []string
|
||||
gFilesPath string
|
||||
gTagsPath string
|
||||
gMarksPath string
|
||||
@@ -95,6 +96,11 @@ func init() {
|
||||
filepath.Join(config, "lf", "icons"),
|
||||
}
|
||||
|
||||
gRulerPaths = []string{
|
||||
filepath.Join(os.Getenv("ProgramData"), "lf", "ruler"),
|
||||
filepath.Join(config, "lf", "ruler"),
|
||||
}
|
||||
|
||||
data := cmp.Or(
|
||||
os.Getenv("LF_DATA_HOME"),
|
||||
os.Getenv("XDG_DATA_HOME"),
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed etc/ruler.default
|
||||
var gDefaultRuler string
|
||||
|
||||
type statData struct {
|
||||
Path string
|
||||
Name string
|
||||
Size uint64
|
||||
Permissions string
|
||||
ModTime string
|
||||
LinkCount string
|
||||
User string
|
||||
Group string
|
||||
Target 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
|
||||
LinePercentage string
|
||||
ScrollPercentage string
|
||||
Filter []string
|
||||
Mode string
|
||||
Options map[string]string
|
||||
UserOptions map[string]string
|
||||
Stat *statData
|
||||
}
|
||||
|
||||
func parseRuler() (*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,
|
||||
}
|
||||
|
||||
for i := len(gRulerPaths) - 1; i >= 0; i-- {
|
||||
path := gRulerPaths[i]
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("reading file: %s", path)
|
||||
return template.New("ruler").Funcs(funcs).ParseFiles(path)
|
||||
}
|
||||
|
||||
return template.New("ruler").Funcs(funcs).Parse(gDefaultRuler)
|
||||
}
|
||||
|
||||
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.TrimSuffix(b.String(), "\n")
|
||||
s = strings.ReplaceAll(s, "\n", "\033[0;7m\\n\033[0m")
|
||||
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
|
||||
}
|
||||
@@ -6,11 +6,13 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"text/template"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
@@ -668,6 +670,8 @@ type ui struct {
|
||||
keyCount []rune
|
||||
styles styleMap
|
||||
icons iconMap
|
||||
ruler *template.Template
|
||||
rulerErr error
|
||||
currentFile string
|
||||
pasteEvent bool
|
||||
}
|
||||
@@ -691,6 +695,7 @@ func newUI(screen tcell.Screen) *ui {
|
||||
currentFile: "",
|
||||
sxScreen: sixelScreen{},
|
||||
}
|
||||
ui.ruler, ui.rulerErr = parseRuler()
|
||||
|
||||
go ui.pollEvents()
|
||||
|
||||
@@ -1022,6 +1027,137 @@ func (ui *ui) drawRuler(nav *nav) {
|
||||
ui.msgWin.printRight(ui.screen, 0, st, ruler.String())
|
||||
}
|
||||
|
||||
func (ui *ui) drawRulerFile(nav *nav) {
|
||||
if ui.rulerErr != nil {
|
||||
err := fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), fmt.Errorf("parsing ruler: %w", ui.rulerErr))
|
||||
ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, err)
|
||||
return
|
||||
}
|
||||
|
||||
var stat *statData
|
||||
curr, err := nav.currFile()
|
||||
if err == nil {
|
||||
if curr.err == nil {
|
||||
stat = &statData{
|
||||
Path: curr.path,
|
||||
Name: curr.Name(),
|
||||
Size: uint64(curr.Size()),
|
||||
Permissions: curr.Mode().String(),
|
||||
ModTime: curr.ModTime().Format(gOpts.timefmt),
|
||||
LinkCount: linkCount(curr),
|
||||
User: userName(curr),
|
||||
Group: groupName(curr),
|
||||
Target: curr.linkTarget,
|
||||
}
|
||||
} else {
|
||||
ui.echoerrf("stat: %s", curr.err)
|
||||
}
|
||||
}
|
||||
|
||||
dir := nav.currDir()
|
||||
tot := len(dir.files)
|
||||
ind := min(dir.ind+1, tot)
|
||||
hid := len(dir.allFiles) - tot
|
||||
|
||||
var linePercentage string
|
||||
if tot == 0 {
|
||||
linePercentage = "100%"
|
||||
} else {
|
||||
linePercentage = fmt.Sprintf("%d%%", ind*100/tot)
|
||||
}
|
||||
|
||||
var scrollPercentage string
|
||||
beg := max(dir.ind-dir.pos, 0)
|
||||
switch {
|
||||
case tot <= nav.height:
|
||||
scrollPercentage = "All"
|
||||
case beg == 0:
|
||||
scrollPercentage = "Top"
|
||||
case beg == tot-nav.height:
|
||||
scrollPercentage = "Bot"
|
||||
default:
|
||||
scrollPercentage = fmt.Sprintf("%2d%%", beg*100/(tot-nav.height))
|
||||
}
|
||||
|
||||
var copy []string
|
||||
var cut []string
|
||||
if nav.clipboard.mode == clipboardCopy {
|
||||
copy = nav.clipboard.paths
|
||||
} else {
|
||||
cut = nav.clipboard.paths
|
||||
}
|
||||
|
||||
currSelections := nav.currSelections()
|
||||
currVSelections := nav.currDir().visualSelections()
|
||||
|
||||
progress := []string{}
|
||||
|
||||
if nav.copyJobs > 0 {
|
||||
if nav.copyTotal == 0 {
|
||||
progress = append(progress, fmt.Sprintf("[0%%]"))
|
||||
} else {
|
||||
progress = append(progress, fmt.Sprintf("[%d%%]", nav.copyBytes*100/nav.copyTotal))
|
||||
}
|
||||
}
|
||||
|
||||
if nav.moveTotal > 0 {
|
||||
progress = append(progress, fmt.Sprintf("[%d/%d]", nav.moveCount, nav.moveTotal))
|
||||
}
|
||||
|
||||
if nav.deleteTotal > 0 {
|
||||
progress = append(progress, fmt.Sprintf("[%d/%d]", nav.deleteCount, nav.deleteTotal))
|
||||
}
|
||||
|
||||
mode := "NORMAL"
|
||||
if nav.isVisualMode() {
|
||||
mode = "VISUAL"
|
||||
}
|
||||
|
||||
options := make(map[string]string)
|
||||
v := reflect.ValueOf(gOpts)
|
||||
t := v.Type()
|
||||
for i := range v.NumField() {
|
||||
name := t.Field(i).Name
|
||||
switch name {
|
||||
case "nkeys", "vkeys", "cmdkeys", "cmds", "user":
|
||||
continue
|
||||
default:
|
||||
options[name] = fieldToString(v.Field(i))
|
||||
}
|
||||
}
|
||||
|
||||
data := rulerData{
|
||||
SPACER: "\x1f",
|
||||
Message: ui.msg,
|
||||
Keys: string(ui.keyCount) + string(ui.keyAcc),
|
||||
Progress: progress,
|
||||
Copy: copy,
|
||||
Cut: cut,
|
||||
Select: currSelections,
|
||||
Visual: currVSelections,
|
||||
Index: ind,
|
||||
Total: tot,
|
||||
Hidden: hid,
|
||||
LinePercentage: linePercentage,
|
||||
ScrollPercentage: scrollPercentage,
|
||||
Filter: dir.filter,
|
||||
Mode: mode,
|
||||
Options: options,
|
||||
UserOptions: gOpts.user,
|
||||
Stat: stat,
|
||||
}
|
||||
|
||||
left, right, err := renderRuler(ui.ruler, data, ui.msgWin.w)
|
||||
if err != nil {
|
||||
err := fmt.Sprintf(optionToFmtstr(gOpts.errorfmt), fmt.Errorf("rendering ruler: %w", err))
|
||||
ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, err)
|
||||
return
|
||||
}
|
||||
|
||||
ui.msgWin.print(ui.screen, 0, 0, tcell.StyleDefault, left)
|
||||
ui.msgWin.printRight(ui.screen, 0, tcell.StyleDefault, right)
|
||||
}
|
||||
|
||||
func (ui *ui) drawBox() {
|
||||
st := parseEscapeSequence(gOpts.borderfmt)
|
||||
|
||||
@@ -1129,8 +1265,12 @@ func (ui *ui) draw(nav *nav) {
|
||||
|
||||
switch ui.cmdPrefix {
|
||||
case "":
|
||||
ui.drawStat(nav)
|
||||
ui.drawRuler(nav)
|
||||
if gOpts.rulerfile {
|
||||
ui.drawRulerFile(nav)
|
||||
} else {
|
||||
ui.drawStat(nav)
|
||||
ui.drawRuler(nav)
|
||||
}
|
||||
ui.screen.HideCursor()
|
||||
case ">":
|
||||
maxWidth := ui.msgWin.w - 1 // leave space for cursor at the end
|
||||
|
||||
Reference in new issue
Block a user