Skip to content

Commit

Permalink
Make key binds more DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 24, 2023
1 parent 2344bfa commit fd2ed9c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
18 changes: 2 additions & 16 deletions emacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
9 changes: 9 additions & 0 deletions key_bind_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 9 additions & 10 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package prompt

import (
"bytes"
"fmt"
"log"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fd2ed9c

Please sign in to comment.