/// <summary>
        /// 
        /// Constructs an instance 
        /// 
        /// </summary>
        /// <param name="parent"></param>
        /// <exception/>

        internal ConsoleHostUserInterface(ConsoleHost parent)
        {
            Dbg.Assert(parent != null, "parent may not be null");

            _parent = parent;
            _rawui = new ConsoleHostRawUserInterface(this);

#if UNIX
            SupportsVirtualTerminal = true;
#else
            try
            {
                // Turn on virtual terminal if possible.

                // This might throw - not sure how exactly (no console), but if it does, we shouldn't fail to start.
                var handle = ConsoleControl.GetActiveScreenBufferHandle();
                var m = ConsoleControl.GetMode(handle);
                if (ConsoleControl.NativeMethods.SetConsoleMode(handle.DangerousGetHandle(), (uint)(m | ConsoleControl.ConsoleModes.VirtualTerminal)))
                {
                    // We only know if vt100 is supported if the previous call actually set the new flag, older
                    // systems ignore the setting.
                    m = ConsoleControl.GetMode(handle);
                    this.SupportsVirtualTerminal = (m & ConsoleControl.ConsoleModes.VirtualTerminal) != 0;
                }
            }
            catch
            {
            }
#endif

            _isInteractiveTestToolListening = false;
        }
Example #2
0
        public override void SetBufferContents(Coordinates origin, BufferCell[,] contents)
        {
            ConsoleControl.CONSOLE_SCREEN_BUFFER_INFO cONSOLESCREENBUFFERINFO;
            if (contents == null)
            {
                PSTraceSource.NewArgumentNullException("contents");
            }
            SafeFileHandle bufferInfo = ConsoleHostRawUserInterface.GetBufferInfo(out cONSOLESCREENBUFFERINFO);

            ConsoleHostRawUserInterface.CheckCoordinateWithinBuffer(ref origin, ref cONSOLESCREENBUFFERINFO, "origin");
            ConsoleControl.WriteConsoleOutput(bufferInfo, origin, contents);
        }
Example #3
0
 public override BufferCell[,] GetBufferContents(Rectangle region)
 {
     ConsoleControl.CONSOLE_SCREEN_BUFFER_INFO cONSOLESCREENBUFFERINFO;
     if (region.Right >= region.Left)
     {
         if (region.Bottom >= region.Top)
         {
             SafeFileHandle bufferInfo = ConsoleHostRawUserInterface.GetBufferInfo(out cONSOLESCREENBUFFERINFO);
             int            x          = cONSOLESCREENBUFFERINFO.BufferSize.X;
             int            y          = cONSOLESCREENBUFFERINFO.BufferSize.Y;
             if (region.Left >= x || region.Top >= y || region.Right < 0 || region.Bottom < 0)
             {
                 ConsoleHostRawUserInterface.tracer.WriteLine("region outside boundaries", new object[0]);
                 return(new BufferCell[0, 0]);
             }
             else
             {
                 int         num        = Math.Max(0, region.Left);
                 int         num1       = Math.Min(x - 1, region.Right);
                 int         num2       = Math.Max(0, region.Top);
                 int         num3       = Math.Min(y - 1, region.Bottom);
                 Coordinates coordinate = new Coordinates(num, num2);
                 Rectangle   left       = new Rectangle();
                 left.Left   = Math.Max(0, -region.Left);
                 left.Top    = Math.Max(0, -region.Top);
                 left.Right  = left.Left + num1 - num;
                 left.Bottom = left.Top + num3 - num2;
                 BufferCell[,] bufferCellArray = new BufferCell[region.Bottom - region.Top + 1, region.Right - region.Left + 1];
                 ConsoleControl.ReadConsoleOutput(bufferInfo, coordinate, left, ref bufferCellArray);
                 return(bufferCellArray);
             }
         }
         else
         {
             object[] objArray = new object[2];
             objArray[0] = "region.Bottom";
             objArray[1] = "region.Top";
             throw PSTraceSource.NewArgumentException("region", "ConsoleHostRawUserInterfaceStrings", "InvalidRegionErrorTemplate", objArray);
         }
     }
     else
     {
         object[] objArray1 = new object[2];
         objArray1[0] = "region.Right";
         objArray1[1] = "region.Left";
         throw PSTraceSource.NewArgumentException("region", "ConsoleHostRawUserInterfaceStrings", "InvalidRegionErrorTemplate", objArray1);
     }
 }
