From 7bb0c9ae0b5231c2ce31d4815e83b449486e8316 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 15 Jun 2021 19:20:13 +0200 Subject: [PATCH] refactor(tooltips): better invocation for pwsh --- .vscode/launch.json | 2 +- docs/docs/beta.mdx | 6 ++--- src/init/omp.ps1 | 55 +++++++++++++++++++++------------------------ 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index cff40dd6..e15a2f6e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,7 +15,7 @@ "request": "launch", "mode": "debug", "program": "${workspaceRoot}/src", - "args": ["--config=/Users/jan/.jandedobbeleer.omp.json", "--tooltip=git"] + "args": ["--config=${workspaceRoot}/themes/jandedobbeleer.omp.json", "--command=git", "--shell=pwsh"] }, { "name": "Launch tests", diff --git a/docs/docs/beta.mdx b/docs/docs/beta.mdx index 2d2eb1bd..216cdab2 100644 --- a/docs/docs/beta.mdx +++ b/docs/docs/beta.mdx @@ -66,10 +66,10 @@ are being explored. }> -Import/invoke Oh My Posh in your `$PROFILE` and add the following setting: +Import/invoke Oh My Posh in your `$PROFILE` and add the following line below: ```pwsh -$Global:PoshSettings.EnableToolTips = $true +Enable-PoshTooltips ``` Restart your shell or reload your `$PROFILE` using `. $PROFILE` for the changes to take effect. @@ -77,7 +77,7 @@ Restart your shell or reload your `$PROFILE` using `. $PROFILE` for the changes -Invoke Oh My Posh in `.zshrc` and add the following setting: +Invoke Oh My Posh in `.zshrc` and add the following line below: ```bash enable_poshtooltips diff --git a/src/init/omp.ps1 b/src/init/omp.ps1 index e257b716..9bae3466 100644 --- a/src/init/omp.ps1 +++ b/src/init/omp.ps1 @@ -22,8 +22,7 @@ Set-DefaultEnvValue("AZ_ENABLED") Set-DefaultEnvValue("POSH_GIT_ENABLED") $global:PoshSettings = New-Object -TypeName PSObject -Property @{ - Theme = ""; - EnableToolTips = $false; + Theme = ""; } # used to detect empty hit @@ -36,6 +35,13 @@ if (Test-Path $config) { function global:Set-PoshContext {} +function global:Get-PoshContext { + $config = $global:PoshSettings.Theme + $cleanPWD = $PWD.ProviderPath.TrimEnd("\") + $cleanPSWD = $PWD.ToString().TrimEnd("\") + return $config, $cleanPWD, $cleanPSWD +} + function global:Initialize-ModuleSupport { if ($env:POSH_GIT_ENABLED -eq $true -and (Get-Module -Name "posh-git")) { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSProvideCommentHelp', '', Justification = 'Variable used later(not in this scope)')] @@ -57,24 +63,6 @@ function global:Initialize-ModuleSupport { } catch {} } - - # Set the keyhandler to enable tooltips - if ($global:PoshSettings.EnableToolTips -eq $true) { - Set-PSReadlineKeyHandler -Key SpaceBar -ScriptBlock { - [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') - $position = $host.UI.RawUI.CursorPosition - $omp = "::OMP::" - $config = $global:PoshSettings.Theme - $cleanPWD = $PWD.ProviderPath.TrimEnd("\") - $cleanPSWD = $PWD.ToString().TrimEnd("\") - $tooltip = $null - $cursor = $null - [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$tooltip, [ref]$cursor) - $standardOut = @(&$omp --pwd="$cleanPWD" --pswd="$cleanPSWD" --config="$config" --tooltip="$tooltip" 2>&1) - Write-Host $standardOut -NoNewline - $host.UI.RawUI.CursorPosition = $position - } - } } [ScriptBlock]$Prompt = { @@ -111,9 +99,7 @@ function global:Initialize-ModuleSupport { $global:omp_lastHistoryId = $history.Id } $omp = "::OMP::" - $config = $global:PoshSettings.Theme - $cleanPWD = $PWD.ProviderPath.TrimEnd("\") - $cleanPSWD = $PWD.ToString().TrimEnd("\") + $config, $cleanPWD, $cleanPSWD = Get-PoshContext $standardOut = @(&$omp --error="$errorCode" --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time="$executionTime" --stack-count="$stackCount" --config="$config" 2>&1) # the output can be multiline, joining these ensures proper rendering by adding line breaks with `n $standardOut -join "`n" @@ -126,9 +112,7 @@ Set-Item -Path Function:prompt -Value $Prompt -Force function global:Write-PoshDebug { $omp = "::OMP::" - $config = $global:PoshSettings.Theme - $cleanPWD = $PWD.ProviderPath.TrimEnd("\") - $cleanPSWD = $PWD.ToString().TrimEnd("\") + $config, $cleanPWD, $cleanPSWD = Get-PoshContext $standardOut = @(&$omp --error=1337 --pwd="$cleanPWD" --pswd="$cleanPSWD" --execution-time=9001 --config="$config" --debug 2>&1) $standardOut -join "`n" } @@ -197,9 +181,22 @@ function global:Export-PoshImage { } $omp = "::OMP::" - $config = $global:PoshSettings.Theme - $cleanPWD = $PWD.ProviderPath.TrimEnd("\") - $cleanPSWD = $PWD.ToString().TrimEnd("\") + $config, $cleanPWD, $cleanPSWD = Get-PoshContext $standardOut = @(&$omp --config="$config" --pwd="$cleanPWD" --pswd="$cleanPSWD" --export-png --rprompt-offset="$RPromptOffset" --cursor-padding="$CursorPadding" $Author 2>&1) $standardOut -join "`n" } + +function global:Enable-PoshTooltips { + Set-PSReadlineKeyHandler -Key SpaceBar -ScriptBlock { + [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') + $position = $host.UI.RawUI.CursorPosition + $omp = "::OMP::" + $config, $cleanPWD, $cleanPSWD = Get-PoshContext + $tooltip = $null + $cursor = $null + [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$tooltip, [ref]$cursor) + $standardOut = @(&$omp --pwd="$cleanPWD" --pswd="$cleanPSWD" --config="$config" --tooltip="$tooltip" 2>&1) + Write-Host $standardOut -NoNewline + $host.UI.RawUI.CursorPosition = $position + } +}