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

Support icon color #1674

Merged
merged 5 commits into from
Apr 20, 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
41 changes: 33 additions & 8 deletions icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ import (
"os"
"path/filepath"
"strings"

"github.com/gdamore/tcell/v2"
)

type iconMap map[string]string
type iconDef struct {
icon string
hasStyle bool
style tcell.Style
}

type iconMap map[string]iconDef

func iconWithoutStyle(icon string) iconDef {
return iconDef{icon, false, tcell.StyleDefault}
}

func iconWithStyle(icon string, style tcell.Style) iconDef {
return iconDef{icon, true, style}
}

func parseIcons() iconMap {
im := make(iconMap)
Expand Down Expand Up @@ -54,22 +70,31 @@ func (im iconMap) parseFile(path string) {
}
defer f.Close()

pairs, err := readPairs(f)
arrs, err := readArrays(f, 1, 3)
if err != nil {
log.Printf("reading icons file: %s", err)
return
}

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

key = replaceTilde(key)

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

im[key] = val
switch len(arr) {
case 1:
delete(im, key)
case 2:
icon := arr[1]
im[key] = iconWithoutStyle(icon)
case 3:
icon, color := arr[1], arr[2]
im[key] = iconWithStyle(icon, applyAnsiCodes(color, tcell.StyleDefault))
}
}
}

Expand All @@ -94,11 +119,11 @@ func (im iconMap) parseEnv(env string) {
key = filepath.Clean(key)
}

im[key] = val
im[key] = iconWithoutStyle(val)
}
}

func (im iconMap) get(f *file) string {
func (im iconMap) get(f *file) iconDef {
if val, ok := im[f.path]; ok {
return val
}
Expand Down Expand Up @@ -164,5 +189,5 @@ func (im iconMap) get(f *file) string {
return val
}

return " "
return iconWithoutStyle(" ")
}
25 changes: 16 additions & 9 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func splitWord(s string) (word, rest string) {
// or double quotes can be used to escape whitespaces. Hash characters can be
// used to add a comment until the end of line. Leading and trailing space is
// trimmed. Empty lines are skipped.
func readPairs(r io.Reader) ([][]string, error) {
func readArrays(r io.Reader, min_cols, max_cols int) ([][]string, error) {
var pairs [][]string
s := bufio.NewScanner(r)
for s.Scan() {
Expand All @@ -181,7 +181,7 @@ func readPairs(r io.Reader) ([][]string, error) {
}

squote, dquote = false, false
pair := strings.FieldsFunc(line, func(r rune) bool {
arr := strings.FieldsFunc(line, func(r rune) bool {
if r == '\'' && !dquote {
squote = !squote
} else if r == '"' && !squote {
Expand All @@ -190,14 +190,17 @@ func readPairs(r io.Reader) ([][]string, error) {
return !squote && !dquote && unicode.IsSpace(r)
})

if len(pair) != 2 {
return nil, fmt.Errorf("expected pair but found: %s", s.Text())
if len(arr) < min_cols || len(arr) > max_cols {
if min_cols == max_cols {
return nil, fmt.Errorf("expected %d columns but found: %s", min_cols, s.Text())
}
return nil, fmt.Errorf("expected %d~%d columns but found: %s", min_cols, max_cols, s.Text())
}

for i := 0; i < len(pair); i++ {
for i := 0; i < len(arr); i++ {
squote, dquote = false, false
buf := make([]rune, 0, len(pair[i]))
for _, r := range pair[i] {
buf := make([]rune, 0, len(arr[i]))
for _, r := range arr[i] {
if r == '\'' && !dquote {
squote = !squote
continue
Expand All @@ -208,15 +211,19 @@ func readPairs(r io.Reader) ([][]string, error) {
}
buf = append(buf, r)
}
pair[i] = string(buf)
arr[i] = string(buf)
}

pairs = append(pairs, pair)
pairs = append(pairs, arr)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might want to rename this variable:

Suggested change
pairs = append(pairs, arr)
arrs = append(arrs, arr)

}

return pairs, nil
}

func readPairs(r io.Reader) ([][]string, error) {
return readArrays(r, 2, 2)
}

// This function converts a size in bytes to a human readable form using metric
// suffixes (e.g. 1K = 1000). For values less than 10 the first significant
// digit is shown, otherwise it is hidden. Numbers are always rounded down.
Expand Down
9 changes: 8 additions & 1 deletion ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,11 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
// leave space for displaying the tag
s = append(s, ' ')

var icon iconDef

if gOpts.icons {
s = append(s, []rune(dirStyle.icons.get(f))...)
icon = dirStyle.icons.get(f)
s = append(s, []rune(icon.icon)...)
s = append(s, ' ')
}

Expand Down Expand Up @@ -497,6 +500,10 @@ func (win *win) printDir(ui *ui, dir *dir, context *dirContext, dirStyle *dirSty
styledFilename := fmt.Sprintf(cursorescapefmt, string(s))
win.print(ui.screen, lnwidth+1, i, st, styledFilename)

if icon.hasStyle && i != dir.pos {
win.print(ui.screen, lnwidth+2, i, icon.style, icon.icon)
}

tag, ok := context.tags[path]
if ok {
if i == dir.pos {
Expand Down