Exemplo n.º 1
0
        public BufferChar[] ReadBufferLines(int top, int count)
        {
            var result = new CHAR_INFO[BufferWidth * count];
            var handle = NativeMethods.GetStdHandle((uint)StandardHandleId.Output);

            var readBufferSize = new COORD {
                X = (short)BufferWidth,
                Y = (short)count
            };
            var readBufferCoord = new COORD {
                X = 0, Y = 0
            };
            var readRegion = new SMALL_RECT
            {
                Top    = (short)top,
                Left   = 0,
                Bottom = (short)(top + count),
                Right  = (short)(BufferWidth - 1)
            };

            NativeMethods.ReadConsoleOutput(handle, result,
                                            readBufferSize, readBufferCoord, ref readRegion);

            return(ToBufferCharBuffer(result));
        }
Exemplo n.º 2
0
        public CHAR_INFO ToCharInfo()
        {
            int fg = (int)ForegroundColor;
            int bg = (int)BackgroundColor;

            if (fg < 0 || fg > 0xf || bg < 0 || bg > 0xf)
            {
                throw new InvalidOperationException();
            }

            if (Inverse)
            {
                // TODO: check $host.UI.SupportsVirtualTerminal and maybe set Inverse instead (it does look weird)
                fg = fg ^ 7;
                bg = bg ^ 7;
            }
            ushort attrs = (ushort)(fg | (bg << 4));

            if (IsLeadByte)
            {
                attrs |= (ushort)CHAR_INFO_Attributes.COMMON_LVB_LEADING_BYTE;
            }
            if (IsTrailByte)
            {
                attrs |= (ushort)CHAR_INFO_Attributes.COMMON_LVB_TRAILING_BYTE;
            }
            CHAR_INFO result = new CHAR_INFO
            {
                UnicodeChar = UnicodeChar,
                Attributes  = attrs,
            };

            return(result);
        }
Exemplo n.º 3
0
        public static BufferChar FromCharInfo(CHAR_INFO charInfo)
        {
            BufferChar result = new BufferChar
            {
                UnicodeChar     = (char)charInfo.UnicodeChar,
                ForegroundColor = (ConsoleColor)(charInfo.Attributes & 0xf),
                BackgroundColor = (ConsoleColor)((charInfo.Attributes & 0x00f0) >> 4),
            };

            return(result);
        }
Exemplo n.º 4
0
        public void ScrollBuffer(int lines)
        {
            var handle = NativeMethods.GetStdHandle((uint)StandardHandleId.Output);

            var scrollRectangle = new SMALL_RECT
            {
                Top    = (short)lines,
                Left   = 0,
                Bottom = (short)(Console.BufferHeight - 1),
                Right  = (short)Console.BufferWidth
            };
            var destinationOrigin = new COORD {
                X = 0, Y = 0
            };
            var fillChar = new CHAR_INFO(' ', Console.ForegroundColor, Console.BackgroundColor);

            NativeMethods.ScrollConsoleScreenBuffer(handle, ref scrollRectangle, IntPtr.Zero, destinationOrigin, ref fillChar);
        }
Exemplo n.º 5
0
 public static extern bool ScrollConsoleScreenBuffer(IntPtr hConsoleOutput,
                                                     ref SMALL_RECT lpScrollRectangle,
                                                     IntPtr lpClipRectangle,
                                                     COORD dwDestinationOrigin,
                                                     ref CHAR_INFO lpFill);
Exemplo n.º 6
0
 public static extern bool WriteConsoleOutput(IntPtr consoleOutput, CHAR_INFO[] buffer, COORD bufferSize, COORD bufferCoord, ref SMALL_RECT writeRegion);
Exemplo n.º 7
0
        public CHAR_INFO[] ReadBufferLines(int top, int count)
        {
            var result = new CHAR_INFO[BufferWidth * count];
            var handle = NativeMethods.GetStdHandle((uint) StandardHandleId.Output);

            var readBufferSize = new COORD {
                X = (short)BufferWidth,
                Y = (short)count};
            var readBufferCoord = new COORD {X = 0, Y = 0};
            var readRegion = new SMALL_RECT
            {
                Top = (short)top,
                Left = 0,
                Bottom = (short)(top + count),
                Right = (short)(BufferWidth - 1)
            };
            NativeMethods.ReadConsoleOutput(handle, result,
                readBufferSize, readBufferCoord, ref readRegion);
            return result;
        }
