feat(cmd): support Windows CMD

This commit is contained in:
Jan De Dobbeleer 2021-11-13 13:30:41 +01:00 committed by Jan De Dobbeleer
parent 1d192d63d8
commit b3371136c7
7 changed files with 85 additions and 5 deletions

View file

@ -67,13 +67,13 @@ are being explored.
Import/invoke Oh My Posh in your `$PROFILE` and add the following line below: Import/invoke Oh My Posh in your `$PROFILE` and add the following line below:
```pwsh ```powershell
Enable-PoshTooltips Enable-PoshTooltips
``` ```
For example: For example:
```pwsh ```powershell
# $PROFILE # $PROFILE
oh-my-posh --init --shell pwsh --config ~\wildertheme.json | Invoke-Expression oh-my-posh --init --shell pwsh --config ~\wildertheme.json | Invoke-Expression
Enable-PoshTooltips Enable-PoshTooltips

View file

@ -74,7 +74,7 @@ properties below. Defaults to `{{ .Shell }}> `
Import/invoke Oh My Posh in your `$PROFILE` and add the following line below: Import/invoke Oh My Posh in your `$PROFILE` and add the following line below:
```pwsh ```powershell
Enable-PoshTransientPrompt Enable-PoshTransientPrompt
``` ```

View file

@ -102,7 +102,7 @@ one at [VIM][vim-wt].
This issue occurs when you're using plain ZSH in combination with Oh My Posh. This issue occurs when you're using plain ZSH in combination with Oh My Posh.
You fix this by can adding the right configuration to `~/.zshrc`. You fix this by can adding the right configuration to `~/.zshrc`.
```zsh ```bash
HISTFILE=~/.zsh_history HISTFILE=~/.zsh_history
HISTSIZE=10000 HISTSIZE=10000
SAVEHIST=10000 SAVEHIST=10000

View file

@ -15,6 +15,7 @@ oh-my-posh --print-shell
groupId="shell" groupId="shell"
values={[ values={[
{ label: 'powershell', value: 'powershell', }, { label: 'powershell', value: 'powershell', },
{ label: 'cmd', value: 'cmd', },
{ label: 'zsh', value: 'zsh', }, { label: 'zsh', value: 'zsh', },
{ label: 'bash', value: 'bash', }, { label: 'bash', value: 'bash', },
{ label: 'fish', value: 'fish', }, { label: 'fish', value: 'fish', },
@ -35,6 +36,26 @@ Once added, reload your profile for the changes to take effect.
. $PROFILE . $PROFILE
``` ```
</TabItem>
<TabItem value="cmd">
There's no out of the box support for Windows CMD when it comes to custom prompts.
There is however a way to do it using [Clink][clink], which at the same time supercharges
your cmd experience. Follow the installation instructions and make sure you select autostart.
Integrating Oh my Posh with Clink is easy: create a new file called oh-my-posh.lua in your Clink
scripts directory (run `clink info` inside cmd to find that file's location).
:::warning
Use the full path to the config file, not the relative path.
:::
```lua title="oh-my-posh.lua"
load(io.popen("oh-my-posh --config=\"C:/Users/jan/jandedobbeleer.omp.json\" --init --shell cmd"):read("*a"))()
```
Once added, restart cmd for the changes to take effect.
</TabItem> </TabItem>
<TabItem value="zsh"> <TabItem value="zsh">
@ -111,3 +132,5 @@ Restart nu shell for the changes to take effect.
</TabItem> </TabItem>
</Tabs> </Tabs>
[clink]: https://chrisant996.github.io/clink/

View file

@ -14,6 +14,7 @@ module.exports = {
prism: { prism: {
theme: require("prism-react-renderer/themes/duotoneLight"), theme: require("prism-react-renderer/themes/duotoneLight"),
darkTheme: require("prism-react-renderer/themes/oceanicNext"), darkTheme: require("prism-react-renderer/themes/oceanicNext"),
additionalLanguages: ['powershell', 'lua'],
}, },
navbar: { navbar: {
title: "Oh My Posh", title: "Oh My Posh",

50
src/init/omp.lua Normal file
View file

@ -0,0 +1,50 @@
-- Duration functions
local endedit_time
local last_duration
local function duration_onbeginedit()
last_duration = 0
if endedit_time then
local beginedit_time = io.popen("::OMP:: --millis"):read("*n")
local elapsed = beginedit_time - endedit_time
if elapsed >= 0 then
last_duration = elapsed
end
end
end
local function duration_onendedit()
endedit_time = io.popen("::OMP:: --millis"):read("*n")
end
-- Prompt functions
local function get_posh_prompt(rprompt)
local prompt_exe = string.format("::OMP:: --config=\"::CONFIG::\" --execution-time %s --error %s --rprompt=%s", last_duration, os.geterrorlevel(), rprompt)
prompt = io.popen(prompt_exe):read("*a")
return prompt
end
local p = clink.promptfilter(1)
function p:filter(prompt)
return get_posh_prompt(false)
end
function p:rightfilter(prompt)
return get_posh_prompt(true), false
end
-- Event handlers
local function builtin_modules_onbeginedit()
_cached_state = {}
duration_onbeginedit()
end
local function builtin_modules_onendedit()
duration_onendedit()
end
clink.onbeginedit(builtin_modules_onbeginedit)
clink.onendedit(builtin_modules_onendedit)

View file

@ -26,6 +26,9 @@ var bashInit string
//go:embed init/omp.zsh //go:embed init/omp.zsh
var zshInit string var zshInit string
//go:embed init/omp.lua
var cmdInit string
const ( const (
noExe = "echo \"Unable to find Oh My Posh executable\"" noExe = "echo \"Unable to find Oh My Posh executable\""
zsh = "zsh" zsh = "zsh"
@ -33,6 +36,7 @@ const (
pwsh = "pwsh" pwsh = "pwsh"
fish = "fish" fish = "fish"
powershell5 = "powershell" powershell5 = "powershell"
winCMD = "cmd"
plain = "shell" plain = "shell"
) )
@ -284,7 +288,7 @@ func initShell(shell, configFile string) string {
switch shell { switch shell {
case pwsh: case pwsh:
return fmt.Sprintf("(@(&\"%s\" --print-init --shell=pwsh --config=\"%s\") -join \"`n\") | Invoke-Expression", executable, configFile) return fmt.Sprintf("(@(&\"%s\" --print-init --shell=pwsh --config=\"%s\") -join \"`n\") | Invoke-Expression", executable, configFile)
case zsh, bash, fish: case zsh, bash, fish, winCMD:
return printShellInit(shell, configFile) return printShellInit(shell, configFile)
default: default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
@ -305,6 +309,8 @@ func printShellInit(shell, configFile string) string {
return getShellInitScript(executable, configFile, bashInit) return getShellInitScript(executable, configFile, bashInit)
case fish: case fish:
return getShellInitScript(executable, configFile, fishInit) return getShellInitScript(executable, configFile, fishInit)
case winCMD:
return getShellInitScript(executable, configFile, cmdInit)
default: default:
return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) return fmt.Sprintf("echo \"No initialization script available for %s\"", shell)
} }