Example #4
0
        public override void SetBufferContents(Rectangle region, BufferCell fill)
        {
            ConsoleControl.CONSOLE_SCREEN_BUFFER_INFO cONSOLESCREENBUFFERINFO;
            uint num = 0;

            if (region.Right >= region.Left)
            {
                if (region.Bottom >= region.Top)
                {
                    SafeFileHandle bufferInfo = ConsoleHostRawUserInterface.GetBufferInfo(out cONSOLESCREENBUFFERINFO);
                    int            x          = cONSOLESCREENBUFFERINFO.BufferSize.X;
                    int            y          = cONSOLESCREENBUFFERINFO.BufferSize.Y;
                    ushort         wORD       = ConsoleControl.ColorToWORD(fill.ForegroundColor, fill.BackgroundColor);
                    Coordinates    coordinate = new Coordinates(0, 0);
                    if (region.Left != -1 || region.Right != -1 || region.Top != -1 || region.Bottom != -1)
                    {
                        if (region.Left >= x || region.Top >= y || region.Right < 0 || region.Bottom < 0)
                        {
                            ConsoleHostRawUserInterface.tracer.WriteLine("region outside boundaries", new object[0]);
                            return;
                        }
                        else
                        {
                            int num1 = Math.Max(0, region.Left);
                            int num2 = Math.Min(x - 1, region.Right);
                            int num3 = num2 - num1 + 1;
                            coordinate.X = num1;
                            int num4 = Math.Max(0, region.Top);
                            int num5 = Math.Min(y - 1, region.Bottom);
                            coordinate.Y = num4;
                            if (ConsoleControl.IsCJKOutputCodePage(out num))
                            {
                                Rectangle rectangle = new Rectangle(0, 0, 1, num5 - num4);
                                int       num6      = this.LengthInBufferCells(fill.Character);
                                if (coordinate.X > 0)
                                {
                                    BufferCell[,] bufferCellArray = new BufferCell[rectangle.Bottom + 1, 2];
                                    ConsoleControl.ReadConsoleOutputCJK(bufferInfo, num, new Coordinates(coordinate.X - 1, coordinate.Y), rectangle, ref bufferCellArray);
                                    int num7 = 0;
                                    while (num7 <= rectangle.Bottom)
                                    {
                                        if (bufferCellArray[num7, 0].BufferCellType != BufferCellType.Leading)
                                        {
                                            num7++;
                                        }
                                        else
                                        {
                                            throw PSTraceSource.NewArgumentException("fill");
                                        }
                                    }
                                }
                                if (num2 != x - 1)
                                {
                                    BufferCell[,] bufferCellArray1 = new BufferCell[rectangle.Bottom + 1, 2];
                                    ConsoleControl.ReadConsoleOutputCJK(bufferInfo, num, new Coordinates(num2, coordinate.Y), rectangle, ref bufferCellArray1);
                                    if (num3 % 2 != 0)
                                    {
                                        int num8 = 0;
                                        while (num8 <= rectangle.Bottom)
                                        {
                                            if (!(bufferCellArray1[num8, 0].BufferCellType == BufferCellType.Leading ^ num6 == 2))
                                            {
                                                num8++;
                                            }
                                            else
                                            {
                                                throw PSTraceSource.NewArgumentException("fill");
                                            }
                                        }
                                    }
                                    else
                                    {
                                        int num9 = 0;
                                        while (num9 <= rectangle.Bottom)
                                        {
                                            if (bufferCellArray1[num9, 0].BufferCellType != BufferCellType.Leading)
                                            {
                                                num9++;
                                            }
                                            else
                                            {
                                                throw PSTraceSource.NewArgumentException("fill");
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (num6 == 2)
                                    {
                                        throw PSTraceSource.NewArgumentException("fill");
                                    }
                                }
                                if (num3 % 2 == 1)
                                {
                                    num3++;
                                }
                            }
                            for (int i = num4; i <= num5; i++)
                            {
                                coordinate.Y = i;
                                ConsoleControl.FillConsoleOutputCharacter(bufferInfo, fill.Character, num3, coordinate);
                                ConsoleControl.FillConsoleOutputAttribute(bufferInfo, wORD, num3, coordinate);
                            }
                            return;
                        }
                    }
                    else
                    {
                        if (x % 2 != 1 || !ConsoleControl.IsCJKOutputCodePage(out num) || this.LengthInBufferCells(fill.Character) != 2)
                        {
                            int num10 = x * y;
                            ConsoleControl.FillConsoleOutputCharacter(bufferInfo, fill.Character, num10, coordinate);
                            ConsoleControl.FillConsoleOutputAttribute(bufferInfo, wORD, num10, coordinate);
                            return;
                        }
                        else
                        {
                            throw PSTraceSource.NewArgumentException("fill");
                        }
                    }
                }
                else
                {
                    object[] objArray = new object[2];
                    objArray[0] = "region.Bottom";
                    objArray[1] = "region.Top";
                    throw PSTraceSource.NewArgumentException("region", "ConsoleHostRawUserInterfaceStrings", "InvalidRegionErrorTemplate", objArray);
                }
            }
            else
            {
                object[] objArray1 = new object[2];
                objArray1[0] = "region.Right";
                objArray1[1] = "region.Left";
                throw PSTraceSource.NewArgumentException("region", "ConsoleHostRawUserInterfaceStrings", "InvalidRegionErrorTemplate", objArray1);
            }
        }
Example #5
0
        public override KeyInfo ReadKey(ReadKeyOptions options)
        {
            KeyInfo keyInfo;

            if ((options & (ReadKeyOptions.IncludeKeyDown | ReadKeyOptions.IncludeKeyUp)) != 0)
            {
                if (this.cachedKeyEvent.RepeatCount > 0)
                {
                    if ((options & ReadKeyOptions.AllowCtrlC) != 0 || this.cachedKeyEvent.UnicodeChar != '\u0003')
                    {
                        if ((options & ReadKeyOptions.IncludeKeyUp) == 0 && !this.cachedKeyEvent.KeyDown || (options & ReadKeyOptions.IncludeKeyDown) == 0 && this.cachedKeyEvent.KeyDown)
                        {
                            this.cachedKeyEvent.RepeatCount = 0;
                        }
                    }
                    else
                    {
                        ConsoleControl.KEY_EVENT_RECORD kEYEVENTRECORDPointer = this.cachedKeyEvent;
                        this.cachedKeyEvent.RepeatCount = (ushort)(this.cachedKeyEvent.RepeatCount - 1);
                        throw this.NewPipelineStoppedException();
                    }
                }
                if (this.cachedKeyEvent.RepeatCount <= 0)
                {
                    SafeFileHandle inputHandle = ConsoleControl.GetInputHandle();
                    ConsoleControl.INPUT_RECORD[] nPUTRECORDArray = new ConsoleControl.INPUT_RECORD[1];
                    ConsoleControl.ConsoleModes   mode            = ConsoleControl.GetMode(inputHandle);
                    ConsoleControl.ConsoleModes   consoleMode     = mode & (ConsoleControl.ConsoleModes.ProcessedInput | ConsoleControl.ConsoleModes.LineInput | ConsoleControl.ConsoleModes.EchoInput | ConsoleControl.ConsoleModes.MouseInput | ConsoleControl.ConsoleModes.Insert | ConsoleControl.ConsoleModes.QuickEdit | ConsoleControl.ConsoleModes.Extended | ConsoleControl.ConsoleModes.AutoPosition | ConsoleControl.ConsoleModes.ProcessedOutput | ConsoleControl.ConsoleModes.WrapEndOfLine) & (ConsoleControl.ConsoleModes.ProcessedInput | ConsoleControl.ConsoleModes.LineInput | ConsoleControl.ConsoleModes.EchoInput | ConsoleControl.ConsoleModes.WindowInput | ConsoleControl.ConsoleModes.Insert | ConsoleControl.ConsoleModes.QuickEdit | ConsoleControl.ConsoleModes.Extended | ConsoleControl.ConsoleModes.AutoPosition | ConsoleControl.ConsoleModes.ProcessedOutput | ConsoleControl.ConsoleModes.WrapEndOfLine) & (ConsoleControl.ConsoleModes.LineInput | ConsoleControl.ConsoleModes.EchoInput | ConsoleControl.ConsoleModes.WindowInput | ConsoleControl.ConsoleModes.MouseInput | ConsoleControl.ConsoleModes.Insert | ConsoleControl.ConsoleModes.QuickEdit | ConsoleControl.ConsoleModes.Extended | ConsoleControl.ConsoleModes.AutoPosition | ConsoleControl.ConsoleModes.WrapEndOfLine);
                    try
                    {
                        ConsoleControl.SetMode(inputHandle, consoleMode);
                        do
                        {
Label0:
                            int num = ConsoleControl.ReadConsoleInput(inputHandle, ref nPUTRECORDArray);
                            if (num == 1 && nPUTRECORDArray[0].EventType == 1 && nPUTRECORDArray[0].KeyEvent.RepeatCount != 0)
                            {
                                if ((options & ReadKeyOptions.AllowCtrlC) != 0 || nPUTRECORDArray[0].KeyEvent.UnicodeChar != '\u0003')
                                {
                                    continue;
                                }
                                ConsoleHostRawUserInterface.CacheKeyEvent(nPUTRECORDArray[0].KeyEvent, ref this.cachedKeyEvent);
                                throw this.NewPipelineStoppedException();
                            }
                            else
                            {
                                goto Label0;
                            }
                        }while (((options & ReadKeyOptions.IncludeKeyDown) == 0 || !nPUTRECORDArray[0].KeyEvent.KeyDown) && ((options & ReadKeyOptions.IncludeKeyUp) == 0 || nPUTRECORDArray[0].KeyEvent.KeyDown));
                        ConsoleHostRawUserInterface.CacheKeyEvent(nPUTRECORDArray[0].KeyEvent, ref this.cachedKeyEvent);
                        ConsoleHostRawUserInterface.KEY_EVENT_RECORDToKeyInfo(nPUTRECORDArray[0].KeyEvent, out keyInfo);
                    }
                    finally
                    {
                        ConsoleControl.SetMode(inputHandle, mode);
                    }
                }
                else
                {
                    ConsoleHostRawUserInterface.KEY_EVENT_RECORDToKeyInfo(this.cachedKeyEvent, out keyInfo);
                    this.cachedKeyEvent.RepeatCount = (ushort)(this.cachedKeyEvent.RepeatCount - 1);
                }
                if ((options & ReadKeyOptions.NoEcho) == 0)
                {
                    char character = keyInfo.Character;
                    this.parent.WriteToConsole(character.ToString(CultureInfo.CurrentCulture), true);
                }
                return(keyInfo);
            }
            else
            {
                throw PSTraceSource.NewArgumentException("options", "ConsoleHostRawUserInterfaceStrings", "InvalidReadKeyOptionsError", new object[0]);
            }
        }
		internal ConsoleHostUserInterface(ConsoleHost parent)
		{
			this.errorForegroundColor = ConsoleColor.Red;
			this.warningForegroundColor = ConsoleColor.Yellow;
			this.debugForegroundColor = ConsoleColor.Yellow;
			this.verboseForegroundColor = ConsoleColor.Yellow;
			this.progressForegroundColor = ConsoleColor.Yellow;
			this.progressBackgroundColor = ConsoleColor.DarkCyan;
			this.instanceLock = new object();
			this.parent = parent;
			this.rawui = new ConsoleHostRawUserInterface(this);
			ConsoleHostUserInterface.debugFormatString = ConsoleHostUserInterfaceStrings.DebugFormatString;
			ConsoleHostUserInterface.verboseFormatString = ConsoleHostUserInterfaceStrings.VerboseFormatString;
			ConsoleHostUserInterface.warningFormatString = ConsoleHostUserInterfaceStrings.WarningFormatString;
			this.isInteractiveTestToolListening = false;
			this.isTestingShiftTab = false;
			lastForegroundColor = this.rawui.ForegroundColor;
			lastBackgroundColor = this.RawUI.BackgroundColor;
		}