feat: colorise filler

This commit is contained in:
Jan De Dobbeleer 2022-02-12 18:40:02 +01:00 committed by Jan De Dobbeleer
parent d3d370309f
commit a0d4fc6cba
3 changed files with 29 additions and 19 deletions

2
.vscode/launch.json vendored
View file

@ -10,7 +10,7 @@
"args": [ "args": [
"--config=${workspaceRoot}/themes/jandedobbeleer.omp.json", "--config=${workspaceRoot}/themes/jandedobbeleer.omp.json",
"--shell=pwsh", "--shell=pwsh",
"--pwd=/Users/jan/Projects/test" "--terminal-width=200",
] ]
}, },
{ {

View file

@ -46,8 +46,9 @@ Tell the engine if the block should be left or right-aligned.
### Filler ### Filler
When you want to join a right and left aligned block with a repeated set of characters, add the character When you want to join a right and left aligned block with a repeated set of characters, add the character(s)
to be repeated to this property. Add this property to the _right_ aligned block. to be repeated to this property. Add this property to the _right_ aligned block. This supports the use of
[color overrides][color-overrides].
```json ```json
"alignment": "right", "alignment": "right",
@ -57,3 +58,5 @@ to be repeated to this property. Add this property to the _right_ aligned block.
### Segments ### Segments
Array of one or more segments. Array of one or more segments.
[color-overrides]: /docs/config-colors#color-overrides

View file

@ -8,7 +8,6 @@ import (
"oh-my-posh/template" "oh-my-posh/template"
"strings" "strings"
"time" "time"
"unicode/utf8"
) )
type Engine struct { type Engine struct {
@ -81,6 +80,28 @@ func (e *Engine) newline() {
e.currentLineLength = 0 e.currentLineLength = 0
} }
func (e *Engine) shouldFill(block *Block, length int) (string, bool) {
if len(block.Filler) == 0 {
return "", false
}
terminalWidth, err := e.Env.TerminalWidth()
if err != nil && terminalWidth == 0 {
return "", false
}
padLength := terminalWidth - e.currentLineLength - length
if padLength <= 0 {
return "", false
}
e.Writer.Write("", "", block.Filler)
filler, lenFiller := e.Writer.String()
e.Writer.Reset()
if lenFiller == 0 {
return "", false
}
repeat := padLength / lenFiller
return strings.Repeat(filler, repeat), true
}
func (e *Engine) renderBlock(block *Block) { func (e *Engine) renderBlock(block *Block) {
// when in bash, for rprompt blocks we need to write plain // when in bash, for rprompt blocks we need to write plain
// and wrap in escaped mode or the prompt will not render correctly // and wrap in escaped mode or the prompt will not render correctly
@ -93,20 +114,6 @@ 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()
} }
@ -123,7 +130,7 @@ func (e *Engine) renderBlock(block *Block) {
switch block.Alignment { switch block.Alignment {
case Right: case Right:
text, length := block.renderSegments() text, length := block.renderSegments()
if padText, OK := shouldFill(length); OK { if padText, OK := e.shouldFill(block, length); OK {
e.write(padText) e.write(padText)
} }
e.writeANSI(e.Ansi.CarriageForward()) e.writeANSI(e.Ansi.CarriageForward())