Skip to content

Commit

Permalink
Enable focus events through configuration by using focus (option), on…
Browse files Browse the repository at this point in the history
…-ui-enter/on-ui-exit (events) and echoraw (command).
  • Loading branch information
laktak committed Oct 15, 2022
1 parent 654b873 commit 48b2cbb
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ func (app *app) loop() {
}()
}

onUiEnter(app)

for {
select {
case <-app.quitChan:
Expand All @@ -321,6 +323,7 @@ func (app *app) loop() {
cmd.eval(app, nil)
}

onUiExit(app)
app.quit()

app.nav.previewChan <- ""
Expand Down Expand Up @@ -466,6 +469,7 @@ func (app *app) loop() {
app.ui.draw(app.nav)
}
}

}

// This function is used to run a shell command. Modes are as follows:
Expand All @@ -492,6 +496,7 @@ func (app *app) runShell(s string, args []string, prefix string) {
app.nav.previewChan <- ""
app.nav.dirPreviewChan <- nil

onUiExit(app)
if err := app.ui.suspend(); err != nil {
log.Printf("suspend: %s", err)
}
Expand All @@ -501,6 +506,7 @@ func (app *app) runShell(s string, args []string, prefix string) {
os.Exit(3)
return
}
onUiEnter(app)
}()
defer app.nav.renew()

Expand Down
20 changes: 20 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The following commands are provided by lf:
echo
echomsg
echoerr
echoraw
cd
select
delete (modal)
Expand Down Expand Up @@ -123,6 +124,7 @@ The following options can be used to customize the behavior of lf:
errorfmt string (default "\033[7;31;47m%s\033[0m")
filesep string (default "\n")
findlen int (default 1)
focus string (default "on")
globsearch bool (default off)
hidden bool (default off)
hiddenfiles []string (default '.*')
Expand Down Expand Up @@ -188,6 +190,8 @@ The following special shell commands are used to customize the behavior of lf wh
pre-cd
on-cd
on-select
on-ui-enter
on-ui-exit
on-quit
The following commands/keybindings are provided by default:
Expand Down Expand Up @@ -388,6 +392,10 @@ Print given arguments to the message line at the bottom and also to the log file
Print given arguments to the message line at the bottom as 'errorfmt' and also to the log file.
echoraw
Print string directly to the terminal. This should only be used to send escape sequences to your terminal program or tmux.
cd
Change the working directory to the given argument.
Expand Down Expand Up @@ -625,6 +633,10 @@ File separator used in environment variables 'fs' and 'fx'.
Number of characters prompted for the find command.
When this value is set to 0, find command prompts until there is only a single match left.
focus string (default "on")
This can be used to customize the focus line in combination with a focus event (see wiki). Currently accepts "on" (inverse) and "off" (hidden).
globsearch bool (default off)
When this option is enabled, search command patterns are considered as globs, otherwise they are literals.
Expand Down Expand Up @@ -913,6 +925,14 @@ This shell command can be defined to be executed after changing a directory.
This shell command can be defined to be executed after the selection changes.
on-ui-enter
This shell command can be defined to be executed when the UI of lf becomes active. This will be triggered after lf starts and when foreground shell commands finish.
on-ui-exit
This shell command can be defined to be executed when the UI of lf becomes inactive. This will be triggered before lf exits and before starting foreground shell commands.
on-quit
This shell command can be defined to be executed before quit.
Expand Down
26 changes: 26 additions & 0 deletions docstring.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions eval.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"
"log"
"os"
Expand Down Expand Up @@ -430,6 +431,10 @@ func (e *setExpr) eval(app *app, args []string) {
}
case "tagfmt":
gOpts.tagfmt = e.val
case "focus":
gOpts.focus = e.val
app.ui.loadFile(app, true)
app.ui.loadFileInfo(app.nav)
case "timefmt":
gOpts.timefmt = e.val
case "infotimefmtnew":
Expand Down Expand Up @@ -501,6 +506,18 @@ func onSelect(app *app) {
}
}

func onUiEnter(app *app) {
if cmd, ok := gOpts.cmds["on-ui-enter"]; ok {
cmd.eval(app, nil)
}
}

func onUiExit(app *app) {
if cmd, ok := gOpts.cmds["on-ui-exit"]; ok {
cmd.eval(app, nil)
}
}

