Repository: gokcehan/lf
Pull request #291 (2020-01-25 19:24:30)

Add possibility to execute commands when directory changes (on_chdir)
When directory changes, and we have a cmd with the name on_chdir in our lfrc, it will run.
I made this because i didnt find an easy way set the git branch in the prompt, but it should be useful for other use cases as well.

![lf_on_chdir](https://user-images.githubusercontent.com/45574139/73126197-f3c7b680-3faf-11ea-80f6-cff8fa8d704f.png)


gokcehan: @ath3 Thanks for working on this.

I think `select` command is missing in the patch which also changes directory.

Also we currently don't use snake_case anywhere. Can you change `on_chdir` in the Go code to `onChdir` and the name of special command from `on_chdir` to `on-chdir` or maybe even better to `on-cd` instead?

We should also have this special command mentioned in the documentation somewhere. I will try think of a place to put this information.

Patch:
diff --git a/.travis.yml b/.travis.yml
index 93b0b25..6639f76 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,6 @@
 language: go
 install:
-  - go get github.com/nsf/termbox-go
+  - go get github.com/doronbehar/termbox-go
   - go get gopkg.in/djherbis/times.v1
 before_deploy:
   - gen/xbuild.sh
diff --git a/client.go b/client.go
index 6d19ba7..1c580bb 100644
--- a/client.go
+++ b/client.go
@@ -10,7 +10,7 @@ import (
 	"strings"
 	"time"
 
-	"github.com/nsf/termbox-go"
+	"github.com/doronbehar/termbox-go"
 )
 
 func run() {
diff --git a/colors.go b/colors.go
index a976831..9d3ac83 100644
--- a/colors.go
+++ b/colors.go
@@ -7,7 +7,7 @@ import (
 	"strconv"
 	"strings"
 
-	"github.com/nsf/termbox-go"
+	"github.com/doronbehar/termbox-go"
 )
 
 const gAnsiColorResetMask = termbox.AttrBold | termbox.AttrUnderline | termbox.AttrReverse
diff --git a/eval.go b/eval.go
index 2e78c52..0ef9604 100644
--- a/eval.go
+++ b/eval.go
@@ -330,6 +330,12 @@ func (e *cmdExpr) eval(app *app, args []string) {
 	app.ui.loadFileInfo(app.nav)
 }
 
+func onChdir(app *app) {
+	if cmd, ok := gOpts.cmds["on-cd"]; ok {
+		cmd.eval(app, nil)
+	}
+}
+
 func splitKeys(s string) (keys []string) {
 	for i := 0; i < len(s); {
 		r, w := utf8.DecodeRuneInString(s[i:])
@@ -535,6 +541,7 @@ func insert(app *app, arg string) {
 
 		if wd != path {
 			app.nav.marks["'"] = wd
+			onChdir(app)
 		}
 	case app.ui.cmdPrefix == "mark-remove: ":
 		normal(app)
@@ -610,6 +617,7 @@ func (e *callExpr) eval(app *app, args []string) {
 		}
 		app.ui.loadFile(app.nav)
 		app.ui.loadFileInfo(app.nav)
+		onChdir(app)
 	case "open":
 		if app.ui.cmdPrefix != "" && app.ui.cmdPrefix != ">" {
 			normal(app)
@@ -628,6 +636,7 @@ func (e *callExpr) eval(app *app, args []string) {
 			}
 			app.ui.loadFile(app.nav)
 			app.ui.loadFileInfo(app.nav)
+			onChdir(app)
 			return
 		}
 
@@ -903,6 +912,7 @@ func (e *callExpr) eval(app *app, args []string) {
 
 		if wd != path {
 			app.nav.marks["'"] = wd
+			onChdir(app)
 		}
 	case "select":
 		if len(e.args) != 1 {
@@ -932,6 +942,7 @@ func (e *callExpr) eval(app *app, args []string) {
 
 		if wd != path {
 			app.nav.marks["'"] = wd
+			onChdir(app)
 		}
 	case "glob-select":
 		if len(e.args) != 1 {
diff --git a/go.mod b/go.mod
index 7b68baf..3508d63 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/gokcehan/lf
 go 1.12
 
 require (
+	github.com/doronbehar/termbox-go v0.0.0-20191006141301-8c9470559e05
 	github.com/mattn/go-runewidth v0.0.4
-	github.com/nsf/termbox-go v0.0.0-20190325093121-288510b9734e
 	gopkg.in/djherbis/times.v1 v1.2.0
 )
diff --git a/go.sum b/go.sum
index e8f606b..c7cbdf9 100644
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,10 @@
+github.com/doronbehar/termbox-go v0.0.0-20190817171036-93860e161317 h1:mdOm+1RdYwVwC4Wjb53M5L96fRwWJ8gI7YXwwiCsHTk=
+github.com/doronbehar/termbox-go v0.0.0-20190817171036-93860e161317/go.mod h1:cHYbFQiZNC8LPG6clYoLMKAl0xx7KR75RPsHtG/f1Hk=
+github.com/doronbehar/termbox-go v0.0.0-20191006140624-aa2cdd26fb8c h1:qvLYQAhDCaMq+XiQ+t4YLaTJtUyHC1r20odd6ryKYIk=
+github.com/doronbehar/termbox-go v0.0.0-20191006140624-aa2cdd26fb8c/go.mod h1:cHYbFQiZNC8LPG6clYoLMKAl0xx7KR75RPsHtG/f1Hk=
+github.com/doronbehar/termbox-go v0.0.0-20191006141301-8c9470559e05 h1:pmXx7j8+mmkFJB2+qoB1C/Mz8SWvMlHTxSWOptJrr7E=
+github.com/doronbehar/termbox-go v0.0.0-20191006141301-8c9470559e05/go.mod h1:cHYbFQiZNC8LPG6clYoLMKAl0xx7KR75RPsHtG/f1Hk=
 github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
 github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
-github.com/nsf/termbox-go v0.0.0-20190325093121-288510b9734e h1:Vbib8wJAaMEF9jusI/kMSYMr/LtRzM7+F9MJgt/nH8k=
-github.com/nsf/termbox-go v0.0.0-20190325093121-288510b9734e/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
 gopkg.in/djherbis/times.v1 v1.2.0 h1:UCvDKl1L/fmBygl2Y7hubXCnY7t4Yj46ZrBFNUipFbM=
 gopkg.in/djherbis/times.v1 v1.2.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8=
diff --git a/ui.go b/ui.go
index c3c38bf..c68222a 100644
--- a/ui.go
+++ b/ui.go
@@ -16,7 +16,7 @@ import (
 	"unicode/utf8"
 
 	"github.com/mattn/go-runewidth"
-	"github.com/nsf/termbox-go"
+	"github.com/doronbehar/termbox-go"
 )
 
 const gEscapeCode = 27
@@ -870,27 +870,6 @@ func (ui *ui) readEvent(ch chan<- expr, ev termbox.Event) {
 			ui.keyAcc = nil
 			ui.keyCount = nil
 			ui.menuBuf = nil
-		case 1:
-			if ok {
-				if len(ui.keyCount) > 0 {
-					c, err := strconv.Atoi(string(ui.keyCount))
-					if err != nil {
-						log.Printf("converting command count: %s", err)
-					}
-					count = c
-				}
-				expr := gOpts.keys[string(ui.keyAcc)]
-				if e, ok := expr.(*callExpr); ok {
-					e.count = count
-				}
-				ch <- expr
-				ui.keyAcc = nil
-				ui.keyCount = nil
-				ui.menuBuf = nil
-			} else {
-				ui.menuBuf = listBinds(binds)
-				ch <- draw
-			}
 		default:
 			if ok {
 				if len(ui.keyCount) > 0 {


Test patch:
diff --git a/colors_test.go b/colors_test.go
index ff93b48..2d4b868 100644
--- a/colors_test.go
+++ b/colors_test.go
@@ -3,7 +3,7 @@ package main
 import (
 	"testing"
 
-	"github.com/nsf/termbox-go"
+	"github.com/doronbehar/termbox-go"
 )
 
 func TestApplyAnsiCodes(t *testing.T) {