From 04d40832f128a7889528498a036b039a62493cbb Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sun, 16 Oct 2022 08:51:26 -0700 Subject: [PATCH] For directories in preview column, dim cursor (#924) * For directories in preview column, dim cursor When preview is on and consists of a directory listing, make the cursor in there dimmer to emphasize it's not active. This makes it visually obvious which cursor is active at all times, without having to read the path. The active cursor is now always the rightmost bright one, regardless of whether the `preview` option is on or off. See screenshots in the pull request at https://github.com/gokcehan/lf/pull/924. Before this, I noticed that I often open a file by accident because I got confused which of the three cursors on the screen is the active one. * No-op changes to ui.go Reduce number of arguments to printDir and list non-configurable colors --- qq | 0 ui.go | 124 +++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 qq diff --git a/qq b/qq new file mode 100644 index 00000000..e69de29b diff --git a/ui.go b/ui.go index eef09916..04c35f69 100644 --- a/ui.go +++ b/ui.go @@ -324,24 +324,45 @@ func fileInfo(f *file, d *dir) string { return info } -func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]int, saves map[string]bool, tags map[string]string, colors styleMap, icons iconMap, previewAllowed bool) { +type dirContext struct { + selections map[string]int + saves map[string]bool + tags map[string]string +} +type dirStyle struct { + colors styleMap + icons iconMap + previewing bool +} + +// These colors are not currently customizeable +const LineNumberColor = tcell.ColorOlive +const SelectionColor = tcell.ColorPurple +const YankColor = tcell.ColorOlive +const CutColor = tcell.ColorMaroon +const DimCursorColor = tcell.ColorGrey + +func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dirStyle *dirStyle) { if win.w < 5 || dir == nil { return } + messageStyle := tcell.StyleDefault.Reverse(true) + if dirStyle.previewing { + messageStyle = messageStyle.Foreground(DimCursorColor) + } + if dir.noPerm { - win.print(screen, 2, 0, tcell.StyleDefault.Reverse(true), "permission denied") + win.print(screen, 2, 0, messageStyle, "permission denied") + return + } + if (dir.loading && len(dir.files) == 0) || (dirStyle.previewing && dir.loading && gOpts.dirpreviews) { + win.print(screen, 2, 0, messageStyle, "loading...") return } - if (dir.loading && len(dir.files) == 0) || (previewAllowed && dir.loading && gOpts.dirpreviews) { - win.print(screen, 2, 0, tcell.StyleDefault.Reverse(true), "loading...") - return - } - - if previewAllowed && gOpts.dirpreviews && len(gOpts.previewer) > 0 { - + if dirStyle.previewing && gOpts.dirpreviews && len(gOpts.previewer) > 0 { // Print previewer result instead of default directory print operation. st := tcell.StyleDefault for i, l := range dir.lines { @@ -353,9 +374,8 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in } return } - if len(dir.files) == 0 { - win.print(screen, 2, 0, tcell.StyleDefault.Reverse(true), "empty") + win.print(screen, 2, 0, messageStyle, "empty") return } @@ -381,7 +401,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in } for i, f := range dir.files[beg:end] { - st := colors.get(f) + st := dirStyle.colors.get(f) if lnwidth > 0 { var ln string @@ -401,23 +421,26 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in } } - win.print(screen, 0, i, tcell.StyleDefault.Foreground(tcell.ColorOlive), ln) + win.print(screen, 0, i, tcell.StyleDefault.Foreground(LineNumberColor), ln) } path := filepath.Join(dir.path, f.Name()) - if _, ok := selections[path]; ok { - win.print(screen, lnwidth, i, st.Background(tcell.ColorPurple), " ") - } else if cp, ok := saves[path]; ok { + if _, ok := context.selections[path]; ok { + win.print(screen, lnwidth, i, st.Background(SelectionColor), " ") + } else if cp, ok := context.saves[path]; ok { if cp { - win.print(screen, lnwidth, i, st.Background(tcell.ColorOlive), " ") + win.print(screen, lnwidth, i, st.Background(YankColor), " ") } else { - win.print(screen, lnwidth, i, st.Background(tcell.ColorMaroon), " ") + win.print(screen, lnwidth, i, st.Background(CutColor), " ") } } if i == dir.pos { st = st.Reverse(true) + if dirStyle.previewing { + st = st.Foreground(DimCursorColor) + } } var s []rune @@ -427,7 +450,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in var iwidth int if gOpts.icons { - s = append(s, []rune(icons.get(f))...) + s = append(s, []rune(dirStyle.icons.get(f))...) s = append(s, ' ') iwidth = 2 } @@ -465,7 +488,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in win.print(screen, lnwidth+1, i, st, string(s)) - tag, ok := tags[path] + tag, ok := context.tags[path] if ok { if i == dir.pos { win.print(screen, lnwidth+1, i, st, tag) @@ -476,33 +499,6 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in } } -type ui struct { - screen tcell.Screen - polling bool - wins []*win - promptWin *win - msgWin *win - menuWin *win - msg string - regPrev *reg - dirPrev *dir - exprChan chan expr - keyChan chan string - tevChan chan tcell.Event - evChan chan tcell.Event - menuBuf *bytes.Buffer - cmdPrefix string - cmdAccLeft []rune - cmdAccRight []rune - cmdYankBuf []rune - cmdTmp []rune - keyAcc []rune - keyCount []rune - styles styleMap - icons iconMap - currentFile string -} - func getWidths(wtot int) []int { rsum := 0 for _, r := range gOpts.ratios { @@ -547,6 +543,33 @@ func getWins(screen tcell.Screen) []*win { return wins } +type ui struct { + screen tcell.Screen + polling bool + wins []*win + promptWin *win + msgWin *win + menuWin *win + msg string + regPrev *reg + dirPrev *dir + exprChan chan expr + keyChan chan string + tevChan chan tcell.Event + evChan chan tcell.Event + menuBuf *bytes.Buffer + cmdPrefix string + cmdAccLeft []rune + cmdAccRight []rune + cmdYankBuf []rune + cmdTmp []rune + keyAcc []rune + keyCount []rune + styles styleMap + icons iconMap + currentFile string +} + func newUI(screen tcell.Screen) *ui { wtot, htot := screen.Size() @@ -853,6 +876,7 @@ func (ui *ui) drawBox() { func (ui *ui) draw(nav *nav) { st := tcell.StyleDefault + context := dirContext{selections: nav.selections, saves: nav.saves, tags: nav.tags} wtot, htot := ui.screen.Size() for i := 0; i < wtot; i++ { @@ -873,7 +897,8 @@ func (ui *ui) draw(nav *nav) { doff := len(nav.dirs) - length for i := 0; i < length; i++ { - ui.wins[woff+i].printDir(ui.screen, nav.dirs[doff+i], nav.selections, nav.saves, nav.tags, ui.styles, ui.icons, false) + ui.wins[woff+i].printDir(ui.screen, nav.dirs[doff+i], &context, + &dirStyle{colors: ui.styles, icons: ui.icons, previewing: false}) } switch ui.cmdPrefix { @@ -907,7 +932,8 @@ func (ui *ui) draw(nav *nav) { preview := ui.wins[len(ui.wins)-1] if curr.IsDir() { - preview.printDir(ui.screen, ui.dirPrev, nav.selections, nav.saves, nav.tags, ui.styles, ui.icons, true) + preview.printDir(ui.screen, ui.dirPrev, &context, + &dirStyle{colors: ui.styles, icons: ui.icons, previewing: true}) } else if curr.Mode().IsRegular() { preview.printReg(ui.screen, ui.regPrev) }