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 17, 2022
1 parent 624992a commit 28429a9
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 9 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
15 changes: 15 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 @@ -188,6 +189,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 +391,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 @@ -922,6 +929,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
20 changes: 20 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.hasFocus = e.val == "on"
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
21 changes: 21 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 @@ -211,6 +212,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 +467,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 @@ -1134,6 +1143,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
hasFocus bool
}

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.hasFocus = true

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

Expand Down
33 changes: 24 additions & 9 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir
}
}

if i == dir.pos {
if i == dir.pos && gOpts.hasFocus {
st = st.Reverse(true)
if dirStyle.previewing {
st = st.Foreground(DimCursorColor)
Expand Down Expand Up @@ -490,8 +490,8 @@ func (win *win) printDir(screen tcell.Screen, dir *dir, context *dirContext, dir

tag, ok := context.tags[path]
if ok {
if i == dir.pos {
win.print(screen, lnwidth+1, i, st, tag)
if i == dir.pos && gOpts.hasFocus {
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 @@ -1186,17 +1186,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 @@ -1214,7 +1229,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 28429a9

Please sign in to comment.