-
Notifications
You must be signed in to change notification settings - Fork 6
/
Get-OKTokenColor.ps1
158 lines (128 loc) · 6.29 KB
/
Get-OKTokenColor.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function Get-OKTokenColor([System.Management.Automation.Language.TokenKind]$tokenKind,
[System.Management.Automation.Language.TokenFlags]$tokenFlags, $debugMode = $false) {
if ($debugMode) {
Write-Host "<# tk: $tokenKind, Tf: $tokenFlags #>" -f DarkMagenta -n;
}
# [Enum]::GetValues([System.Management.Automation.Language.TokenKind])
# [Enum]::GetValues([System.Management.Automation.Language.TokenFlags])
# First, we check the token kind...
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Number) {
return [System.ConsoleColor]::White;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Variable) {
return [System.ConsoleColor]::Cyan; #Green;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Parameter) {
return [System.ConsoleColor]::DarkGray;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::SplattedVariable) {
return [System.ConsoleColor]::Green;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Redirection) {
return [System.ConsoleColor]::White;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::NewLine) {
return [System.ConsoleColor]::Gray;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Comment) {
return [System.ConsoleColor]::DarkGreen;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Param) {
return [System.ConsoleColor]::Green;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Function) {
return [System.ConsoleColor]::Green;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::StringExpandable -or
$tokenKind -eq [System.Management.Automation.Language.TokenKind]::StringLiteral -or
$tokenKind -eq [System.Management.Automation.Language.TokenKind]::HereStringExpandable
) {
return [System.ConsoleColor]::DarkCyan;
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::LBracket -or
$tokenKind -eq [System.Management.Automation.Language.TokenKind]::RBracket) {
return [System.ConsoleColor]::DarkGray; # another operator
}
# Now we switch to checking tokenFlags...
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::AssignmentOperator)) {
return [System.ConsoleColor]::DarkGray; # same as 'operator'
}
#UnaryOperator
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::UnaryOperator)) {
return [System.ConsoleColor]::DarkGray; # same as 'operator'
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::CommandName)) {
return [System.ConsoleColor]::Yellow; # same as 'command'
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::TypeName)) {
return [System.ConsoleColor]::Gray; # same as 'type'
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::MemberName)) {
return [System.ConsoleColor]::White; # same as 'member'
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::SpecialOperator)) {
return [System.ConsoleColor]::DarkGray; # same as 'operator'
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::BinaryOperator)) {
return [System.ConsoleColor]::DarkGray; # same as 'operator'
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::Keyword)) {
return [System.ConsoleColor]::Green;
}
if ($tokenFlags.HasFlag([System.Management.Automation.Language.TokenFlags]::ParseModeInvariant)) {
return [System.ConsoleColor]::Gray; # same as 'operator'
}
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Identifier) {
return [System.ConsoleColor]::Gray;
}
# Last chance...
if ($tokenKind -eq [System.Management.Automation.Language.TokenKind]::Generic) {
return [System.ConsoleColor]::Gray;
}
#>
Write-Host "<# TokenKind: $tokenKind, TokenFlags: $tokenFlags #>" -f red -n;
return [System.ConsoleColor]::red;
#Write-Host "`nTokenKind: $tokenKind, TokenFlags: $tokenFlags" -f red;
<#
Attribute) { $c = [System.ConsoleColor]::Yellow;}
Command) { $c = [System.ConsoleColor]::Yellow;} # DONE
CommandArgument) { $c = [System.ConsoleColor]::Gray;}
CommandParameter) { $c = [System.ConsoleColor]::DarkGray;}
Comment) { $c = [System.ConsoleColor]::DakrGreen;}
GroupEnd) { $c = [System.ConsoleColor]::White;}
GroupStart) { $c = [System.ConsoleColor]::White;}
Keyword) { $c = [System.ConsoleColor]::White;}
LineContinuation) { $c = [System.ConsoleColor]::Gray;} # DONE
LoopLabel) { $c = [System.ConsoleColor]::White;}
Member) { $c = [System.ConsoleColor]::White;} # DONE (memberName)
NewLine) { $c = [System.ConsoleColor]::Yellow;} # DONE
Number) { $c = [System.ConsoleColor]::White;} # DONE
Operator) { $c = [System.ConsoleColor]::Gray;} # DONE
Position) { $c = [System.ConsoleColor]::Blue;}
StatementSeparator) { $c = [System.ConsoleColor]::Yellow;}
String) { $c = [System.ConsoleColor]::DarkCyan;} # DONE once
Type) { $c = [System.ConsoleColor]::Gray;} # DONE
Unknown) { $c = [System.ConsoleColor]::Gray;}
Variable) { $c = [System.ConsoleColor]::Green;} -- DONE
#>
# switch ($tokenKind) {
# ([System.Management.Automation.Language.TokenKind]::Variable) {
# Write-Host "`n$tokenKind" -f yellow -n;
# $c = [System.ConsoleColor]::Yellow;
# }
# ([System.Management.Automation.Language.TokenKind]::Comment) {
# Write-Host "`n$tokenKind" -f green -n;
# $c = [System.ConsoleColor]::Green;
# }
# ([System.Management.Automation.Language.TokenKind]::Comment) { c = [System.ConsoleColor]::Green; }
# # {
# # ([System.Management.Automation.PSTokenType]::Attribute) { $c = [System.ConsoleColor]::Yellow;}
# # ([System.Management.Automation.PSTokenType]::Command) { $c = [System.ConsoleColor]::Yellow;}
# default {
# #Write-Host $tokenKind -f red;
# Write-Host "`n$tokenKind $tokenFlags" -f darkred -n;
# $c = [System.ConsoleColor]::Gray;
# }
# }
#return [System.ConsoleColor]::gray;
}