Skip to content

Commit

Permalink
See changelog for v0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Oct 23, 2018
1 parent 36c5f1e commit fec2213
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 112 deletions.
Binary file modified PSReadlineHelper.psd1
Binary file not shown.
160 changes: 71 additions & 89 deletions PSReadlineHelper.psm1
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
#requires -version 5.1

#not all commands and features of this module will not work with PSReadline version 2.0.0
#not all commands and features of this module will not work with PSReadline version 2.0.

$ver = (get-module psreadline).version
if ( $ve -gt 1.2) {
write-warning "Some commands in this module will not work with newer versions of PSReadline."
#bail out
#return
}
. $PSScriptRoot\PSReadlinehelpers.ps1

. $psscriptroot\PSReadlinehelpers.ps1

#region my PSReadlineOptions

Expand All @@ -24,12 +18,10 @@ $token = @{
ForegroundColor = "Cyan"
}
}

Set-PSReadlineOption @token

$options = @{
#ContinuationPromptForegroundColor = "White"
#ContinuationPromptBackgroundColor = "Magenta"
#ErrorForegroundColor = "Green"
HistoryNoDuplicates = $True
HistorySaveStyle = "SaveIncrementally"
DingDuration = 300
Expand All @@ -52,75 +44,82 @@ Set-PSReadlineKeyHandler -key Ctrl+h -BriefDescription "Open PSReadlineHistory"
Invoke-Item -Path $(Get-PSReadlineOption).HistorySavePath
}

