feat: filler between blocks

This commit is contained in:
Jan De Dobbeleer 2022-02-04 17:22:40 +01:00 committed by Jan De Dobbeleer
parent 04e6579a8e
commit aba4093aba
4 changed files with 52 additions and 34 deletions

View file

@ -25,8 +25,7 @@ Let's take a closer look at what defines a block.
- type: `prompt` | `rprompt` - type: `prompt` | `rprompt`
- newline: `boolean` - newline: `boolean`
- alignment: `left` | `right` - alignment: `left` | `right`
- vertical_offset: `int` - filler: `string`
- horizontal_offset: `int`
- segments: `array` of one or more `segments` - segments: `array` of one or more `segments`
### Type ### 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. 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` When you want to join a right and left aligned block with a repeated set of characters, add the character
moves it up one line. to be repeated to this property. Add this property to the _left_ aligned block.
### Horizontal offset ```json
"alignment": "left",
Moves the segment to the left or the right to have it exactly where you want it to be. Works like `vertical_offset` "filler": "."
but on a horizontal level where a negative number moves the block left and a positive number right. ```
### Segments ### Segments

View file

@ -34,6 +34,7 @@ type Block struct {
VerticalOffset int `json:"vertical_offset,omitempty"` VerticalOffset int `json:"vertical_offset,omitempty"`
Segments []*Segment `json:"segments,omitempty"` Segments []*Segment `json:"segments,omitempty"`
Newline bool `json:"newline,omitempty"` Newline bool `json:"newline,omitempty"`
Filler string `json:"filler,omitempty"`
env environment.Environment env environment.Environment
writer color.Writer writer color.Writer

View file

@ -8,6 +8,7 @@ import (
"oh-my-posh/template" "oh-my-posh/template"
"strings" "strings"
"time" "time"
"unicode/utf8"
) )
type Engine struct { type Engine struct {
@ -92,6 +93,20 @@ func (e *Engine) renderBlock(block *Block) {
if !block.enabled() { if !block.enabled() {
return 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 { if block.Newline {
e.newline() e.newline()
} }
@ -107,10 +122,13 @@ func (e *Engine) renderBlock(block *Block) {
} }
switch block.Alignment { switch block.Alignment {
case Right: case Right:
e.writeANSI(e.Ansi.CarriageForward())
text, length := block.renderSegments() 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.writeANSI(e.Ansi.GetCursorForRightWrite(length, block.HorizontalOffset))
e.currentLineLength = 0
e.write(text) e.write(text)
case Left: case Left:
text, length := block.renderSegments() text, length := block.renderSegments()