From 94d7de7f4eeeaa220c077250b325b02d33ee42b3 Mon Sep 17 00:00:00 2001 From: "Michael J. Ryan" Date: Thu, 7 Oct 2021 11:08:31 -0700 Subject: [PATCH] feat: Add starship_precmd_user_func support for PowerShell. (#3115) --- docs/advanced-config/README.md | 26 ++++++++++++++++++++++++++ src/init/starship.ps1 | 7 +++++++ 2 files changed, 33 insertions(+) diff --git a/docs/advanced-config/README.md b/docs/advanced-config/README.md index 412b74de..2b768a68 100644 --- a/docs/advanced-config/README.md +++ b/docs/advanced-config/README.md @@ -42,6 +42,21 @@ trap blastoff DEBUG # Trap DEBUG *before* running starship eval $(starship init bash) ``` +## Custom pre-prompt and pre-execution Commands in PowerShell + +PowerShell does not have a formal preexec/precmd framework like most other shells. +Because of this, it is difficult to provide fully customizable hooks in `powershell`. +However, Starship does give you limited ability to insert your own functions +into the prompt-rendering procedure: + +Create a function named `Invoke-Starship-PreCommand` + +```powershell +function Invoke-Starship-PreCommand { + $host.ui.Write("🚀") +} +``` + ## Change Window Title Some shell prompts will automatically change the window title for you (e.g. to @@ -85,6 +100,17 @@ function set_win_title(){ starship_precmd_user_func="set_win_title" ``` +You can also set a similar output with PowerShell by creating a function named `Invoke-Starship-PreCommand`. + +```powershell +# edit $PROFILE +function Invoke-Starship-PreCommand { + $host.ui.Write("`e]0; PS> $env:USERNAME@$env:COMPUTERNAME`: $pwd `a") +} + +Invoke-Expression (&starship init powershell) +``` + ## Enable Right Prompt Some shells support a right prompt which renders on the same line as the input. Starship can diff --git a/src/init/starship.ps1 b/src/init/starship.ps1 index 8d0179ff..e1001680 100644 --- a/src/init/starship.ps1 +++ b/src/init/starship.ps1 @@ -63,6 +63,13 @@ function global:prompt { $process.StandardOutput.ReadToEnd(); } + # Invoke precmd, if specified + try { + if (Test-Path function:Invoke-Starship-PreCommand) { + Invoke-Starship-PreCommand + } + } catch {} + $origDollarQuestion = $global:? $origLastExitCode = $global:LASTEXITCODE