尝试数组切片get-psreadlineoption命令。我想选择所有的字符串包含‘颜色’,然后输入到‘设置-psreadline’设置所有颜色。我不明白在powershell中这一切是如何工作的。这是个数组,对吧?我可以这样循环:
foreach($a in $i) { $i; if ($i -contains 'MemberColor') {$i;} }
但这并没有提供任何产出。我也不能将它作为数组访问:
get-psreadlineoption[21..36] get-psreadlineoption['EditMode']
不管用。基本上我想做的是:
foreach (get-psreadlines[21..36]) { $array = append(<string that contains 'Color'>) } foreach ($a in $array) { set-psreadline($a, ...)}
我该怎么做呢?
(P.S.我只想以尽可能简洁的方式将所有这些颜色设置为相同的颜色)
发布于 2022-01-04 16:41:31
这是一个常见的问题,如何遍历属性在PSObject属性上迭代PowerShell。您也可以使用get成员获取属性列表.你也可以管道到“选择*颜色”。
Get-PSReadLineOption | get-member *color | % name
CommandColor
CommentColor
ContinuationPromptColor
DefaultTokenColor
EmphasisColor
ErrorColor
KeywordColor
MemberColor
NumberColor
OperatorColor
ParameterColor
PredictionColor
SelectionColor
StringColor
TypeColor
VariableColor发布于 2022-01-04 15:03:53
解决方案取决于您使用的PSReadLine模块的哪个版本:
Install-Module PSReadLine)中按需安装2.x。使用(Get-Module PSReadLine).Version.ToString()查看您使用的是哪个版本。
PSReadline v2.x解决方案:
若要获取所有颜色属性的名称,通过过滤以color结尾的名称(情况不敏感),使用反射,PowerShell通过属性提供方便。
(Get-PSReadLineOption).psobject.Properties.Name -like '*color'注意:
Get-PSReadLineOption | Select-Object *color.但是,结果是一个[pscustomobject],而不是一个哈希表,为了将前者转换为后者,您需要同样的技术,如下所示。Set-PSReadLineOption (如下面所示)设置颜色的一种替代方法,您还可以使用底部部分中为PSReadLine v1.x描述的方法(例如,(Get-PSReadlineOption).CommentColor = 'DarkGray'),在这种情况下,您可以使用Get-PSReadLineOption | Select-Object *color输出中显示的属性名称。注意,属性名在v2.x和v1.x之间不同;而且,与下面的Set-PSReadLineOption -Colors一样,设置背景色需要使用VT转义序列。警告:这些transformation属性名称需要才能用作哈希表的密钥,您可以传递给 Set-PSReadLineOption -Colors (从PSReadLine 2.1.0开始):
必须删除后缀Color,如果是DefaultTokenColor,则必须删除后缀TokenColor,下面的-replace操作是这样做的:
(Get-PSReadLineOption).psobject.Properties.Name -like '*color' `
-replace '(Token)?Color$'这将产生以下颜色键名(截至PSReadLine 2.1.0):
ContinuationPrompt
Default
Comment
Keyword
String
Operator
Variable
Command
Parameter
Type
Number
Member
Emphasis
Error
Selection
InlinePrediction您现在可以传递一个有选择地更新颜色的散列表;例如,下面将Comment (CommentColor)颜色设置为深灰色(同时保留所有其他颜色不变):
Set-PSReadLineOption -Color @{ Comment = 'DarkGray' }颜色值可以是:
[ConsoleColor]枚举类型的值之一;例如,[ConsoleColor]::DarkGray,您可以将其作为字符串'DarkGray'传递,如上面所示。47)上的注释颜色设置为深灰色(90)
Set-PSReadLineOption -Color @{注释=“e[90;47m”}若要创建包含所有颜色键的哈希表,请使用当前属性值作为起点,请使用以下命令:
$allColors = @{}
(Get-PSReadLineOption).psobject.Properties.
Where({ $_.Name -like '*color' }).
ForEach({ $allColors[$_.Name -replace '(Token)?Color$'] = $_.Value })注意:当您打印$allColors时,Value列可能看起来是空的,但值是存在的,其形式是-自身不可见的虚拟终端(VT)控制序列。
PSReadline v1.x解决方案(PowerShell的旧版本):
与v2.x版本不同的是:
Set-PSReadlineOption对各种颜色使用-TokenKind和-ForegroundColor / -BackgroundColor参数的组合,对于一般颜色设置,使用单个参数(例如-ErrorForegroundColor) (而不是接受哈希表的单个v2.x -Colors参数);此外,所有颜色设置都分为前景值和背景值。[ConsoleColor]枚举类型的值,例如'DarkGray' ([ConsoleColor]::DarkGray)。对于设置颜色Get-PSReadLineOption的统一方法,可以放弃Set-PSReadLineOption和直接设置E 2107E 1111返回E 2112的 Microsoft.PowerShell.PSConsoleReadLineOptions 实例的属性。
例如,下面将注释的前景色设置为深灰色:
(Get-PSReadlineOption).CommentForegroundColor = 'DarkGray'这相当于以下v1.x Set-PSReadLineOption调用:
Set-PSReadLineOption -TokenKind Comment -ForegroundColor DarkGray要获取所有与颜色相关的属性的名称,请使用与v2.x相同的方法:
(Get-PSReadLineOption).psobject.Properties.Name -like '*color'这将产生以下颜色属性名称
ContinuationPromptForegroundColor
ContinuationPromptBackgroundColor
DefaultTokenForegroundColor
CommentForegroundColor
KeywordForegroundColor
StringForegroundColor
OperatorForegroundColor
VariableForegroundColor
CommandForegroundColor
ParameterForegroundColor
TypeForegroundColor
NumberForegroundColor
MemberForegroundColor
DefaultTokenBackgroundColor
CommentBackgroundColor
KeywordBackgroundColor
StringBackgroundColor
OperatorBackgroundColor
VariableBackgroundColor
CommandBackgroundColor
ParameterBackgroundColor
TypeBackgroundColor
NumberBackgroundColor
MemberBackgroundColor
EmphasisForegroundColor
EmphasisBackgroundColor
ErrorForegroundColor
ErrorBackgroundColorhttps://stackoverflow.com/questions/70578032
复制相似问题