Skip to content

Commit

Permalink
refactor: change powerline config to segment
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 5, 2020
1 parent 9a50814 commit 1d8e7b2
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 58 deletions.
7 changes: 6 additions & 1 deletion Themes/agnoster.json
Expand Up @@ -3,11 +3,11 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "root",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#ffe9aa",
"properties": {
Expand All @@ -17,6 +17,7 @@
{
"type": "session",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#ffffff",
"properties": {
Expand All @@ -26,6 +27,7 @@
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#91ddff",
"properties" : {
Expand All @@ -39,6 +41,7 @@
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#95ffa4",
"properties": {
Expand All @@ -54,6 +57,7 @@
{
"type": "virtualenv",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#906cff",
"properties": {
Expand All @@ -63,6 +67,7 @@
{
"type": "exit",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#ff8080",
"properties": {
Expand Down
4 changes: 3 additions & 1 deletion Themes/agnosterplus.json
Expand Up @@ -21,11 +21,11 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "session",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#ffffff",
"properties": {
Expand All @@ -36,6 +36,7 @@
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#91ddff",
"properties" : {
Expand All @@ -49,6 +50,7 @@
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#95ffa4",
"properties": {
Expand Down
3 changes: 2 additions & 1 deletion Themes/aliens.json
Expand Up @@ -3,7 +3,6 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "session",
Expand All @@ -20,6 +19,7 @@
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#C678DD",
"properties": {
Expand All @@ -30,6 +30,7 @@
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#95ffa4",
"properties": {
Expand Down
1 change: 0 additions & 1 deletion Themes/fish.json
Expand Up @@ -3,7 +3,6 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "exit",
Expand Down
6 changes: 5 additions & 1 deletion Themes/jandedobbeleer.json
Expand Up @@ -3,7 +3,6 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "session",
Expand All @@ -20,6 +19,7 @@
{
"type": "spotify",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#1BD760",
"properties": {
Expand All @@ -31,6 +31,7 @@
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#61AFEF",
"properties": {
Expand All @@ -41,6 +42,7 @@
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#ffeb3b",
"properties": {
Expand All @@ -56,6 +58,7 @@
{
"type": "battery",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#ffeb3b",
"properties": {
Expand All @@ -73,6 +76,7 @@
{
"type": "shell",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#0077c2",
"properties": {
Expand Down
7 changes: 6 additions & 1 deletion Themes/paradox.json
Expand Up @@ -3,11 +3,11 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "root",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#ffe9aa",
"properties": {
Expand All @@ -17,6 +17,7 @@
{
"type": "session",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#ffffff",
"properties": {
Expand All @@ -26,6 +27,7 @@
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#91ddff",
"properties": {
Expand All @@ -39,6 +41,7 @@
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#95ffa4",
"properties": {
Expand All @@ -54,6 +57,7 @@
{
"type": "virtualenv",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#906cff",
"properties": {
Expand All @@ -63,6 +67,7 @@
{
"type": "exit",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#ff8080",
"properties": {
Expand Down
1 change: 0 additions & 1 deletion Themes/powerlevel10k_lean.json
Expand Up @@ -21,7 +21,6 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "path",
Expand Down
5 changes: 4 additions & 1 deletion Themes/powerline.json
Expand Up @@ -3,7 +3,6 @@
{
"type": "prompt",
"alignment": "left",
"powerline_separator": "",
"segments": [
{
"type": "root",
Expand All @@ -24,6 +23,7 @@
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#91ddff",
"properties" : {
Expand All @@ -34,6 +34,7 @@
{
"type": "git",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#193549",
"background": "#95ffa4",
"properties": {
Expand All @@ -49,6 +50,7 @@
{
"type": "virtualenv",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#100e23",
"background": "#906cff",
"properties": {
Expand All @@ -58,6 +60,7 @@
{
"type": "exit",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#ff8080",
"properties": {
Expand Down
19 changes: 12 additions & 7 deletions engine.go
Expand Up @@ -26,25 +26,29 @@ 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() {
if e.activeSegment != nil &&
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)
}

Expand Down Expand Up @@ -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()
}

Expand Down
11 changes: 10 additions & 1 deletion main.go
Expand Up @@ -11,6 +11,7 @@ type args struct {
ErrorCode *int
PrintConfig *bool
Config *string
Shell *string
}

func main() {
Expand All @@ -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{
Expand All @@ -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,
Expand Down
18 changes: 10 additions & 8 deletions segment.go
Expand Up @@ -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
Expand Down

0 comments on commit 1d8e7b2

Please sign in to comment.