-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsyntax-highlighting.psm1
More file actions
139 lines (116 loc) · 5.39 KB
/
Copy pathsyntax-highlighting.psm1
File metadata and controls
139 lines (116 loc) · 5.39 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
$script:ThrottleLimit = 50
$global:lastRender = Get-Date
$printableChars = [char[]] (0x20..0x7e + 0xa0..0xff)
# Combine normal keys with backspace/delete to trigger color resets
$allKeys = $printableChars + "Tab" + "Backspace" + "Delete"
$allKeys | ForEach-Object {
Set-PSReadLineKeyHandler -Key $_ `
-BriefDescription ValidatePrograms `
-LongDescription "Validate typed program's existence in path variable" `
-ScriptBlock {
param($key, $arg)
# 1. Force execution of native typing/deleting actions immediately
if ($key.Key -eq [System.ConsoleKey]::Tab) {
[Microsoft.PowerShell.PSConsoleReadLine]::TabCompleteNext($key)
} elseif ($key.Key -eq [System.ConsoleKey]::Backspace) {
[Microsoft.PowerShell.PSConsoleReadLine]::BackwardDeleteChar($key)
} elseif ($key.Key -eq [System.ConsoleKey]::Delete) {
[Microsoft.PowerShell.PSConsoleReadLine]::DeleteChar($key)
} else {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($key.KeyChar)
}
#
if (((get-date) - $global:lastRender).TotalMilliseconds -le $script:ThrottleLimit) {
return
}
# 2. Read live text string out of the prompt window buffer
$lineString = $null; $cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$lineString, [ref]$cursor)
if ([string]::IsNullOrWhiteSpace($lineString)) {
return
}
# Calculate accurate leading space padding metrics
$trimmedLine = $lineString.TrimStart()
$leadingSpacesCount = $lineString.Length - $trimmedLine.Length
# FIX: Regex matching guarantees it extracts the true full word safely, even without spaces
if ($trimmedLine -match '^([^\s()\[\]{}|;]+)') {
$tokenText = $Matches[1]
} else {
return
}
# Skip checking symbols, comments, exit, and variables
if ([string]::IsNullOrEmpty($tokenText) -or
$tokenText -like '$*' -or
$tokenText -like '#*' -or
$tokenText -in (
'function', 'exit', 'filter', 'workflow', 'class', 'enum',
'if', 'elseif', 'switch', 'foreach', 'from', 'for', 'while',
'do', 'try', 'trap', 'throw', 'return', 'break', 'continue',
'data', 'parallel', 'sequence', 'define'
)
) {
return
}
# 3. Locate precise prompt coordinates on screen
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition(0)
$cursorPosX = $host.UI.RawUI.CursorPosition.X
$cursorPosY = $host.UI.RawUI.CursorPosition.Y
[Microsoft.PowerShell.PSConsoleReadLine]::SetCursorPosition($cursor)
$tokenLength = $tokenText.Length
# 4. Check system PATH for command presence
$color = "Red"
if (Get-Command $tokenText -ErrorAction Ignore) {
$color = "Green"
}
# Handle cleanup state if a command was completely deleted
if ($tokenLength -le 1 -and ($key.Key -eq [System.ConsoleKey]::Backspace -or $key.Key -eq [System.ConsoleKey]::Delete)) {
$color = "White"
}
# Map the boundary vectors safely
$sX = $cursorPosX + $leadingSpacesCount
$Y = $cursorPosY
$eX = ($sX + $tokenLength)
$nextLine = $false
$painted = 0
$bufSize = $host.UI.RawUI.BufferSize.Width
# 5. Core rendering engine loop
while ($painted -ne $tokenLength) {
$scanXEnd = $eX
if ($eX -gt $bufSize) {
$scanXEnd = $bufSize
$eX = $eX - $bufSize
$nextLine = $true
}
$finalRec = New-Object System.Management.Automation.Host.Rectangle($sX, $Y, $scanXEnd, $Y)
$finalBuf = $host.UI.RawUI.GetBufferContents($finalRec)
for ($xPosition = 0; $xPosition -lt ($scanXEnd - $sX); $xPosition++) {
$bufferItem = $finalBuf.GetValue(0, $xPosition)
if ($bufferItem) {
$bufferItem.ForegroundColor = $color
$finalBuf.SetValue($bufferItem, 0, $xPosition)
}
$painted++
}
$coords = New-Object System.Management.Automation.Host.Coordinates $sX, $Y
$host.ui.RawUI.SetBufferContents($coords, $finalBuf)
if ($nextLine) {
$sX = 0
$Y++
$nextLine = $false
}
}
$global:lastRender = Get-Date
}
}
function Set-SyntaxHighlightingThrottle {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[int]$Milliseconds
)
# Update the module-scoped tracking variable
$script:ThrottleLimit = $Milliseconds
Write-Verbose "Syntax-highlighting throttle updated to ${Milliseconds}ms."
}
# Create a clean user-facing alias matching your desired command format
Set-Alias -Name syntax-highlighting -Value Set-SyntaxHighlightingThrottle