From 1d8e7b2458845a376fa7c122e9ede309a65e4c33 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 26 Sep 2020 13:41:28 +0200 Subject: [PATCH] refactor: change powerline config to segment --- Themes/agnoster.json | 7 +++- Themes/agnosterplus.json | 4 +- Themes/aliens.json | 3 +- Themes/fish.json | 1 - Themes/jandedobbeleer.json | 6 ++- Themes/paradox.json | 7 +++- Themes/powerlevel10k_lean.json | 1 - Themes/powerline.json | 5 ++- engine.go | 19 +++++---- main.go | 11 +++++- segment.go | 18 +++++---- settings.go | 71 ++++++++++++++++++---------------- 12 files changed, 95 insertions(+), 58 deletions(-) diff --git a/Themes/agnoster.json b/Themes/agnoster.json index 15ae2816..5dc605e7 100644 --- a/Themes/agnoster.json +++ b/Themes/agnoster.json @@ -3,11 +3,11 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "root", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#ffe9aa", "properties": { @@ -17,6 +17,7 @@ { "type": "session", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#ffffff", "properties": { @@ -26,6 +27,7 @@ { "type": "path", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#91ddff", "properties" : { @@ -39,6 +41,7 @@ { "type": "git", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#95ffa4", "properties": { @@ -54,6 +57,7 @@ { "type": "virtualenv", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#906cff", "properties": { @@ -63,6 +67,7 @@ { "type": "exit", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#ff8080", "properties": { diff --git a/Themes/agnosterplus.json b/Themes/agnosterplus.json index 81cebf07..3b7ea0e3 100644 --- a/Themes/agnosterplus.json +++ b/Themes/agnosterplus.json @@ -21,11 +21,11 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "session", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#ffffff", "properties": { @@ -36,6 +36,7 @@ { "type": "path", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#91ddff", "properties" : { @@ -49,6 +50,7 @@ { "type": "git", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#95ffa4", "properties": { diff --git a/Themes/aliens.json b/Themes/aliens.json index 952755f4..9ba734c6 100644 --- a/Themes/aliens.json +++ b/Themes/aliens.json @@ -3,7 +3,6 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "session", @@ -20,6 +19,7 @@ { "type": "path", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#C678DD", "properties": { @@ -30,6 +30,7 @@ { "type": "git", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#95ffa4", "properties": { diff --git a/Themes/fish.json b/Themes/fish.json index fb03dafe..fb0ec455 100644 --- a/Themes/fish.json +++ b/Themes/fish.json @@ -3,7 +3,6 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "exit", diff --git a/Themes/jandedobbeleer.json b/Themes/jandedobbeleer.json index 9e8cbd4f..33293213 100644 --- a/Themes/jandedobbeleer.json +++ b/Themes/jandedobbeleer.json @@ -3,7 +3,6 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "session", @@ -20,6 +19,7 @@ { "type": "spotify", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#1BD760", "properties": { @@ -31,6 +31,7 @@ { "type": "path", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#61AFEF", "properties": { @@ -41,6 +42,7 @@ { "type": "git", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#ffeb3b", "properties": { @@ -56,6 +58,7 @@ { "type": "battery", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#ffeb3b", "properties": { @@ -73,6 +76,7 @@ { "type": "shell", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#0077c2", "properties": { diff --git a/Themes/paradox.json b/Themes/paradox.json index 375799f3..46399888 100644 --- a/Themes/paradox.json +++ b/Themes/paradox.json @@ -3,11 +3,11 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "root", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#ffe9aa", "properties": { @@ -17,6 +17,7 @@ { "type": "session", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#ffffff", "properties": { @@ -26,6 +27,7 @@ { "type": "path", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#91ddff", "properties": { @@ -39,6 +41,7 @@ { "type": "git", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#95ffa4", "properties": { @@ -54,6 +57,7 @@ { "type": "virtualenv", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#906cff", "properties": { @@ -63,6 +67,7 @@ { "type": "exit", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#ff8080", "properties": { diff --git a/Themes/powerlevel10k_lean.json b/Themes/powerlevel10k_lean.json index 3e546a9d..6fd3f666 100644 --- a/Themes/powerlevel10k_lean.json +++ b/Themes/powerlevel10k_lean.json @@ -21,7 +21,6 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "path", diff --git a/Themes/powerline.json b/Themes/powerline.json index 0f7848ad..7a93b3a9 100644 --- a/Themes/powerline.json +++ b/Themes/powerline.json @@ -3,7 +3,6 @@ { "type": "prompt", "alignment": "left", - "powerline_separator": "", "segments": [ { "type": "root", @@ -24,6 +23,7 @@ { "type": "path", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#91ddff", "properties" : { @@ -34,6 +34,7 @@ { "type": "git", "style": "powerline", + "powerline_symbol": "", "foreground": "#193549", "background": "#95ffa4", "properties": { @@ -49,6 +50,7 @@ { "type": "virtualenv", "style": "powerline", + "powerline_symbol": "", "foreground": "#100e23", "background": "#906cff", "properties": { @@ -58,6 +60,7 @@ { "type": "exit", "style": "powerline", + "powerline_symbol": "", "foreground": "#ffffff", "background": "#ff8080", "properties": { diff --git a/engine.go b/engine.go index 15e53811..f025d94d 100644 --- a/engine.go +++ b/engine.go @@ -26,12 +26,16 @@ func (e *engine) getPowerlineColor(foreground bool) string { return e.previousActiveSegment.Background } -func (e *engine) writePowerLineSeparator(background string, foreground string) { - if e.activeBlock.InvertPowerlineSeparatorColor { - e.renderer.write(foreground, background, e.activeBlock.PowerlineSeparator) +func (e *engine) writePowerLineSeparator(background string, foreground string, end bool) { + symbol := e.activeSegment.PowerlineSymbol + if end { + symbol = e.previousActiveSegment.PowerlineSymbol + } + if e.activeSegment.InvertPowerlineSymbolColor { + e.renderer.write(foreground, background, symbol) return } - e.renderer.write(background, foreground, e.activeBlock.PowerlineSeparator) + e.renderer.write(background, foreground, symbol) } func (e *engine) endPowerline() { @@ -39,12 +43,12 @@ func (e *engine) endPowerline() { e.activeSegment.Style != Powerline && e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline { - e.writePowerLineSeparator(e.getPowerlineColor(false), e.previousActiveSegment.Background) + e.writePowerLineSeparator(e.getPowerlineColor(false), e.previousActiveSegment.Background, true) } } func (e *engine) renderPowerLineSegment(text string) { - e.writePowerLineSeparator(e.activeSegment.Background, e.getPowerlineColor(true)) + e.writePowerLineSeparator(e.activeSegment.Background, e.getPowerlineColor(true), false) e.renderText(text) } @@ -99,8 +103,9 @@ func (e *engine) renderBlockSegments(block *Block) string { e.renderSegmentText(text) } if e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline { - e.writePowerLineSeparator(Transparent, e.previousActiveSegment.Background) + e.writePowerLineSeparator(Transparent, e.previousActiveSegment.Background, true) } + // e.endPowerline() return e.renderer.string() } diff --git a/main.go b/main.go index e00aaf3b..0560ffdf 100755 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ type args struct { ErrorCode *int PrintConfig *bool Config *string + Shell *string } func main() { @@ -27,6 +28,10 @@ func main() { "config", "", "Add the path to a configuration you wish to load"), + Shell: flag.String( + "shell", + "", + "Override the shell you are working in"), } flag.Parse() env := &environment{ @@ -41,7 +46,11 @@ func main() { colorWriter := &Renderer{ Buffer: new(bytes.Buffer), } - colorWriter.init(env.getShellName()) + shell := env.getShellName() + if *args.Shell != "" { + shell = *args.Shell + } + colorWriter.init(shell) engine := &engine{ settings: settings, env: env, diff --git a/segment.go b/segment.go index 414bc72a..12ae0cbc 100644 --- a/segment.go +++ b/segment.go @@ -4,14 +4,16 @@ import "errors" //Segment represent a single segment and it's configuration type Segment struct { - Type SegmentType `json:"type"` - Style SegmentStyle `json:"style"` - Foreground string `json:"foreground"` - Background string `json:"background"` - LeadingDiamond string `json:"leading_diamond"` - TrailingDiamond string `json:"trailing_diamond"` - Properties map[Property]interface{} `json:"properties"` - writer SegmentWriter + Type SegmentType `json:"type"` + Style SegmentStyle `json:"style"` + PowerlineSymbol string `json:"powerline_symbol"` + InvertPowerlineSymbolColor bool `json:"invert_powerline_symbol_color"` + Foreground string `json:"foreground"` + Background string `json:"background"` + LeadingDiamond string `json:"leading_diamond"` + TrailingDiamond string `json:"trailing_diamond"` + Properties map[Property]interface{} `json:"properties"` + writer SegmentWriter } //SegmentWriter is the interface used to define what and if to write to the prompt diff --git a/settings.go b/settings.go index 06788c72..959cb0e6 100755 --- a/settings.go +++ b/settings.go @@ -31,13 +31,11 @@ const ( //Block defines a part of the prompt with optional segments type Block struct { - Type BlockType `json:"type"` - Alignment BlockAlignment `json:"alignment"` - PowerlineSeparator string `json:"powerline_separator"` - InvertPowerlineSeparatorColor bool `json:"invert_powerline_separator_color"` - HorizontalOffset int `json:"horizontal_offset"` - VerticalOffset int `json:"vertical_offset"` - Segments []*Segment `json:"segments"` + Type BlockType `json:"type"` + Alignment BlockAlignment `json:"alignment"` + HorizontalOffset int `json:"horizontal_offset"` + VerticalOffset int `json:"vertical_offset"` + Segments []*Segment `json:"segments"` } //GetSettings returns the default configuration including possible user overrides @@ -72,45 +70,50 @@ func getDefaultSettings() *Settings { EndSpaceEnabled: true, Blocks: []*Block{ { - Type: Prompt, - Alignment: Left, - PowerlineSeparator: "", + Type: Prompt, + Alignment: Left, Segments: []*Segment{ { - Type: Root, - Style: Powerline, - Background: "#ffe9aa", - Foreground: "#100e23", + Type: Root, + Style: Powerline, + PowerlineSymbol: "", + Background: "#ffe9aa", + Foreground: "#100e23", }, { - Type: Session, - Style: Powerline, - Background: "#ffffff", - Foreground: "#100e23", + Type: Session, + Style: Powerline, + PowerlineSymbol: "", + Background: "#ffffff", + Foreground: "#100e23", }, { - Type: Path, - Style: Powerline, - Background: "#91ddff", - Foreground: "#100e23", + Type: Path, + Style: Powerline, + PowerlineSymbol: "", + Background: "#91ddff", + Foreground: "#100e23", }, { - Type: Git, - Style: Powerline, - Background: "#95ffa4", - Foreground: "#100e23", + Type: Git, + Style: Powerline, + PowerlineSymbol: "", + Background: "#95ffa4", + Foreground: "#100e23", }, { - Type: Venv, - Style: Powerline, - Background: "#906cff", - Foreground: "#100e23", + Type: Venv, + Style: Powerline, + PowerlineSymbol: "", + Background: "#906cff", + Foreground: "#100e23", }, { - Type: Exit, - Style: Powerline, - Background: "#ff8080", - Foreground: "#ffffff", + Type: Exit, + Style: Powerline, + PowerlineSymbol: "", + Background: "#ff8080", + Foreground: "#ffffff", }, }, },