Skip to content

Commit

Permalink
Map Ctrl-U, Ctrl-N, Ctrl-P Keys in REPL
Browse files Browse the repository at this point in the history
Map ctrl-u to delete to beginning of line.

Map ctrl-p to previous line from history.

Map ctrl-n to next line from history.
  • Loading branch information
cwarden committed Aug 2, 2024
1 parent 8b70a4f commit 7a4c9f9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
9 changes: 9 additions & 0 deletions evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ func constructKeyEvent(r rune, k keyboard.Key) util.KeyEvent {
case keyboard.KeyCtrlL:
ch = "l"
ctrl = true
case keyboard.KeyCtrlN:
ch = "n"
ctrl = true
case keyboard.KeyCtrlP:
ch = "p"
ctrl = true
case keyboard.KeyCtrlU:
ch = "u"
ctrl = true

case keyboard.KeyEnter:
code = 13
Expand Down
88 changes: 53 additions & 35 deletions util/microliner.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,45 @@ startOfHere:
s.getColumns()
traceTop(strconv.Itoa(s.columns)+"**", 0)

histPrev := func() {
if historyStale {
historyPrefix = s.getHistoryByPrefix(string(line))
historyPos = len(historyPrefix)
historyStale = false
}
if historyPos > 0 {
if historyPos == len(historyPrefix) {
historyEnd = string(line)
}
historyPos--
line = []rune(historyPrefix[historyPos])
pos = len(line)
s.needRefresh = true
} else {
s.doBeep()
}
}

histNext := func() {
if historyStale {
historyPrefix = s.getHistoryByPrefix(string(line))
historyPos = len(historyPrefix)
historyStale = false
}
if historyPos < len(historyPrefix) {
historyPos++
if historyPos == len(historyPrefix) {
line = []rune(historyEnd)
} else {
line = []rune(historyPrefix[historyPos])
}
pos = len(line)
s.needRefresh = true
} else {
s.doBeep()
}
}

// JM
// s_instr := 0

Expand Down Expand Up @@ -565,6 +604,18 @@ startOfHere:
case "l":
s.eraseScreen()
s.needRefresh = true
case "u": // delete to beginning of line
if pos == 0 {
s.doBeep()
} else {
line = line[pos:]
pos = 0
s.needRefresh = true
}
case "n":
histNext()
case "p":
histPrev()
}
} else if next.Alt {
switch strings.ToLower(next.Key) {
Expand Down Expand Up @@ -710,42 +761,9 @@ startOfHere:
s.doBeep()
}
case 38: // Up
// historyAction = true
if historyStale {
historyPrefix = s.getHistoryByPrefix(string(line))
historyPos = len(historyPrefix)
historyStale = false
}
if historyPos > 0 {
if historyPos == len(historyPrefix) {
historyEnd = string(line)
}
historyPos--
line = []rune(historyPrefix[historyPos])
pos = len(line)
s.needRefresh = true
} else {
s.doBeep()
}
histPrev()
case 40: // Down
// historyAction = true
if historyStale {
historyPrefix = s.getHistoryByPrefix(string(line))
historyPos = len(historyPrefix)
historyStale = false
}
if historyPos < len(historyPrefix) {
historyPos++
if historyPos == len(historyPrefix) {
line = []rune(historyEnd)
} else {
line = []rune(historyPrefix[historyPos])
}
pos = len(line)
s.needRefresh = true
} else {
s.doBeep()
}
histNext()
case 36: // Home
pos = 0
case 35: // End
Expand Down

0 comments on commit 7a4c9f9

Please sign in to comment.