-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpsreadlinecfg.ps1
More file actions
173 lines (163 loc) · 6.22 KB
/
psreadlinecfg.ps1
File metadata and controls
173 lines (163 loc) · 6.22 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Configure PSReadline
$PSReadLineOPtions = @{
ExtraPromptLineCount = 1
HistoryNoDuplicates = $true
HistorySearchCursorMovesToEnd = $true
BellStyle = 'visual'
PredictionSource = 'History'
}
Set-PSReadLineOption @PSReadLineOptions
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Key 'Alt+9' `
-BriefDescription ParenthesizeSelection `
-LongDescription 'Put parenthesis around the selection or entire line and move the cursor to after the closing parenthesis' `
-ScriptBlock {
param($key, $arg)
$selectionStart = $null
$selectionLength = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetSelectionState([ref]$selectionStart, [ref]$selectionLength)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if ($selectionStart -ne -1) {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace($selectionStart, $selectionLength, '(' + $line.SubString($selectionStart, $selectionLength) + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($selectionStart + $selectionLength + 2)
}
else {
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(0, $line.Length, '(' + $line + ')')
[Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
}
Set-PSReadLineKeyHandler -Chord "Ctrl+'", "Ctrl+Shift+`"" `
-BriefDescription SmartInsertQuote `
-Description 'Insert paired quotes if not already on a quote' `
-ScriptBlock {
param($key, $arg)
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
$keyChar = $key.KeyChar
if ($key.Key -eq 'Oem7') {
if ($key.Modifiers -eq 'Control') {
$keyChar = "`'"
}
elseif ($key.Modifiers -eq 'Shift', 'Control') {
$keyChar = '"'
}
}
if ($line[$cursor] -eq $key.KeyChar) {
# Just move the cursor
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor + 1)
}
else {
# Insert matching quotes, move cursor to be in between the quotes
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("$keyChar" * 2)
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor - 1)
}
}
Set-PSReadLineKeyHandler -Chord Alt+c `
-BriefDescription CopyCurrentPathToClipboard `
-LongDescription 'Copy the current path to the clipboard' `
-ScriptBlock {
param($key, $arg)
Set-Clipboard $pwd.Path
}
Set-PSReadLineKeyHandler -Chord Alt+v `
-BriefDescription PasteAsHereString `
-LongDescription 'Paste the clipboard text as a here string' `
-ScriptBlock {
param($key, $arg)
$clipboardText = Get-Clipboard
if ($clipboardText) {
# Remove trailing spaces, convert \r\n to \n, and remove the final \n.
$text = $clipboardText.TrimEnd() -join "`n"
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("@'`n$text`n'@")
}
else {
[Microsoft.PowerShell.PSConsoleReadLine]::Ding()
}
}
Set-PSReadLineKeyHandler -Chord Alt+r `
-BriefDescription ResolveAliases `
-LongDescription 'Replace all aliases with the full command' `
-ScriptBlock {
param($key, $arg)
$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)
$startAdjustment = 0
foreach ($token in $tokens) {
if ($token.TokenFlags -band [System.Management.Automation.Language.TokenFlags]::CommandName) {
$alias = $ExecutionContext.InvokeCommand.GetCommand($token.Extent.Text, 'Alias')
if ($alias -ne $null) {
$resolvedCommand = $alias.ResolvedCommandName
if ($resolvedCommand -ne $null) {
$extent = $token.Extent
$length = $extent.EndOffset - $extent.StartOffset
[Microsoft.PowerShell.PSConsoleReadLine]::Replace(
$extent.StartOffset + $startAdjustment,
$length,
$resolvedCommand)
# Our copy of the tokens won't have been updated, so we need to
# adjust by the difference in length
$startAdjustment += ($resolvedCommand.Length - $length)
}
}
}
}
}
function PSReadLineRunCommand {
param(
[string]$command
)
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($command)
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
Set-PSReadLineKeyHandler -Chord Ctrl+b `
-BriefDescription Build `
-LongDescription 'Builds the project in the current location' `
-ScriptBlock {
param($key, $arg)
if (Test-Path './build.ps1') {
PSReadLineRunCommand './build.ps1'
}
elseif (Test-Path './*.sln') {
PSReadLineRunCommand 'dotnet build'
}
elseif (Test-Path './*.csproj') {
PSReadLineRunCommand 'dotnet build'
}
}
Set-PSReadLineKeyHandler -Chord Ctrl+t `
-BriefDescription Build `
-LongDescription 'Builds and tests the project in the current location' `
-ScriptBlock {
param($key, $arg)
if (Test-Path './test.ps1') {
PSReadLineRunCommand './test.ps1'
}
elseif (Test-Path './build.ps1') {
$tasks = & './build.ps1' ? | Select-Object -ExpandProperty Name
if ($tasks -contains 'test') {
PSReadLineRunCommand './build.ps1 test'
}
elseif ($tasks -contains 'unittest') {
PSReadLineRunCommand './build.ps1 unittest'
}
elseif ($tasks -contains 'unit-test') {
PSReadLineRunCommand './build.ps1 unit-test'
}
}
elseif (Test-Path './*.sln') {
PSReadLineRunCommand 'dotnet clean && dotnet test'
}
elseif (Test-Path './*.csproj') {
PSReadLineRunCommand 'dotnet clean && dotnet test'
}
}