Add query server command to expose internal state (#1384)

* Implement recv mechanism

* Implement recv commands

* Add mutex for state data structure

* Use string to store data instead of byte buffer

* Remove runPagerOn function

* Add history command

* Add documentation

* Add default `maps` command

* Add default `cmds` command

* Reduce locking time

* Remove empty line

* Fix documentation for `recv`

* Add comment about `recv` thread

* Change `recv` to `query`

* Fix comment

* test

* Add comment about connection lifetime

* Map `cmaps` by default
This commit is contained in:
Joe Lim authored and GitHub committed 2023-09-08 18:52:19 +10:00
1 parent c56e319211
commit 9068801831
11 files changed
+243 -118

No files matched your search

+8 -14
View File
@@ -500,6 +500,14 @@ func (app *app) runShell(s string, args []string, prefix string) {
app.ui.exportSizes()
exportOpts()
gState.mutex.Lock()
gState.data["maps"] = listBinds(gOpts.keys).String()
gState.data["cmaps"] = listBinds(gOpts.cmdkeys).String()
gState.data["cmds"] = listCmds().String()
gState.data["jumps"] = listJumps(app.nav.jumpList, app.nav.jumpListInd).String()
gState.data["history"] = listHistory(app.cmdHistory).String()
gState.mutex.Unlock()
cmd := shellCommand(s, args)
var out io.Reader
@@ -584,17 +592,3 @@ func (app *app) runShell(s string, args []string, prefix string) {
}
}
func (app *app) runPagerOn(stdin io.Reader) {
app.nav.exportFiles()
app.ui.exportSizes()
exportOpts()
cmd := shellCommand(envPager, nil)
cmd.Stdin = stdin
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
app.runCmdSync(cmd, false)
}
+30 -3
View File
@@ -8,11 +8,23 @@ import (
"net"
"os"
"strings"
"sync"
"time"
"github.com/gdamore/tcell/v2"
)
type State struct {
mutex sync.Mutex
data map[string]string
}
var gState State
func init() {
gState.data = make(map[string]string)
}
func run() {
var screen tcell.Screen
var err error
@@ -130,9 +142,24 @@ func readExpr() <-chan expr {
s := bufio.NewScanner(c)
for s.Scan() {
log.Printf("recv: %s", s.Text())
p := newParser(strings.NewReader(s.Text()))
if p.parse() {
ch <- p.expr
// `query` has to be handled outside of the main thread, which is
// blocked when running a synchronous shell command ("$" or "!").
// This is important since `query` is often the result of the user
// running `$lf -remote "query $id <something>"`.
if word, rest := splitWord(s.Text()); word == "query" {
gState.mutex.Lock()
state, ok := gState.data[rest]
gState.mutex.Unlock()
if ok {
fmt.Fprint(c, state)
}
fmt.Fprintln(c, "")
} else {
p := newParser(strings.NewReader(s.Text()))
if p.parse() {
ch <- p.expr
}
}
}
-4
View File
@@ -13,11 +13,8 @@ var (
"set",
"setlocal",
"map",
"maps",
"cmap",
"cmaps",
"cmd",
"cmds",
"quit",
"up",
"half-up",
@@ -31,7 +28,6 @@ var (
"open",
"jump-next",
"jump-prev",
"jumps",
"top",
"bottom",
"high",
+48 -27
View File
@@ -80,10 +80,6 @@ The following commands are provided by lf:
mark-remove (modal) (default '"')
tag
tag-toggle (default 't')
maps
cmaps
cmds
jumps
The following command line commands are provided by lf:
@@ -216,13 +212,27 @@ The following special shell commands are used to customize the behavior of lf wh
The following commands/keybindings are provided by default:
Unix Windows
cmd open &$OPENER "$f" cmd open &%OPENER% %f%
map e $$EDITOR "$f" map e $%EDITOR% %f%
map i $$PAGER "$f" map i !%PAGER% %f%
map w $$SHELL map w $%SHELL%
cmd doc $$lf -doc | $PAGER cmd doc !%lf% -doc | %PAGER%
map <f-1> doc map <f-1> doc
Unix
cmd open &$OPENER "$f"
map e $$EDITOR "$f"
map i $$PAGER "$f"
map w $$SHELL
cmd doc $$lf -doc | $PAGER
map <f-1> doc
cmd maps $lf -remote "query $id maps" | $PAGER
cmd cmaps $lf -remote "query $id cmaps" | $PAGER
cmd cmds $lf -remote "query $id cmds" | $PAGER
Windows
cmd open &%OPENER% %f%
map e $%EDITOR% %f%
map i !%PAGER% %f%
map w $%SHELL%
cmd doc !%lf% -doc | %PAGER%
map <f-1> doc
cmd maps !%lf% -remote "query %id% maps" | %PAGER%
cmd cmaps !%lf% -remote "query %id% cmaps" | %PAGER%
cmd cmds !%lf% -remote "query %id% cmds" | %PAGER%
The following additional keybindings are provided by default:
@@ -643,21 +653,6 @@ Delete the previous word in backward direction.
Capitalize/uppercase/lowercase the current word and jump to the next word.
maps
cmaps
List all key mappings in normal mode or command-line editing mode.
cmds
List all custom commands defined using the `cmd` command
jumps
List the contents of the jump list, in order of the most recently visited locations.
Each location is marked with the count that can be used with the `jump-prev` and `jump-next` commands (e.g. use `3[` to move three spaces backwards in the jump list).
A '>' is used to mark the current location in the jump list.
# Options
This section shows information about options to customize the behavior.
@@ -1418,7 +1413,33 @@ For example, you can configure the number of columns in the ui with respect to t
fi
}}
Besides 'send' command, there is a 'quit' command to quit the server when there are no connected clients left, and a 'quit!' command to force quit the server by closing client connections first:
In addition, the 'query' command can be used to obtain information about a specific lf instance by providing its id:
lf -remote "query $id maps"
The following types of information are supported:
maps list of mappings created by the 'map' command
cmaps list of mappings created by the 'cmap' command
cmds list of commands created by the 'cmd' command
jumps contents of the jump list, showing previously visited locations
history list of previously executed commands on the command line
This is useful for scripting actions based on the internal state of lf.
For example, to select a previous command using fzf and execute it:
map <a-h> ${{
clear
cmd=$(
lf -remote "query $id history" |
awk -F'\t' 'NR > 1 { print $NF}' |
sort -u |
fzf --reverse --prompt='Execute command: '
)
lf -remote "send $id $cmd"
}}
There is also a 'quit' command to quit the server when there are no connected clients left, and a 'quit!' command to force quit the server by closing client connections first:
lf -remote 'quit'
lf -remote 'quit!'
+51 -31
View File
@@ -83,10 +83,6 @@ The following commands are provided by lf:
mark-remove (modal) (default '"')
tag
tag-toggle (default 't')
maps
cmaps
cmds
jumps
The following command line commands are provided by lf:
@@ -220,13 +216,27 @@ when defined:
The following commands/keybindings are provided by default:
Unix Windows
cmd open &$OPENER "$f" cmd open &%OPENER% %f%
map e $$EDITOR "$f" map e $%EDITOR% %f%
map i $$PAGER "$f" map i !%PAGER% %f%
map w $$SHELL map w $%SHELL%
cmd doc $$lf -doc | $PAGER cmd doc !%lf% -doc | %PAGER%
map <f-1> doc map <f-1> doc
Unix
cmd open &$OPENER "$f"
map e $$EDITOR "$f"
map i $$PAGER "$f"
map w $$SHELL
cmd doc $$lf -doc | $PAGER
map <f-1> doc
cmd maps $lf -remote "query $id maps" | $PAGER
cmd cmaps $lf -remote "query $id cmaps" | $PAGER
cmd cmds $lf -remote "query $id cmds" | $PAGER
Windows
cmd open &%OPENER% %f%
map e $%EDITOR% %f%
map i !%PAGER% %f%
map w $%SHELL%
cmd doc !%lf% -doc | %PAGER%
map <f-1> doc
cmd maps !%lf% -remote "query %id% maps" | %PAGER%
cmd cmaps !%lf% -remote "query %id% cmaps" | %PAGER%
cmd cmds !%lf% -remote "query %id% cmds" | %PAGER%
The following additional keybindings are provided by default:
@@ -672,23 +682,6 @@ Delete the previous word in backward direction.
Capitalize/uppercase/lowercase the current word and jump to the next word.
maps
cmaps
List all key mappings in normal mode or command-line editing mode.
cmds
List all custom commands defined using the 'cmd' command
jumps
List the contents of the jump list, in order of the most recently visited
locations. Each location is marked with the count that can be used with the
'jump-prev' and 'jump-next' commands (e.g. use '3[' to move three spaces
backwards in the jump list). A '>' is used to mark the current location in the
jump list.
# Options
This section shows information about options to customize the behavior.
@@ -1540,9 +1533,36 @@ respect to the terminal width as follows:
fi
}}
Besides 'send' command, there is a 'quit' command to quit the server when there
are no connected clients left, and a 'quit!' command to force quit the server by
closing client connections first:
In addition, the 'query' command can be used to obtain information about a
specific lf instance by providing its id:
lf -remote "query $id maps"
The following types of information are supported:
maps list of mappings created by the 'map' command
cmaps list of mappings created by the 'cmap' command
cmds list of commands created by the 'cmd' command
jumps contents of the jump list, showing previously visited locations
history list of previously executed commands on the command line
This is useful for scripting actions based on the internal state of lf.
For example, to select a previous command using fzf and execute it:
map <a-h> ${{
clear
cmd=$(
lf -remote "query $id history" |
awk -F'\t' 'NR > 1 { print $NF}' |
sort -u |
fzf --reverse --prompt='Execute command: '
)
lf -remote "send $id $cmd"
}}
There is also a 'quit' command to quit the server when there are no connected
clients left, and a 'quit!' command to force quit the server by closing client
connections first:
lf -remote 'quit'
lf -remote 'quit!'
-8
View File
@@ -2736,14 +2736,6 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.cmdAccLeft = acc
update(app)
case "cmds":
app.runPagerOn(listCmds())
case "maps":
app.runPagerOn(listBinds(gOpts.keys))
case "cmaps":
app.runPagerOn(listBinds(gOpts.cmdkeys))
case "jumps":
app.runPagerOn(listJumps(app.nav.jumpList, app.nav.jumpListInd))
default:
cmd, ok := gOpts.cmds[e.name]
if !ok {
+55 -31
View File
@@ -95,10 +95,6 @@ The following commands are provided by lf:
mark-remove (modal) (default '"')
tag
tag-toggle (default 't')
maps
cmaps
cmds
jumps
.EE
.PP
The following command line commands are provided by lf:
@@ -241,13 +237,29 @@ The following special shell commands are used to customize the behavior of lf wh
The following commands/keybindings are provided by default:
.PP
.EX
Unix Windows
cmd open &$OPENER "$f" cmd open &%OPENER% %f%
map e $$EDITOR "$f" map e $%EDITOR% %f%
map i $$PAGER "$f" map i !%PAGER% %f%
map w $$SHELL map w $%SHELL%
cmd doc $$lf -doc | $PAGER cmd doc !%lf% -doc | %PAGER%
map <f-1> doc map <f-1> doc
Unix
cmd open &$OPENER "$f"
map e $$EDITOR "$f"
map i $$PAGER "$f"
map w $$SHELL
cmd doc $$lf -doc | $PAGER
map <f-1> doc
cmd maps $lf -remote "query $id maps" | $PAGER
cmd cmaps $lf -remote "query $id cmaps" | $PAGER
cmd cmds $lf -remote "query $id cmds" | $PAGER
.EE
.PP
.EX
Windows
cmd open &%OPENER% %f%
map e $%EDITOR% %f%
map i !%PAGER% %f%
map w $%SHELL%
cmd doc !%lf% -doc | %PAGER%
map <f-1> doc
cmd maps !%lf% -remote "query %id% maps" | %PAGER%
cmd cmaps !%lf% -remote "query %id% cmaps" | %PAGER%
cmd cmds !%lf% -remote "query %id% cmds" | %PAGER%
.EE
.PP
The following additional keybindings are provided by default:
@@ -790,25 +802,6 @@ Delete the previous word in backward direction.
.EE
.PP
Capitalize/uppercase/lowercase the current word and jump to the next word.
.PP
.EX
maps
cmaps
.EE
.PP
List all key mappings in normal mode or command-line editing mode.
.PP
.EX
cmds
.EE
.PP
List all custom commands defined using the `cmd` command
.PP
.EX
jumps
.EE
.PP
List the contents of the jump list, in order of the most recently visited locations. Each location is marked with the count that can be used with the `jump-prev` and `jump-next` commands (e.g. use `3[` to move three spaces backwards in the jump list). A '>' is used to mark the current location in the jump list.
.SH OPTIONS
This section shows information about options to customize the behavior. Character ':' is used as the separator for list options '[]int' and '[]string'.
.PP
@@ -1657,7 +1650,38 @@ Since lf does not have control flow syntax, remote commands are used for such ne
}}
.EE
.PP
Besides 'send' command, there is a 'quit' command to quit the server when there are no connected clients left, and a 'quit!' command to force quit the server by closing client connections first:
In addition, the 'query' command can be used to obtain information about a specific lf instance by providing its id:
.PP
.EX
lf -remote "query $id maps"
.EE
.PP
The following types of information are supported:
.PP
.EX
maps list of mappings created by the 'map' command
cmaps list of mappings created by the 'cmap' command
cmds list of commands created by the 'cmd' command
jumps contents of the jump list, showing previously visited locations
history list of previously executed commands on the command line
.EE
.PP
This is useful for scripting actions based on the internal state of lf. For example, to select a previous command using fzf and execute it:
.PP
.EX
map <a-h> ${{
clear
cmd=$(
lf -remote "query $id history" |
awk -F'\et' 'NR > 1 { print $NF}' |
sort -u |
fzf --reverse --prompt='Execute command: '
)
lf -remote "send $id $cmd"
}}
.EE
.PP
There is also a 'quit' command to quit the server when there are no connected clients left, and a 'quit!' command to force quit the server by closing client connections first:
.PP
.EX
lf -remote 'quit'
+4
View File
@@ -170,6 +170,10 @@ func setDefaults() {
gOpts.cmds["doc"] = &execExpr{"$", `"$lf" -doc | $PAGER`}
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
gOpts.cmds["maps"] = &execExpr{"$", `"$lf" -remote "query $id maps" | $PAGER`}
gOpts.cmds["cmaps"] = &execExpr{"$", `"$lf" -remote "query $id cmaps" | $PAGER`}
gOpts.cmds["cmds"] = &execExpr{"$", `"$lf" -remote "query $id cmds" | $PAGER`}
}
func setUserUmask() {
+4
View File
@@ -136,6 +136,10 @@ func setDefaults() {
gOpts.cmds["doc"] = &execExpr{"!", "%lf% -doc | %PAGER%"}
gOpts.keys["<f-1>"] = &callExpr{"doc", nil, 1}
gOpts.cmds["maps"] = &execExpr{"!", `%lf% -remote "query %id% maps" | %PAGER%`}
gOpts.cmds["cmaps"] = &execExpr{"!", `%lf% -remote "query %id% cmaps" | %PAGER%`}
gOpts.cmds["cmds"] = &execExpr{"!", `%lf% -remote "query %id% cmds" | %PAGER%`}
}
func setUserUmask() {}
+27
View File
@@ -83,7 +83,10 @@ Loop:
if err != nil {
echoerr(c, "listen: conn: client id should be a number")
} else {
// lifetime of the connection is managed by the server and
// will be cleaned up via the `drop` command
gConnList[id] = c
return
}
} else {
echoerr(c, "listen: conn: requires a client id")
@@ -95,6 +98,9 @@ Loop:
if err != nil {
echoerr(c, "listen: drop: client id should be a number")
} else {
if c2, ok := gConnList[id]; ok {
c2.Close()
}
delete(gConnList, id)
}
} else {
@@ -116,6 +122,27 @@ Loop:
}
}
}
case "query":
if rest == "" {
echoerr(c, "listen: query: requires a client id")
break
}
word2, rest2 := splitWord(rest)
id, err := strconv.Atoi(word2)
if err != nil {
echoerr(c, "listen: query: client id should be a number")
break
}
c2, ok := gConnList[id]
if !ok {
echoerr(c, "listen: query: no such client id is connected")
break
}
fmt.Fprintln(c2, "query "+rest2)
s2 := bufio.NewScanner(c2)
for s2.Scan() && s2.Text() != "" {
fmt.Fprintln(c, s2.Text())
}
case "quit":
if len(gConnList) == 0 {
gQuitChan <- struct{}{}
+16
View File
@@ -1167,6 +1167,22 @@ func listJumps(jumps []string, ind int) *bytes.Buffer {
return b
}
func listHistory(history []cmdItem) *bytes.Buffer {
t := new(tabwriter.Writer)
b := new(bytes.Buffer)
maxlength := len(strconv.Itoa(len(history)))
t.Init(b, 0, gOpts.tabstop, 2, '\t', 0)
fmt.Fprintln(t, "number\tcommand")
for i, cmd := range history {
fmt.Fprintf(t, "%*d\t%s%s\n", maxlength, i+1, cmd.prefix, cmd.value)
}
t.Flush()
return b
}
func listMarks(marks map[string]string) *bytes.Buffer {
t := new(tabwriter.Writer)
b := new(bytes.Buffer)