# encoding: ascii
# api: powershell
# title: Format-TablePlus
# description: Robert,  the problem is that this script requires version 2.0 … that “GetSteppablePipeline” is a new feature. I forgot to mark it.
# version: 0.1
# type: function
# author: Robert Riegler
# license: CC0
# function: Format-TablePlus
# x-poshcode-id: 1209
# x-archived: 2009-07-21T09:52:01
#
# This is a wrapper function for Format-Table that adds a -Width parameter, and a -PadEnd parameter (without which it trims the end of every line of output. Set -PadEnd to get the original Format-Table behavior of adding needless whitespace on the end of every line of output.
#
#requires -version 2.0
## Format-Table with wrapping and string trimming.
function Format-TablePlus() {
[CmdletBinding()]
param(
   [Switch]
   ${AutoSize},

   [Switch]
   ${HideTableHeaders},

   [Switch]
   ${Wrap},

   [Parameter(Position=0)]
   [System.Object[]]
   ${Property},

   [System.Object]
   ${GroupBy},

   [System.String]
   ${View},

   [Switch]
   ${ShowError},

   [Switch]
   ${DisplayError},

   [Switch]
   ${Force},

   [ValidateSet('CoreOnly','EnumOnly','Both')]
   [System.String]
   ${Expand},

   [System.Int32]
   ${Width} = $Host.Ui.RawUI.BufferSize.Width,

   [Switch]
   ${PadEnd},

   [Parameter(ValueFromPipeline=$true)]
   [System.Management.Automation.PSObject]
   ${InputObject}
)

begin
{
   try {
      $outBuffer = $null
      if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
      {
         $PSBoundParameters['OutBuffer'] = 1
      }
      # need to get rid of these before we pass this to the Format-Table cmdlet
      $null = $PSBoundParameters.Remove("Width")
      $null = $PSBoundParameters.Remove("TrimEnd")
      
      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Format-Table', [System.Management.Automation.CommandTypes]::Cmdlet)
      
      ## I made the trimming optional, but defaulted it to on ;)
      $scriptCmd = &{ 
         if($PadEnd) {
            {& $wrappedCmd @PSBoundParameters | Out-String -Stream -Width $Width }
         } else {
            {& $wrappedCmd @PSBoundParameters | Out-String -Stream -Width $Width | % { $_.TrimEnd() } }
         }
      }
      $steppablePipeline = $scriptCmd.($myInvocation.CommandOrigin)
      $steppablePipeline.Begin($PSCmdlet) 
   } catch {
      throw
   }
}

process
{
   try {
      $steppablePipeline.Process($_)
   } catch {
      throw
   }
}

end
{
   try {
      $steppablePipeline.End()
   } catch {
      throw
   }
}
<#

.ForwardHelpTargetName Format-Table
.ForwardHelpCategory Cmdlet

#>
}
