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`
- 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

View file

@ -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

View file

@ -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()