feat(transient): add Filler

This commit is contained in:
Jan De Dobbeleer 2023-04-15 17:53:18 +02:00 committed by Jan De Dobbeleer
parent d1265c5ff3
commit cdc560c7bd
4 changed files with 28 additions and 16 deletions

View file

@ -130,8 +130,8 @@ func (e *Engine) isWarp() bool {
return e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal" return e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal"
} }
func (e *Engine) shouldFill(block *Block, length int) (string, bool) { func (e *Engine) shouldFill(filler string, length int) (string, bool) {
if len(block.Filler) == 0 { if len(filler) == 0 {
return "", false return "", false
} }
terminalWidth, err := e.Env.TerminalWidth() terminalWidth, err := e.Env.TerminalWidth()
@ -142,7 +142,7 @@ func (e *Engine) shouldFill(block *Block, length int) (string, bool) {
if padLength <= 0 { if padLength <= 0 {
return "", false return "", false
} }
e.Writer.Write("", "", block.Filler) e.Writer.Write("", "", filler)
filler, lenFiller := e.Writer.String() filler, lenFiller := e.Writer.String()
if lenFiller == 0 { if lenFiller == 0 {
return "", false return "", false
@ -228,14 +228,14 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
e.newline() e.newline()
case Hide: case Hide:
// make sure to fill if needed // make sure to fill if needed
if padText, OK := e.shouldFill(block, 0); OK { if padText, OK := e.shouldFill(block.Filler, 0); OK {
e.write(padText) e.write(padText)
} }
return return
} }
} }
if padText, OK := e.shouldFill(block, length); OK { if padText, OK := e.shouldFill(block.Filler, length); OK {
// in this case we can print plain // in this case we can print plain
e.write(padText) e.write(padText)
e.write(text) e.write(text)
@ -462,10 +462,15 @@ func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
background := prompt.BackgroundTemplates.FirstMatch(nil, e.Env, prompt.Background) background := prompt.BackgroundTemplates.FirstMatch(nil, e.Env, prompt.Background)
e.Writer.SetColors(background, foreground) e.Writer.SetColors(background, foreground)
e.Writer.Write(background, foreground, promptText) e.Writer.Write(background, foreground, promptText)
str, length := e.Writer.String()
if promptType == Transient {
if padText, OK := e.shouldFill(prompt.Filler, length); OK {
str += padText
}
}
switch e.Env.Shell() { switch e.Env.Shell() {
case shell.ZSH: case shell.ZSH:
// escape double quotes contained in the prompt // escape double quotes contained in the prompt
str, _ := e.Writer.String()
if promptType == Transient { if promptType == Transient {
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(str, "\"", "\"\"")) prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(str, "\"", "\"\""))
// empty RPROMPT // empty RPROMPT
@ -475,13 +480,11 @@ func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
return str return str
case shell.PWSH, shell.PWSH5: case shell.PWSH, shell.PWSH5:
// Return the string and empty our buffer // Return the string and empty our buffer
str, _ := e.Writer.String()
// clear the line afterwards to prevent text from being written on the same line // clear the line afterwards to prevent text from being written on the same line
// see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3628 // see https://github.com/JanDeDobbeleer/oh-my-posh/issues/3628
return str + e.Writer.ClearAfter() return str + e.Writer.ClearAfter()
case shell.CMD, shell.BASH, shell.FISH, shell.NU, shell.GENERIC: case shell.CMD, shell.BASH, shell.FISH, shell.NU, shell.GENERIC:
// Return the string and empty our buffer // Return the string and empty our buffer
str, _ := e.Writer.String()
return str return str
} }
return "" return ""

View file

@ -39,6 +39,7 @@ type Segment struct {
Alias string `json:"alias,omitempty"` Alias string `json:"alias,omitempty"`
MaxWidth int `json:"max_width,omitempty"` MaxWidth int `json:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty"` MinWidth int `json:"min_width,omitempty"`
Filler string `json:"filler,omitempty"`
Enabled bool `json:"-"` Enabled bool `json:"-"`

View file

@ -3168,7 +3168,14 @@
"transient_prompt": { "transient_prompt": {
"$ref": "#/definitions/extra_prompt", "$ref": "#/definitions/extra_prompt",
"title": "Transient Prompt Setting", "title": "Transient Prompt Setting",
"description": "https://ohmyposh.dev/docs/configuration/transient" "description": "https://ohmyposh.dev/docs/configuration/transient",
"properties": {
"filler": {
"type": "string",
"title": "Filler",
"description": "Right aligned filler text, will span the remaining width"
}
}
}, },
"valid_line": { "valid_line": {
"$ref": "#/definitions/extra_prompt", "$ref": "#/definitions/extra_prompt",

View file

@ -44,13 +44,14 @@ You need to extend or create a custom theme with your transient prompt. For exam
## Properties ## Properties
| Name | Type | Description | | Name | Type | Description |
| ---------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | ---------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `foreground` | `string` | [color][colors] | | `foreground` | `string` | [color][colors] |
| `foreground_templates` | `array` | [color templates][color-templates] | | `foreground_templates` | `array` | [color templates][color-templates] |
| `background` | `string` | [color][colors] | | `background` | `string` | [color][colors] |
| `background_templates` | `array` | [color templates][color-templates] | | `background_templates` | `array` | [color templates][color-templates] |
| `template` | `string` | a go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the properties below - defaults to `{{ .Shell }}> ` | | `template` | `string` | a go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the properties below - defaults to `{{ .Shell }}> ` |
| `filler` | `string` | when you want to create a line with a repeated set of characters spanning the width of the terminal. Will be added _after_ the `template` text |
## Enable the feature ## Enable the feature