-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPSExpandLine.psm1
More file actions
129 lines (104 loc) · 5.36 KB
/
PSExpandLine.psm1
File metadata and controls
129 lines (104 loc) · 5.36 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
# PSExpandLine 2.0.0
[CmdletBinding()]
Param()
# Set variables
$Module = @{}
$Module.ModulePath = $PSCommandPath
$Module.DefaultHotlist = $null
$Module.NativeHotstringsFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'config' -AdditionalChildPath 'PSExpandLine_native.csv'
$Module.CustomHotstringsFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'config' -AdditionalChildPath 'PSExpandLine_custom.csv'
$Module.CustomHotListsFilePath = Join-Path -Path $PSScriptRoot -ChildPath 'config' -AdditionalChildPath 'PSExpandLine_hotlist.csv'
$Module.Name = 'PSExpandLine'
$script:listPosn = 0
# Include functions
$functionsDirPath = Join-Path -Path $PSScriptRoot -ChildPath 'functions'
ForEach ($file in Get-ChildItem -Path $functionsDirPath -Filter '*.ps1')
{
. "$($file.FullName)"
}
# Import the hotstrings
$hotstrings = [Ordered]@{}
If (Test-Path -Path $Module.NativeHotstringsFilePath)
{
Import-Csv -Path $Module.NativeHotstringsFilePath | ForEach-Object { $hotstrings.$($_.Name) = $_.Definition }
}
If (Test-Path -Path $Module.CustomHotstringsFilePath)
{
# custom hotstrings can overwrite native hotstrings
Import-Csv -Path $Module.CustomHotstringsFilePath | ForEach-Object { $hotstrings.$($_.Name) = $_.Definition }
}
# Import the hotlists
$script:hotlists = @{}
If (Test-Path -Path $Module.CustomHotListsFilePath)
{
Import-Csv -Path $Module.CustomHotListsFilePath `
| Where-Object Name -match 'Ctrl\+[1-9]' `
| ForEach-Object {
$chord = $_.Name
$isDefault = [Bool][Int]$_.IsDefault
$separator = $_.Separator
$listItems = $_.Definition
If ($listItems[0] -eq '{' -and $listItems[-1] -eq '}') # script block
{
$sbListItems = [ScriptBlock]::Create($listItems.Substring(1,$listItems.Length-2).Trim())
$listItems = Invoke-Command -ScriptBlock $sbListItems -ErrorAction Ignore `
| ForEach-Object { $_.ToString().Trim() } `
| Select-Object -Unique
}
Else
{
$listItems = $listItems.Split($separator) | Select-Object -Unique
}
$script:hotlists[$chord] = $listItems
If ($isDefault)
{
$Module.DefaultHotlist = $chord
}
}
}
# Set the key handler for hotstring expansion
. "$(Join-Path -Path $PSScriptRoot -ChildPath 'PSExpandLine_sbExpandHotstring.ps1')"
Set-PSReadLineKeyHandler -Chord 'Spacebar' -ScriptBlock $sbExpand -BriefDescription $Module.Name -Description 'Hotstrings: expand a defined hotstring to its value.'
# Set the key handler for suppression of hotstring expansion
Set-PSReadLineKeyHandler -Chord 'Shift+SpaceBar' -ScriptBlock { [Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ') } -BriefDescription $Module.Name -Description 'Hotstrings: suppress expansion of a defined hotstring.'
# Set key handlers for hotlist selection
. "$(Join-Path -Path $PSScriptRoot -ChildPath 'PSExpandLine_sbSelectHotlist.ps1')"
ForEach ($chord in $script:hotlists.Keys)
{
$displayList = $($script:hotlists[$chord] -join ' ')
$displayList = $displayList.Length -gt 75 ? $displayList.Substring(0,74)+'…' : $displayList
Set-PSReadLineKeyHandler -Chord $chord -ScriptBlock $sbSelect -BriefDescription $Module.Name -Description "Hotlists: select hotlist: $displayList"
}
Set-PSReadLineKeyHandler -Chord 'Ctrl+0' -ScriptBlock $sbSelect -BriefDescription $Module.Name -Description "Hotlists: deactivate all hotlists."
# Set the default hotlist
If ($Module.DefaultHotlist)
{
& $sbSelect
}
# Set the key handlers for hotlist insertion
. "$(Join-Path -Path $PSScriptRoot -ChildPath 'PSExpandLine_sbInsertListItem.ps1')"
Set-PSReadLineKeyHandler -Chord 'Ctrl+DownArrow' -ScriptBlock $sbInsert -BriefDescription $Module.Name -Description 'Hotlists: insert the next item in the selected hotlist.'
Set-PSReadLineKeyHandler -Chord 'Ctrl+UpArrow' -ScriptBlock $sbInsert -BriefDescription $Module.Name -Description 'Hotlists: insert the previous item in the selected hotlist.'
Set-PSReadLineKeyHandler -Chord 'Shift+DownArrow' -ScriptBlock $sbInsert -BriefDescription $Module.Name -Description 'Hotlists: insert the next item in the selected hotlist with single-quotes.'
Set-PSReadLineKeyHandler -Chord 'Shift+UpArrow' -ScriptBlock $sbInsert -BriefDescription $Module.Name -Description 'Hotlists: insert the previous item in the selected hotlist with single-quotes.'
Set-PSReadLineKeyHandler -Chord 'Ctrl+Shift+DownArrow' -ScriptBlock $sbInsert -BriefDescription $Module.Name -Description 'Hotlists: insert the next item in the selected hotlist with double-quotes.'
Set-PSReadLineKeyHandler -Chord 'Ctrl+Shift+UpArrow' -ScriptBlock $sbInsert -BriefDescription $Module.Name -Description 'Hotlists: insert the previous item in the selected hotlist with double-quotes.'
# Export module members
$AliasesToExport = @()
$CmdletsToExport = @()
$FunctionsToExport = @('Edit-CustomHotlist','Edit-CustomHotstring','Save-AliasAsHotstring')
$VariablesToExport = @('PSExpandLine')
$moduleMembers =
@{
'Alias' = $AliasesToExport
'Cmdlet' = $CmdletsToExport
'Function' = $FunctionsToExport
'Variable' = $VariablesToExport
}
Export-ModuleMember @moduleMembers
# Add OnRemove logic
$onRemove =
{
Get-PSReadLineKeyHandler | Where-Object Function -eq PSExpandLine | ForEach-Object { Remove-PSReadLineKeyHandler -Chord $_.Key }
}
$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = $onRemove