mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-31 13:57:26 -08:00
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)
This commit is contained in:
parent
24ac34c95a
commit
edc02ddb48
|
@ -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))
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
55
src/engine/new.go
Normal file
55
src/engine/new.go
Normal file
|
@ -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
|
||||
}
|
|
@ -62,6 +62,7 @@ type Flags struct {
|
|||
Strict bool
|
||||
Debug bool
|
||||
Manual bool
|
||||
Plain bool
|
||||
}
|
||||
|
||||
type CommandError struct {
|
||||
|
|
Loading…
Reference in a new issue