feat: secondary prompt

This commit is contained in:
Jan De Dobbeleer 2022-02-20 13:56:28 +01:00 committed by Jan De Dobbeleer
parent cb70ed4b41
commit dec5691435
12 changed files with 95 additions and 6 deletions

View file

@ -31,7 +31,7 @@ You need to extend or create a custom theme with your prompt overrides. For exam
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": {
...
}
},
"valid_line": {
"background": "transparent",
"foreground": "#ffffff",

View file

@ -0,0 +1,54 @@
---
id: config-secondary-prompt
title: Secondary prompt
sidebar_label: Secondary prompt
---
:::info
This feature only works in `powershell`, `zsh` and `bash` for the time being.
:::
The secondary prompt is displayed when a command text spans multiple lines. The default is `> `.
You can use go [text/template][go-text-template] templates extended with [sprig][sprig] to enrich the text.
Environment variables are available, just like the [`console_title_template`][console-title] functionality.
## Configuration
You need to extend or create a custom theme with your secondary prompt override. For example:
```json
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": {
...
},
"secondary_prompt": {
"background": "transparent",
"foreground": "#ffffff",
"template": "-> "
}
}
```
The configuration has the following properties:
- background: `string` [color][colors]
- foreground: `string` [color][colors]
- template: `string` - A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the
properties below - defaults to ` `
## Template ([info][templates])
- `.Root`: `boolean` - is the current user root/admin or not
- `.PWD`: `string` - the current working directory
- `.Folder`: `string` - the current working folder
- `.Shell`: `string` - the current shell name
- `.UserName`: `string` - the current user name
- `.HostName`: `string` - the host name
- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name
[go-text-template]: https://golang.org/pkg/text/template/
[sprig]: https://masterminds.github.io/sprig/
[console-title]: /docs/config-title#console-title-template
[templates]: /docs/config-templates

View file

@ -33,7 +33,7 @@ You need to extend or create a custom theme with your transient prompt. For exam
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": {
...
}
},
"transient_prompt": {
"background": "transparent",
"foreground": "#ffffff",

View file

@ -26,6 +26,7 @@ module.exports = {
"config-title",
"config-colors",
"config-templates",
"config-secondary-prompt",
"config-transient",
"config-line-error",
"config-tooltips",

View file

@ -42,6 +42,7 @@ type Config struct {
TransientPrompt *ExtraPrompt `json:"transient_prompt,omitempty"`
ValidLine *ExtraPrompt `json:"valid_line,omitempty"`
ErrorLine *ExtraPrompt `json:"error_line,omitempty"`
SecondaryPrompt *ExtraPrompt `json:"secondary_prompt,omitempty"`
Palette color.Palette `json:"palette,omitempty"`
format string

View file

@ -272,6 +272,7 @@ const (
Transient ExtraPromptType = iota
Valid
Error
Secondary
)
func (e *Engine) RenderExtraPrompt(promptType ExtraPromptType) string {
@ -283,6 +284,8 @@ func (e *Engine) RenderExtraPrompt(promptType ExtraPromptType) string {
prompt = e.Config.ValidLine
case Error:
prompt = e.Config.ErrorLine
case Secondary:
prompt = e.Config.SecondaryPrompt
}
if prompt == nil {
return ""
@ -294,6 +297,8 @@ func (e *Engine) RenderExtraPrompt(promptType ExtraPromptType) string {
switch promptType { // nolint: exhaustive
case Transient:
return "{{ .Shell }}> "
case Secondary:
return "> "
default:
return ""
}
@ -312,10 +317,14 @@ func (e *Engine) RenderExtraPrompt(promptType ExtraPromptType) string {
case zsh:
// escape double quotes contained in the prompt
str, _ := e.Writer.String()
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(str, "\"", "\"\""))
prompt += "\nRPROMPT=\"\""
return prompt
case pwsh, powershell5, winCMD:
if promptType == Transient {
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(str, "\"", "\"\""))
// empty RPROMPT
prompt += "\nRPROMPT=\"\""
return prompt
}
return str
case pwsh, powershell5, winCMD, bash:
str, _ := e.Writer.String()
return str
}

View file

@ -9,7 +9,10 @@ if [[ ! -d "/tmp" ]]; then
TIMER_START="${HOME}/.${USER}.start.$$"
fi
# start timer on command start
PS0='$(::OMP:: --millis > "$TIMER_START")'
# set secondary prompt
PS2="$(::OMP:: --config="$POSH_THEME" --shell=bash --print-secondary)"
function _omp_hook() {
local ret=$?

View file

@ -31,6 +31,10 @@ if (Test-Path $omp_config) {
Remove-Variable omp_value -Confirm:$false
Remove-Variable omp_config -Confirm:$false
# set secondary prompt
$secondaryPrompt = @(&"::OMP::" --config="$Env:POSH_THEME" --print-secondary 2>&1) -join "`n"
Set-PSReadLineOption -ContinuationPrompt $secondaryPrompt
function global:Set-PoshContext {}
function global:Get-PoshContext {

View file

@ -2,6 +2,9 @@ export POSH_THEME="::CONFIG::"
export POWERLINE_COMMAND="oh-my-posh"
export CONDA_PROMPT_MODIFIER=false
# set secondary prompt
PS2="$(::OMP:: --config="$POSH_THEME" --shell=zsh --print-secondary)"
function _omp-preexec() {
omp_start_time=$(::OMP:: --millis)
}

View file

@ -33,6 +33,7 @@ type Args struct {
PrintConfig *bool
PrintShell *bool
PrintTransient *bool
PrintSecondary *bool
PrintValid *bool
PrintError *bool
Config *string

View file

@ -141,6 +141,10 @@ func main() {
"print-error",
false,
"Print the failed prompt"),
PrintSecondary: flag.Bool(
"print-secondary",
false,
"Print the secondary prompt"),
}
flag.Parse()
if *args.Version {
@ -229,6 +233,10 @@ func main() {
fmt.Print(eng.RenderExtraPrompt(engine.Error))
return
}
if *args.PrintSecondary {
fmt.Print(eng.RenderExtraPrompt(engine.Secondary))
return
}
if len(*args.Command) != 0 {
fmt.Print(eng.RenderTooltip(*args.Command))
return

View file

@ -2051,6 +2051,11 @@
"title": "Error Prompt Setting",
"description": "https://ohmyposh.dev/docs/config-prompt-override"
},
"secondary_prompt": {
"$ref": "#/definitions/extra_prompt",
"title": "Secondary Prompt Setting",
"description": "https://ohmyposh.dev/docs/config-secondary-prompt"
},
"palette": {
"type": "object",
"title": "Palette",