feat(shell): support xonsh

This commit is contained in:
Jan De Dobbeleer 2023-02-18 16:46:31 +01:00 committed by Jan De Dobbeleer
parent f84e7b209f
commit 90b8178b91
9 changed files with 202 additions and 132 deletions

View file

@ -250,8 +250,8 @@ func (w *Writer) FormatTitle(title string) string {
title = strings.NewReplacer("`", "\\`", `\`, `\\`).Replace(title)
case shell.ZSH:
title = strings.NewReplacer("`", "\\`", `%`, `%%`).Replace(title)
case shell.ELVISH:
// elvish doesn't support this
case shell.ELVISH, shell.XONSH:
// these shells don't support setting the title
return ""
}
return fmt.Sprintf(w.title, title)

View file

@ -31,6 +31,7 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal
"nu",
"tcsh",
"elvish",
"xonsh",
},
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {

View file

@ -84,7 +84,8 @@ func (e *Engine) PrintPrimary() string {
func (e *Engine) printPWD() {
// only print when supported
if e.Env.Shell() == shell.ELVISH {
sh := e.Env.Shell()
if sh == shell.ELVISH || sh == shell.XONSH {
return
}
// only print when relevant

View file

@ -11,4 +11,5 @@ const (
GENERIC = "shell"
TCSH = "tcsh"
ELVISH = "elvish"
XONSH = "xonsh"
)

View file

@ -37,6 +37,9 @@ var tcshInit string
//go:embed scripts/omp.elv
var elvishInit string
//go:embed scripts/omp.py
var xonshInit string
const (
noExe = "echo \"Unable to find Oh My Posh executable\""
)
@ -181,7 +184,7 @@ func Init(env platform.Environment) string {
config = quotePosixStr(env.Flags().Config)
}
return fmt.Sprintf(command, quotePwshStr(executable), shell, config, additionalParams)
case ZSH, BASH, FISH, CMD, TCSH:
case ZSH, BASH, FISH, CMD, TCSH, XONSH:
return PrintInit(env)
case NU:
createNuInit(env)
@ -240,6 +243,10 @@ func PrintInit(env platform.Environment) string {
executable = quotePosixStr(executable)
configFile = quotePosixStr(configFile)
script = elvishInit
case XONSH:
executable = quotePosixStr(executable)
configFile = quotePosixStr(configFile)
script = xonshInit
default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
}

23
src/shell/scripts/omp.py Normal file
View file

@ -0,0 +1,23 @@
import uuid
$POWERLINE_COMMAND = "oh-my-posh"
$POSH_THEME = "::CONFIG::"
$POSH_PID = uuid.uuid4().hex
def get_command_context():
last_cmd = __xonsh__.history[-1] if __xonsh__.history else None
status = last_cmd.rtn if last_cmd else 0
duration = round((last_cmd.ts[1] - last_cmd.ts[0]) * 1000) if last_cmd else 0
return status, duration
def posh_primary():
status, duration = get_command_context()
return $(::OMP:: print primary --config=@($POSH_THEME) --shell=xonsh --error=@(status) --execution-time=@(duration) | cat)
def posh_right():
status, duration = get_command_context()
return $(::OMP:: print right --config=@($POSH_THEME) --shell=xonsh --error=@(status) --execution-time=@(duration) | cat)
$PROMPT = posh_primary
$RIGHT_PROMPT = posh_right

View file

@ -237,6 +237,10 @@ oh-my-posh config migrate glyphs --write
This will update your configuration file to use the new glyph locations. Do know they might look different, as they also
updated the icons themselves. A backup of the current config can be found in the same location with a `.bak` extension.
### Xonsh: Right prompt jumps to bottom of the screen
This is a known problem with Xonsh. The issue is tracked [here][xonsh-issue].
[exclusion]: https://support.microsoft.com/en-us/windows/add-an-exclusion-to-windows-security-811816c0-4dfd-af4a-47e4-c301afe13b26
[arch-terminfo]: https://wiki.archlinux.org/title/Bash/Prompt_customization#Terminfo_escape_sequences
[ps-ansi-docs]: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-7.2
@ -254,3 +258,4 @@ updated the icons themselves. A backup of the current config can be found in the
[vim-wt]: https://github.com/vim/vim/issues/5092
[utf-8]: https://github.com/PowerShell/PowerShell/issues/7233#issuecomment-640243647
[fish-changelog]: https://fishshell.com/docs/current/relnotes.html#id1
[xonsh-issue]: https://github.com/xonsh/xonsh/issues/3810

View file

@ -71,41 +71,35 @@ to reflect your use-case.
defaultValue="powershell"
groupId="shell"
values={[
{ label: 'powershell', value: 'powershell', },
{ label: 'cmd', value: 'cmd', },
{ label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', },
{ label: 'cmd', value: 'cmd', },
{ label: 'elvish', value: 'elvish', },
{ label: 'fish', value: 'fish', },
{ label: 'nu', value: 'nu', },
{ label: 'powershell', value: 'powershell', },
{ label: 'tcsh', value: 'tcsh', },
{ label: 'elvish', value: 'elvish', },
{ label: 'xonsh', value: 'xonsh', },
{ label: 'zsh', value: 'zsh', },
]
}>
<TabItem value="powershell">
<TabItem value="bash">
Adjust or add the following line in your `$PROFILE`:
:::caution Git bash
Use the full path to the config file, not the relative path or `~` as a shorthand for `$HOME`.
:::
```powershell
oh-my-posh init pwsh --config ~/jandedobbeleer.omp.json | Invoke-Expression
Adjust or add the following line in `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment):
```bash
eval "$(oh-my-posh init bash --config ~/jandedobbeleer.omp.json)"
```
Once altered, reload your profile for the changes to take effect.
```powershell
. $PROFILE
```bash
exec bash
```
:::info
When the above command gives an error, make sure to create the profile first and add the `oh-my-posh init` above.
```powershell
New-Item -Path $PROFILE -Type File -Force
```
In this scenario, it can also be that PowerShell blocks running local scripts. To solve that, set PowerShell
to only require remote scripts to be signed using `Set-ExecutionPolicy RemoteSigned`, or [sign the profile][sign].
:::
</TabItem>
<TabItem value="cmd">
@ -123,37 +117,18 @@ avoid having to use double backslashes.
Once altered, restart cmd for the changes to take effect.
</TabItem>
<TabItem value="zsh">
<TabItem value="elvish">
Adjust or add the following line to `~/.zshrc`:
Adjust or add the following line at the end of `~/.elvish/rc.elv`:
```bash
eval "$(oh-my-posh init zsh --config ~/jandedobbeleer.omp.json)"
eval (oh-my-posh init elvish --config ~/jandedobbeleer.omp.json)
```
Once altered, reload your profile for the changes to take effect.
Once added, reload your profile for the changes to take effect.
```bash
exec zsh
```
</TabItem>
<TabItem value="bash">
:::caution Git bash
Use the full path to the config file, not the relative path or `~` as a shorthand for `$HOME`.
:::
Adjust or add the following line in `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment):
```bash
eval "$(oh-my-posh init bash --config ~/jandedobbeleer.omp.json)"
```
Once altered, reload your profile for the changes to take effect.
```bash
exec bash
exec elvish
```
</TabItem>
@ -209,6 +184,32 @@ source /mylocation/myscript.nu
Once altered, restart Nushell for the changes to take effect.
</TabItem>
<TabItem value="powershell">
Adjust or add the following line in your `$PROFILE`:
```powershell
oh-my-posh init pwsh --config ~/jandedobbeleer.omp.json | Invoke-Expression
```
Once altered, reload your profile for the changes to take effect.
```powershell
. $PROFILE
```
:::info
When the above command gives an error, make sure to create the profile first and add the `oh-my-posh init` above.
```powershell
New-Item -Path $PROFILE -Type File -Force
```
In this scenario, it can also be that PowerShell blocks running local scripts. To solve that, set PowerShell
to only require remote scripts to be signed using `Set-ExecutionPolicy RemoteSigned`, or [sign the profile][sign].
:::
</TabItem>
<TabItem value="tcsh">
@ -225,18 +226,33 @@ exec tcsh
```
</TabItem>
<TabItem value="elvish">
<TabItem value="xonsh">
Adjust or add the following line at the end of ~/.elvish/rc.elv:
Adjust or add the following line at the end of `~/.xonshrc`:
```bash
eval (oh-my-posh init elvish --config ~/jandedobbeleer.omp.json)
execx($(oh-my-posh init xonsh --config ~/jandedobbeleer.omp.json))
```
Once added, reload your profile for the changes to take effect.
```bash
exec elvish
exec xonsh
```
</TabItem>
<TabItem value="zsh">
Adjust or add the following line to `~/.zshrc`:
```bash
eval "$(oh-my-posh init zsh --config ~/jandedobbeleer.omp.json)"
```
Once altered, reload your profile for the changes to take effect.
```bash
exec zsh
```
</TabItem>

View file

@ -19,56 +19,35 @@ oh-my-posh get shell
defaultValue="powershell"
groupId="shell"
values={[
{ label: 'powershell', value: 'powershell', },
{ label: 'cmd', value: 'cmd', },
{ label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', },
{ label: 'cmd', value: 'cmd', },
{ label: 'elvish', value: 'elvish', },
{ label: 'fish', value: 'fish', },
{ label: 'nu', value: 'nu', },
{ label: 'powershell', value: 'powershell', },
{ label: 'tcsh', value: 'tcsh', },
{ label: 'elvish', value: 'elvish', },
{ label: 'xonsh', value: 'xonsh', },
{ label: 'zsh', value: 'zsh', },
]
}>
<TabItem value="powershell">
<TabItem value="bash">
Edit your PowerShell profile script, you can find its location under the `$PROFILE` variable in your preferred PowerShell version. For example, using notepad:
Add the following to `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment):
```powershell
notepad $PROFILE
```bash
eval "$(oh-my-posh init bash)"
```
:::info
When the above command gives an error, make sure to create the profile first.
```powershell
New-Item -Path $PROFILE -Type File -Force
```
In this scenario, it can also be that PowerShell blocks running local scripts. To solve that, set PowerShell
to only require remote scripts to be signed using `Set-ExecutionPolicy RemoteSigned`, or [sign the profile][sign].
:::
Then add the following line.
```powershell
oh-my-posh init pwsh | Invoke-Expression
```
:::tip Antivirus software
If adding an exception to your Antivirus software still blocks Oh My Posh for Windows, you can try using the below
alternate line in your PowerShell profile. This is a drop-in replacement to the above call with Invoke-Expression.
Tested with PowerShell 7.2.4 with Bitdefender Antivirus installed and requires NO exceptions for oh-my-posh.exe.
Bitdefender blocks the calls to Invoke-Expression as malicious code and this alternate line skips the double calls:
```powershell
& ([ScriptBlock]::Create((oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" --print) -join "`n"))
```
:::
Once added, reload your profile for the changes to take effect.
```powershell
. $PROFILE
```bash
exec bash
```
Or, when using `~/.profile`.
```bash
. ~/.profile
```
</TabItem>
@ -88,51 +67,18 @@ load(io.popen('oh-my-posh init cmd'):read("*a"))()
Once added, restart cmd for the changes to take effect.
</TabItem>
<TabItem value="zsh">
<TabItem value="elvish">
Add the following to `~/.zshrc`:
Add the following at the end of `~/.elvish/rc.elv`:
```bash
eval "$(oh-my-posh init zsh)"
```
:::tip
As the standard terminal has issues displaying the ANSI characters correctly, you might want to skip loading just for that terminal and instead of the line above, place this in your `~/.zshrc`:
```bash
if [ "$TERM_PROGRAM" != "Apple_Terminal" ]; then
eval "$(oh-my-posh init zsh)"
fi
```
Note this will still load Oh My Posh for [iTerm2][iterm2] or any other modern day macOS terminal that supports ANSI characters.
:::
Once added, reload your profile for the changes to take effect.
```bash
exec zsh
```
</TabItem>
<TabItem value="bash">
Add the following to `~/.bashrc` (could be `~/.profile` or `~/.bash_profile` depending on your environment):
```bash
eval "$(oh-my-posh init bash)"
eval (oh-my-posh init elvish)
```
Once added, reload your profile for the changes to take effect.
```bash
exec bash
```
Or, when using `~/.profile`.
```bash
. ~/.profile
exec elvish
```
</TabItem>
@ -188,6 +134,49 @@ source /mylocation/myscript.nu
Once added, restart Nushell for the changes to take effect.
</TabItem>
<TabItem value="powershell">
Edit your PowerShell profile script, you can find its location under the `$PROFILE` variable in your preferred PowerShell version. For example, using notepad:
```powershell
notepad $PROFILE
```
:::info
When the above command gives an error, make sure to create the profile first.
```powershell
New-Item -Path $PROFILE -Type File -Force
```
In this scenario, it can also be that PowerShell blocks running local scripts. To solve that, set PowerShell
to only require remote scripts to be signed using `Set-ExecutionPolicy RemoteSigned`, or [sign the profile][sign].
:::
Then add the following line.
```powershell
oh-my-posh init pwsh | Invoke-Expression
```
:::tip Antivirus software
If adding an exception to your Antivirus software still blocks Oh My Posh for Windows, you can try using the below
alternate line in your PowerShell profile. This is a drop-in replacement to the above call with Invoke-Expression.
Tested with PowerShell 7.2.4 with Bitdefender Antivirus installed and requires NO exceptions for oh-my-posh.exe.
Bitdefender blocks the calls to Invoke-Expression as malicious code and this alternate line skips the double calls:
```powershell
& ([ScriptBlock]::Create((oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" --print) -join "`n"))
```
:::
Once added, reload your profile for the changes to take effect.
```powershell
. $PROFILE
```
</TabItem>
<TabItem value="tcsh">
@ -204,18 +193,45 @@ exec tcsh
```
</TabItem>
<TabItem value="elvish">
<TabItem value="xonsh">
Add the following at the end of ~/.elvish/rc.elv:
Add the following line at the end of `~/.xonshrc`:
```bash
eval (oh-my-posh init elvish)
execx($(oh-my-posh init xonsh))
```
Once added, reload your profile for the changes to take effect.
```bash
exec elvish
exec xonsh
```
</TabItem>
<TabItem value="zsh">
Add the following to `~/.zshrc`:
```bash
eval "$(oh-my-posh init zsh)"
```
:::tip
As the standard terminal has issues displaying the ANSI characters correctly, you might want to skip loading just for that terminal and instead of the line above, place this in your `~/.zshrc`:
```bash
if [ "$TERM_PROGRAM" != "Apple_Terminal" ]; then
eval "$(oh-my-posh init zsh)"
fi
```
Note this will still load Oh My Posh for [iTerm2][iterm2] or any other modern day macOS terminal that supports ANSI characters.
:::
Once added, reload your profile for the changes to take effect.
```bash
exec zsh
```
</TabItem>