mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-05 16:27: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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"oh-my-posh/color"
|
|
||||||
"oh-my-posh/console"
|
|
||||||
"oh-my-posh/engine"
|
"oh-my-posh/engine"
|
||||||
"oh-my-posh/platform"
|
"oh-my-posh/platform"
|
||||||
"oh-my-posh/shell"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -46,9 +43,8 @@ var printCmd = &cobra.Command{
|
||||||
_ = cmd.Help()
|
_ = cmd.Help()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
env := &platform.Shell{
|
|
||||||
Version: cliVersion,
|
flags := &platform.Flags{
|
||||||
CmdFlags: &platform.Flags{
|
|
||||||
Config: config,
|
Config: config,
|
||||||
PWD: pwd,
|
PWD: pwd,
|
||||||
PSWD: pswd,
|
PSWD: pswd,
|
||||||
|
@ -59,40 +55,11 @@ var printCmd = &cobra.Command{
|
||||||
Eval: eval,
|
Eval: eval,
|
||||||
Shell: shellName,
|
Shell: shellName,
|
||||||
ShellVersion: shellVersion,
|
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,
|
Plain: plain,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eng := engine.New(flags)
|
||||||
|
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
case "debug":
|
case "debug":
|
||||||
fmt.Print(eng.PrintExtraPrompt(engine.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) {
|
func (a *PlainWriter) String() (string, int) {
|
||||||
|
defer a.builder.Reset()
|
||||||
return a.builder.String(), a.length
|
return a.builder.String(), a.length
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *PlainWriter) Reset() {
|
|
||||||
a.builder.Reset()
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ const (
|
||||||
type Writer interface {
|
type Writer interface {
|
||||||
Write(background, foreground, text string)
|
Write(background, foreground, text string)
|
||||||
String() (string, int)
|
String() (string, int)
|
||||||
Reset()
|
|
||||||
SetColors(background, foreground string)
|
SetColors(background, foreground string)
|
||||||
SetParentColors(background, foreground string)
|
SetParentColors(background, foreground string)
|
||||||
ClearParentColors()
|
ClearParentColors()
|
||||||
|
@ -238,10 +237,9 @@ func (a *AnsiWriter) expandKeyword(keyword string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AnsiWriter) String() (string, int) {
|
func (a *AnsiWriter) String() (string, int) {
|
||||||
return a.builder.String(), a.length
|
defer func() {
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AnsiWriter) Reset() {
|
|
||||||
a.length = 0
|
a.length = 0
|
||||||
a.builder.Reset()
|
a.builder.Reset()
|
||||||
|
}()
|
||||||
|
return a.builder.String(), a.length
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,7 +129,6 @@ func (b *Block) setSegmentsText() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) RenderSegments() (string, int) {
|
func (b *Block) RenderSegments() (string, int) {
|
||||||
defer b.writer.Reset()
|
|
||||||
for _, segment := range b.Segments {
|
for _, segment := range b.Segments {
|
||||||
if !segment.Enabled && segment.Style != Accordion {
|
if !segment.Enabled && segment.Style != Accordion {
|
||||||
continue
|
continue
|
||||||
|
@ -222,7 +221,6 @@ func (b *Block) Debug() (int, []*SegmentTiming) {
|
||||||
b.setActiveSegment(segment)
|
b.setActiveSegment(segment)
|
||||||
b.renderActiveSegment()
|
b.renderActiveSegment()
|
||||||
segmentTiming.text, _ = b.writer.String()
|
segmentTiming.text, _ = b.writer.String()
|
||||||
b.writer.Reset()
|
|
||||||
}
|
}
|
||||||
segmentTiming.duration = time.Since(start)
|
segmentTiming.duration = time.Since(start)
|
||||||
segmentTimings = append(segmentTimings, &segmentTiming)
|
segmentTimings = append(segmentTimings, &segmentTiming)
|
||||||
|
|
|
@ -5,15 +5,16 @@ import (
|
||||||
json2 "encoding/json"
|
json2 "encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
"oh-my-posh/platform"
|
"oh-my-posh/platform"
|
||||||
"oh-my-posh/properties"
|
"oh-my-posh/properties"
|
||||||
"oh-my-posh/segments"
|
"oh-my-posh/segments"
|
||||||
"oh-my-posh/template"
|
"oh-my-posh/template"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gookit/config/v2"
|
"github.com/gookit/config/v2"
|
||||||
"github.com/gookit/config/v2/json"
|
"github.com/gookit/config/v2/json"
|
||||||
|
|
|
@ -2,13 +2,14 @@ package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
"oh-my-posh/console"
|
"oh-my-posh/console"
|
||||||
"oh-my-posh/platform"
|
"oh-my-posh/platform"
|
||||||
"oh-my-posh/shell"
|
"oh-my-posh/shell"
|
||||||
"oh-my-posh/template"
|
"oh-my-posh/template"
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
|
@ -37,7 +38,9 @@ func (e *Engine) writeANSI(text string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) string() 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 {
|
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)
|
e.Writer.Write("", "", block.Filler)
|
||||||
filler, lenFiller := e.Writer.String()
|
filler, lenFiller := e.Writer.String()
|
||||||
e.Writer.Reset()
|
|
||||||
if lenFiller == 0 {
|
if lenFiller == 0 {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
@ -314,6 +316,7 @@ func (e *Engine) print() string {
|
||||||
prompt = e.Ansi.FormatText(prompt)
|
prompt = e.Ansi.FormatText(prompt)
|
||||||
e.write(prompt)
|
e.write(prompt)
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.string()
|
return e.string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,7 +345,7 @@ func (e *Engine) PrintTooltip(tip string) string {
|
||||||
Segments: []*Segment{tooltip},
|
Segments: []*Segment{tooltip},
|
||||||
}
|
}
|
||||||
switch e.Env.Shell() {
|
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)
|
block.Init(e.Env, e.Writer, e.Ansi)
|
||||||
if !block.Enabled() {
|
if !block.Enabled() {
|
||||||
return ""
|
return ""
|
||||||
|
@ -431,7 +434,8 @@ func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
|
||||||
return prompt
|
return prompt
|
||||||
}
|
}
|
||||||
return str
|
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()
|
str, _ := e.Writer.String()
|
||||||
return str
|
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
|
Strict bool
|
||||||
Debug bool
|
Debug bool
|
||||||
Manual bool
|
Manual bool
|
||||||
|
Plain bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandError struct {
|
type CommandError struct {
|
||||||
|
|
Loading…
Reference in a new issue