|
|
@ -3,10 +3,12 @@ |
|
|
|
package liner |
|
|
|
package liner |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"bufio" |
|
|
|
"container/ring" |
|
|
|
"container/ring" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"io" |
|
|
|
|
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"unicode" |
|
|
|
"unicode" |
|
|
|
"unicode/utf8" |
|
|
|
"unicode/utf8" |
|
|
@ -90,6 +92,10 @@ const ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func (s *State) refresh(prompt []rune, buf []rune, pos int) error { |
|
|
|
func (s *State) refresh(prompt []rune, buf []rune, pos int) error { |
|
|
|
|
|
|
|
if s.columns == 0 { |
|
|
|
|
|
|
|
return ErrInternal |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
s.needRefresh = false |
|
|
|
s.needRefresh = false |
|
|
|
if s.multiLineMode { |
|
|
|
if s.multiLineMode { |
|
|
|
return s.refreshMultiLine(prompt, buf, pos) |
|
|
|
return s.refreshMultiLine(prompt, buf, pos) |
|
|
@ -351,8 +357,8 @@ func (s *State) tabComplete(p []rune, line []rune, pos int) ([]rune, int, interf |
|
|
|
} |
|
|
|
} |
|
|
|
hl := utf8.RuneCountInString(head) |
|
|
|
hl := utf8.RuneCountInString(head) |
|
|
|
if len(list) == 1 { |
|
|
|
if len(list) == 1 { |
|
|
|
s.refresh(p, []rune(head+list[0]+tail), hl+utf8.RuneCountInString(list[0])) |
|
|
|
err := s.refresh(p, []rune(head+list[0]+tail), hl+utf8.RuneCountInString(list[0])) |
|
|
|
return []rune(head + list[0] + tail), hl + utf8.RuneCountInString(list[0]), rune(esc), nil |
|
|
|
return []rune(head + list[0] + tail), hl + utf8.RuneCountInString(list[0]), rune(esc), err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
direction := tabForward |
|
|
|
direction := tabForward |
|
|
@ -366,7 +372,10 @@ func (s *State) tabComplete(p []rune, line []rune, pos int) ([]rune, int, interf |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return line, pos, rune(esc), err |
|
|
|
return line, pos, rune(esc), err |
|
|
|
} |
|
|
|
} |
|
|
|
s.refresh(p, []rune(head+pick+tail), hl+utf8.RuneCountInString(pick)) |
|
|
|
err = s.refresh(p, []rune(head+pick+tail), hl+utf8.RuneCountInString(pick)) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return line, pos, rune(esc), err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
next, err := s.readNext() |
|
|
|
next, err := s.readNext() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -392,7 +401,10 @@ func (s *State) tabComplete(p []rune, line []rune, pos int) ([]rune, int, interf |
|
|
|
// reverse intelligent search, implements a bash-like history search.
|
|
|
|
// reverse intelligent search, implements a bash-like history search.
|
|
|
|
func (s *State) reverseISearch(origLine []rune, origPos int) ([]rune, int, interface{}, error) { |
|
|
|
func (s *State) reverseISearch(origLine []rune, origPos int) ([]rune, int, interface{}, error) { |
|
|
|
p := "(reverse-i-search)`': " |
|
|
|
p := "(reverse-i-search)`': " |
|
|
|
s.refresh([]rune(p), origLine, origPos) |
|
|
|
err := s.refresh([]rune(p), origLine, origPos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return origLine, origPos, rune(esc), err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
line := []rune{} |
|
|
|
line := []rune{} |
|
|
|
pos := 0 |
|
|
|
pos := 0 |
|
|
@ -478,7 +490,10 @@ func (s *State) reverseISearch(origLine []rune, origPos int) ([]rune, int, inter |
|
|
|
case action: |
|
|
|
case action: |
|
|
|
return []rune(foundLine), foundPos, next, err |
|
|
|
return []rune(foundLine), foundPos, next, err |
|
|
|
} |
|
|
|
} |
|
|
|
s.refresh(getLine()) |
|
|
|
err = s.refresh(getLine()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return []rune(foundLine), foundPos, rune(esc), err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -535,7 +550,10 @@ func (s *State) yank(p []rune, text []rune, pos int) ([]rune, int, interface{}, |
|
|
|
line = append(line, lineEnd...) |
|
|
|
line = append(line, lineEnd...) |
|
|
|
|
|
|
|
|
|
|
|
pos = len(lineStart) + len(value) |
|
|
|
pos = len(lineStart) + len(value) |
|
|
|
s.refresh(p, line, pos) |
|
|
|
err := s.refresh(p, line, pos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return line, pos, 0, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
next, err := s.readNext() |
|
|
|
next, err := s.readNext() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -577,6 +595,11 @@ func (s *State) PromptWithSuggestion(prompt string, text string, pos int) (strin |
|
|
|
if s.inputRedirected || !s.terminalSupported { |
|
|
|
if s.inputRedirected || !s.terminalSupported { |
|
|
|
return s.promptUnsupported(prompt) |
|
|
|
return s.promptUnsupported(prompt) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
p := []rune(prompt) |
|
|
|
|
|
|
|
const minWorkingSpace = 10 |
|
|
|
|
|
|
|
if s.columns < countGlyphs(p)+minWorkingSpace { |
|
|
|
|
|
|
|
return s.tooNarrow(prompt) |
|
|
|
|
|
|
|
} |
|
|
|
if s.outputRedirected { |
|
|
|
if s.outputRedirected { |
|
|
|
return "", ErrNotTerminalOutput |
|
|
|
return "", ErrNotTerminalOutput |
|
|
|
} |
|
|
|
} |
|
|
@ -585,7 +608,6 @@ func (s *State) PromptWithSuggestion(prompt string, text string, pos int) (strin |
|
|
|
defer s.historyMutex.RUnlock() |
|
|
|
defer s.historyMutex.RUnlock() |
|
|
|
|
|
|
|
|
|
|
|
fmt.Print(prompt) |
|
|
|
fmt.Print(prompt) |
|
|
|
p := []rune(prompt) |
|
|
|
|
|
|
|
var line = []rune(text) |
|
|
|
var line = []rune(text) |
|
|
|
historyEnd := "" |
|
|
|
historyEnd := "" |
|
|
|
var historyPrefix []string |
|
|
|
var historyPrefix []string |
|
|
@ -600,7 +622,10 @@ func (s *State) PromptWithSuggestion(prompt string, text string, pos int) (strin |
|
|
|
pos = len(text) |
|
|
|
pos = len(text) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(line) > 0 { |
|
|
|
if len(line) > 0 { |
|
|
|
s.refresh(p, line, pos) |
|
|
|
err := s.refresh(p, line, pos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
restart: |
|
|
|
restart: |
|
|
@ -624,7 +649,10 @@ mainLoop: |
|
|
|
switch v { |
|
|
|
switch v { |
|
|
|
case cr, lf: |
|
|
|
case cr, lf: |
|
|
|
if s.needRefresh { |
|
|
|
if s.needRefresh { |
|
|
|
s.refresh(p, line, pos) |
|
|
|
err := s.refresh(p, line, pos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if s.multiLineMode { |
|
|
|
if s.multiLineMode { |
|
|
|
s.resetMultiLine(p, line, pos) |
|
|
|
s.resetMultiLine(p, line, pos) |
|
|
@ -958,7 +986,10 @@ mainLoop: |
|
|
|
s.needRefresh = true |
|
|
|
s.needRefresh = true |
|
|
|
} |
|
|
|
} |
|
|
|
if s.needRefresh && !s.inputWaiting() { |
|
|
|
if s.needRefresh && !s.inputWaiting() { |
|
|
|
s.refresh(p, line, pos) |
|
|
|
err := s.refresh(p, line, pos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if !historyAction { |
|
|
|
if !historyAction { |
|
|
|
historyStale = true |
|
|
|
historyStale = true |
|
|
@ -978,7 +1009,7 @@ func (s *State) PasswordPrompt(prompt string) (string, error) { |
|
|
|
return "", ErrInvalidPrompt |
|
|
|
return "", ErrInvalidPrompt |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if !s.terminalSupported { |
|
|
|
if !s.terminalSupported || s.columns == 0 { |
|
|
|
return "", errors.New("liner: function not supported in this terminal") |
|
|
|
return "", errors.New("liner: function not supported in this terminal") |
|
|
|
} |
|
|
|
} |
|
|
|
if s.inputRedirected { |
|
|
|
if s.inputRedirected { |
|
|
@ -988,6 +1019,12 @@ func (s *State) PasswordPrompt(prompt string) (string, error) { |
|
|
|
return "", ErrNotTerminalOutput |
|
|
|
return "", ErrNotTerminalOutput |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p := []rune(prompt) |
|
|
|
|
|
|
|
const minWorkingSpace = 1 |
|
|
|
|
|
|
|
if s.columns < countGlyphs(p)+minWorkingSpace { |
|
|
|
|
|
|
|
return s.tooNarrow(prompt) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
defer s.stopPrompt() |
|
|
|
defer s.stopPrompt() |
|
|
|
|
|
|
|
|
|
|
|
restart: |
|
|
|
restart: |
|
|
@ -995,7 +1032,6 @@ restart: |
|
|
|
s.getColumns() |
|
|
|
s.getColumns() |
|
|
|
|
|
|
|
|
|
|
|
fmt.Print(prompt) |
|
|
|
fmt.Print(prompt) |
|
|
|
p := []rune(prompt) |
|
|
|
|
|
|
|
var line []rune |
|
|
|
var line []rune |
|
|
|
pos := 0 |
|
|
|
pos := 0 |
|
|
|
|
|
|
|
|
|
|
@ -1014,7 +1050,10 @@ mainLoop: |
|
|
|
switch v { |
|
|
|
switch v { |
|
|
|
case cr, lf: |
|
|
|
case cr, lf: |
|
|
|
if s.needRefresh { |
|
|
|
if s.needRefresh { |
|
|
|
s.refresh(p, line, pos) |
|
|
|
err := s.refresh(p, line, pos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if s.multiLineMode { |
|
|
|
if s.multiLineMode { |
|
|
|
s.resetMultiLine(p, line, pos) |
|
|
|
s.resetMultiLine(p, line, pos) |
|
|
@ -1032,7 +1071,10 @@ mainLoop: |
|
|
|
s.restartPrompt() |
|
|
|
s.restartPrompt() |
|
|
|
case ctrlL: // clear screen
|
|
|
|
case ctrlL: // clear screen
|
|
|
|
s.eraseScreen() |
|
|
|
s.eraseScreen() |
|
|
|
s.refresh(p, []rune{}, 0) |
|
|
|
err := s.refresh(p, []rune{}, 0) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return "", err |
|
|
|
|
|
|
|
} |
|
|
|
case ctrlH, bs: // Backspace
|
|
|
|
case ctrlH, bs: // Backspace
|
|
|
|
if pos <= 0 { |
|
|
|
if pos <= 0 { |
|
|
|
fmt.Print(beep) |
|
|
|
fmt.Print(beep) |
|
|
@ -1068,3 +1110,20 @@ mainLoop: |
|
|
|
} |
|
|
|
} |
|
|
|
return string(line), nil |
|
|
|
return string(line), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *State) tooNarrow(prompt string) (string, error) { |
|
|
|
|
|
|
|
// Docker and OpenWRT and etc sometimes return 0 column width
|
|
|
|
|
|
|
|
// Reset mode temporarily. Restore baked mode in case the terminal
|
|
|
|
|
|
|
|
// is wide enough for the next Prompt attempt.
|
|
|
|
|
|
|
|
m, merr := TerminalMode() |
|
|
|
|
|
|
|
s.origMode.ApplyMode() |
|
|
|
|
|
|
|
if merr == nil { |
|
|
|
|
|
|
|
defer m.ApplyMode() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if s.r == nil { |
|
|
|
|
|
|
|
// Windows does not always set s.r
|
|
|
|
|
|
|
|
s.r = bufio.NewReader(os.Stdin) |
|
|
|
|
|
|
|
defer func() { s.r = nil }() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return s.promptUnsupported(prompt) |
|
|
|
|
|
|
|
} |
|
|
|