From 3fda9592aa49ad213dd08568c2be135081dfe1a2 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sun, 29 Jan 2023 20:57:34 -0400 Subject: [PATCH] fix: indent line based on last prompt and fix cursor pos --- readline/line.go | 45 ++++++++++++++++++++++++++++++--------------- readline/prompt.go | 8 ++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/readline/line.go b/readline/line.go index e9905f5..5f4e4b8 100644 --- a/readline/line.go +++ b/readline/line.go @@ -92,7 +92,7 @@ func (rl *Instance) computeLinePos() { func (rl *Instance) computeCursorPos(startLine, cpos, lineIdx int) { termWidth := GetTermWidth() cursorStart := cpos - startLine - //cursorStart += rl.Prompt.inputAt(rl) + cursorStart += rl.getPromptPos() cursorY := cursorStart / termWidth cursorX := cursorStart % termWidth @@ -159,22 +159,11 @@ func (rl *Instance) echo() { // Print the prompt print(string(rl.realPrompt)) - // Assemble the line, taking virtual completions into account - var line []rune - 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)) - } + // print the line + rl.printBuffer() + // update cursor positions rl.computeLinePos() - print(printed) } // Update references with new coordinates only now, because @@ -325,6 +314,32 @@ func (rl *Instance) clearLine() { 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() { rl.resetVirtualComp(false) // Keep the line length up until the cursor diff --git a/readline/prompt.go b/readline/prompt.go index 0f6ca5a..634f35b 100644 --- a/readline/prompt.go +++ b/readline/prompt.go @@ -218,3 +218,11 @@ func (rl *Instance) echoRightPrompt() { print(rl.rightPrompt) } } + +func (rl *Instance) getPromptPos() int { + if !rl.Multiline { + return getRealLength(rl.mainPrompt) + } else { + return getRealLength(rl.MultilinePrompt) + } +}