void ClearBuffer() { for (int i = 0; i < buffer.Length; i++) { buffer[i] = new CHAR_INFO(' ', ForegroundColor, BackgroundColor); } }
static private void SetPrompt(string prompt) { if (string.IsNullOrEmpty(prompt)) { return; } var handle = NativeMethods.GetStdHandle((uint)StandardHandleId.Output); var lineCount = 1 + prompt.Count(c => c == '\n'); if (lineCount > 1) { var options = new SetPSReadlineOption { ExtraPromptLineCount = lineCount - 1 }; PSConsoleReadLine.SetOptions(options); } int bufferWidth = Console.BufferWidth; var consoleBuffer = new CHAR_INFO[lineCount * bufferWidth]; int j = 0; for (int i = 0; i < prompt.Length; i++, j++) { if (prompt[i] == '\n') { for (; j % Console.BufferWidth != 0; j++) { consoleBuffer[j] = new CHAR_INFO(' ', Console.ForegroundColor, Console.BackgroundColor); } Console.CursorTop += 1; Console.CursorLeft = 0; j -= 1; // We don't actually write the newline } else { consoleBuffer[j] = new CHAR_INFO(prompt[i], Console.ForegroundColor, Console.BackgroundColor); Console.CursorLeft += 1; } } var bufferSize = new COORD { X = (short)bufferWidth, Y = (short)lineCount }; var bufferCoord = new COORD { X = 0, Y = 0 }; var writeRegion = new SMALL_RECT { Top = 0, Left = 0, Bottom = (short)(lineCount - 1), Right = (short)bufferWidth }; NativeMethods.WriteConsoleOutput(handle, consoleBuffer, bufferSize, bufferCoord, ref writeRegion); }
private CHAR_INFO[] CreateCharInfoBuffer(int lines, params object[] items) { var result = new List <CHAR_INFO>(); var fg = Console.ForegroundColor; var bg = Console.BackgroundColor; foreach (var i in items) { var item = i; if (item is char) { result.Add(new CHAR_INFO((char)item, fg, bg)); continue; } if (item is InvertedToken) { fg = (ConsoleColor)((int)fg ^ 7); bg = (ConsoleColor)((int)bg ^ 7); continue; } if (item is NextLineToken) { item = new string(' ', Console.BufferWidth - (result.Count % Console.BufferWidth)); fg = Console.ForegroundColor; bg = Console.BackgroundColor; // Fallthrough to string case. } var str = item as string; if (str != null) { result.AddRange(str.Select(c => new CHAR_INFO(c, fg, bg))); continue; } if (item is TokenClassification) { fg = ForegroundColors[(int)(TokenClassification)item]; bg = BackgroundColors[(int)(TokenClassification)item]; continue; } var tuple = item as Tuple <ConsoleColor, ConsoleColor>; if (tuple != null) { fg = tuple.Item1; bg = tuple.Item2; continue; } throw new ArgumentException("Unexpected type"); } var extraSpacesNeeded = (lines * Console.BufferWidth) - result.Count; if (extraSpacesNeeded > 0) { var space = new CHAR_INFO(' ', Console.ForegroundColor, Console.BackgroundColor); result.AddRange(Enumerable.Repeat(space, extraSpacesNeeded)); } return(result.ToArray()); }
public CHAR_INFO[] ReadBufferLines(int top, int count) { var toCopy = BufferWidth * count; var result = new CHAR_INFO[toCopy]; Array.Copy(buffer, top * BufferWidth, result, 0, toCopy); return(result); }
private static void ScrollBuffer(int lines) { var handle = NativeMethods.GetStdHandle((uint) StandardHandleId.Output); var scrollRectangle = new SMALL_RECT { Top = (short) lines, Left = 0, Bottom = (short) (lines + 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); }
private static CHAR_INFO[] ReadBufferLines(int top, int count) { var result = new CHAR_INFO[Console.BufferWidth * count]; var handle = NativeMethods.GetStdHandle((uint) StandardHandleId.Output); var readBufferSize = new COORD { X = (short)Console.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)(Console.BufferWidth - 1) }; NativeMethods.ReadConsoleOutput(handle, result, readBufferSize, readBufferCoord, ref readRegion); return result; }