fix(engine): always return a new writer for init

resolves #3268
This commit is contained in:
Jan De Dobbeleer 2022-12-29 21:27:22 +01:00 committed by Jan De Dobbeleer
parent 198b2c8193
commit cf4aa4ed62
2 changed files with 71 additions and 70 deletions

View file

@ -228,72 +228,72 @@ const (
// Segments contains all available prompt segment writers. // Segments contains all available prompt segment writers.
// Consumers of the library can also add their own segment writer. // Consumers of the library can also add their own segment writer.
var Segments = map[SegmentType]SegmentWriter{ var Segments = map[SegmentType]func() SegmentWriter{
ANGULAR: &segments.Angular{}, ANGULAR: func() SegmentWriter { return &segments.Angular{} },
AWS: &segments.Aws{}, AWS: func() SegmentWriter { return &segments.Aws{} },
AZ: &segments.Az{}, AZ: func() SegmentWriter { return &segments.Az{} },
AZFUNC: &segments.AzFunc{}, AZFUNC: func() SegmentWriter { return &segments.AzFunc{} },
BATTERY: &segments.Battery{}, BATTERY: func() SegmentWriter { return &segments.Battery{} },
BREWFATHER: &segments.Brewfather{}, BREWFATHER: func() SegmentWriter { return &segments.Brewfather{} },
CDS: &segments.Cds{}, CDS: func() SegmentWriter { return &segments.Cds{} },
CF: &segments.Cf{}, CF: func() SegmentWriter { return &segments.Cf{} },
CFTARGET: &segments.CfTarget{}, CFTARGET: func() SegmentWriter { return &segments.CfTarget{} },
CMD: &segments.Cmd{}, CMD: func() SegmentWriter { return &segments.Cmd{} },
CONNECTION: &segments.Connection{}, CONNECTION: func() SegmentWriter { return &segments.Connection{} },
CRYSTAL: &segments.Crystal{}, CRYSTAL: func() SegmentWriter { return &segments.Crystal{} },
CMAKE: &segments.Cmake{}, CMAKE: func() SegmentWriter { return &segments.Cmake{} },
DART: &segments.Dart{}, DART: func() SegmentWriter { return &segments.Dart{} },
DENO: &segments.Deno{}, DENO: func() SegmentWriter { return &segments.Deno{} },
DOTNET: &segments.Dotnet{}, DOTNET: func() SegmentWriter { return &segments.Dotnet{} },
EXECUTIONTIME: &segments.Executiontime{}, EXECUTIONTIME: func() SegmentWriter { return &segments.Executiontime{} },
EXIT: &segments.Exit{}, EXIT: func() SegmentWriter { return &segments.Exit{} },
FLUTTER: &segments.Flutter{}, FLUTTER: func() SegmentWriter { return &segments.Flutter{} },
FOSSIL: &segments.Fossil{}, FOSSIL: func() SegmentWriter { return &segments.Fossil{} },
GCP: &segments.Gcp{}, GCP: func() SegmentWriter { return &segments.Gcp{} },
GIT: &segments.Git{}, GIT: func() SegmentWriter { return &segments.Git{} },
GITVERSION: &segments.GitVersion{}, GITVERSION: func() SegmentWriter { return &segments.GitVersion{} },
GOLANG: &segments.Golang{}, GOLANG: func() SegmentWriter { return &segments.Golang{} },
HASKELL: &segments.Haskell{}, HASKELL: func() SegmentWriter { return &segments.Haskell{} },
IPIFY: &segments.IPify{}, IPIFY: func() SegmentWriter { return &segments.IPify{} },
ITERM: &segments.ITerm{}, ITERM: func() SegmentWriter { return &segments.ITerm{} },
JAVA: &segments.Java{}, JAVA: func() SegmentWriter { return &segments.Java{} },
JULIA: &segments.Julia{}, JULIA: func() SegmentWriter { return &segments.Julia{} },
KOTLIN: &segments.Kotlin{}, KOTLIN: func() SegmentWriter { return &segments.Kotlin{} },
KUBECTL: &segments.Kubectl{}, KUBECTL: func() SegmentWriter { return &segments.Kubectl{} },
LUA: &segments.Lua{}, LUA: func() SegmentWriter { return &segments.Lua{} },
NBGV: &segments.Nbgv{}, NBGV: func() SegmentWriter { return &segments.Nbgv{} },
NIGHTSCOUT: &segments.Nightscout{}, NIGHTSCOUT: func() SegmentWriter { return &segments.Nightscout{} },
NODE: &segments.Node{}, NODE: func() SegmentWriter { return &segments.Node{} },
NPM: &segments.Npm{}, NPM: func() SegmentWriter { return &segments.Npm{} },
NX: &segments.Nx{}, NX: func() SegmentWriter { return &segments.Nx{} },
OS: &segments.Os{}, OS: func() SegmentWriter { return &segments.Os{} },
OWM: &segments.Owm{}, OWM: func() SegmentWriter { return &segments.Owm{} },
PATH: &segments.Path{}, PATH: func() SegmentWriter { return &segments.Path{} },
PERL: &segments.Perl{}, PERL: func() SegmentWriter { return &segments.Perl{} },
PHP: &segments.Php{}, PHP: func() SegmentWriter { return &segments.Php{} },
PLASTIC: &segments.Plastic{}, PLASTIC: func() SegmentWriter { return &segments.Plastic{} },
PROJECT: &segments.Project{}, PROJECT: func() SegmentWriter { return &segments.Project{} },
PYTHON: &segments.Python{}, PYTHON: func() SegmentWriter { return &segments.Python{} },
R: &segments.R{}, R: func() SegmentWriter { return &segments.R{} },
ROOT: &segments.Root{}, ROOT: func() SegmentWriter { return &segments.Root{} },
RUBY: &segments.Ruby{}, RUBY: func() SegmentWriter { return &segments.Ruby{} },
RUST: &segments.Rust{}, RUST: func() SegmentWriter { return &segments.Rust{} },
SESSION: &segments.Session{}, SESSION: func() SegmentWriter { return &segments.Session{} },
SHELL: &segments.Shell{}, SHELL: func() SegmentWriter { return &segments.Shell{} },
SPOTIFY: &segments.Spotify{}, SPOTIFY: func() SegmentWriter { return &segments.Spotify{} },
STRAVA: &segments.Strava{}, STRAVA: func() SegmentWriter { return &segments.Strava{} },
SVN: &segments.Svn{}, SVN: func() SegmentWriter { return &segments.Svn{} },
SWIFT: &segments.Swift{}, SWIFT: func() SegmentWriter { return &segments.Swift{} },
SYSTEMINFO: &segments.SystemInfo{}, SYSTEMINFO: func() SegmentWriter { return &segments.SystemInfo{} },
TERRAFORM: &segments.Terraform{}, TERRAFORM: func() SegmentWriter { return &segments.Terraform{} },
TEXT: &segments.Text{}, TEXT: func() SegmentWriter { return &segments.Text{} },
TIME: &segments.Time{}, TIME: func() SegmentWriter { return &segments.Time{} },
UI5TOOLING: &segments.UI5Tooling{}, UI5TOOLING: func() SegmentWriter { return &segments.UI5Tooling{} },
WAKATIME: &segments.Wakatime{}, WAKATIME: func() SegmentWriter { return &segments.Wakatime{} },
WINREG: &segments.WindowsRegistry{}, WINREG: func() SegmentWriter { return &segments.WindowsRegistry{} },
WITHINGS: &segments.Withings{}, WITHINGS: func() SegmentWriter { return &segments.Withings{} },
XMAKE: &segments.XMake{}, XMAKE: func() SegmentWriter { return &segments.XMake{} },
YTM: &segments.Ytm{}, YTM: func() SegmentWriter { return &segments.Ytm{} },
} }
func (segment *Segment) style() SegmentStyle { func (segment *Segment) style() SegmentStyle {
@ -374,7 +374,8 @@ func (segment *Segment) mapSegmentWithWriter(env platform.Environment) error {
segment.Properties = make(properties.Map) segment.Properties = make(properties.Map)
} }
if writer, ok := Segments[segment.Type]; ok { if f, ok := Segments[segment.Type]; ok {
writer := f()
writer.Init(segment.Properties, env) writer.Init(segment.Properties, env)
segment.writer = writer segment.writer = writer
return nil return nil

View file

@ -4,7 +4,7 @@ title: Add Segment
sidebar_label: Add Segment sidebar_label: Add Segment
--- ---
## Create the logic ## Create the logic (Writer)
Add a new file in the `./src/segments` folder: `new.go`. Add a new file in the `./src/segments` folder: `new.go`.
Ensure `New` is a single verb indicating the context the segment renders. Ensure `New` is a single verb indicating the context the segment renders.
@ -69,10 +69,10 @@ NEW SegmentType = "new"
## Add the SegmentType mapping ## Add the SegmentType mapping
Map your `SegmentType` to your Segment in the `mapSegmentWithWriter` function (respect the alphabetical order). Add your `SegmentType` and `Writer` to the `Segments` map (respect the alphabetical order).
```go ```go
NEW: &New{}, NEW: func() SegmentWriter { return &segments.New{} },
``` ```
## Test your functionality ## Test your functionality