From aba4093abad9a6a4887c8c4c141a33596bc92a54 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 4 Feb 2022 17:22:40 +0100 Subject: [PATCH] feat: filler between blocks --- docs/docs/config-block.md | 17 +++++++-------- src/engine/block.go | 1 + src/engine/engine.go | 22 +++++++++++++++++-- themes/atomic.omp.json | 46 +++++++++++++++++++-------------------- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/docs/docs/config-block.md b/docs/docs/config-block.md index adc946df..d0f3ff98 100644 --- a/docs/docs/config-block.md +++ b/docs/docs/config-block.md @@ -25,8 +25,7 @@ Let's take a closer look at what defines a block. - type: `prompt` | `rprompt` - newline: `boolean` - alignment: `left` | `right` -- vertical_offset: `int` -- horizontal_offset: `int` +- filler: `string` - segments: `array` of one or more `segments` ### Type @@ -45,15 +44,15 @@ Start the block on a new line - defaults to `false`. Tell the engine if the block should be left or right-aligned. -### Vertical offset +### Filler -Move the block up or down x lines. For example, `vertical_offset: 1` moves the prompt down one line, `vertical_offset: -1` -moves it up one line. +When you want to join a right and left aligned block with a repeated set of characters, add the character +to be repeated to this property. Add this property to the _left_ aligned block. -### Horizontal offset - -Moves the segment to the left or the right to have it exactly where you want it to be. Works like `vertical_offset` -but on a horizontal level where a negative number moves the block left and a positive number right. +```json +"alignment": "left", +"filler": "." +``` ### Segments diff --git a/src/engine/block.go b/src/engine/block.go index 5ee591f8..c610efda 100644 --- a/src/engine/block.go +++ b/src/engine/block.go @@ -34,6 +34,7 @@ type Block struct { VerticalOffset int `json:"vertical_offset,omitempty"` Segments []*Segment `json:"segments,omitempty"` Newline bool `json:"newline,omitempty"` + Filler string `json:"filler,omitempty"` env environment.Environment writer color.Writer diff --git a/src/engine/engine.go b/src/engine/engine.go index 36e7439c..71cfb568 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -8,6 +8,7 @@ import ( "oh-my-posh/template" "strings" "time" + "unicode/utf8" ) type Engine struct { @@ -92,6 +93,20 @@ func (e *Engine) renderBlock(block *Block) { if !block.enabled() { return } + shouldFill := func(blockLength int) (string, bool) { + if len(block.Filler) == 0 { + return "", false + } + if terminalWidth, err := e.Env.TerminalWidth(); err == nil && terminalWidth > 0 { + padLength := terminalWidth - e.currentLineLength - blockLength + var filler string + for utf8.RuneCountInString(filler) < padLength { + filler += block.Filler + } + return filler, true + } + return "", false + } if block.Newline { e.newline() } @@ -107,10 +122,13 @@ func (e *Engine) renderBlock(block *Block) { } switch block.Alignment { case Right: - e.writeANSI(e.Ansi.CarriageForward()) text, length := block.renderSegments() - e.currentLineLength += length + if padText, OK := shouldFill(length); OK { + e.write(padText) + } + e.writeANSI(e.Ansi.CarriageForward()) e.writeANSI(e.Ansi.GetCursorForRightWrite(length, block.HorizontalOffset)) + e.currentLineLength = 0 e.write(text) case Left: text, length := block.renderSegments() diff --git a/themes/atomic.omp.json b/themes/atomic.omp.json index f8701281..919108ca 100644 --- a/themes/atomic.omp.json +++ b/themes/atomic.omp.json @@ -76,7 +76,7 @@ { "background": "#303030", "foreground": "#3C873A", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "fetch_package_manager": true, "npm_icon": " <#cc3a3a>\ue5fa ", @@ -84,62 +84,62 @@ "yarn_icon": " <#348cba>\uf61a" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "node" }, { "background": "#306998", "foreground": "#FFE873", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}{{ .Venv }} {{ end }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "python" }, { "background": "#0e8ac8", "foreground": "#ec2729", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ue738 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "java" }, { "background": "#0e0e0e", "foreground": "#0d6da8", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ue77f {{ if .Unsupported }}\uf071{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "dotnet" }, { "background": "#ffffff", "foreground": "#06aad5", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ufcd1 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "go" }, { "background": "#f3f0ec", "foreground": "#925837", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ue7a8 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "rust" }, { @@ -150,7 +150,7 @@ "template": "\ue798 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "dart" }, { @@ -161,7 +161,7 @@ "template": "\ue753 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "angular" }, { @@ -172,46 +172,46 @@ "template": "<#ca3c34>\ue624 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "julia" }, { "background": "#ffffff", "foreground": "#9c1006", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ue791 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "ruby" }, { "background": "#ffffff", "foreground": "#5398c2", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\uf104<#f5bf45>\uf0e7\uf105 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "azfunc" }, { "background": "#565656", "foreground": "#faa029", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ue7ad {{.Profile}}{{if .Region}}@{{.Region}}{{end}}" }, "style": "diamond", - "trailing_diamond": "\ue0b4", + "trailing_diamond": "\ue0b4 ", "type": "aws" }, { "background": "#316ce4", "foreground": "#ffffff", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "template": "\ufd31 {{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}" }, @@ -222,7 +222,7 @@ { "background": "#b2bec3", "foreground": "#222222", - "leading_diamond": " \ue0b6", + "leading_diamond": "\ue0b6", "properties": { "linux": "\ue27f", "macos": "\ue27f",