diff --git a/docs/docs/config-tooltips.mdx b/docs/docs/config-tooltips.mdx index b132c935..1078a9f0 100644 --- a/docs/docs/config-tooltips.mdx +++ b/docs/docs/config-tooltips.mdx @@ -67,13 +67,13 @@ are being explored. Import/invoke Oh My Posh in your `$PROFILE` and add the following line below: -```pwsh +```powershell Enable-PoshTooltips ``` For example: -```pwsh +```powershell # $PROFILE oh-my-posh --init --shell pwsh --config ~\wildertheme.json | Invoke-Expression Enable-PoshTooltips diff --git a/docs/docs/config-transient.mdx b/docs/docs/config-transient.mdx index 26e68904..c805fcb1 100644 --- a/docs/docs/config-transient.mdx +++ b/docs/docs/config-transient.mdx @@ -74,7 +74,7 @@ properties below. Defaults to `{{ .Shell }}> ` Import/invoke Oh My Posh in your `$PROFILE` and add the following line below: -```pwsh +```powershell Enable-PoshTransientPrompt ``` diff --git a/docs/docs/faq.mdx b/docs/docs/faq.mdx index cd3cd079..fce7cbb7 100644 --- a/docs/docs/faq.mdx +++ b/docs/docs/faq.mdx @@ -102,7 +102,7 @@ one at [VIM][vim-wt]. 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`. -```zsh +```bash HISTFILE=~/.zsh_history HISTSIZE=10000 SAVEHIST=10000 diff --git a/docs/docs/install-shells.mdx b/docs/docs/install-shells.mdx index 316f0f6a..aef260f8 100644 --- a/docs/docs/install-shells.mdx +++ b/docs/docs/install-shells.mdx @@ -15,6 +15,7 @@ oh-my-posh --print-shell groupId="shell" values={[ { label: 'powershell', value: 'powershell', }, + { label: 'cmd', value: 'cmd', }, { label: 'zsh', value: 'zsh', }, { label: 'bash', value: 'bash', }, { label: 'fish', value: 'fish', }, @@ -35,6 +36,26 @@ Once added, reload your profile for the changes to take effect. . $PROFILE ``` + + + +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. + @@ -111,3 +132,5 @@ Restart nu shell for the changes to take effect. + +[clink]: https://chrisant996.github.io/clink/ diff --git a/docs/docusaurus.config.js b/docs/docusaurus.config.js index 6a09c018..ed67b876 100644 --- a/docs/docusaurus.config.js +++ b/docs/docusaurus.config.js @@ -14,6 +14,7 @@ module.exports = { prism: { theme: require("prism-react-renderer/themes/duotoneLight"), darkTheme: require("prism-react-renderer/themes/oceanicNext"), + additionalLanguages: ['powershell', 'lua'], }, navbar: { title: "Oh My Posh", diff --git a/src/init/omp.lua b/src/init/omp.lua new file mode 100644 index 00000000..eb80b913 --- /dev/null +++ b/src/init/omp.lua @@ -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) diff --git a/src/main.go b/src/main.go index 9efa81d5..b5c45108 100644 --- a/src/main.go +++ b/src/main.go @@ -26,6 +26,9 @@ var bashInit string //go:embed init/omp.zsh var zshInit string +//go:embed init/omp.lua +var cmdInit string + const ( noExe = "echo \"Unable to find Oh My Posh executable\"" zsh = "zsh" @@ -33,6 +36,7 @@ const ( pwsh = "pwsh" fish = "fish" powershell5 = "powershell" + winCMD = "cmd" plain = "shell" ) @@ -284,7 +288,7 @@ func initShell(shell, configFile string) string { switch shell { case pwsh: 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) default: 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) case fish: return getShellInitScript(executable, configFile, fishInit) + case winCMD: + return getShellInitScript(executable, configFile, cmdInit) default: return fmt.Sprintf("echo \"No initialization script available for %s\"", shell) }