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

View file

@ -4,7 +4,7 @@ title: Add Segment
sidebar_label: Add Segment
---
## Create the logic
## Create the logic (Writer)
Add a new file in the `./src/segments` folder: `new.go`.
Ensure `New` is a single verb indicating the context the segment renders.
@ -69,10 +69,10 @@ NEW SegmentType = "new"
## 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
NEW: &New{},
NEW: func() SegmentWriter { return &segments.New{} },
```
## Test your functionality