feat(transient): allow setting a newline

This commit is contained in:
Jan De Dobbeleer 2024-07-05 16:04:04 +02:00 committed by Jan De Dobbeleer
parent 67b7e6a959
commit 8b6933160a
5 changed files with 35 additions and 22 deletions

View file

@ -55,7 +55,7 @@ type Segment struct {
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
Background color.Ansi `json:"background" toml:"background"`
Foreground color.Ansi `json:"foreground" toml:"foreground"`
// color.Set
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
Enabled bool `json:"-" toml:"-"`

View file

@ -119,26 +119,28 @@ func (e *Engine) pwd() {
e.write(terminal.Pwd(pwdType, user, host, cwd))
}
func (e *Engine) newline() {
defer func() {
e.currentLineLength = 0
}()
func (e *Engine) getNewline() string {
// WARP terminal will remove \n from the prompt, so we hack a newline in
if e.isWarp() {
e.write(terminal.LineBreak())
return
return terminal.LineBreak()
}
// TCSH needs a space before the LITERAL newline character or it will not render correctly
// don't ask why, it be like that sometimes.
// https://unix.stackexchange.com/questions/99101/properly-defining-a-multi-line-prompt-in-tcsh#comment1342462_322189
if e.Env.Shell() == shell.TCSH {
e.write(` \n`)
return
return ` \n`
}
e.write("\n")
return "\n"
}
func (e *Engine) writeNewline() {
defer func() {
e.currentLineLength = 0
}()
e.write(e.getNewline())
}
func (e *Engine) isWarp() bool {
@ -192,7 +194,7 @@ func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
// when we're printin the first primary prompt in
// the shell
if !cancelNewline {
e.newline()
e.writeNewline()
}
return false
}
@ -213,7 +215,7 @@ func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
// when we're printin the first primary prompt in
// the shell
if block.Newline && !cancelNewline {
e.newline()
e.writeNewline()
}
text, length := e.renderBlockSegments(block)
@ -244,7 +246,7 @@ func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
if !OK {
switch block.Overflow {
case config.Break:
e.newline()
e.writeNewline()
case config.Hide:
// make sure to fill if needed
if padText, OK := e.shouldFill(block.Filler, space, 0); OK {

View file

@ -58,6 +58,10 @@ func (e *Engine) ExtraPrompt(promptType ExtraPromptType) string {
promptText = err.Error()
}
if promptType == Transient && prompt.Newline {
promptText = fmt.Sprintf("%s%s", e.getNewline(), promptText)
}
if promptType == Transient && e.Config.ShellIntegration {
exitCode, _ := e.Env.StatusCodes()
e.write(terminal.CommandFinished(exitCode, e.Env.Flags().NoExitCode))

View file

@ -4808,6 +4808,12 @@
"type": "string",
"title": "Filler",
"description": "Right aligned filler text, will span the remaining width"
},
"newline": {
"type": "boolean",
"title": "Newline",
"description": "Add a newline before the prompt",
"default": false
}
}
},

View file

@ -44,14 +44,15 @@ You need to extend or create a custom theme with your transient prompt. For exam
## Properties
| Name | Type | Description |
| ---------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `foreground` | `string` | [color][colors] |
| `foreground_templates` | `array` | [color templates][color-templates] |
| `background` | `string` | [color][colors] |
| `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 }}> ` |
| `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 |
| Name | Type | Description |
| ---------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `foreground` | `string` | [color][colors] |
| `foreground_templates` | `array` | [color templates][color-templates] |
| `background` | `string` | [color][colors] |
| `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 }}> ` |
| `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 |
| `newline` | `boolean` | add a newline before the prompt |
## Enable the feature