Skip to content

Commit

Permalink
fix: render newline correctly cross platform/shell
Browse files Browse the repository at this point in the history
Powershell has an issue rendering multiline prompts when it comes to
PSReadline on MacOS. To mitigate that, we make it a multiline
prompt by moving the cursor all the way to the end. That way, PSReadline
believes this is still a one line prompt and everything works as
expected.

PowerShell/PowerShell#3687
  • Loading branch information
JanDeDobbeleer committed Sep 17, 2020
1 parent 5c4af19 commit 6fd9f0b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
22 changes: 9 additions & 13 deletions engine.go
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"fmt"
"regexp"

Expand Down Expand Up @@ -122,34 +121,31 @@ func (e *engine) lenWithoutANSI(str string) int {
return count
}

func (e *engine) string() string {
var buffer bytes.Buffer
defer buffer.Reset()
func (e *engine) render() {
for _, block := range e.settings.Blocks {
// if line break, append a line break
if block.Type == LineBreak {
buffer.WriteString("\x1b[1B")
fmt.Printf("\x1b[%dC ", 1000)
continue
}
if block.LineOffset < 0 {
buffer.WriteString(fmt.Sprintf("\x1b[%dF", -block.LineOffset))
fmt.Printf("\x1b[%dF", -block.LineOffset)
} else if block.LineOffset > 0 {
buffer.WriteString(fmt.Sprintf("\x1b[%dB", block.LineOffset))
fmt.Printf("\x1b[%dB", block.LineOffset)
}
switch block.Alignment {
case Right:
buffer.WriteString(fmt.Sprintf("\x1b[%dC", 1000))
fmt.Printf("\x1b[%dC", 1000)
blockText := e.renderBlockSegments(block)
buffer.WriteString(fmt.Sprintf("\x1b[%dD", e.lenWithoutANSI(blockText)+e.settings.RightSegmentOffset))
buffer.WriteString(blockText)
fmt.Printf("\x1b[%dD", e.lenWithoutANSI(blockText)+e.settings.RightSegmentOffset)
fmt.Print(blockText)
default:
buffer.WriteString(e.renderBlockSegments(block))
fmt.Print(e.renderBlockSegments(block))
}
}
if e.settings.EndSpaceEnabled {
buffer.WriteString(" ")
fmt.Print(" ")
}
return buffer.String()
}

func (e *engine) reset() {
Expand Down
3 changes: 1 addition & 2 deletions main.go
Expand Up @@ -45,6 +45,5 @@ func main() {
Buffer: new(bytes.Buffer),
},
}
prompt := engine.string()
fmt.Print(prompt)
engine.render()
}

0 comments on commit 6fd9f0b

Please sign in to comment.