Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use symlink target for stat styling & icons #1644

Merged
merged 6 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import (
"github.com/gdamore/tcell/v2"
)

type styleMap map[string]tcell.Style
type styleMap struct {
styles map[string]tcell.Style
useLinkTarget bool
}

func parseStyles() styleMap {
sm := make(styleMap)
sm := styleMap{
styles: make(map[string]tcell.Style),
useLinkTarget: false,
}

// Default values from dircolors
//
Expand Down Expand Up @@ -199,12 +205,12 @@ func (sm styleMap) parseFile(path string) {
key = filepath.Clean(key)
}

sm[key] = applyAnsiCodes(val, tcell.StyleDefault)
sm.styles[key] = applyAnsiCodes(val, tcell.StyleDefault)
joelim-work marked this conversation as resolved.
Show resolved Hide resolved
}
}

// This function parses $LS_COLORS environment variable.
func (sm styleMap) parseGNU(env string) {
func (sm *styleMap) parseGNU(env string) {
for _, entry := range strings.Split(env, ":") {
if entry == "" {
continue
Expand All @@ -225,7 +231,13 @@ func (sm styleMap) parseGNU(env string) {
key = filepath.Clean(key)
}

sm[key] = applyAnsiCodes(val, tcell.StyleDefault)
if key == "ln" && val == "target" {
sm.useLinkTarget = true
log.Printf("using link target for styles")
joelim-work marked this conversation as resolved.
Show resolved Hide resolved
continue
}

sm.styles[key] = applyAnsiCodes(val, tcell.StyleDefault)
}
}

Expand Down Expand Up @@ -267,26 +279,24 @@ func (sm styleMap) parseBSD(env string) {
}

for i, key := range colorNames {
sm[key] = getStyle(env[i*2], env[i*2+1])
sm.styles[key] = getStyle(env[i*2], env[i*2+1])
}
}

func (sm styleMap) get(f *file) tcell.Style {
if val, ok := sm[f.path]; ok {
if val, ok := sm.styles[f.path]; ok {
return val
}

if f.IsDir() {
if val, ok := sm[f.Name()+"/"]; ok {
if val, ok := sm.styles[f.Name()+"/"]; ok {
return val
}
}

var key string

switch {
case f.linkState == working:
key = "ln"
case f.linkState == broken:
key = "or"
case f.IsDir() && f.Mode()&os.ModeSticky != 0 && f.Mode()&0002 != 0:
Expand All @@ -313,27 +323,31 @@ func (sm styleMap) get(f *file) tcell.Style {
key = "ex"
}

if val, ok := sm[key]; ok {
if f.linkState == working && !sm.useLinkTarget {
key = "ln"
}
joelim-work marked this conversation as resolved.
Show resolved Hide resolved

if val, ok := sm.styles[key]; ok {
return val
}

if val, ok := sm[f.Name()+"*"]; ok {
if val, ok := sm.styles[f.Name()+"*"]; ok {
return val
}

if val, ok := sm["*"+f.Name()]; ok {
if val, ok := sm.styles["*"+f.Name()]; ok {
return val
}

if val, ok := sm[filepath.Base(f.Name())+".*"]; ok {
if val, ok := sm.styles[filepath.Base(f.Name())+".*"]; ok {
return val
}

if val, ok := sm["*"+strings.ToLower(f.ext)]; ok {
if val, ok := sm.styles["*"+strings.ToLower(f.ext)]; ok {
return val
}

if val, ok := sm["fi"]; ok {
if val, ok := sm.styles["fi"]; ok {
return val
}

Expand Down
2 changes: 2 additions & 0 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,7 @@ You may instead divide it into multiple lines in between double quotes by escapi
ex=01;32:\
"

The `ln` entry supports the special value `target`, which will use the link target to select a style. (this mirrors GNU's ls and dircolors behavior)
joelim-work marked this conversation as resolved.
Show resolved Hide resolved
Having such a long variable definition in a shell configuration file might be undesirable.
You may instead use the colors file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)) for configuration.
A sample colors file can be found at
Expand All @@ -1803,6 +1804,7 @@ Icons are configured using `LF_ICONS` environment variable or an icons file (ref
The variable uses the same syntax as `LS_COLORS/LF_COLORS`.
Instead of colors, you should put a single characters as values of entries.
The icons file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)) should consist of whitespace-separated pairs with a `#` character to start comments until the end of the line.
The `ln` entry supports the special value `target`, which will use the link target to select an icon. (this mirrors GNU's ls and dircolors behavior)
Do not forget to add `set icons true` to your `lfrc` to see the icons.
Default values are as follows given with their matching order in lf:

Expand Down
10 changes: 7 additions & 3 deletions doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,8 @@ escaping newlines with backslashes as follows:
ex=01;32:\
"

The ln entry supports the special value target, which will use the link
target to select a style. (this mirrors GNU's ls and dircolors behavior)
Having such a long variable definition in a shell configuration file
might be undesirable. You may instead use the colors file (refer to the
CONFIGURATION section) for configuration. A sample colors file can be
Expand All @@ -2040,9 +2042,11 @@ file (refer to the CONFIGURATION section). The variable uses the same
syntax as LS_COLORS/LF_COLORS. Instead of colors, you should put a
single characters as values of entries. The icons file (refer to the
CONFIGURATION section) should consist of whitespace-separated pairs with
a # character to start comments until the end of the line. Do not forget
to add set icons true to your lfrc to see the icons. Default values are
as follows given with their matching order in lf:
a # character to start comments until the end of the line. The ln entry
supports the special value target, which will use the link target to
select an icon. (this mirrors GNU's ls and dircolors behavior) Do not
forget to add set icons true to your lfrc to see the icons. Default
values are as follows given with their matching order in lf:

ln l
or l
Expand Down
67 changes: 38 additions & 29 deletions icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import (
"strings"
)

type iconMap map[string]string
type iconMap struct {
icons map[string]string
useLinkTarget bool
}

func parseIcons() iconMap {
im := make(iconMap)
im := iconMap{
icons: make(map[string]string),
useLinkTarget: false,
}

defaultIcons := []string{
"ln=l",
Expand Down Expand Up @@ -44,7 +50,7 @@ func parseIcons() iconMap {
return im
}

func (im iconMap) parseFile(path string) {
func (im *iconMap) parseFile(path string) {
log.Printf("reading file: %s", path)

f, err := os.Open(path)
Expand All @@ -61,19 +67,11 @@ func (im iconMap) parseFile(path string) {
}

for _, pair := range pairs {
key, val := pair[0], pair[1]

key = replaceTilde(key)

if filepath.IsAbs(key) {
key = filepath.Clean(key)
}

im[key] = val
im.parsePair(pair)
}
}

func (im iconMap) parseEnv(env string) {
func (im *iconMap) parseEnv(env string) {
for _, entry := range strings.Split(env, ":") {
if entry == "" {
continue
Expand All @@ -86,34 +84,41 @@ func (im iconMap) parseEnv(env string) {
return
}

key, val := pair[0], pair[1]
im.parsePair(pair)
}
}

func (im *iconMap) parsePair(pair []string) {
key, val := pair[0], pair[1]

key = replaceTilde(key)
key = replaceTilde(key)

if filepath.IsAbs(key) {
key = filepath.Clean(key)
}
if filepath.IsAbs(key) {
key = filepath.Clean(key)
}

im[key] = val
if key == "ln" && val == "target" {
im.useLinkTarget = true
log.Printf("using link target for icons")
}

im.icons[key] = val
}

func (im iconMap) get(f *file) string {
if val, ok := im[f.path]; ok {
if val, ok := im.icons[f.path]; ok {
return val
}

if f.IsDir() {
if val, ok := im[f.Name()+"/"]; ok {
if val, ok := im.icons[f.Name()+"/"]; ok {
return val
}
}

var key string

switch {
case f.linkState == working:
key = "ln"
case f.linkState == broken:
key = "or"
case f.IsDir() && f.Mode()&os.ModeSticky != 0 && f.Mode()&0002 != 0:
Expand All @@ -140,27 +145,31 @@ func (im iconMap) get(f *file) string {
key = "ex"
}

if val, ok := im[key]; ok {
if f.linkState == working && !im.useLinkTarget {
key = "ln"
}

if val, ok := im.icons[key]; ok {
return val
}

if val, ok := im[f.Name()+"*"]; ok {
if val, ok := im.icons[f.Name()+"*"]; ok {
return val
}

if val, ok := im["*"+f.Name()]; ok {
if val, ok := im.icons["*"+f.Name()]; ok {
return val
}

if val, ok := im[filepath.Base(f.Name())+".*"]; ok {
if val, ok := im.icons[filepath.Base(f.Name())+".*"]; ok {
return val
}

if val, ok := im["*"+strings.ToLower(f.ext)]; ok {
if val, ok := im.icons["*"+strings.ToLower(f.ext)]; ok {
return val
}

if val, ok := im["fi"]; ok {
if val, ok := im.icons["fi"]; ok {
return val
}

Expand Down
14 changes: 9 additions & 5 deletions lf.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Automatically generated by Pandoc 2.11.4
.\"
.TH "LF" "1" "2024-02-02" "" "DOCUMENTATION"
.TH "LF" "1" "2024-03-17" "r31-50-g33d880c" "DOCUMENTATION"
.hy
.SH NAME
.PP
Expand Down Expand Up @@ -2363,8 +2363,10 @@ ex=01;32:\[rs]
\f[R]
.fi
.PP
Having such a long variable definition in a shell configuration file
might be undesirable.
The \f[C]ln\f[R] entry supports the special value \f[C]target\f[R],
which will use the link target to select a style.
(this mirrors GNU\[aq]s ls and dircolors behavior) Having such a long
variable definition in a shell configuration file might be undesirable.
You may instead use the colors file (refer to the CONFIGURATION
section (https://github.com/gokcehan/lf/blob/master/doc.md#configuration))
for configuration.
Expand All @@ -2384,8 +2386,10 @@ The icons file (refer to the CONFIGURATION
section (https://github.com/gokcehan/lf/blob/master/doc.md#configuration))
should consist of whitespace-separated pairs with a \f[C]#\f[R]
character to start comments until the end of the line.
Do not forget to add \f[C]set icons true\f[R] to your \f[C]lfrc\f[R] to
see the icons.
The \f[C]ln\f[R] entry supports the special value \f[C]target\f[R],
which will use the link target to select an icon.
(this mirrors GNU\[aq]s ls and dircolors behavior) Do not forget to add
\f[C]set icons true\f[R] to your \f[C]lfrc\f[R] to see the icons.
Default values are as follows given with their matching order in lf:
.IP
.nf
Expand Down
Loading