From fd2ed9c158cb22584da9f8dc20ced0745a0732fa Mon Sep 17 00:00:00 2001 From: Mateusz Drewniak Date: Mon, 24 Jul 2023 10:38:59 +0200 Subject: [PATCH] Make key binds more DRY --- emacs.go | 18 ++---------------- key_bind_func.go | 9 +++++++++ prompt.go | 19 +++++++++---------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/emacs.go b/emacs.go index 3314d273..09098b7a 100644 --- a/emacs.go +++ b/emacs.go @@ -138,25 +138,11 @@ var emacsKeyBindings = []KeyBind{ // Cut the Word before the cursor. { Key: ControlW, - Fn: func(p *Prompt) bool { - p.Buffer.DeleteBeforeCursor( - istrings.RuneCount(p.Buffer.Document().GetWordBeforeCursorWithSpace()), - p.renderer.col, - p.renderer.row, - ) - return true - }, + Fn: DeleteWordBeforeCursor, }, { Key: AltBackspace, - Fn: func(p *Prompt) bool { - p.Buffer.DeleteBeforeCursor( - istrings.RuneCount(p.Buffer.Document().GetWordBeforeCursorWithSpace()), - p.renderer.col, - p.renderer.row, - ) - return true - }, + Fn: DeleteWordBeforeCursor, }, // Clear the Screen, similar to the clear command { diff --git a/key_bind_func.go b/key_bind_func.go index 6f604adf..21e203bd 100644 --- a/key_bind_func.go +++ b/key_bind_func.go @@ -37,3 +37,12 @@ func GoRightChar(p *Prompt) bool { func GoLeftChar(p *Prompt) bool { return p.CursorLeft(1) } + +func DeleteWordBeforeCursor(p *Prompt) bool { + p.Buffer.DeleteBeforeCursor( + istrings.RuneCount(p.Buffer.Document().GetWordBeforeCursorWithSpace()), + p.renderer.col, + p.renderer.row, + ) + return true +} diff --git a/prompt.go b/prompt.go index 615ce15a..32cc2a00 100644 --- a/prompt.go +++ b/prompt.go @@ -2,8 +2,6 @@ package prompt import ( "bytes" - "fmt" - "log" "os" "strings" "time" @@ -140,14 +138,14 @@ func (p *Prompt) Run() { } } -func Log(format string, a ...any) { - f, err := os.OpenFile("log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) - if err != nil { - log.Fatalf("error opening file: %v", err) - } - defer f.Close() - fmt.Fprintf(f, format+"\n", a...) -} +// func Log(format string, a ...any) { +// f, err := os.OpenFile("log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) +// if err != nil { +// log.Fatalf("error opening file: %v", err) +// } +// defer f.Close() +// fmt.Fprintf(f, format+"\n", a...) +// } func (p *Prompt) feed(b []byte) (shouldExit bool, rerender bool, userInput *UserInput) { key := GetKey(b) @@ -483,6 +481,7 @@ func (p *Prompt) readBuffer(bufCh chan []byte, stopCh chan struct{}) { break } bytes = bytes[:n] + // Log("%#v", bytes) if len(bytes) == 1 && bytes[0] == '\t' { // if only a single Tab key has been pressed // handle it as a keybind