oh-my-posh/src/ansi_renderer.go
Jan De Dobbeleer 860eeb478a feat: support osc9;9
resolves #409
2021-02-14 14:54:02 +01:00

62 lines
1.6 KiB
Go

package main
import (
"fmt"
"strings"
)
// AnsiRenderer exposes functionality using ANSI
type AnsiRenderer struct {
builder strings.Builder
formats *ansiFormats
}
func (r *AnsiRenderer) carriageForward() {
r.builder.WriteString(fmt.Sprintf(r.formats.left, 1000))
}
func (r *AnsiRenderer) setCursorForRightWrite(text string, offset int) {
strippedLen := r.formats.lenWithoutANSI(text) + -offset
r.builder.WriteString(fmt.Sprintf(r.formats.right, strippedLen))
}
func (r *AnsiRenderer) changeLine(numberOfLines int) {
position := "B"
if numberOfLines < 0 {
position = "F"
numberOfLines = -numberOfLines
}
r.builder.WriteString(fmt.Sprintf(r.formats.linechange, numberOfLines, position))
}
func (r *AnsiRenderer) creset() {
r.builder.WriteString(r.formats.creset)
}
func (r *AnsiRenderer) write(text string) {
r.builder.WriteString(text)
// Due to a bug in Powershell, the end of the line needs to be cleared.
// If this doesn't happen, the portion after the prompt gets colored in the background
// color of the line above the new input line. Clearing the line fixes this,
// but can hopefully one day be removed when this is resolved natively.
if r.formats.shell == pwsh || r.formats.shell == powershell5 {
r.builder.WriteString(r.formats.clearEOL)
}
}
func (r *AnsiRenderer) string() string {
return r.builder.String()
}
func (r *AnsiRenderer) saveCursorPosition() {
r.builder.WriteString(r.formats.saveCursorPosition)
}
func (r *AnsiRenderer) restoreCursorPosition() {
r.builder.WriteString(r.formats.restoreCursorPosition)
}
func (r *AnsiRenderer) osc99(pwd string) {
r.builder.WriteString(fmt.Sprintf(r.formats.osc99, pwd))
}