-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellIntegration.ps1
142 lines (98 loc) · 3.57 KB
/
ShellIntegration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/pwsh
<#
This file is part of helpimnotdrowning's PwshUtils.
PwshUtils is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
PwshUtils is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
PwshUtils. If not, see <https://www.gnu.org/licenses/>.
#>
### Experimental wt GUI shell command completion via
# https://github.com/microsoft/terminal/wiki/Experimental-Shell-Completion-Menu ###
function Send-Completions {
$CommandLine = ""
$CursorIndex = 0
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$CommandLine, [ref]$CursorIndex)
$CompletionPrefix = $CommandLine
$Result = "`e]633;Completions"
if ($CompletionPrefix.Length -gt 0) {
$Completions = TabExpansion2 -InputScript $CompletionPrefix -CursorColumn $CursorIndex
if ($null -ne $Completions.CompletionMatches) {
$Result += ";$($Completions.ReplacementIndex);$($Completions.ReplacementLength);$($CursorIndex);"
$Result += $Completions.CompletionMatches | ConvertTo-Json -Compress
}
}
$Result += "`a"
Write-Host -NoNewLine $Result
}
function Set-MappedKeyHandlers {
# Terminal suggest - always on keybindings
Set-PSReadLineKeyHandler -Chord 'F12,b' -ScriptBlock {
Send-Completions
}
}
### Windows Terminal shell integration via
# https://devblogs.microsoft.com/commandline/shell-integration-in-the-windows-terminal/ ###
$Global:LastHistoryId__ = -1
function Global:Get-TerminalLastExitCode__ {
if ($? -eq $True) {
return 0
}
if ("$LASTEXITCODE" -ne "") {
return $LASTEXITCODE
}
return -1
}
### OWN CODE FOR A BIT ###
function Enable-OMP {
param (
[String] $ThemePath
)
# windows termianl needs to make sure psreadline is here
if (Get-Module -Name PSReadLine) {
Set-MappedKeyHandlers
} else {
Write-Host "PsReadline was disabled. Shell Completion was not enabled."
}
# nuke ompprompt beforehand!
if (Test-Path Function:/ompprompt) {
Remove-Item Function:/ompprompt
}
# run OMP
if ($ThemePath) {
oh-my-posh init pwsh --config $ThemePath | Invoke-Expression
} else {
oh-my-posh init pwsh | Invoke-Expression
}
# wait: why script: scope?
# well, setting the prompt functions scope to global: isn't enough to
# modify the parent scope (the interactive shell itself). apparently
# script: scope works for this AND scope can be set by renaming the
# function, isn't that neat!
# save for later
Rename-Item Function:/prompt script:ompprompt
# MICROSOFT CODE BELOW
# prompt wrapper with escape codes for signifying end and start of prompt
function script:prompt {
# First, emit a mark for the _end_ of the previous command.
$LastHistoryEntry = $(Get-History -Count 1)
# Skip finishing the command if the first command has not yet started
if ($Global:LastHistoryId__ -ne -1) {
if ($LastHistoryEntry.Id -eq $Global:LastHistoryId__) {
# Don't provide a command line or exit code if there was no history
# entry (eg. ^C, enter on no command)
$out += "`e]133;D`a"
} else {
$out += "`e]133;D;$(Get-TerminalLastExitCode__)`a"
}
}
$out += "`e]133;A`a$(ompprompt)`e]133;B`a"
$Global:LastHistoryId__ = $LastHistoryEntry.Id
return $out
}
# END MS CODE
}