Changing behavior for TryBindKey() for lua to not write to bindings.json

This commit is contained in:
Neko Box Coder
2025-09-06 20:25:47 +01:00
parent 0b9c7c0c4a
commit b39b5b5916
4 changed files with 19 additions and 10 deletions

View File

@@ -73,7 +73,7 @@ func luaImportMicroConfig() *lua.LTable {
ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete)) ulua.L.SetField(pkg, "OptionComplete", luar.New(ulua.L, action.OptionComplete))
ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete)) ulua.L.SetField(pkg, "OptionValueComplete", luar.New(ulua.L, action.OptionValueComplete))
ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil)) ulua.L.SetField(pkg, "NoComplete", luar.New(ulua.L, nil))
ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKey)) ulua.L.SetField(pkg, "TryBindKey", luar.New(ulua.L, action.TryBindKeyPlug))
ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig)) ulua.L.SetField(pkg, "Reload", luar.New(ulua.L, action.ReloadConfig))
ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory)) ulua.L.SetField(pkg, "AddRuntimeFileFromMemory", luar.New(ulua.L, config.PluginAddRuntimeFileFromMemory))
ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory)) ulua.L.SetField(pkg, "AddRuntimeFilesFromDirectory", luar.New(ulua.L, config.PluginAddRuntimeFilesFromDirectory))

View File

@@ -261,9 +261,14 @@ func eventsEqual(e1 Event, e2 Event) bool {
return e1 == e2 return e1 == e2
} }
// TryBindKeyPlug tries to bind a key for the plugin without writing to bindings.json.
func TryBindKeyPlug(k, v string, overwrite bool) (bool, error) {
return TryBindKey(k, v, overwrite, false)
}
// TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json // TryBindKey tries to bind a key by writing to config.ConfigDir/bindings.json
// Returns true if the keybinding already existed and a possible error // Returns true if the keybinding already existed or is binded successfully and a possible error
func TryBindKey(k, v string, overwrite bool) (bool, error) { func TryBindKey(k, v string, overwrite bool, writeToFile bool) (bool, error) {
var e error var e error
var parsed map[string]any var parsed map[string]any
@@ -310,7 +315,12 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
txt, _ := json.MarshalIndent(parsed, "", " ") txt, _ := json.MarshalIndent(parsed, "", " ")
txt = append(txt, '\n') txt = append(txt, '\n')
return true, writeFile(filename, txt)
if writeToFile {
return true, writeFile(filename, txt)
} else {
return true, nil
}
} }
return false, e return false, e
} }

View File

@@ -844,7 +844,7 @@ func (h *BufPane) BindCmd(args []string) {
return return
} }
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true) _, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true)
if err != nil { if err != nil {
if errors.Is(err, util.ErrOverwrite) { if errors.Is(err, util.ErrOverwrite) {
screen.TermMessage(err) screen.TermMessage(err)

View File

@@ -174,11 +174,10 @@ The packages and their contents are listed below (in Go type signatures):
values afterwards values afterwards
- `NoComplete`: no autocompletion suggestions - `NoComplete`: no autocompletion suggestions
- `TryBindKey(k, v string, overwrite bool) (bool, error)`: bind the key - `TryBindKey(k, v string, overwrite bool) (bool, error)`:
`k` to the string `v` in the `bindings.json` file. If `overwrite` is bind the key `k` to the string `v`. If `overwrite` is true, this will
true, this will overwrite any existing binding to key `k`. Returns true overwrite any existing binding to key `k`.
if the binding was made, and a possible error (for example writing to Returns true if the binding was made, and a possible error.
`bindings.json` can cause an error).
- `Reload()`: reload configuration files. - `Reload()`: reload configuration files.