feat: init for Nushell

This commit is contained in:
Jan De Dobbeleer 2022-04-10 19:33:13 +02:00 committed by Jan De Dobbeleer
parent a5efda25e4
commit ebce0f30f5
6 changed files with 52 additions and 16 deletions

View file

@ -146,28 +146,25 @@ exec fish
</TabItem> </TabItem>
<TabItem value="nu"> <TabItem value="nu">
Set the prompt and restart nu shell: :::warning
Oh My Posh requires Nushell >= 0.60.0
:::
**Nu < 0.32.0** Edit `$nu.config-path` and add the following lines at the bottom.
```bash ```bash
config set prompt "= `{{$(oh-my-posh print primary | str collect)}}`" oh-my-posh init nu
source ~/.oh-my-posh.nu
``` ```
**Nu >= 0.32.0** If you want to save the initialization script elsewhere, replace the lines above with these:
```bash ```bash
config set prompt "(oh-my-posh print primary | str collect)" oh-my-posh init nu --print | save /mylocation/myscript.nu
source /mylocation/myscript.nu
``` ```
**Nu >= 0.60.0** Restart Nushell for the changes to take effect.
```bash
let-env PROMPT_COMMAND = { oh-my-posh print primary }
let-env PROMPT_COMMAND_RIGHT = { oh-my-posh print right }
```
Restart nu shell for the changes to take effect.
</TabItem> </TabItem>
</Tabs> </Tabs>

View file

@ -16,7 +16,7 @@ var (
print bool print bool
initCmd = &cobra.Command{ initCmd = &cobra.Command{
Use: "init [bash|zsh|fish|powershell|pwsh|cmd] --config ~/.mytheme.omp.json", Use: "init [bash|zsh|fish|powershell|pwsh|cmd|nu] --config ~/.mytheme.omp.json",
Short: "Initialize your shell and configuration", Short: "Initialize your shell and configuration",
Long: `Allows to initialize your shell and configuration. Long: `Allows to initialize your shell and configuration.
See the documentation to initialize your shell: https://ohmyposh.dev/docs/prompt.`, See the documentation to initialize your shell: https://ohmyposh.dev/docs/prompt.`,
@ -27,6 +27,7 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/prompt
"powershell", "powershell",
"pwsh", "pwsh",
"cmd", "cmd",
"nu",
}, },
Args: cobra.OnlyValidArgs, Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {

View file

@ -214,7 +214,7 @@ func (e *Engine) print() string {
prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), "\"", "\"\"")) prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), "\"", "\"\""))
prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt) prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt)
return prompt return prompt
case shell.PWSH, shell.PWSH5, shell.BASH, shell.PLAIN: case shell.PWSH, shell.PWSH5, shell.BASH, shell.PLAIN, shell.NU:
if e.rprompt == "" || !e.canWriteRPrompt() || e.Plain { if e.rprompt == "" || !e.canWriteRPrompt() || e.Plain {
break break
} }
@ -334,7 +334,7 @@ func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
return prompt return prompt
} }
return str return str
case shell.PWSH, shell.PWSH5, shell.CMD, shell.BASH, shell.FISH: case shell.PWSH, shell.PWSH5, shell.CMD, shell.BASH, shell.FISH, shell.NU:
str, _ := e.Writer.String() str, _ := e.Writer.String()
return str return str
} }

View file

@ -7,5 +7,6 @@ const (
FISH = "fish" FISH = "fish"
PWSH5 = "powershell" PWSH5 = "powershell"
CMD = "cmd" CMD = "cmd"
NU = "nu"
PLAIN = "shell" PLAIN = "shell"
) )

View file

@ -2,6 +2,7 @@ package shell
import ( import (
_ "embed" _ "embed"
"path/filepath"
"fmt" "fmt"
"oh-my-posh/environment" "oh-my-posh/environment"
@ -25,6 +26,9 @@ var zshInit string
//go:embed scripts/omp.lua //go:embed scripts/omp.lua
var cmdInit string var cmdInit string
//go:embed scripts/omp.nu
var nuInit string
const ( const (
noExe = "echo \"Unable to find Oh My Posh executable\"" noExe = "echo \"Unable to find Oh My Posh executable\""
) )
@ -58,6 +62,9 @@ func Init(env environment.Environment) string {
return fmt.Sprintf("(@(&\"%s\" init %s --config=\"%s\" --print) -join \"`n\") | Invoke-Expression", executable, shell, env.Flags().Config) return fmt.Sprintf("(@(&\"%s\" init %s --config=\"%s\" --print) -join \"`n\") | Invoke-Expression", executable, shell, env.Flags().Config)
case ZSH, BASH, FISH, CMD: case ZSH, BASH, FISH, CMD:
return PrintInit(env) return PrintInit(env)
case NU:
createNuInit(env)
return ""
default: default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
} }
@ -81,6 +88,8 @@ func PrintInit(env environment.Environment) string {
return getShellInitScript(executable, configFile, fishInit) return getShellInitScript(executable, configFile, fishInit)
case CMD: case CMD:
return getShellInitScript(executable, configFile, cmdInit) return getShellInitScript(executable, configFile, cmdInit)
case NU:
return getShellInitScript(executable, configFile, nuInit)
default: default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
} }
@ -92,6 +101,19 @@ func getShellInitScript(executable, configFile, script string) string {
return script return script
} }
func createNuInit(env environment.Environment) {
initPath := filepath.Join(env.Home(), ".oh-my-posh.nu")
f, err := os.OpenFile(initPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return
}
_, err = f.WriteString(PrintInit(env))
if err != nil {
return
}
_ = f.Close()
}
func ConsoleBackgroundColor(env environment.Environment, backgroundColorTemplate string) string { func ConsoleBackgroundColor(env environment.Environment, backgroundColorTemplate string) string {
if len(backgroundColorTemplate) == 0 { if len(backgroundColorTemplate) == 0 {
return backgroundColorTemplate return backgroundColorTemplate

15
src/shell/scripts/omp.nu Normal file
View file

@ -0,0 +1,15 @@
let-env POWERLINE_COMMAND = 'oh-my-posh'
let-env POSH_THEME = '::CONFIG::'
let-env PROMPT_INDICATOR = ""
# By default displays the right prompt on the first line
# making it annoying when you have a multiline prompt
# making the behavior different compared to other shells
let-env PROMPT_COMMAND_RIGHT = {''}
# PROMPTS
let-env PROMPT_MULTILINE_INDICATOR = (^::OMP:: print secondary $"--config=($env.POSH_THEME)" --shell=nu)
let-env PROMPT_COMMAND = {
let width = (term size -c | get columns | into string)
^::OMP:: print primary $"--config=($env.POSH_THEME)" $"--execution-time=($env.CMD_DURATION_MS)" $"--error=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)"
}