oh-my-posh/src/engine/engine.go

364 lines
9.1 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
2019-03-13 04:14:30 -07:00
import (
"fmt"
2022-01-26 04:09:21 -08:00
"oh-my-posh/color"
2022-01-26 22:44:35 -08:00
"oh-my-posh/console"
"oh-my-posh/environment"
2022-03-21 23:41:36 -07:00
"oh-my-posh/shell"
2022-01-26 06:54:36 -08:00
"oh-my-posh/template"
"strings"
"time"
2019-03-13 04:14:30 -07:00
)
2022-01-26 23:38:46 -08:00
type Engine struct {
Config *Config
Env environment.Environment
Writer color.Writer
Ansi *color.Ansi
ConsoleTitle *console.Title
Plain bool
2021-04-20 12:30:46 -07:00
console strings.Builder
currentLineLength int
rprompt string
rpromptLength int
}
2022-01-26 23:38:46 -08:00
func (e *Engine) write(text string) {
2021-04-20 12:30:46 -07:00
e.console.WriteString(text)
}
2022-01-26 23:38:46 -08:00
func (e *Engine) writeANSI(text string) {
if e.Plain {
2021-11-02 06:53:46 -07:00
return
}
e.console.WriteString(text)
}
2022-01-26 23:38:46 -08:00
func (e *Engine) string() string {
2021-04-20 12:30:46 -07:00
return e.console.String()
}
2022-01-26 23:38:46 -08:00
func (e *Engine) canWriteRPrompt() bool {
consoleWidth, err := e.Env.TerminalWidth()
2021-05-25 09:31:23 -07:00
if err != nil || consoleWidth == 0 {
return true
}
promptWidth := e.currentLineLength
availableSpace := consoleWidth - promptWidth
// spanning multiple lines
if availableSpace < 0 {
overflow := promptWidth % consoleWidth
availableSpace = consoleWidth - overflow
}
promptBreathingRoom := 30
canWrite := (availableSpace - e.rpromptLength) >= promptBreathingRoom
return canWrite
}
2022-03-12 13:04:08 -08:00
func (e *Engine) PrintPrimary() string {
2022-01-26 23:38:46 -08:00
for _, block := range e.Config.Blocks {
e.renderBlock(block)
2019-03-13 04:14:30 -07:00
}
2022-03-14 11:14:10 -07:00
if len(e.Config.ConsoleTitleTemplate) > 0 {
2022-01-26 23:38:46 -08:00
e.writeANSI(e.ConsoleTitle.GetTitle())
2020-10-12 00:02:33 -07:00
}
2022-01-26 23:38:46 -08:00
e.writeANSI(e.Ansi.ColorReset())
if e.Config.FinalSpace {
2021-04-20 12:30:46 -07:00
e.write(" ")
2019-03-13 04:14:30 -07:00
}
2022-01-26 23:38:46 -08:00
if !e.Config.OSC99 {
return e.print()
2021-02-15 13:19:19 -08:00
}
2022-01-26 23:38:46 -08:00
cwd := e.Env.Pwd()
e.writeANSI(e.Ansi.ConsolePwd(cwd))
return e.print()
2020-12-17 23:59:45 -08:00
}
func (e *Engine) newline() {
e.write("\n")
e.currentLineLength = 0
}
2022-02-12 09:40:02 -08:00
func (e *Engine) shouldFill(block *Block, length int) (string, bool) {
if len(block.Filler) == 0 {
return "", false
}
terminalWidth, err := e.Env.TerminalWidth()
if err != nil && terminalWidth == 0 {
return "", false
}
padLength := terminalWidth - e.currentLineLength - length
if padLength <= 0 {
return "", false
}
e.Writer.Write("", "", block.Filler)
filler, lenFiller := e.Writer.String()
e.Writer.Reset()
if lenFiller == 0 {
return "", false
}
repeat := padLength / lenFiller
return strings.Repeat(filler, repeat), true
}
2022-01-26 23:38:46 -08:00
func (e *Engine) renderBlock(block *Block) {
// when in bash, for rprompt blocks we need to write plain
// and wrap in escaped mode or the prompt will not render correctly
2022-03-21 23:41:36 -07:00
if block.Type == RPrompt && e.Env.Shell() == shell.BASH {
2022-01-26 23:38:46 -08:00
block.initPlain(e.Env, e.Config)
} else {
2022-01-26 23:38:46 -08:00
block.init(e.Env, e.Writer, e.Ansi)
}
2022-02-02 03:16:39 -08:00
block.renderSegmentsText()
if !block.enabled() {
return
}
if block.Newline {
e.newline()
}
switch block.Type {
// This is deprecated but leave if to not break current configs
// It is encouraged to used "newline": true on block level
// rather than the standalone the linebreak block
case LineBreak:
e.newline()
case Prompt:
if block.VerticalOffset != 0 {
2022-01-26 23:38:46 -08:00
e.writeANSI(e.Ansi.ChangeLine(block.VerticalOffset))
}
switch block.Alignment {
case Right:
text, length := block.renderSegments()
2022-02-12 09:40:02 -08:00
if padText, OK := e.shouldFill(block, length); OK {
2022-02-04 08:22:40 -08:00
e.write(padText)
}
e.writeANSI(e.Ansi.CarriageForward())
e.writeANSI(e.Ansi.GetCursorForRightWrite(length, block.HorizontalOffset))
2022-02-04 08:22:40 -08:00
e.currentLineLength = 0
e.write(text)
case Left:
text, length := block.renderSegments()
e.currentLineLength += length
e.write(text)
}
case RPrompt:
text, length := block.renderSegments()
e.rpromptLength = length
2022-03-21 23:41:36 -07:00
if e.Env.Shell() == shell.BASH {
text = e.Ansi.FormatText(text)
}
e.rprompt = 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.
2022-03-21 23:41:36 -07:00
if e.Env.Shell() == shell.PWSH || e.Env.Shell() == shell.PWSH5 {
2022-01-26 23:38:46 -08:00
e.writeANSI(e.Ansi.ClearAfter())
}
}
// debug will loop through your config file and output the timings for each segments
2022-03-12 13:04:08 -08:00
func (e *Engine) PrintDebug(version string) string {
var segmentTimings []*SegmentTiming
largestSegmentNameLength := 0
2022-01-26 23:38:46 -08:00
e.write(fmt.Sprintf("\n\x1b[1mVersion:\x1b[0m %s\n", version))
e.write("\n\x1b[1mSegments:\x1b[0m\n\n")
2021-01-14 21:10:36 -08:00
// console title timing
start := time.Now()
2022-03-14 11:14:10 -07:00
title := e.ConsoleTitle.GetTitle()
title = strings.TrimPrefix(title, "\x1b]0;")
title = strings.TrimSuffix(title, "\a")
2021-01-14 21:10:36 -08:00
duration := time.Since(start)
segmentTiming := &SegmentTiming{
2022-02-02 03:16:39 -08:00
name: "ConsoleTitle",
nameLength: 12,
2022-03-14 11:14:10 -07:00
active: len(e.Config.ConsoleTitleTemplate) > 0,
text: title,
2022-02-02 03:16:39 -08:00
duration: duration,
2021-01-14 21:10:36 -08:00
}
segmentTimings = append(segmentTimings, segmentTiming)
// loop each segments of each blocks
2022-01-26 23:38:46 -08:00
for _, block := range e.Config.Blocks {
block.init(e.Env, e.Writer, e.Ansi)
longestSegmentName, timings := block.debug()
segmentTimings = append(segmentTimings, timings...)
if longestSegmentName > largestSegmentNameLength {
largestSegmentNameLength = longestSegmentName
}
}
2021-01-14 21:10:36 -08:00
// pad the output so the tabs render correctly
largestSegmentNameLength += 7
for _, segment := range segmentTimings {
2022-02-02 03:16:39 -08:00
duration := segment.duration.Milliseconds()
segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.active)
e.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.text))
}
2021-11-16 23:25:52 -08:00
e.write(fmt.Sprintf("\n\x1b[1mRun duration:\x1b[0m %s\n", time.Since(start)))
2022-01-26 23:38:46 -08:00
e.write(fmt.Sprintf("\n\x1b[1mCache path:\x1b[0m %s\n", e.Env.CachePath()))
e.write("\n\x1b[1mLogs:\x1b[0m\n\n")
2022-01-26 23:38:46 -08:00
e.write(e.Env.Logs())
2021-04-20 12:30:46 -07:00
return e.string()
}
2022-01-26 23:38:46 -08:00
func (e *Engine) print() string {
switch e.Env.Shell() {
2022-03-21 23:41:36 -07:00
case shell.ZSH:
2022-03-12 13:04:08 -08:00
if !e.Env.Flags().Eval {
break
2020-12-23 04:31:21 -08:00
}
// escape double quotes contained in the prompt
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), "\"", "\"\""))
prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt)
return prompt
2022-03-21 23:41:36 -07:00
case shell.PWSH, shell.PWSH5, shell.BASH, shell.PLAIN:
2022-01-26 23:38:46 -08:00
if e.rprompt == "" || !e.canWriteRPrompt() || e.Plain {
break
2020-12-17 23:59:45 -08:00
}
2022-01-26 23:38:46 -08:00
e.write(e.Ansi.SaveCursorPosition())
e.write(e.Ansi.CarriageForward())
e.write(e.Ansi.GetCursorForRightWrite(e.rpromptLength, 0))
e.write(e.rprompt)
2022-01-26 23:38:46 -08:00
e.write(e.Ansi.RestoreCursorPosition())
2020-12-15 05:58:15 -08:00
}
2021-04-20 12:30:46 -07:00
return e.string()
2019-03-13 04:14:30 -07:00
}
2021-06-05 07:14:44 -07:00
2022-03-12 13:04:08 -08:00
func (e *Engine) PrintTooltip(tip string) string {
2021-06-05 07:14:44 -07:00
tip = strings.Trim(tip, " ")
var tooltip *Segment
2022-01-26 23:38:46 -08:00
for _, tp := range e.Config.Tooltips {
2021-06-05 07:14:44 -07:00
if !tp.shouldInvokeWithTip(tip) {
continue
}
tooltip = tp
}
if tooltip == nil {
return ""
}
2022-01-26 23:38:46 -08:00
if err := tooltip.mapSegmentWithWriter(e.Env); err != nil {
2021-06-05 07:14:44 -07:00
return ""
}
2022-02-02 03:16:39 -08:00
if !tooltip.writer.Enabled() {
2021-06-05 07:14:44 -07:00
return ""
}
2022-02-02 03:16:39 -08:00
tooltip.text = tooltip.string()
tooltip.enabled = true
2021-06-05 07:14:44 -07:00
// little hack to reuse the current logic
block := &Block{
Alignment: Right,
Segments: []*Segment{tooltip},
}
2022-01-26 23:38:46 -08:00
switch e.Env.Shell() {
2022-03-22 11:53:37 -07:00
case shell.ZSH, shell.CMD, shell.FISH:
2022-01-26 23:38:46 -08:00
block.init(e.Env, e.Writer, e.Ansi)
text, _ := block.renderSegments()
return text
2022-03-21 23:41:36 -07:00
case shell.PWSH, shell.PWSH5:
2022-01-26 23:38:46 -08:00
block.initPlain(e.Env, e.Config)
text, length := block.renderSegments()
2022-01-26 23:38:46 -08:00
e.write(e.Ansi.ClearAfter())
e.write(e.Ansi.CarriageForward())
e.write(e.Ansi.GetCursorForRightWrite(length, 0))
e.write(text)
2021-06-05 07:14:44 -07:00
return e.string()
}
2022-03-22 11:53:37 -07:00
return " "
2021-06-05 07:14:44 -07:00
}
2021-06-15 12:23:08 -07:00
2022-02-19 07:49:26 -08:00
type ExtraPromptType int
const (
Transient ExtraPromptType = iota
Valid
Error
2022-02-20 04:56:28 -08:00
Secondary
2022-03-15 12:16:04 -07:00
Debug
2022-02-19 07:49:26 -08:00
)
2022-03-12 13:04:08 -08:00
func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
2022-02-19 07:49:26 -08:00
var prompt *ExtraPrompt
switch promptType {
2022-03-15 12:16:04 -07:00
case Debug:
prompt = e.Config.DebugPrompt
2022-02-19 07:49:26 -08:00
case Transient:
prompt = e.Config.TransientPrompt
case Valid:
prompt = e.Config.ValidLine
case Error:
prompt = e.Config.ErrorLine
2022-02-20 04:56:28 -08:00
case Secondary:
prompt = e.Config.SecondaryPrompt
2022-02-19 07:49:26 -08:00
}
if prompt == nil {
return ""
}
2022-02-19 07:49:26 -08:00
getTemplate := func(template string) string {
if len(template) != 0 {
return template
}
switch promptType { // nolint: exhaustive
2022-03-15 12:16:04 -07:00
case Debug:
return "[DBG]: "
2022-02-19 07:49:26 -08:00
case Transient:
return "{{ .Shell }}> "
2022-02-20 04:56:28 -08:00
case Secondary:
return "> "
2022-02-19 07:49:26 -08:00
default:
return ""
}
2021-06-15 12:23:08 -07:00
}
2022-01-26 06:54:36 -08:00
tmpl := &template.Text{
2022-02-19 07:49:26 -08:00
Template: getTemplate(prompt.Template),
2022-01-26 23:38:46 -08:00
Env: e.Env,
2021-06-15 12:23:08 -07:00
}
2022-02-19 07:49:26 -08:00
promptText, err := tmpl.Render()
if err != nil {
2022-02-19 07:49:26 -08:00
promptText = err.Error()
}
2022-02-19 07:49:26 -08:00
e.Writer.SetColors(prompt.Background, prompt.Foreground)
e.Writer.Write(prompt.Background, prompt.Foreground, promptText)
2022-01-26 23:38:46 -08:00
switch e.Env.Shell() {
2022-03-21 23:41:36 -07:00
case shell.ZSH:
2021-07-03 09:38:23 -07:00
// escape double quotes contained in the prompt
str, _ := e.Writer.String()
2022-02-20 04:56:28 -08:00
if promptType == Transient {
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(str, "\"", "\"\""))
// empty RPROMPT
prompt += "\nRPROMPT=\"\""
return prompt
}
return str
2022-03-23 02:32:54 -07:00
case shell.PWSH, shell.PWSH5, shell.CMD, shell.BASH, shell.FISH:
str, _ := e.Writer.String()
return str
}
2021-07-03 09:38:23 -07:00
return ""
2021-06-15 12:23:08 -07:00
}
2021-11-13 10:35:15 -08:00
2022-03-12 13:04:08 -08:00
func (e *Engine) PrintRPrompt() string {
2021-11-13 10:35:15 -08:00
filterRPromptBlock := func(blocks []*Block) *Block {
for _, block := range blocks {
if block.Type == RPrompt {
return block
}
}
return nil
}
2022-01-26 23:38:46 -08:00
block := filterRPromptBlock(e.Config.Blocks)
2021-11-13 10:35:15 -08:00
if block == nil {
return ""
}
2022-01-26 23:38:46 -08:00
block.init(e.Env, e.Writer, e.Ansi)
2022-02-02 03:16:39 -08:00
block.renderSegmentsText()
2021-11-13 10:35:15 -08:00
if !block.enabled() {
return ""
}
text, length := block.renderSegments()
e.rpromptLength = length
return text
2021-11-13 10:35:15 -08:00
}