what emits the header stuff when I display a directory entry?
Embarrassing to admit, but after working with Monad when it was in beta, I've somehow neglected PowerShell ever since. So I'm just now dipping my toes in.
I wanted a function to colorize directory listing and I found one on the web:
function LL {
param ($dir = ".", $all = $false)
$origFg = $host.ui.rawui.foregroundColor
if ( $all ) { $toList = ls -force $dir }
else { $toList = ls $dir }
foreach ($Item in $toList) {
Switch ($Item.Extension) {
".Exe" {$host.ui.rawui.foregroundColor = "Yellow"}
".cmd" {$host.ui.rawui.foregroundColor = "Red"}
".msh" {$host.ui.rawui.foregroundColor = "Red"}
".vbs" {$host.ui.rawui.foregroundColor = "Red"}
Default {$host.ui.rawui.foregroundColor = $origFg}
}
## if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "White"}
if ($item.PSIsContainer) {$host.ui.rawui.foregroundColor = "White"}
$item
}
$host.ui.rawui.foregroundColor = $origFg
}
The only problem is that the header stuff in the resulting display is always shown the color assigned to the first entry in the directory.
So I decided to look at the array assigned to the $tolist variable:
09:47:10|# $tolist = ls
09:47:26|# $开发者_C百科tolist[0]
Directory: D:\Documents and Settings\200018252
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 7/15/2011 8:15 AM .ssh
09:47:37|# $tolist[9]
Directory: D:\Documents and Settings\200018252
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/10/2011 1:14 PM vimperator
09:48:19|#
So when displayed every element of $tolist is prefaced with a blank line, then one showing the directory, another blank line and then the column headers.
I need to control the foreground color for this information. What emits it?
What emits it will be one of the format-* commands, either format-table or format-list, depending on the object type and how many properties are in the default member set.
It uses format-table up to a certain number of members, then reverts to format list if there are too many for format-table (I don't remember where that threshold number is set). The properties included in the default member set are defined in the types.ps1xml file:
http://technet.microsoft.com/en-us/library/dd347581.aspx
You can modify this file, but there doesn't appear to be any provision for specifying display colors.
Very hackish way of getting a header colour ( I was waiting to see if someone posts something that wasn't an hack like this, but since no one has posted, here is my solution):
$dummy = "justadummynametoaddtothebeginningofthelistofitems"
$headerColor = "green"
function LL {
param ($dir = ".", $all = $false)
new-object System.IO.FileInfo $dummy
$origFg = "white"
if ( $all ) { $toList = ls -force $dir }
else { $toList = ls $dir }
foreach ($Item in $toList) {
Switch ($Item.Extension) {
".Exe" {$host.ui.rawui.foregroundColor = "Yellow"}
".cmd" {$host.ui.rawui.foregroundColor = "Red"}
".msh" {$host.ui.rawui.foregroundColor = "Red"}
".vbs" {$host.ui.rawui.foregroundColor = "Red"}
Default {$host.ui.rawui.foregroundColor = $origFg}
}
## if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "White"}
if ($item.PSIsContainer) {$host.ui.rawui.foregroundColor = "White"}
$item
}
$host.ui.rawui.foregroundColor = $origFg
}
function CheckDummy{
param($name,$otherProp)
$origFg = $host.ui.rawui.foregroundColor
if($name -ne $dummy){
$host.ui.rawui.foregroundColor = $origFg
$otherProp
}
else{
$host.ui.rawui.foregroundColor = $headerColor
$null
}
}
function LLNew{
$a = @{Name="Mode";Expression = {CheckDummy $_.Name $_.Mode; }},
@{Name="LastWriteTime";Expression = {CheckDummy $_.Name $_.LastWriteTime}},
@{Name="Length";Expression = {CheckDummy $_.Name$_.Length}},
@{Name="Name";Expression = {CheckDummy $_.Name $_.Name}}
LL | ft $a
}
Probably not worth it to get the header colour, but had to try :)
Btw, easier way to get colouring like you want is like below:
ls | %{
if($_.extension -match ".exe"){
[console]::ForegroundColor="green"; $_;
} else {
[console]::ForegroundColor="white"; $_;
}
}
Work in progress...
This is handled by the type formatting system of PowerShell. The definitions for them are stored in XML files. You can change these formatting rules using the steps below.
First, get a copy of the current rules. You can use two methods.
## 1, export the rules (but the result will not be nicely formatted XML, so needs some cleanup)
Get-FormatData -TypeName FileSystemTypes | Export-FormatData test.format.ps1xml
## 2, grab the source XML directly (be sure to remove the signature from the bottom of the file)
Copy-Item C:\Windows\System32\WindowsPowerShell\v1.0\FileSystem.format.ps1xml .
Next, we need to modify the rules to do the coloring. (I need to come back to this once I have figured out the edits needed).
## TODO: modify the formatting from controls to direct host output
Last, we have our profile update the format data when PowerShell is loaded.
Update-FormatData -PrependPath test.format.ps1xml
精彩评论