From edc02ddb4894581c8303c172e9aac444897f6dee Mon Sep 17 00:00:00 2001 From: maxlandon Date: Sat, 17 Dec 2022 22:30:23 +0100 Subject: [PATCH] refactor(engine): new instantiation function this commit adds a function to instantiate a new prompt engine to be used within a Go application (typically a readline shell) --- src/cli/print.go | 65 ++++++++++----------------------------- src/color/plain_writer.go | 5 +-- src/color/writer.go | 10 +++--- src/engine/block.go | 2 -- src/engine/config.go | 9 +++--- src/engine/engine.go | 16 ++++++---- src/engine/new.go | 55 +++++++++++++++++++++++++++++++++ src/platform/shell.go | 1 + 8 files changed, 92 insertions(+), 71 deletions(-) create mode 100644 src/engine/new.go diff --git a/src/cli/print.go b/src/cli/print.go index 10ad3b2e..7725e706 100644 --- a/src/cli/print.go +++ b/src/cli/print.go @@ -2,11 +2,8 @@ package cli import ( "fmt" - "oh-my-posh/color" - "oh-my-posh/console" "oh-my-posh/engine" "oh-my-posh/platform" - "oh-my-posh/shell" "github.com/spf13/cobra" ) @@ -46,53 +43,23 @@ var printCmd = &cobra.Command{ _ = cmd.Help() return } - env := &platform.Shell{ - Version: cliVersion, - CmdFlags: &platform.Flags{ - Config: config, - PWD: pwd, - PSWD: pswd, - ErrorCode: exitCode, - ExecutionTime: timing, - StackCount: stackCount, - TerminalWidth: terminalWidth, - Eval: eval, - Shell: shellName, - ShellVersion: shellVersion, - }, - } - env.Init() - defer env.Close() - cfg := engine.LoadConfig(env) - ansi := &color.Ansi{} - ansi.Init(env.Shell()) - var writer color.Writer - if plain { - ansi.InitPlain() - writer = &color.PlainWriter{ - Ansi: ansi, - } - } else { - writerColors := cfg.MakeColors() - writer = &color.AnsiWriter{ - Ansi: ansi, - TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground), - AnsiColors: writerColors, - } - } - consoleTitle := &console.Title{ - Env: env, - Ansi: ansi, - Template: cfg.ConsoleTitleTemplate, - } - eng := &engine.Engine{ - Config: cfg, - Env: env, - Writer: writer, - ConsoleTitle: consoleTitle, - Ansi: ansi, - Plain: plain, + + flags := &platform.Flags{ + Config: config, + PWD: pwd, + PSWD: pswd, + ErrorCode: exitCode, + ExecutionTime: timing, + StackCount: stackCount, + TerminalWidth: terminalWidth, + Eval: eval, + Shell: shellName, + ShellVersion: shellVersion, + Plain: plain, } + + eng := engine.New(flags) + switch args[0] { case "debug": fmt.Print(eng.PrintExtraPrompt(engine.Debug)) diff --git a/src/color/plain_writer.go b/src/color/plain_writer.go index 6718eca0..e6090f14 100644 --- a/src/color/plain_writer.go +++ b/src/color/plain_writer.go @@ -39,9 +39,6 @@ func (a *PlainWriter) Write(background, foreground, text string) { } func (a *PlainWriter) String() (string, int) { + defer a.builder.Reset() return a.builder.String(), a.length } - -func (a *PlainWriter) Reset() { - a.builder.Reset() -} diff --git a/src/color/writer.go b/src/color/writer.go index 163ae719..92428699 100644 --- a/src/color/writer.go +++ b/src/color/writer.go @@ -13,7 +13,6 @@ const ( type Writer interface { Write(background, foreground, text string) String() (string, int) - Reset() SetColors(background, foreground string) SetParentColors(background, foreground string) ClearParentColors() @@ -238,10 +237,9 @@ func (a *AnsiWriter) expandKeyword(keyword string) string { } func (a *AnsiWriter) String() (string, int) { + defer func() { + a.length = 0 + a.builder.Reset() + }() return a.builder.String(), a.length } - -func (a *AnsiWriter) Reset() { - a.length = 0 - a.builder.Reset() -} diff --git a/src/engine/block.go b/src/engine/block.go index dab34afa..8f2c3d01 100644 --- a/src/engine/block.go +++ b/src/engine/block.go @@ -129,7 +129,6 @@ func (b *Block) setSegmentsText() { } func (b *Block) RenderSegments() (string, int) { - defer b.writer.Reset() for _, segment := range b.Segments { if !segment.Enabled && segment.Style != Accordion { continue @@ -222,7 +221,6 @@ func (b *Block) Debug() (int, []*SegmentTiming) { b.setActiveSegment(segment) b.renderActiveSegment() segmentTiming.text, _ = b.writer.String() - b.writer.Reset() } segmentTiming.duration = time.Since(start) segmentTimings = append(segmentTimings, &segmentTiming) diff --git a/src/engine/config.go b/src/engine/config.go index 776f8fe8..3fa1f3f5 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -5,15 +5,16 @@ import ( json2 "encoding/json" "fmt" "io" + "os" + "path/filepath" + "strings" + "time" + "oh-my-posh/color" "oh-my-posh/platform" "oh-my-posh/properties" "oh-my-posh/segments" "oh-my-posh/template" - "os" - "path/filepath" - "strings" - "time" "github.com/gookit/config/v2" "github.com/gookit/config/v2/json" diff --git a/src/engine/engine.go b/src/engine/engine.go index 8a4b425c..630cef6b 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -2,13 +2,14 @@ package engine import ( "fmt" + "strings" + "time" + "oh-my-posh/color" "oh-my-posh/console" "oh-my-posh/platform" "oh-my-posh/shell" "oh-my-posh/template" - "strings" - "time" ) type Engine struct { @@ -37,7 +38,9 @@ func (e *Engine) writeANSI(text string) { } func (e *Engine) string() string { - return e.console.String() + text := e.console.String() + e.console.Reset() + return text } func (e *Engine) canWriteRPrompt(rprompt bool) bool { @@ -130,7 +133,6 @@ func (e *Engine) shouldFill(block *Block, length int) (string, bool) { } e.Writer.Write("", "", block.Filler) filler, lenFiller := e.Writer.String() - e.Writer.Reset() if lenFiller == 0 { return "", false } @@ -314,6 +316,7 @@ func (e *Engine) print() string { prompt = e.Ansi.FormatText(prompt) e.write(prompt) } + return e.string() } @@ -342,7 +345,7 @@ func (e *Engine) PrintTooltip(tip string) string { Segments: []*Segment{tooltip}, } switch e.Env.Shell() { - case shell.ZSH, shell.CMD, shell.FISH: + case shell.ZSH, shell.CMD, shell.FISH, shell.PLAIN: block.Init(e.Env, e.Writer, e.Ansi) if !block.Enabled() { return "" @@ -431,7 +434,8 @@ func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string { return prompt } return str - case shell.PWSH, shell.PWSH5, shell.CMD, shell.BASH, shell.FISH, shell.NU: + case shell.PWSH, shell.PWSH5, shell.CMD, shell.BASH, shell.FISH, shell.NU, shell.PLAIN: + // Return the string and empty our buffer str, _ := e.Writer.String() return str } diff --git a/src/engine/new.go b/src/engine/new.go new file mode 100644 index 00000000..a3ecf28d --- /dev/null +++ b/src/engine/new.go @@ -0,0 +1,55 @@ +package engine + +import ( + "oh-my-posh/color" + "oh-my-posh/console" + "oh-my-posh/platform" + "oh-my-posh/shell" +) + +// New returns a prompt engine initialized with the +// given configuration options, and is ready to print any +// of the prompt components. +func New(flags *platform.Flags) *Engine { + env := &platform.Shell{ + CmdFlags: flags, + } + + env.Init() + defer env.Close() + cfg := LoadConfig(env) + ansi := &color.Ansi{} + ansi.Init(env.Shell()) + + var writer color.Writer + if flags.Plain { + ansi.InitPlain() + writer = &color.PlainWriter{ + Ansi: ansi, + } + } else { + writerColors := cfg.MakeColors() + writer = &color.AnsiWriter{ + Ansi: ansi, + TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground), + AnsiColors: writerColors, + } + } + + consoleTitle := &console.Title{ + Env: env, + Ansi: ansi, + Template: cfg.ConsoleTitleTemplate, + } + + eng := &Engine{ + Config: cfg, + Env: env, + Writer: writer, + ConsoleTitle: consoleTitle, + Ansi: ansi, + Plain: flags.Plain, + } + + return eng +} diff --git a/src/platform/shell.go b/src/platform/shell.go index 93cc5984..35a7d0fd 100644 --- a/src/platform/shell.go +++ b/src/platform/shell.go @@ -62,6 +62,7 @@ type Flags struct { Strict bool Debug bool Manual bool + Plain bool } type CommandError struct {