Skip to content

Commit

Permalink
Use symlink target for stat styling & icons (#566) (#1644)
Browse files Browse the repository at this point in the history
* use symlink target for stat styling & icons (#566)

Support LS_COLORS/dircolors use of "target" for symlinks to select icons
and styles for them according to the target stat. This mainly applies to
directories - file name-based styles will still be based on the name of
the link, not its target. This mirrors `ls` behavior.

* remove unnecessary debugging and factor out parsePair in color parsing

* move conditional to switch block

* clarify ln target behavior in docs

* run gofmt on single file

---------

Co-authored-by: Gökçehan Kara <[email protected]>
  • Loading branch information
lorentzforces and gokcehan authored Apr 7, 2024
1 parent 21d256d commit 5b10803
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 59 deletions.
62 changes: 34 additions & 28 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 @@ -191,20 +197,12 @@ func (sm styleMap) parseFile(path string) {
}

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

key = replaceTilde(key)

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

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

// 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 @@ -217,16 +215,24 @@ func (sm styleMap) parseGNU(env string) {
return
}

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

key = replaceTilde(key)
func (sm *styleMap) parsePair(pair []string) {
key, val := pair[0], pair[1]

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

sm[key] = applyAnsiCodes(val, tcell.StyleDefault)
if filepath.IsAbs(key) {
key = filepath.Clean(key)
}

if key == "ln" && val == "target" {
sm.useLinkTarget = true
}

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

// This function parses $LSCOLORS environment variable.
Expand Down Expand Up @@ -267,25 +273,25 @@ 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:
case f.linkState == working && !sm.useLinkTarget:
key = "ln"
case f.linkState == broken:
key = "or"
Expand Down Expand Up @@ -313,27 +319,27 @@ func (sm styleMap) get(f *file) tcell.Style {
key = "ex"
}

if val, ok := sm[key]; ok {
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. File name rules will still apply based on the link's name -- 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](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)) for configuration.
A sample colors file can be found at
Expand All @@ -1802,6 +1803,7 @@ https://en.wikipedia.org/wiki/ANSI_escape_code
Icons are configured using `LF_ICONS` environment variable or an icons file (refer to the [CONFIGURATION section](https://github.com/gokcehan/lf/blob/master/doc.md#configuration)).
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 `ln` entry supports the special value `target`, which will use the link target to select a icon. File name rules will still apply based on the link's name -- this mirrors GNU's `ls` and `dircolors` behavior.
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.
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
12 changes: 9 additions & 3 deletions doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2026,8 +2026,11 @@ escaping newlines with backslashes as follows:
ex=01;32:\
"

Having such a long variable definition in a shell configuration file
might be undesirable. You may instead use the colors file (refer to the
The ln entry supports the special value target, which will use the link
target to select a style. File name rules will still apply based on the
link's name -- 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
found at https://github.com/gokcehan/lf/blob/master/etc/colors.example
You may also see the wiki page for ANSI escape codes
Expand All @@ -2038,7 +2041,10 @@ ICONS
Icons are configured using LF_ICONS environment variable or an icons
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
single characters as values of entries. The ln entry supports the
special value target, which will use the link target to select a icon.
File name rules will still apply based on the link's name -- this
mirrors GNU's ls and dircolors behavior. 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
Expand Down
62 changes: 34 additions & 28 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,33 +84,41 @@ func (im iconMap) parseEnv(env string) {
return
}

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

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

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

im[key] = val
if filepath.IsAbs(key) {
key = filepath.Clean(key)
}

if key == "ln" && val == "target" {
im.useLinkTarget = true
}

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:
case f.linkState == working && !im.useLinkTarget:
key = "ln"
case f.linkState == broken:
key = "or"
Expand Down Expand Up @@ -140,27 +146,27 @@ func (im iconMap) get(f *file) string {
key = "ex"
}

if val, ok := im[key]; ok {
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
8 changes: 8 additions & 0 deletions lf.1
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,10 @@ ex=01;32:\[rs]
\f[R]
.fi
.PP
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.
File name rules will still apply based on the link\[aq]s name -- this
mirrors GNU\[aq]s \f[C]ls\f[R] and \f[C]dircolors\f[R] 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
Expand All @@ -2380,6 +2384,10 @@ section (https://github.com/gokcehan/lf/blob/master/doc.md#configuration)).
The variable uses the same syntax as \f[C]LS_COLORS/LF_COLORS\f[R].
Instead of colors, you should put a single characters as values of
entries.
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 icon.
File name rules will still apply based on the link\[aq]s name -- this
mirrors GNU\[aq]s \f[C]ls\f[R] and \f[C]dircolors\f[R] behavior.
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]
Expand Down

0 comments on commit 5b10803

Please sign in to comment.