Skip to content

Commit

Permalink
(GH-572) Fix: The handle is invalid
Browse files Browse the repository at this point in the history
When redirecting output, if anything attempts to write to the file
stream, it causes this error. The error is related to using Console
related properties and values in `PoshHostRawUserInterface`. The fix is
to use the `IConsole` adapter instead of calling `System.Console`
directly, as `IConsole` already has the ability to handle redirected
output.
  • Loading branch information
ferventcoder committed Apr 2, 2016
1 parent 7b6945b commit f0e6256
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,40 @@ namespace chocolatey.infrastructure.powershell
{
using System;
using System.Management.Automation.Host;
using adapters;
using Console = adapters.Console;

public class PoshHostRawUserInterface : PSHostRawUserInterface
{
public override ConsoleColor BackgroundColor
private static readonly Lazy<IConsole> _console = new Lazy<IConsole>(() => new Console());
private static IConsole Console { get { return _console.Value; } }

public override ConsoleColor BackgroundColor
{
get { return Console.BackgroundColor; }
get { return Console.BackgroundColor; }
set { Console.BackgroundColor = value; }
}

public override ConsoleColor ForegroundColor
{
get { return Console.ForegroundColor; }
set { Console.ForegroundColor = value; }
}

public override Size BufferSize
{
get { return new Size(Console.BufferWidth, Console.BufferHeight); }
get { return new Size(Console.BufferWidth, Console.BufferHeight); }
set { Console.SetBufferSize(value.Width, value.Height); }
}

public override Coordinates CursorPosition { get; set; }

public override int CursorSize
{
get { return Console.CursorSize; }
get { return Console.CursorSize; }
set { Console.CursorSize = value; }
}

public override ConsoleColor ForegroundColor
{
get { return Console.ForegroundColor; }
set { Console.ForegroundColor = value; }
}

public override bool KeyAvailable
{
get { return Console.KeyAvailable; }
Expand All @@ -61,21 +66,19 @@ public override Size MaxWindowSize
get { return new Size(Console.LargestWindowWidth, Console.LargestWindowHeight); }
}

public override Coordinates WindowPosition
{
get { return new Coordinates(Console.WindowLeft, Console.WindowTop); }
set { Console.SetWindowPosition(value.X, value.Y); }
}
public override Coordinates WindowPosition {

This comment has been minimized.

Copy link
@gep13

gep13 Apr 2, 2016

Member

@ferventcoder the formatting of this property seems a little inconsistent with the rest. Nothing major, just thought I would mention it when I saw it 😸

This comment has been minimized.

Copy link
@ferventcoder

ferventcoder Apr 2, 2016

Author Member

That does look off, thanks!

get { return new Coordinates(Console.WindowLeft, Console.WindowTop); }
set { Console.SetWindowPosition(value.X, value.Y); } }

public override Size WindowSize
{
get { return new Size(Console.WindowWidth, Console.WindowHeight); }
get { return new Size(Console.WindowWidth, Console.WindowHeight); }
set { Console.SetWindowSize(value.Width, value.Height); }
}

public override string WindowTitle
{
get { return Console.Title; }
get { return Console.Title; }
set { Console.Title = value; }
}

Expand Down Expand Up @@ -112,4 +115,4 @@ public override void ScrollBufferContents(Rectangle source, Coordinates destinat

#endregion
}
}
}

0 comments on commit f0e6256

Please sign in to comment.