Danny
Danny

Reputation: 33

Powershell stdout stream: 1 char at a time

The output of many commands in PS get dumped onto the display so abruptly, I don't even remember what command I typed-in. I have to scroll up a few pages to see.

Is it possible to slow the output stream down, so we can read it as it passes by on the screen? ..even if it's an error, if it comes in a civilized and readable stream, like closed captioning on C-SPAN, we could make better sense of even seemingly boring messages...

"More" works but it's a whole page at a time.

I also found this at techrepublic:

function EasyView { process { $_; Start-Sleep -seconds .5}}

Get-ChildItem C: | EasyView 

which is cool but it outputs 1 full record at a time..

I'm looking for streaming, 1 letter every .5 seconds for example. Like in the Matrix, or better yet like in War Games with Matthew Broderick...

Any thoughts?

Upvotes: 0

Views: 2320

Answers (1)

Matt
Matt

Reputation: 46700

By no means perfect but I am just playing here for fun trying to appease your odd but curious to me need

function get-easyview{

    param([int]$Milliseconds= 50)
    $text = $input | Out-String 

    [char[]]$text | ForEach-Object{
        Write-Host -NoNewline $_
        # Only break for a non-whitespace character.
        if($_ -notmatch "\s"){Sleep -Milliseconds $Milliseconds}
    }
}

Get-ChildItem | get-easyview -Milliseconds 50

This will take the input object and convert it into a single string that will then be case as a char array. Then it will painfully display a single character at a time with a gap of X Milliseconds between characters. I wanted to shoot myself in the face using seconds. I hear a typewriter in my head watching this. Note: This only outputs to the host console and not the standard output stream. This would have to be the last pipe command. There is no usable -passthru with this.

Can't find the reference but PowerShell can only go so slow. I don't think I could get the sleep between 1-20 milliseconds with a noticeable difference. I'm probably wrong with the numbers but it is a thing.

The output is not exactly as it is on screen. I'm working on it.

Don't you forget about More

Or the PowerShell equivalent out-host -paging. Using Enter you can "absorb" the information at your own pace. Read more from this answer

Get-EasyView 2.0

I was playing more and I though about setting a switch so that you could go by character or line using some simple parameteres.

function get-easyview{

    param(
        [int]$Milliseconds= 50,
        [ValidateSet("Line","Character")] 
        [String] 
        $Pace = "Character"
    )

    If($pace -eq "Character"){
        $text = [char[]]($input | Out-String)
        $parameters = @{NoNewline = $true}
    } Else {
        $text = ($input | out-string) -split "`r`n"
        $parameters = @{NoNewline = $false}
    }

    $text | ForEach-Object{
        Write-Host $_ @parameters
        if($_ -notmatch "^\s+$"){Sleep -Milliseconds $Milliseconds}
    }
}

So now you can do calls like this

Get-ChildItem | get-easyview -Milliseconds 50
Get-ChildItem | get-easyview -Milliseconds 1000 -Pace Line
Get-ChildItem | get-easyview -Milliseconds 50 -Pace Character

Upvotes: 2

Related Questions