Set-PSReadlineKeyHandler -key Ctrl+Alt+F -BriefDescription "Function Menu" -Description "Display all functions as menu using Out-GridView. [$($env:username)]" -ScriptBlock {
if ($PSedition -eq 'Desktop') {

$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
Set-PSReadlineKeyHandler -key Ctrl+Alt+F -BriefDescription "Function Menu" -Description "Display all functions as menu using Out-GridView. [$($env:username)]" -ScriptBlock {

#filter out the built-in functions for changing drives and a few others
Get-Childitem -path function: | Where-Object {$_.name -notmatch "([A-Za-z]:)|(Get-Verb)|(prompt)|(cd)|(clear-host)|(more)|(pause)"} |
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

#filter out the built-in functions for changing drives and a few others
Get-Childitem -path function: | Where-Object {$_.name -notmatch "([A-Za-z]:)|(Get-Verb)|(prompt)|(cd)|(clear-host)|(more)|(pause)"} |
select-object Name, Version, Source, @{Name = "Syntax"; Expression = {(Get-Command $_.name -Syntax|Out-String).Trim()}} |
Out-GridView -title "Function Menu: Select one to run" -OutputMode Single |
Foreach-Object {
Show-Command -Name $_.name
# [Microsoft.PowerShell.PSConsoleReadLine]::Insert($_.name)
Show-Command -Name $_.name
}
}
}

if ($ver -lt 2.0.0) {
Set-PSReadlineKeyHandler -Key F7 -BriefDescription HistoryList -Description "Show command history with Out-Gridview. [$($env:username)]" -ScriptBlock {
$pattern = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$pattern, [ref]$null)
if ($pattern) {
$pattern = [regex]::Escape($pattern)
}

$history = [System.Collections.ArrayList]@(
$last = ''
$lines = ''
foreach ($line in [System.IO.File]::ReadLines((Get-PSReadlineOption).HistorySavePath)) {
if ($line.EndsWith('`')) {
$line = $line.Substring(0, $line.Length - 1)
$lines = if ($lines) {
"$lines`n$line"
Set-PSReadlineKeyHandler -Key F7 -BriefDescription HistoryList -Description "Show command history with Out-Gridview. [$($env:username)]" -ScriptBlock {
$pattern = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$pattern, [ref]$null)
if ($pattern) {
$pattern = [regex]::Escape($pattern)
}

$history = [System.Collections.ArrayList]@(
$last = ''
$lines = ''
foreach ($line in [System.IO.File]::ReadLines((Get-PSReadlineOption).HistorySavePath)) {
if ($line.EndsWith('`')) {
$line = $line.Substring(0, $line.Length - 1)
$lines = if ($lines) {
"$lines`n$line"
}
else {
$line
}
continue
}
else {
$line

if ($lines) {
$line = "$lines`n$line"
$lines = ''
}
continue
}

if ($lines) {
$line = "$lines`n$line"
$lines = ''
if (($line -cne $last) -and (!$pattern -or ($line -match $pattern))) {
$last = $line
$line
}
}
)
$history.Reverse()

if (($line -cne $last) -and (!$pattern -or ($line -match $pattern))) {
$last = $line
$line
$command = $history | Select-Object -unique | Out-GridView -Title "PSReadline History - Select a command to insert at the prompt" -OutputMode Single
if ($command) {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))
}
}
)
$history.Reverse()

$command = $history | Select-Object -unique | Out-GridView -Title "PSReadline History - Select a command to insert at the prompt" -OutputMode Single
if ($command) {
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(($command -join "`n"))
}
}
}
Set-PSReadlineKeyHandler -key Alt+F5 -BriefDescription "ListMyHandlers" -Description "List my PSReadlineHandlers [$env:username]" -ScriptBlock {

Set-PSReadlineKeyHandler -Key Shift+F1 -BriefDescription OnlineCommandHelp -LongDescription "Open online help for the current command. [$($env:username)]" -ScriptBlock {
(Get-PSReadlineKeyHandler -bound ).Where( {$_.description -match "\[$env:username\]"}) |
Select-Object -Property Key,Description | Out-GridView -title "My handlers"
}
} #if desktop

$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)
Set-PSReadlineKeyHandler -Key Shift+F1 -BriefDescription OnlineCommandHelp -LongDescription "Open online help for the current command. [$($env:username)]" -ScriptBlock {

$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)

$commandAst = $ast.FindAll( {
$commandAst = $ast.FindAll( {
$node = $args[0]
$node -is [System.Management.Automation.Language.CommandAst] -and
$node.Extent.StartOffset -le $cursor -and
Expand Down Expand Up @@ -186,7 +185,7 @@ Set-PSReadlineKeyHandler -Key Ctrl+j -BriefDescription JumpDirectory -LongDescri
}
}

Set-PSReadlineKeyHandler -Key Alt+j -BriefDescription ShowDirectoryMarks -LongDescription "Show the currently marked directories in a popup. [$($env:username)]" -ScriptBlock {
Set-PSReadlineKeyHandler -Key Alt+j -BriefDescription ShowDirectoryMarks -LongDescription "Show the currently marked directories. [$($env:username)]" -ScriptBlock {

$data = $global:PSReadlineMarks.GetEnumerator() | Where-object {$_.key} | Sort-object key
$data | foreach-object -begin {
Expand All @@ -199,36 +198,19 @@ Key`tDirectory

$text += "{0}`t{1}`n" -f $_.key, $_.value
}
$ws = New-Object -ComObject Wscript.Shell
$ws.popup($text, 10, "Use Ctrl+J to jump") | Out-Null

[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
if ($PSedition -eq 'Desktop') {
$ws = New-Object -ComObject Wscript.Shell
$ws.popup($text, 10, "Use Ctrl+J to jump") | Out-Null
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
} else {
write-host "`n$text`n" -ForegroundColor Yellow
}

}

if ($ver -lt 2.0.0) {
Set-PSReadlineKeyHandler -key Alt+F5 -BriefDescription "ListMyHandlers" -Description "List my PSReadlineHandlers [$env:username]" -ScriptBlock {

(Get-PSReadlineKeyHandler -bound ).Where( {$_.description -match "\[$env:username\]"}) |
Select-Object -Property Key,Description | Out-GridView -title "My handlers"
}
}
#endregion

Export-ModuleMember -Variable PSReadlineMarks

<#
$msg = @"
PSReadLineHelper
****************
Added these options:
$($(Get-myPSReadline | format-table | Out-string).Trim())
Added these handlers:
$($(Get-MyPSReadlineKey | Out-String).Trim())
"@
Export-ModuleMember -Variable PSReadlineMarks -Function 'Optimize-PSReadlineHistory','Get-MyPSReadline','Get-MyPSReadlineKey','Get-PSReadlineColorOptions',
'Show-PSReadlineColor','Import-PSReadlineColorOptions'

Write-Host $msg -ForegroundColor green
#>
106 changes: 84 additions & 22 deletions PSReadlineHelpers.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#helper functions for PSReadline v1.2
#helper functions for PSReadline

Function Get-PSReadlineColorOptions {
[cmdletbinding()]
Expand Down Expand Up @@ -29,20 +29,24 @@ Function Get-PSReadlineColorOptions {
}
}

Function Show-PSReadlineColor {
if ($ver -eq 1.2) {

[cmdletbinding()]
Param()
Function Show-PSReadlineColor {

$h = Get-PSReadlineColorOptions | sort-object TokenKind, Setting |
Group-object -Property TokenKind -AsHashTable -AsString
$h.GetEnumerator() | Sort-object Key | foreach-object {
write-host $($_.key).padright(20) -nonewline
$bg = $_.value.value[0]
$fg = $_.value.value[1]
write-Host (" [ {0} on {1} ]" -f $fg, $bg) -ForegroundColor $fg -BackgroundColor $bg
}
[cmdletbinding()]
Param()

#make it prettier on older versions.
$h = Get-PSReadlineColorOptions | sort-object TokenKind, Setting |
Group-object -Property TokenKind -AsHashTable -AsString
$h.GetEnumerator() | Sort-object Key | foreach-object {
write-host $($_.key).padright(20) -nonewline
$bg = $_.value.value[0]
$fg = $_.value.value[1]
write-Host (" [ {0} on {1} ]" -f $fg, $bg) -ForegroundColor $fg -BackgroundColor $bg
}
}
} #if v1.2

Function Import-PSReadlineColorOptions {

Expand All @@ -57,22 +61,52 @@ Function Import-PSReadlineColorOptions {

$data | where-object {$_.name -notmatch 'error|continuation|emphasis'} |
foreach-object {
$Token = $_.name
$Foreground = $_.group.where( {$_.setting -match "fore"}).value
$Background = $_.group.where( {$_.setting -match "back"}).value

if ($PSCmdlet.ShouldProcess($Token, "Set foreground to $Foreground and background to $background")) {
Set-PSReadlineOption -TokenKind $token -ForegroundColor $Foreground -BackgroundColor $Background
}
$Token = $_.name
$Foreground = $_.group.where( {$_.setting -match "fore"}).value
$Background = $_.group.where( {$_.setting -match "back"}).value

if ($ver -gt 1.2) {
$colors = @{
$token = $Foreground
}
$splat = @{
Colors = $colors
}
}
else {
$splat = @{
TokenKind = $token
ForegroundColor = $Foreground
BackgroundColor = $Background
}
}
if ($PSCmdlet.ShouldProcess($Token, "Set foreground to $Foreground and background to $background")) {
Set-PSReadlineOption @splat
}
}

$data | where-object {$_.name -match 'continuation'} |
foreach-object {
$Foreground = $_.group.where( {$_.setting -match "fore"}).value
$Background = $_.group.where( {$_.setting -match "back"}).value

if ($ver -gt 1.2) {
$colors = @{
ContinuationPrompt = $Foreground
}
$splat = @{
Colors = $colors
}
}
else {
$splat = @{
continuationPromptBackgroundColor = $background
ContinuationPromptForegroundColor = $Foreground
}
}

if ($PSCmdlet.ShouldProcess("ContinuationPrompt", "Set foreground to $Foreground and background to $background")) {
set-psreadlineoption -continuationPromptBackgroundColor $background -ContinuationPromptForegroundColor $Foreground
set-psreadlineoption @splat
}
}

Expand All @@ -81,8 +115,22 @@ Function Import-PSReadlineColorOptions {
$Foreground = $_.group.where( {$_.setting -match "fore"}).value
$Background = $_.group.where( {$_.setting -match "back"}).value

if ($ver -gt 1.2) {
$colors = @{
Emphasis = $Foreground
}
$splat = @{
Colors = $colors
}
}
else {
$splat = @{
EmphasisBackgroundColor = $background
EmphasisForegroundColor = $Foreground
}
}
if ($PSCmdlet.ShouldProcess("Emphasis", "Set foreground to $Foreground and background to $background")) {
set-psreadlineoption -EmphasisBackgroundColor $background -EmphasisForegroundColor $Foreground
set-psreadlineoption @splat
}
}

Expand All @@ -91,8 +139,22 @@ Function Import-PSReadlineColorOptions {
$Foreground = $_.group.where( {$_.setting -match "fore"}).value
$Background = $_.group.where( {$_.setting -match "back"}).value

if ($ver -gt 1.2) {
$colors = @{
Error = $Foreground
}
$splat = @{
Colors = $colors
}
}
else {
$splat = @{
ErrorBackgroundColor = $background
ErrorForegroundColor = $Foreground
}
}
if ($PSCmdlet.ShouldProcess("Error", "Set foreground to $Foreground and background to $background")) {
set-psreadlineoption -ErrorBackgroundColor $background -ErrorForegroundColor $Foreground
set-psreadlineoption @splat
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log for MyPSReadLine

## v0.7.0

+ file cleanup for the PowerShell Gallery
+ Modified some code to begin working with PSReadline 2.0 in PowerShell Core

## v0.6.0

+ code cleanup
Expand Down
2 changes: 1 addition & 1 deletion docs/Show-PSReadlineColor.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Show-PSReadlineColor [<CommonParameters>]

## DESCRIPTION

This command will display the current color settings for PSReadline using the corresponding foreground and background colors. Use Set-PSReadlineOption to change anything that is difficult to read.
This command will display the current color settings for PSReadline using the corresponding foreground and background colors. Use Set-PSReadlineOption to change anything that is difficult to read. This function is not needed in version 2.0 and later of PSReadline.

## EXAMPLES

Expand Down

0 comments on commit fec2213

Please sign in to comment.