fix: indent line based on last prompt and fix cursor pos

multiline
sammyette 2023-01-29 20:57:34 -04:00
parent d22428bd08
commit 3fda9592aa
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 38 additions and 15 deletions

View File

@ -92,7 +92,7 @@ func (rl *Instance) computeLinePos() {
func (rl *Instance) computeCursorPos(startLine, cpos, lineIdx int) { func (rl *Instance) computeCursorPos(startLine, cpos, lineIdx int) {
termWidth := GetTermWidth() termWidth := GetTermWidth()
cursorStart := cpos - startLine cursorStart := cpos - startLine
//cursorStart += rl.Prompt.inputAt(rl) cursorStart += rl.getPromptPos()
cursorY := cursorStart / termWidth cursorY := cursorStart / termWidth
cursorX := cursorStart % termWidth cursorX := cursorStart % termWidth
@ -159,22 +159,11 @@ func (rl *Instance) echo() {
// Print the prompt // Print the prompt
print(string(rl.realPrompt)) print(string(rl.realPrompt))
// Assemble the line, taking virtual completions into account // print the line
var line []rune rl.printBuffer()
if len(rl.currentComp) > 0 {
line = rl.lineComp
} else {
line = rl.line
}
printed := string(line)
// Print the input line with optional syntax highlighting
if rl.SyntaxHighlighter != nil {
printed = (rl.SyntaxHighlighter(line))
}
// update cursor positions
rl.computeLinePos() rl.computeLinePos()
print(printed)
} }
// Update references with new coordinates only now, because // Update references with new coordinates only now, because
@ -325,6 +314,32 @@ func (rl *Instance) clearLine() {
rl.clearVirtualComp() rl.clearVirtualComp()
} }
func (rl *Instance) printBuffer() {
// Generate the entire line as an highlighted line,
// and split it at each newline.
line := string(rl.GetLine())
lines := strings.Split(line, "\n")
if len(line) > 0 && line[len(line)-1] == '\n' {
lines = append(lines, "")
}
for i, line := range lines {
// Indent according to the prompt.
if i > 0 {
moveCursorForwards(rl.getPromptPos())
}
if i < len(lines)-1 {
line += "\n"
} else {
line += seqClearScreenBelow
}
print(line)
}
}
func (rl *Instance) deleteToBeginning() { func (rl *Instance) deleteToBeginning() {
rl.resetVirtualComp(false) rl.resetVirtualComp(false)
// Keep the line length up until the cursor // Keep the line length up until the cursor

View File

@ -218,3 +218,11 @@ func (rl *Instance) echoRightPrompt() {
print(rl.rightPrompt) print(rl.rightPrompt)
} }
} }
func (rl *Instance) getPromptPos() int {
if !rl.Multiline {
return getRealLength(rl.mainPrompt)
} else {
return getRealLength(rl.MultilinePrompt)
}
}