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 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
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
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**
```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.
Restart Nushell for the changes to take effect.
</TabItem>
</Tabs>

View file

@ -16,7 +16,7 @@ var (
print bool
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",
Long: `Allows to initialize your shell and configuration.
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",
"pwsh",
"cmd",
"nu",
},
Args: cobra.OnlyValidArgs,
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("\nRPROMPT=\"%s\"", e.rprompt)
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 {
break
}
@ -334,7 +334,7 @@ func (e *Engine) PrintExtraPrompt(promptType ExtraPromptType) string {
return prompt
}
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()
return str
}

View file

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

View file

@ -2,6 +2,7 @@ package shell
import (
_ "embed"
"path/filepath"
"fmt"
"oh-my-posh/environment"
@ -25,6 +26,9 @@ var zshInit string
//go:embed scripts/omp.lua
var cmdInit string
//go:embed scripts/omp.nu
var nuInit string
const (
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)
case ZSH, BASH, FISH, CMD:
return PrintInit(env)
case NU:
createNuInit(env)
return ""
default:
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)
case CMD:
return getShellInitScript(executable, configFile, cmdInit)
case NU:
return getShellInitScript(executable, configFile, nuInit)
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}
@ -92,6 +101,19 @@ func getShellInitScript(executable, configFile, script string) string {
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 {
if len(backgroundColorTemplate) == 0 {
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)"
}