dfinke / PowerShellHumanizer

PowerShell Humanizer wraps Humanizer: meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
Apache License 2.0
121 stars 18 forks source link

Consider adding formatting #3

Closed lzybkr closed 8 years ago

lzybkr commented 8 years ago

It might be useful to add custom formatting for some types, e.g. TimeSpan. The default display for TimeSpan isn't that good, the Humanize method looks much better, Use case:

Measure-Command { dir }

The output should be, well, humanized. In my own personal profile, I use something like:

<View>
<Name>Timespan</Name>
  <ViewSelectedBy>
    <TypeName>System.TimeSpan</TypeName>
  </ViewSelectedBy>
  <CustomControl>
    <CustomEntries>
      <CustomEntry>
        <CustomItem>
          <ExpressionBinding>
            <ScriptBlock>
$d = $_.Days; $h = $_.Hours; $m = $_.Minutes; $as = "","s"
$(if ($d) { "{0} day{1}," -f $d, $as[$d -gt 1] }
  if ($h) { "{0} hour{1}," -f $h, $as[$h -gt 1] }
  if ($m) { "{0} minute{1}," -f $m, $as[$m -gt 1] }
  "{0}.{1}s" -f $_.Seconds, $_.Milliseconds.ToString('D3')) -join " "
</ScriptBlock>
          </ExpressionBinding>
        </CustomItem>
      </CustomEntry>
    </CustomEntries>
  </CustomControl>
</View>   
dfinke commented 8 years ago

Thanks Jason, will do.

cdhunt commented 8 years ago

The format for TimeSpan is finished. I'm trying to get a format for DirectoryInfo and FileInfo, but I'm struggling.

Something like:

Mode   LastWritten    Length (MB) Name
----   -----------    ----------- ----
d----- 3 months ago               addins
d----- 2 months ago               appcompat
d----- 2 months ago               AppPatch
d----- 23 hours ago               AppReadiness
d-r--- 3 days ago                 assembly
d----- 3 months ago               bcastdvr
lzybkr commented 8 years ago

Neat idea. PowerShell Community Extensions does the "Length (MB)" thing, but not the cool LastWritten.

You can look at how they did it: http://pscx.codeplex.com/

cdhunt commented 8 years ago

Getting closer. Works for either DirectoryInfo or FileInfo, but not both. Back to reading the help.

posh> ls -file

Mode   LastWritten   Length Name
----   -----------   ------ ----
-a---l 3 months ago   61 KB bfsvc.exe
-a--s- 6 hours ago    66 KB bootstat.dat
-a---- 6 days ago     10 KB cfgall.ini
-a---- 4 months ago    1 KB cfgrs.ini
-a---- 4 months ago   134 B cfgrs_ex.ini
-a---- 2 months ago    6 KB comsetup.log
dfinke commented 8 years ago

Looking good

lzybkr commented 8 years ago

Look at C:\Windows\System32\WindowsPowerShell\v1.0\FileSystem.format.ps1xml - you'll see SelectionSetName - it lets you specify formatting for a group of related types. Also note EntrySelectedBy - this can be used to do formatting on just one of the types in the group.