Skip to content

Commit

Permalink
125 readline (#126)
Browse files Browse the repository at this point in the history
* Ctrl-C only works at the start of a line

* If the user fills the buffer, return.
  • Loading branch information
skx authored Jun 20, 2024
1 parent 0da3ab3 commit 416d839
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions consolein/consolein.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (ci *ConsoleIn) ReadLine(max uint8) (string, error) {
return "", err
}

// Esc
// Esc?
if x == 27 {
// remove the character from our text, and overwrite on the console
for len(text) > 0 {
Expand All @@ -232,7 +232,7 @@ func (ci *ConsoleIn) ReadLine(max uint8) (string, error) {
continue
}

// Ctrl-N
// Ctrl-N?
if x == 14 {
if offset >= 1 {

Expand All @@ -253,7 +253,7 @@ func (ci *ConsoleIn) ReadLine(max uint8) (string, error) {
continue
}

// Ctrl-P ?
// Ctrl-P?
if x == 16 {
if offset >= len(ci.history) {
continue
Expand All @@ -274,14 +274,18 @@ func (ci *ConsoleIn) ReadLine(max uint8) (string, error) {
}

// Ctrl-C ?
//
if x == 0x03 {
ctrlCount += 1

// If we've hit our limit of consecutive Ctrl-Cs
// then we return the interrupted error-code
if ctrlCount == ci.InterruptCount {
return "", ErrInterrupted
// Ctrl-C should only take effect at the start of the line.
// i.e. When the text is empty.
if text == "" {
ctrlCount += 1

// If we've hit our limit of consecutive Ctrl-Cs
// then we return the interrupted error-code
if ctrlCount == ci.InterruptCount {
return "", ErrInterrupted
}
}
continue
}
Expand All @@ -297,7 +301,7 @@ func (ci *ConsoleIn) ReadLine(max uint8) (string, error) {
if len(ci.history) == 0 {
ci.history = append(ci.history, text)
} else {
// otherwise only add if different to previous entry
// otherwise only add if different to previous entry.
if text != ci.history[len(ci.history)-1] {
ci.history = append(ci.history, text)
}
Expand All @@ -320,12 +324,13 @@ func (ci *ConsoleIn) ReadLine(max uint8) (string, error) {
continue
}

// If the user has entered the maximum then we'll
// avoid further input
// If the user has entered the maximum then we'll say their
// input-time is over now.
if len(text) >= int(max) {
continue
break
}
// Otherwise if it was a printable character we'll keep it.

// Finally if it was a printable character we'll keep it.
if unicode.IsPrint(rune(x)) {
fmt.Printf("%c", x)
text += string(x)
Expand Down

0 comments on commit 416d839

Please sign in to comment.