Exemplo n.º 8
0
        public void WriteBufferLines(CHAR_INFO[] buffer, ref int top, bool ensureBottomLineVisible)
        {
            var handle = NativeMethods.GetStdHandle((uint) StandardHandleId.Output);

            int bufferWidth = Console.BufferWidth;
            int bufferLineCount = buffer.Length / bufferWidth;
            if ((top + bufferLineCount) > Console.BufferHeight)
            {
                var scrollCount = (top + bufferLineCount) - Console.BufferHeight;
                ScrollBuffer(scrollCount);
                top -= scrollCount;
            }
            var bufferSize = new COORD
            {
                X = (short) bufferWidth,
                Y = (short) bufferLineCount
            };
            var bufferCoord = new COORD {X = 0, Y = 0};
            var bottom = top + bufferLineCount - 1;
            var writeRegion = new SMALL_RECT
            {
                Top = (short) top,
                Left = 0,
                Bottom = (short) bottom,
                Right = (short) (bufferWidth - 1)
            };
            NativeMethods.WriteConsoleOutput(handle, buffer,
                                             bufferSize, bufferCoord, ref writeRegion);

            // Now make sure the bottom line is visible
            if (ensureBottomLineVisible &&
                (bottom >= (Console.WindowTop + Console.WindowHeight)))
            {
                Console.CursorTop = bottom;
            }
        }
Exemplo n.º 9
0
        public void ScrollBuffer(int lines)
        {
            var handle = NativeMethods.GetStdHandle((uint) StandardHandleId.Output);

            var scrollRectangle = new SMALL_RECT
            {
                Top = (short) lines,
                Left = 0,
                Bottom = (short)(Console.BufferHeight - 1),
                Right = (short)Console.BufferWidth
            };
            var destinationOrigin = new COORD {X = 0, Y = 0};
            var fillChar = new CHAR_INFO(' ', Console.ForegroundColor, Console.BackgroundColor);
            NativeMethods.ScrollConsoleScreenBuffer(handle, ref scrollRectangle, IntPtr.Zero, destinationOrigin, ref fillChar);
        }
Exemplo n.º 10
0
 public void WriteBufferLines(CHAR_INFO[] buffer, ref int top)
 {
     WriteBufferLines(buffer, ref top, true);
 }
Exemplo n.º 11
0
 public static extern bool ScrollConsoleScreenBuffer(IntPtr hConsoleOutput,
     ref SMALL_RECT lpScrollRectangle,
     IntPtr lpClipRectangle,
     COORD dwDestinationOrigin,
     ref CHAR_INFO lpFill);
Exemplo n.º 12
0
        private BufferChar[] ToBufferCharBuffer(CHAR_INFO[] buffer)
        {
            var result = new BufferChar[buffer.Length];

            for (int i = 0; i < buffer.Length; i++)
            {
                result[i] = BufferChar.FromCharInfo(buffer[i]);
            }

            return result;
        }
Exemplo n.º 13
0
 public static BufferChar FromCharInfo(CHAR_INFO charInfo)
 {
     BufferChar result = new BufferChar
     {
         UnicodeChar = (char) charInfo.UnicodeChar,
         ForegroundColor = (ConsoleColor)(charInfo.Attributes & 0xf),
         BackgroundColor = (ConsoleColor)((charInfo.Attributes & 0x00f0) >> 4),
     };
     return result;
 }
Exemplo n.º 14
0
        public CHAR_INFO ToCharInfo()
        {
            int fg = (int) ForegroundColor;
            int bg = (int) BackgroundColor;

            if (fg < 0 || fg > 0xf || bg < 0 || bg > 0xf)
                throw new InvalidOperationException();

            if (Inverse)
            {
                // TODO: check $host.UI.SupportsVirtualTerminal and maybe set Inverse instead (it does look weird)
                fg = fg ^ 7;
                bg = bg ^ 7;
            }
            ushort attrs = (ushort)(fg | (bg << 4));
            if (IsLeadByte)
                attrs |= (ushort)CHAR_INFO_Attributes.COMMON_LVB_LEADING_BYTE;
            if (IsTrailByte)
                attrs |= (ushort)CHAR_INFO_Attributes.COMMON_LVB_TRAILING_BYTE;
            CHAR_INFO result = new CHAR_INFO
            {
                UnicodeChar = UnicodeChar,
                Attributes = attrs,
            };

            return result;
        }