Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active April 12, 2022 09:57
Show Gist options
  • Save ninmonkey/18203a0bc2b9fae38c95d9abcd9e3c12 to your computer and use it in GitHub Desktop.
Save ninmonkey/18203a0bc2b9fae38c95d9abcd9e3c12 to your computer and use it in GitHub Desktop.
format.ps1xml example : file list

Your format file will have a section like this

<TableColumnItem>
    <PropertyName>Mode</PropertyName>
</TableColumnItem>

You can replace it with a <ScriptBlock>

<TableColumnItem>
    <ScriptBlock>
        [string]$textAcc = ''
        switch($_.Mode[0]) {
            'd' {
                $textAcc += '📁'
                continue;
            }
            'l' {
                $textAcc += 'lnk'
                continue;
            }
            default {
                $textACC += '📄'
                continue;
            }
        }        
        return $textAcc -join ''        
    </ScriptBlock>
</TableColumnItem>

Docs

Utilities

$APP_PATH_ROOT = Get-Item -ea stop $PSScriptRoot
function _exportFormatData {
<#
.synopsis
export formatting and generate file to load from if not existing
#>
param(
# typename
[Parameter(mandatory, valuefromPipeline)]
[string]$TypeName
)
$SubDir = 'format.ps1xml'
Write-Verbose "Type: '$TypeName'"
if (! (Test-Path "$APP_PATH_ROOT\$SubDir")) {
New-Item -Path "$APP_PATH_ROOT\$SubDir" -ItemType Directory
# throw 'folder'
}
$Path_Original = "$APP_PATH_ROOT\$SubDir\format-$TypeName.ps1xml"
$Path_Custom = "$APP_PATH_ROOT\$SubDir\format-$TypeName-nin.ps1xml"
$Fd = Get-FormatData -TypeName $TypeName -PowerShellVersion 7.1 #-PowerShellVersion 5.1
if ($fd) {
if (! (Test-Path $Path_Original) ) {
Write-Verbose "New type, creating: '$Path_Original'"
# you don't want to export the modified version if already existing
Export-FormatData -InputObject $Fd -Path $Path_Original -IncludeScriptBlock
}
} else {
# Label TypeNameNotFound $TypeName -fg orange
write-error "TypeNameNotFound: $TypeName"
}
if (! (Test-Path $Path_Custom) ) {
Write-Verbose "New type, creating: '$Path_Custom'"
Copy-Item -Path $Path_Original -Destination $Path_Custom
}
$customFormat = Get-Item -ea stop $Path_Custom
Update-FormatData -PrependPath $customFormat
Write-Verbose 'success'
}
$typeList = @(
'System.RuntimeType'
'System.IO.FileInfo'
'System.IO.DirectoryInfo'
'System.IO.FileSystemInfo'
) | Sort-Object -Unique
foreach ($name in $typeList) {
_exportFormatData $name -Verbose
}
<!--
from
https://gist.github.com/SeeminglyScience/35c67a450367a493e56b0ab46623bf47#file-profile-format-ps1xml
-->
<Configuration>
<SelectionSets>
<SelectionSet>
<Name>CustomGCI</Name>
<Types>
<TypeName>System.IO.FileSystemInfo</TypeName>
<TypeName>System.IO.FileInfo</TypeName>
<TypeName>System.IO.DirectoryInfo</TypeName>
</Types>
</SelectionSet>
</SelectionSets>
<ViewDefinitions>
<View>
<Name>CustomGci</Name>
<ViewSelectedBy>
<SelectionSetName>CustomGCI</SelectionSetName>
</ViewSelectedBy>
<Controls>
<Control>
<Name>FSIGroup</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<Frame>
<LeftIndent>4</LeftIndent>
<CustomItem>
<Text> Directory: </Text>
<ExpressionBinding>
<ScriptBlock>
$_.PSParentPath.Replace('Microsoft.PowerShell.Core\FileSystem::', '')
</ScriptBlock>
</ExpressionBinding>
<NewLine />
</CustomItem>
</Frame>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
<GroupBy>
<PropertyName>PSParentPath</PropertyName>
<CustomControlName>FSIGroup</CustomControlName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Alignment>Left</Alignment>
<Label>Mode</Label>
<Width>4</Width>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Right</Alignment>
<Label>LastWriteTime</Label>
<Width>26</Width>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Right</Alignment>
<Label>Length</Label>
<Width>10</Width>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Left</Alignment>
<Label>Name</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<ScriptBlock>
$text = (&amp; {
$mode = $_.Mode
if ($mode[0] -eq 'l') {
# nf-fa-link
[char]0xf0c1
} elseif ($mode[0] -eq 'd') {
# nf-mdi-folder
[char]0xf74a
} else {
# nf-fa-file
[char]0xf15b
}
if ($mode[1] -eq 'a') {
# nf-mdi-archive
[char]0xf53b
}
if ($mode[2] -eq 'r') {
# nf-fa-lock
[char]0xf023
}
if ($mode[3] -eq 'h') {
# nf-mdi-file-hidden
[char]0xfb12
}
if ($mode[4] -eq 's') {
# nf-custom-windows
[char]0xe62a
}
}) -join ' '
return "$([char]27)[38;2;255;153;51m$text$([char]27)[0m"
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
[string]::Format(
"{2}{0,10} {1,8}{3}",
$_.LastWriteTime.ToString("d"),
$_.LastWriteTime.ToString("t"),
"$([char]27)[38;2;102;153;255m",
"$([char]27)[0m")
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
if ($_ -is [IO.DirectoryInfo]) {
return ''
}
if ($_.Length -gt 1GB) {
return '{1}{0:N2}GB{2}' -f (
($_.Length / 1GB),
"$([char]27)[38;2;255;153;0m",
"$([char]27)[0m")
}
if ($_.Length -gt 1MB) {
return '{1}{0:N2}MB{2}' -f (
($_.Length / 1MB),
"$([char]27)[38;2;255;255;0m",
"$([char]27)[0m")
}
if ($_.Length -gt 1KB) {
return '{1}{0:N2}KB{2}' -f (
($_.Length / 1KB),
"$([char]27)[38;2;153;255;51m",
"$([char]27)[0m")
}
return '{1}{0:N2}B{2}' -f (
$_.Length,
"$([char]27)[38;2;0;255;51m",
"$([char]27)[0m")
</ScriptBlock>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>
'{1}{0}{2}' -f (
$_.Name,
"$([char]27)[38;2;102;204;255m",
"$([char]27)[0m")
</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment