# encoding: ascii
# api: powershell
# title: Out-IseFile
# description: PowerShells wrapping behaviour may be adequate for the console host. Using ISE it’s far from the optimal. But you don’t need to use the output pane. Send your output to a new ISE Editor. To see the difference just try:
# version: 0.1
# type: function
# license: CC0
# function: Out-IseFile
# x-poshcode-id: 1623
# x-archived: 2010-02-26T16:09:16
#
# get-help *  | out-ISEFile
#


function Out-IseFile
{

    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $True,  ValueFromPipeline = $True) ]        
        $msg, 
        [Parameter(Position = 1, Mandatory = $False, ValueFromPipeline = $False)]        
        $path = $null,
        [Parameter(Position = 2, Mandatory = $False, ValueFromPipeline = $False)]        
        $width = 2000,
        [switch]$fl,
        [switch]$passEditor
    )

    Begin
    {
        $cnt = 0
        $count   = $psise.CurrentPowerShellTab.Files.count
        $null    = $psIse.CurrentPowerShellTab.Files.Add()
        $Newfile = $psIse.CurrentPowerShellTab.Files[$count]
        if ($path)
        {    
            $NewFile.SaveAs($path)
            $NewFile.Save([Text.Encoding]::default)
        }
        $Editor = $Newfile.Editor
        
        filter Remove-TrailingBlanks { $_ -replace '(?m)\s*$', '' }
        #filter Remove-TrailingBlanks { $_.TrimEnd() }
    }

    Process
    {
        $cnt++
       # if ($msg) { $msg | gm | Write-Verbose }
       if ($cnt -eq 1)
       {
        $cmsg = @($msg)
       }
       else
       {
       $cmsg = $cmsg +  @($msg)
       }
    }
    
    End
    {   
        if ($cnt -gt 1)
        {
            $msg = $cmsg
        }
        if ($fl)
        {
            $msg = $msg |  Format-list | out-string -width $width | Remove-trailingBlanks
        }
        else
        {
            $msg = $msg | out-string -width $width | Remove-trailingBlanks
        }
        $msg = $msg +  "`r`n"

        #$Editor.SetCaretPosition($Editor.LineCount, 1)
        $Editor.InsertText($msg)

        if ($passEditor){ $Newfile }
        
        Write-Verbose "Anzahl = $cnt   len msg = $($msg.count)"
    }
}

<#

$profile.psextended | out-IseFile -fl -verb

get-help *  | out-string -width 2000| out-ISEFile -verb
get-help *  | out-ISEFile -verb
get-help *  | out-ISEFile 

 
 
$a =  $profile.psextended
out-IseFile  $a -fl


$a = get-help *  | out-string -width 2000 
out-IseFile  $a
#>
