refactor: change powerline config to segment

This commit is contained in:
Jan De Dobbeleer 2020-09-26 13:41:28 +02:00 committed by Jan De Dobbeleer
parent 9a508142fc
commit 1d8e7b2458
12 changed files with 95 additions and 58 deletions

View file

@ -3,11 +3,11 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "root", "type": "root",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#ffe9aa", "background": "#ffe9aa",
"properties": { "properties": {
@ -17,6 +17,7 @@
{ {
"type": "session", "type": "session",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#ffffff", "background": "#ffffff",
"properties": { "properties": {
@ -26,6 +27,7 @@
{ {
"type": "path", "type": "path",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#91ddff", "background": "#91ddff",
"properties" : { "properties" : {
@ -39,6 +41,7 @@
{ {
"type": "git", "type": "git",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#95ffa4", "background": "#95ffa4",
"properties": { "properties": {
@ -54,6 +57,7 @@
{ {
"type": "virtualenv", "type": "virtualenv",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#906cff", "background": "#906cff",
"properties": { "properties": {
@ -63,6 +67,7 @@
{ {
"type": "exit", "type": "exit",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#ff8080", "background": "#ff8080",
"properties": { "properties": {

View file

@ -21,11 +21,11 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "session", "type": "session",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#ffffff", "background": "#ffffff",
"properties": { "properties": {
@ -36,6 +36,7 @@
{ {
"type": "path", "type": "path",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#91ddff", "background": "#91ddff",
"properties" : { "properties" : {
@ -49,6 +50,7 @@
{ {
"type": "git", "type": "git",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#95ffa4", "background": "#95ffa4",
"properties": { "properties": {

View file

@ -3,7 +3,6 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "session", "type": "session",
@ -20,6 +19,7 @@
{ {
"type": "path", "type": "path",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#C678DD", "background": "#C678DD",
"properties": { "properties": {
@ -30,6 +30,7 @@
{ {
"type": "git", "type": "git",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#95ffa4", "background": "#95ffa4",
"properties": { "properties": {

View file

@ -3,7 +3,6 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "exit", "type": "exit",

View file

@ -3,7 +3,6 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "session", "type": "session",
@ -20,6 +19,7 @@
{ {
"type": "spotify", "type": "spotify",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#1BD760", "background": "#1BD760",
"properties": { "properties": {
@ -31,6 +31,7 @@
{ {
"type": "path", "type": "path",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#61AFEF", "background": "#61AFEF",
"properties": { "properties": {
@ -41,6 +42,7 @@
{ {
"type": "git", "type": "git",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#ffeb3b", "background": "#ffeb3b",
"properties": { "properties": {
@ -56,6 +58,7 @@
{ {
"type": "battery", "type": "battery",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#ffeb3b", "background": "#ffeb3b",
"properties": { "properties": {
@ -73,6 +76,7 @@
{ {
"type": "shell", "type": "shell",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#0077c2", "background": "#0077c2",
"properties": { "properties": {

View file

@ -3,11 +3,11 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "root", "type": "root",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#ffe9aa", "background": "#ffe9aa",
"properties": { "properties": {
@ -17,6 +17,7 @@
{ {
"type": "session", "type": "session",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#ffffff", "background": "#ffffff",
"properties": { "properties": {
@ -26,6 +27,7 @@
{ {
"type": "path", "type": "path",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#91ddff", "background": "#91ddff",
"properties": { "properties": {
@ -39,6 +41,7 @@
{ {
"type": "git", "type": "git",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#95ffa4", "background": "#95ffa4",
"properties": { "properties": {
@ -54,6 +57,7 @@
{ {
"type": "virtualenv", "type": "virtualenv",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#906cff", "background": "#906cff",
"properties": { "properties": {
@ -63,6 +67,7 @@
{ {
"type": "exit", "type": "exit",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#ff8080", "background": "#ff8080",
"properties": { "properties": {

View file

@ -21,7 +21,6 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "path", "type": "path",

View file

@ -3,7 +3,6 @@
{ {
"type": "prompt", "type": "prompt",
"alignment": "left", "alignment": "left",
"powerline_separator": "",
"segments": [ "segments": [
{ {
"type": "root", "type": "root",
@ -24,6 +23,7 @@
{ {
"type": "path", "type": "path",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#91ddff", "background": "#91ddff",
"properties" : { "properties" : {
@ -34,6 +34,7 @@
{ {
"type": "git", "type": "git",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#193549", "foreground": "#193549",
"background": "#95ffa4", "background": "#95ffa4",
"properties": { "properties": {
@ -49,6 +50,7 @@
{ {
"type": "virtualenv", "type": "virtualenv",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23", "foreground": "#100e23",
"background": "#906cff", "background": "#906cff",
"properties": { "properties": {
@ -58,6 +60,7 @@
{ {
"type": "exit", "type": "exit",
"style": "powerline", "style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff", "foreground": "#ffffff",
"background": "#ff8080", "background": "#ff8080",
"properties": { "properties": {

View file

@ -26,12 +26,16 @@ func (e *engine) getPowerlineColor(foreground bool) string {
return e.previousActiveSegment.Background return e.previousActiveSegment.Background
} }
func (e *engine) writePowerLineSeparator(background string, foreground string) { func (e *engine) writePowerLineSeparator(background string, foreground string, end bool) {
if e.activeBlock.InvertPowerlineSeparatorColor { symbol := e.activeSegment.PowerlineSymbol
e.renderer.write(foreground, background, e.activeBlock.PowerlineSeparator) if end {
symbol = e.previousActiveSegment.PowerlineSymbol
}
if e.activeSegment.InvertPowerlineSymbolColor {
e.renderer.write(foreground, background, symbol)
return return
} }
e.renderer.write(background, foreground, e.activeBlock.PowerlineSeparator) e.renderer.write(background, foreground, symbol)
} }
func (e *engine) endPowerline() { func (e *engine) endPowerline() {
@ -39,12 +43,12 @@ func (e *engine) endPowerline() {
e.activeSegment.Style != Powerline && e.activeSegment.Style != Powerline &&
e.previousActiveSegment != nil && e.previousActiveSegment != nil &&
e.previousActiveSegment.Style == Powerline { 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) { 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) e.renderText(text)
} }
@ -99,8 +103,9 @@ func (e *engine) renderBlockSegments(block *Block) string {
e.renderSegmentText(text) e.renderSegmentText(text)
} }
if e.previousActiveSegment != nil && e.previousActiveSegment.Style == Powerline { 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() return e.renderer.string()
} }

11
main.go
View file

@ -11,6 +11,7 @@ type args struct {
ErrorCode *int ErrorCode *int
PrintConfig *bool PrintConfig *bool
Config *string Config *string
Shell *string
} }
func main() { func main() {
@ -27,6 +28,10 @@ func main() {
"config", "config",
"", "",
"Add the path to a configuration you wish to load"), "Add the path to a configuration you wish to load"),
Shell: flag.String(
"shell",
"",
"Override the shell you are working in"),
} }
flag.Parse() flag.Parse()
env := &environment{ env := &environment{
@ -41,7 +46,11 @@ func main() {
colorWriter := &Renderer{ colorWriter := &Renderer{
Buffer: new(bytes.Buffer), Buffer: new(bytes.Buffer),
} }
colorWriter.init(env.getShellName()) shell := env.getShellName()
if *args.Shell != "" {
shell = *args.Shell
}
colorWriter.init(shell)
engine := &engine{ engine := &engine{
settings: settings, settings: settings,
env: env, env: env,

View file

@ -4,14 +4,16 @@ import "errors"
//Segment represent a single segment and it's configuration //Segment represent a single segment and it's configuration
type Segment struct { type Segment struct {
Type SegmentType `json:"type"` Type SegmentType `json:"type"`
Style SegmentStyle `json:"style"` Style SegmentStyle `json:"style"`
Foreground string `json:"foreground"` PowerlineSymbol string `json:"powerline_symbol"`
Background string `json:"background"` InvertPowerlineSymbolColor bool `json:"invert_powerline_symbol_color"`
LeadingDiamond string `json:"leading_diamond"` Foreground string `json:"foreground"`
TrailingDiamond string `json:"trailing_diamond"` Background string `json:"background"`
Properties map[Property]interface{} `json:"properties"` LeadingDiamond string `json:"leading_diamond"`
writer SegmentWriter 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 //SegmentWriter is the interface used to define what and if to write to the prompt

View file

@ -31,13 +31,11 @@ const (
//Block defines a part of the prompt with optional segments //Block defines a part of the prompt with optional segments
type Block struct { type Block struct {
Type BlockType `json:"type"` Type BlockType `json:"type"`
Alignment BlockAlignment `json:"alignment"` Alignment BlockAlignment `json:"alignment"`
PowerlineSeparator string `json:"powerline_separator"` HorizontalOffset int `json:"horizontal_offset"`
InvertPowerlineSeparatorColor bool `json:"invert_powerline_separator_color"` VerticalOffset int `json:"vertical_offset"`
HorizontalOffset int `json:"horizontal_offset"` Segments []*Segment `json:"segments"`
VerticalOffset int `json:"vertical_offset"`
Segments []*Segment `json:"segments"`
} }
//GetSettings returns the default configuration including possible user overrides //GetSettings returns the default configuration including possible user overrides
@ -72,45 +70,50 @@ func getDefaultSettings() *Settings {
EndSpaceEnabled: true, EndSpaceEnabled: true,
Blocks: []*Block{ Blocks: []*Block{
{ {
Type: Prompt, Type: Prompt,
Alignment: Left, Alignment: Left,
PowerlineSeparator: "",
Segments: []*Segment{ Segments: []*Segment{
{ {
Type: Root, Type: Root,
Style: Powerline, Style: Powerline,
Background: "#ffe9aa", PowerlineSymbol: "",
Foreground: "#100e23", Background: "#ffe9aa",
Foreground: "#100e23",
}, },
{ {
Type: Session, Type: Session,
Style: Powerline, Style: Powerline,
Background: "#ffffff", PowerlineSymbol: "",
Foreground: "#100e23", Background: "#ffffff",
Foreground: "#100e23",
}, },
{ {
Type: Path, Type: Path,
Style: Powerline, Style: Powerline,
Background: "#91ddff", PowerlineSymbol: "",
Foreground: "#100e23", Background: "#91ddff",
Foreground: "#100e23",
}, },
{ {
Type: Git, Type: Git,
Style: Powerline, Style: Powerline,
Background: "#95ffa4", PowerlineSymbol: "",
Foreground: "#100e23", Background: "#95ffa4",
Foreground: "#100e23",
}, },
{ {
Type: Venv, Type: Venv,
Style: Powerline, Style: Powerline,
Background: "#906cff", PowerlineSymbol: "",
Foreground: "#100e23", Background: "#906cff",
Foreground: "#100e23",
}, },
{ {
Type: Exit, Type: Exit,
Style: Powerline, Style: Powerline,
Background: "#ff8080", PowerlineSymbol: "",
Foreground: "#ffffff", Background: "#ff8080",
Foreground: "#ffffff",
}, },
}, },
}, },