From a3138a9e86bcc8e604262d47453fb7b2b27c8809 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 15 Jun 2025 15:43:12 +0300 Subject: [PATCH] Add key binding and command to run any other command through input bar --- config/keybindings.yml | 5 ++++- src/command.go | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.go | 4 +++- src/window.go | 9 +++++++-- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/config/keybindings.yml b/config/keybindings.yml index fc5b4c8..4705034 100644 --- a/config/keybindings.yml +++ b/config/keybindings.yml @@ -37,4 +37,7 @@ keybindings: command: "menu-edit" - keybinding: "F3" cursor_modes: ["buffer","dropdown"] - command: "menu-buffers" \ No newline at end of file + command: "menu-buffers" + - keybinding: "Ctrl-E" + cursor_modes: ["buffer"] + command: "execute" \ No newline at end of file diff --git a/src/command.go b/src/command.go index df39a14..90ea948 100644 --- a/src/command.go +++ b/src/command.go @@ -246,6 +246,47 @@ func initCommands() { }, } + executeCmd := Command{ + cmd: "execute", + run: func(window *Window, args ...string) { + inputChannel := RequestInput(window, "Run:", "") + + go func() { + input := strings.TrimSpace(<-inputChannel) + + if input == "" { + return + } + + var arguments []string + + builder := &strings.Builder{} + quoted := false + for _, r := range input { + if r == '"' { + quoted = !quoted + } else if !quoted && r == ' ' { + arguments = append(arguments, builder.String()) + builder.Reset() + } else { + builder.WriteRune(r) + } + } + if builder.Len() > 0 { + arguments = append(arguments, builder.String()) + } + + window.CursorMode = CursorModeBuffer + + if len(arguments) == 1 { + RunCommand(window, arguments[0]) + } else { + RunCommand(window, arguments[0], arguments[1:]...) + } + }() + }, + } + // Register commands commands["copy"] = ©Cmd commands["paste"] = &pasteCmd @@ -260,6 +301,7 @@ func initCommands() { commands["menu-edit"] = &menuEditCmd commands["menu-buffers"] = &menuBuffersCmd commands["quit"] = &quitCmd + commands["execute"] = &executeCmd } func RunCommand(window *Window, cmd string, args ...string) bool { diff --git a/src/main.go b/src/main.go index 989fbaa..50657bd 100644 --- a/src/main.go +++ b/src/main.go @@ -38,9 +38,11 @@ func main() { } } - for window.screen != nil { + for !window.closed { window.Draw() window.ProcessEvents() } + window.screen.Fini() + window.screen = nil } diff --git a/src/window.go b/src/window.go index 93a8e0f..2e47eda 100644 --- a/src/window.go +++ b/src/window.go @@ -36,6 +36,8 @@ type Window struct { CurrentBuffer *Buffer screen tcell.Screen + + closed bool } var mouseHeld = false @@ -637,8 +639,11 @@ func (window *Window) handleMouseInput(ev *tcell.EventMouse) { } func (window *Window) Close() { - window.screen.Fini() - window.screen = nil + window.closed = true + err := window.screen.PostEvent(tcell.NewEventInterrupt(nil)) + if err != nil { + return + } } func (window *Window) GetTextAreaDimensions() (int, int, int, int) {