Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
(feat): allow prompt callback on rune input and initial input
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassabreu committed Jun 29, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 818eaad commit 42156f1
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions terminal/runereader.go
Original file line number Diff line number Diff line change
@@ -31,7 +31,13 @@ func (rr *RuneReader) printChar(char rune, mask rune) {
}
}

func (rr *RuneReader) ReadLine(mask rune, onRunes ...func(rune) bool) ([]rune, error) {
type OnRuneFn func(rune, []rune) ([]rune, bool, error)

func (rr *RuneReader) ReadLine(mask rune, onRunes ...OnRuneFn) ([]rune, error) {
return rr.ReadLineWithDefault(mask, []rune{}, onRunes...)
}

func (rr *RuneReader) ReadLineWithDefault(mask rune, d []rune, onRunes ...OnRuneFn) ([]rune, error) {
line := []rune{}
// we only care about horizontal displacements from the origin so start counting at 0
index := 0
@@ -41,11 +47,26 @@ func (rr *RuneReader) ReadLine(mask rune, onRunes ...func(rune) bool) ([]rune, e
Out: rr.stdio.Out,
}

onRune := func(r rune, line []rune) ([]rune, bool, error) {
return line, false, nil
}

// if the user pressed a key the caller was interested in capturing
if len(onRunes) > 0 {
onRune = onRunes[0]
}

// we get the terminal width and height (if resized after this point the property might become invalid)
terminalSize, _ := cursor.Size(rr.Buffer())
// we set the current location of the cursor once
cursorCurrent, _ := cursor.Location(rr.Buffer())

if len(d) > 0 {
index = len(d)
fmt.Fprint(rr.stdio.Out, string(d))
line = d
}

for {
// wait for some input
r, _, err := rr.ReadRune()
@@ -55,10 +76,10 @@ func (rr *RuneReader) ReadLine(mask rune, onRunes ...func(rune) bool) ([]rune, e
// increment cursor location
cursorCurrent.X++

// if the user pressed a key the caller was interested in capturing
if len(onRunes) > 0 && onRunes[0](r) {
return line, nil
}
if l, stop, err := onRune(r, line); stop || err != nil {
return l, err
}

// if the user pressed enter or some other newline/termination like ctrl+d
if r == '\r' || r == '\n' || r == KeyEndTransmission {
// delete what's printed out on the console screen (cleanup)

0 comments on commit 42156f1

Please sign in to comment.