1

Experts, I'm building a long list of Visual Studio projects using msbuild. And while hiding the msbuild output, displaying a status and an animation based upon some characters is displayed while msbuild doing its job. Here my script to build using msbuild and display wait animation (You will surely like this...):

$anim=@("|","/","-","\","|") # Animation sequence characters
$ReturnCode = @{}
$BuildStatus="Building $(ProjectName)" 

$CursorTop=[Console]::CursorTop #// Cursor position on Y axis
$CursorLeft=$BuildStatus.Length #// Cursor position on X axis 
Write-Host $BuildStatus -ForegroundColor Cyan

#// starting the MsBuild job in background.

$MsbJob = [PowerShell]::Create().AddScript(
{
  param($MsbArgs, $Result)
  & msbuild $MsbArgs | Out-Null
  $Result.Value = $LASTEXITCODE
}
).AddArgument($MsbArgs).AddArgument($ReturnCode)

$async = $MsbJob.BeginInvoke() #// start executing the job.

#// While above script block is doing its job in background, display status and animation in console window.

while (!$async.IsCompleted)
{
    foreach ($item in $anim) #// looping on array containing characters to form animation
    {

       [Console]::SetCursorPosition($CursorLeft + 5,$CursorTop) #//setting position for cursor
       Write-Host $item -ForegroundColor Yellow 
       Start-Sleep -m 50
    }
}

$MsbJob.EndInvoke($async)

most of the time, it works as expected i.e. showing the status like:

Building MyProject (Animating characters)..

but suddenly, it becomes like this:enter image description here

Only Discovered Solution: I was able to correct this is to increase the Screen Buffer Size of Console (Console Properties --> Layout: Screen Buffer Size) to 1024. In addition, I also increased Window Size to fill out the monitor and it didn't break like that.

Is my assessment about Screen Buffer is correct? or anything else is breaking up? If Yes, do I have to increase the screen buffer size pragmatically i.e. from inside my .psm1 script.?

Any help would be really appreciated.

2 Answers 2

5

By default Write-Host puts a newline after the printed string. Instead of re-positioning the cursor try printing the output without the newline, so you can use backspace (`b) to erase the character printed before:

while (!$async.IsCompleted) {
  $anim | % {
    Write-Host "`b$_" -NoNewline -ForegroundColor Yellow 
    Start-Sleep -m 50
  }
}
1
  • Just wondering why it works as expected for some time i.e. It does reposition cursor, but what I can't figure-out where it forgets the cursor's position on Y axis..... Oct 9, 2013 at 18:41
0

So, after digging a little, I find one method which I was also expecting it to work i.e. Increase Screen Buffer Values of Console Window hosting all my crap. So added this line in my script which seems to be preventing the character animation from breaking up:

[Console]::SetBufferSize(512,512)

I also experimented by giving it very low values i.e. 15,5 and yes I reproduced above problem.

So at this time, this seems to be a solution. However, I'm still testing it with time to time...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.