func splitKeys(s string) (keys []string) {
for i := 0; i < len(s); {
r, w := utf8.DecodeRuneInString(s[i:])
Expand Down Expand Up @@ -1540,6 +1557,8 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.echomsg(strings.Join(e.args, " "))
case "echoerr":
app.ui.echoerr(strings.Join(e.args, " "))
case "echoraw":
fmt.Print(strings.Join(e.args, " "))
case "cd":
path := "~"
if len(e.args) > 0 {
Expand Down
28 changes: 28 additions & 0 deletions lf.1
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ The following commands are provided by lf:
echo
echomsg
echoerr
echoraw
cd
select
delete (modal)
Expand Down Expand Up @@ -142,6 +143,7 @@ The following options can be used to customize the behavior of lf:
errorfmt string (default "\e033[7;31;47m%s\e033[0m")
filesep string (default "\en")
findlen int (default 1)
focus string (default "on")
globsearch bool (default off)
hidden bool (default off)
hiddenfiles []string (default '.*')
Expand Down Expand Up @@ -211,6 +213,8 @@ The following special shell commands are used to customize the behavior of lf wh
pre-cd
on-cd
on-select
on-ui-enter
on-ui-exit
on-quit
.EE
.PP
Expand Down Expand Up @@ -464,6 +468,12 @@ Print given arguments to the message line at the bottom and also to the log file
.PP
Print given arguments to the message line at the bottom as 'errorfmt' and also to the log file.
.PP
.EX
echoraw
.EE
.PP
Print string directly to the terminal. This should only be used to send escape sequences to your terminal program or tmux.
.PP
.EX
cd
.EE
Expand Down Expand Up @@ -773,6 +783,12 @@ File separator used in environment variables 'fs' and 'fx'.
.PP
Number of characters prompted for the find command. When this value is set to 0, find command prompts until there is only a single match left.
.PP
.EX
focus string (default "on")
.EE
.PP
This can be used to customize the focus line in combination with a focus event (see wiki). Currently accepts "on" (inverse) and "off" (hidden).
.PP
.EX
globsearch bool (default off)
.EE
Expand Down Expand Up @@ -1125,6 +1141,18 @@ This shell command can be defined to be executed after changing a directory.
.PP
This shell command can be defined to be executed after the selection changes.
.PP
.EX
on-ui-enter
.EE
.PP
This shell command can be defined to be executed when the UI of lf becomes active. This will be triggered after lf starts and when foreground shell commands finish.
.PP
.EX
on-ui-exit
.EE
.PP
This shell command can be defined to be executed when the UI of lf becomes inactive. This will be triggered before lf exits and before starting foreground shell commands.
.PP
.EX
on-quit
.EE
Expand Down
2 changes: 2 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var gOpts struct {
sortType sortType
tempmarks string
tagfmt string
focus string
}

func init() {
Expand Down Expand Up @@ -129,6 +130,7 @@ func init() {
gOpts.sortType = sortType{naturalSort, dirfirstSort}
gOpts.tempmarks = "'"
gOpts.tagfmt = "\033[31m%s\033[0m"
gOpts.focus = "on"

gOpts.keys = make(map[string]expr)

Expand Down
31 changes: 23 additions & 8 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in
}
}

if i == dir.pos {
if i == dir.pos && gOpts.focus == "on" {
st = st.Reverse(true)
}

Expand Down Expand Up @@ -467,7 +467,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, selections map[string]in

tag, ok := tags[path]
if ok {
if i == dir.pos {
if i == dir.pos && gOpts.focus == "on" {
win.print(screen, lnwidth+1, i, st.Reverse(true), tag)
} else {
win.print(screen, lnwidth+1, i, tcell.StyleDefault, fmt.Sprintf(gOpts.tagfmt, tag))
Expand Down Expand Up @@ -1164,17 +1164,32 @@ func (ui *ui) readNormalEvent(ev tcell.Event) expr {
return nil
}

func readCmdEvent(ev tcell.Event) expr {
func (ui *ui) readCmdEvent(ev tcell.Event) expr {
switch tev := ev.(type) {
case *tcell.EventKey:
if tev.Key() == tcell.KeyRune {
if tev.Modifiers() == tcell.ModMask(tcell.ModAlt) {
val := string([]rune{'<', 'a', '-', tev.Rune(), '>'})
if expr, ok := gOpts.cmdkeys[val]; ok {
return expr
// support multi-key bindings but only for ESC[ at the moment
if tev.Rune() == '[' {
ui.keyAcc = append(ui.keyAcc, '<', 'a', '-', tev.Rune(), '>')
} else {
val := string([]rune{'<', 'a', '-', tev.Rune(), '>'})
if expr, ok := gOpts.cmdkeys[val]; ok {
return expr
}
}
} else {
return &callExpr{"cmd-insert", []string{string(tev.Rune())}, 1}
// multi-key bindings
if ui.keyAcc != nil {
ui.keyAcc = append(ui.keyAcc, tev.Rune())
val := string(ui.keyAcc)
ui.keyAcc = nil
if expr, ok := gOpts.cmdkeys[val]; ok {
return expr
}
} else {
return &callExpr{"cmd-insert", []string{string(tev.Rune())}, 1}
}
}
} else {
val := gKeyVal[tev.Key()]
Expand All @@ -1192,7 +1207,7 @@ func (ui *ui) readEvent(ev tcell.Event) expr {
}

if _, ok := ev.(*tcell.EventKey); ok && ui.cmdPrefix != "" {
return readCmdEvent(ev)
return ui.readCmdEvent(ev)
}

return ui.readNormalEvent(ev)
Expand Down

0 comments on commit 48b2cbb

Please sign in to comment.