// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Globalization; #pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings namespace System.Management.Automation.Host { #region Ancillary types. // I would have preferred to make these nested types within PSHostRawUserInterface, but that // is evidently discouraged by the .net design guidelines. /// /// Represents an (x,y) coordinate pair. /// public struct Coordinates { #region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell private int x; private int y; #endregion /// /// Gets and sets the X coordinate. /// public int X { get { return x; } set { x = value; } } /// /// Gets and sets the Y coordinate. /// public int Y { get { return y; } set { y = value; } } /// /// Initializes a new instance of the Coordinates class and defines the X and Y values. /// /// /// The X coordinate /// /// /// The Y coordinate /// public Coordinates(int x, int y) { this.x = x; this.y = y; } /// /// Overrides /// /// /// "a,b" where a and b are the values of the X and Y properties. /// public override string ToString() { return string.Create(CultureInfo.InvariantCulture, $"{X},{Y}"); } /// /// Overrides /// /// /// object to be compared for equality. /// /// /// True if is Coordinates and its X and Y values are the same as those of this instance, /// false if not. /// public override bool Equals(object obj) { bool result = false; if (obj is Coordinates) { result = this == ((Coordinates)obj); } return result; } /// /// Overrides /// /// /// Hash code for this instance. /// public override int GetHashCode() { // idea: consider X the high-order part of a 64-bit in, and Y the lower order half. Then use the int64.GetHashCode. UInt64 i64 = 0; if (X < 0) { if (X == Int32.MinValue) { // add one and invert to avoid an overflow. i64 = (UInt64)(-1 * (X + 1)); } else { i64 = (UInt64)(-X); } } else { i64 = (UInt64)X; } // rotate 32 bits to the left. i64 *= 0x100000000U; // mask in Y if (Y < 0) { if (Y == Int32.MinValue) { i64 += (UInt64)(-1 * (Y + 1)); } else { i64 += (UInt64)(-Y); } } else { i64 += (UInt64)Y; } int result = i64.GetHashCode(); return result; } /// /// Compares two instances for equality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if the respective X and Y values are the same, false otherwise. /// public static bool operator ==(Coordinates first, Coordinates second) { bool result = first.X == second.X && first.Y == second.Y; return result; } /// /// Compares two instances for inequality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if any of the respective either X or Y field is not the same, false otherwise. /// public static bool operator !=(Coordinates first, Coordinates second) { return !(first == second); } } /// /// Represents a width and height pair. /// public struct Size { #region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell private int width; private int height; #endregion /// /// Gets and sets the Width. /// public int Width { get { return width; } set { width = value; } } /// /// Gets and sets the Height. /// public int Height { get { return height; } set { height = value; } } /// /// Initialize a new instance of the Size class and defines the Width and Height values. /// /// /// The Width /// /// /// The Height /// public Size(int width, int height) { this.width = width; this.height = height; } /// /// Overloads /// /// /// "a,b" where a and b are the values of the Width and Height properties. /// public override string ToString() { return string.Create(CultureInfo.InvariantCulture, $"{Width},{Height}"); } /// /// Overrides /// /// /// object to be compared for equality. /// /// /// True if is Size and its Width and Height values are the same as those of this instance, /// false if not. /// public override bool Equals(object obj) { bool result = false; if (obj is Size) { result = this == ((Size)obj); } return result; } /// /// Overrides /// /// /// Hash code for this instance. /// /// public override int GetHashCode() { // idea: consider Width the high-order part of a 64-bit in, and Height the lower order half. Then use the int64.GetHashCode. UInt64 i64 = 0; if (Width < 0) { if (Width == Int32.MinValue) { // add one and invert to avoid an overflow. i64 = (UInt64)(-1 * (Width + 1)); } else { i64 = (UInt64)(-Width); } } else { i64 = (UInt64)Width; } // rotate 32 bits to the left. i64 *= 0x100000000U; // mask in Height if (Height < 0) { if (Height == Int32.MinValue) { i64 += (UInt64)(-1 * (Height + 1)); } else { i64 += (UInt64)(-Height); } } else { i64 += (UInt64)Height; } int result = i64.GetHashCode(); return result; } /// /// Compares two instances for equality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if the respective Width and Height fields are the same, false otherwise. /// public static bool operator ==(Size first, Size second) { bool result = first.Width == second.Width && first.Height == second.Height; return result; } /// /// Compares two instances for inequality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if any of the respective Width and Height fields are not the same, false otherwise. /// public static bool operator !=(Size first, Size second) { return !(first == second); } } /// /// Governs the behavior of /// and /// [Flags] public enum ReadKeyOptions { /// /// Allow Ctrl-C to be processed as a keystroke, as opposed to causing a break event. /// AllowCtrlC = 0x0001, /// /// Do not display the character for the key in the window when pressed. /// NoEcho = 0x0002, /// /// Include key down events. Either one of IncludeKeyDown and IncludeKeyUp or both must be specified. /// IncludeKeyDown = 0x0004, /// /// Include key up events. Either one of IncludeKeyDown and IncludeKeyUp or both must be specified. /// IncludeKeyUp = 0x0008 } /// /// Defines the states of Control Key. /// [Flags] public enum ControlKeyStates { /// /// The right alt key is pressed. /// RightAltPressed = 0x0001, /// /// The left alt key is pressed. /// LeftAltPressed = 0x0002, /// /// The right ctrl key is pressed. /// RightCtrlPressed = 0x0004, /// /// The left ctrl key is pressed. /// LeftCtrlPressed = 0x0008, /// /// The shift key is pressed. /// ShiftPressed = 0x0010, /// /// The numlock light is on. /// NumLockOn = 0x0020, /// /// The scrolllock light is on. /// ScrollLockOn = 0x0040, /// /// The capslock light is on. /// CapsLockOn = 0x0080, /// /// The key is enhanced. /// EnhancedKey = 0x0100 } /// /// Represents information of a keystroke. /// public struct KeyInfo { #region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell private int virtualKeyCode; private char character; private ControlKeyStates controlKeyState; private bool keyDown; #endregion /// /// Gets and set device-independent key. /// public int VirtualKeyCode { get { return virtualKeyCode; } set { virtualKeyCode = value; } } /// /// Gets and set unicode Character of the key. /// public char Character { get { return character; } set { character = value; } } /// /// State of the control keys. /// public ControlKeyStates ControlKeyState { get { return controlKeyState; } set { controlKeyState = value; } } /// /// Gets and set the status of whether this instance is generated by a key pressed or released. /// public bool KeyDown { get { return keyDown; } set { keyDown = value; } } /// /// Initialize a new instance of the KeyInfo class and defines the VirtualKeyCode, /// Character, ControlKeyState and KeyDown values. /// /// /// The virtual key code /// /// /// The character /// /// /// The control key state /// /// /// Whether the key is pressed or released /// public KeyInfo ( int virtualKeyCode, char ch, ControlKeyStates controlKeyState, bool keyDown ) { this.virtualKeyCode = virtualKeyCode; this.character = ch; this.controlKeyState = controlKeyState; this.keyDown = keyDown; } /// /// Overloads /// /// /// "a,b,c,d" where a, b, c, and d are the values of the VirtualKeyCode, Character, ControlKeyState, and KeyDown properties. /// public override string ToString() { return string.Create(CultureInfo.InvariantCulture, $"{VirtualKeyCode},{Character},{ControlKeyState},{KeyDown}"); } /// /// Overrides /// /// /// object to be compared for equality. /// /// /// True if is KeyInfo and its VirtualKeyCode, Character, ControlKeyState, and KeyDown values are the /// same as those of this instance, false if not. /// public override bool Equals(object obj) { bool result = false; if (obj is KeyInfo) { result = this == ((KeyInfo)obj); } return result; } /// /// Overrides /// /// /// Hash code for this instance. /// /// public override int GetHashCode() { // idea: consider KeyDown (true == 1, false == 0) the highest-order nibble, // ControlKeyState the second to fourth highest-order nibbles // VirtualKeyCode the lower-order nibbles of a 32-bit int, // Then use the UInt32.GetHashCode. UInt32 i32 = KeyDown ? 0x10000000U : 0; // mask in ControlKeyState i32 |= ((uint)ControlKeyState) << 16; // mask in the VirtualKeyCode i32 |= (UInt32)VirtualKeyCode; return i32.GetHashCode(); } /// /// Compares two instances for equality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if the respective Character, ControlKeyStates , KeyDown, and VirtualKeyCode fields /// are the same, false otherwise. /// /// public static bool operator ==(KeyInfo first, KeyInfo second) { bool result = first.Character == second.Character && first.ControlKeyState == second.ControlKeyState && first.KeyDown == second.KeyDown && first.VirtualKeyCode == second.VirtualKeyCode; return result; } /// /// Compares two instances for inequality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if any of the respective Character, ControlKeyStates , KeyDown, or VirtualKeyCode fields /// are the different, false otherwise. /// /// public static bool operator !=(KeyInfo first, KeyInfo second) { return !(first == second); } } /// /// Represents a rectangular region of the screen. /// /// public struct Rectangle { #region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell private int left; private int top; private int right; private int bottom; #endregion /// /// Gets and sets the left side of the rectangle. /// public int Left { get { return left; } set { left = value; } } /// /// Gets and sets the top of the rectangle. /// public int Top { get { return top; } set { top = value; } } /// /// Gets and sets the right side of the rectangle. /// public int Right { get { return right; } set { right = value; } } /// /// Gets and sets the bottom of the rectangle. /// public int Bottom { get { return bottom; } set { bottom = value; } } /// /// Initialize a new instance of the Rectangle class and defines the Left, Top, Right, and Bottom values. /// /// /// The left side of the rectangle /// /// /// The top of the rectangle /// /// /// The right side of the rectangle /// /// /// The bottom of the rectangle /// /// /// is less than ; /// is less than /// public Rectangle(int left, int top, int right, int bottom) { if (right < left) { // "right" and "left" are not localizable throw PSTraceSource.NewArgumentException(nameof(right), MshHostRawUserInterfaceStrings.LessThanErrorTemplate, "right", "left"); } if (bottom < top) { // "bottom" and "top" are not localizable throw PSTraceSource.NewArgumentException(nameof(bottom), MshHostRawUserInterfaceStrings.LessThanErrorTemplate, "bottom", "top"); } this.left = left; this.top = top; this.right = right; this.bottom = bottom; } /// /// Initializes a new instance of the Rectangle class and defines the Left, Top, Right, and Bottom values /// by , the upper left corner and , the lower /// right corner. /// /// /// /// The Coordinates of the upper left corner of the Rectangle /// /// /// The Coordinates of the lower right corner of the Rectangle /// /// public Rectangle(Coordinates upperLeft, Coordinates lowerRight) : this(upperLeft.X, upperLeft.Y, lowerRight.X, lowerRight.Y) { } /// /// Overloads /// /// /// "a,b ; c,d" where a, b, c, and d are values of the Left, Top, Right, and Bottom properties. /// public override string ToString() { return string.Create(CultureInfo.InvariantCulture, $"{Left},{Top} ; {Right},{Bottom}"); } /// /// Overrides /// /// /// object to be compared for equality. /// /// /// True if is Rectangle and its Left, Top, Right, and Bottom values are the same as those of this instance, /// false if not. /// public override bool Equals(object obj) { bool result = false; if (obj is Rectangle) { result = this == ((Rectangle)obj); } return result; } /// /// Overrides /// /// /// Hash code for this instance. /// /// /// public override int GetHashCode() { // idea: consider (Top XOR Bottom) the high-order part of a 64-bit int, // (Left XOR Right) the lower order half. Then use the int64.GetHashCode. UInt64 i64 = 0; int upper = Top ^ Bottom; if (upper < 0) { if (upper == Int32.MinValue) { // add one and invert to avoid an overflow. i64 = (UInt64)(-1 * (upper + 1)); } else { i64 = (UInt64)(-upper); } } else { i64 = (UInt64)upper; } // rotate 32 bits to the left. i64 *= 0x100000000U; // mask in lower int lower = Left ^ Right; if (lower < 0) { if (lower == Int32.MinValue) { i64 += (UInt64)(-1 * (lower + 1)); } else { i64 += (UInt64)(-upper); } } else { i64 += (UInt64)lower; } int result = i64.GetHashCode(); return result; } /// /// Compares two instances for equality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if the respective Top, Left, Bottom, and Right fields are the same, false otherwise. /// public static bool operator ==(Rectangle first, Rectangle second) { bool result = first.Top == second.Top && first.Left == second.Left && first.Bottom == second.Bottom && first.Right == second.Right; return result; } /// /// Compares two instances for inequality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if any of the respective Top, Left, Bottom, and Right fields are not the same, false otherwise. /// /// public static bool operator !=(Rectangle first, Rectangle second) { return !(first == second); } } /// /// Represents a character, a foregroundColor color, and background color. /// public struct BufferCell { #region DO NOT REMOVE OR RENAME THESE FIELDS - it will break remoting compatibility with Windows PowerShell private char character; private ConsoleColor foregroundColor; private ConsoleColor backgroundColor; private BufferCellType bufferCellType; #endregion /// /// Gets and sets the character value. /// public char Character { get { return character; } set { character = value; } } // we reuse System.ConsoleColor - it's in the core assembly, and I think it would be confusing to create another // essentially identical enum /// /// Gets and sets the foreground color. /// public ConsoleColor ForegroundColor { get { return foregroundColor; } set { foregroundColor = value; } } /// /// Gets and sets the background color. /// public ConsoleColor BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; } } /// /// Gets and sets the type value. /// public BufferCellType BufferCellType { get { return bufferCellType; } set { bufferCellType = value; } } /// /// Initializes a new instance of the BufferCell class and defines the /// Character, ForegroundColor, BackgroundColor and Type values. /// /// /// The character in this BufferCell object /// /// /// The foreground color of this BufferCell object /// /// /// The foreground color of this BufferCell object /// /// /// The type of this BufferCell object /// public BufferCell(char character, ConsoleColor foreground, ConsoleColor background, BufferCellType bufferCellType) { this.character = character; this.foregroundColor = foreground; this.backgroundColor = background; this.bufferCellType = bufferCellType; } /// /// Overloads /// /// /// "'a' b c d" where a, b, c, and d are the values of the Character, ForegroundColor, BackgroundColor, and Type properties. /// public override string ToString() { return string.Create(CultureInfo.InvariantCulture, $"'{Character}' {ForegroundColor} {BackgroundColor} {BufferCellType}"); } /// /// Overrides /// /// /// object to be compared for equality. /// /// /// True if is BufferCell and its Character, ForegroundColor, BackgroundColor, and BufferCellType values /// are the same as those of this instance, false if not. /// public override bool Equals(object obj) { bool result = false; if (obj is BufferCell) { result = this == ((BufferCell)obj); } return result; } /// /// Overrides /// /// /// /// Hash code for this instance. /// /// public override int GetHashCode() { // idea: consider (ForegroundColor XOR BackgroundColor) the high-order part of a 32-bit int, // and Character the lower order half. Then use the int32.GetHashCode. UInt32 i32 = ((uint)(ForegroundColor ^ BackgroundColor)) << 16; // mask in Height i32 |= (UInt16)Character; int result = i32.GetHashCode(); return result; } /// /// Compares two instances for equality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if the respective Character, ForegroundColor, BackgroundColor, and BufferCellType values are the same, false otherwise. /// public static bool operator ==(BufferCell first, BufferCell second) { bool result = first.Character == second.Character && first.BackgroundColor == second.BackgroundColor && first.ForegroundColor == second.ForegroundColor && first.BufferCellType == second.BufferCellType; return result; } /// /// Compares two instances for inequality. /// /// /// The left side operand. /// /// /// The right side operand. /// /// /// true if any of the respective Character, ForegroundColor, BackgroundColor, and BufferCellType values are not the same, /// false otherwise. /// public static bool operator !=(BufferCell first, BufferCell second) { return !(first == second); } private const string StringsBaseName = "MshHostRawUserInterfaceStrings"; } /// /// Defines three types of BufferCells to accommodate for hosts that use up to two cells /// to display a character in some languages such as Chinese and Japanese. /// public enum BufferCellType { /// /// Character occupies one BufferCell. /// Complete, /// /// Character occupies two BufferCells and this is the leading one. /// Leading, /// /// Preceded by a Leading BufferCell. /// Trailing } #endregion Ancillary types /// /// Defines the lowest-level user interface functions that an interactive application hosting PowerShell /// can choose to implement if it wants to /// support any cmdlet that does character-mode interaction with the user. /// /// /// It models an 2-dimensional grid of cells called a Buffer. A buffer has a visible rectangular region, called a window. /// Each cell of the grid has a character, a foreground color, and a background color. When the buffer has input focus, it /// shows a cursor positioned in one cell. Keystrokes can be read from the buffer and optionally echoed at the current /// cursor position. /// /// /// public abstract class PSHostRawUserInterface { /// /// Protected constructor which does nothing. Provided per .Net design guidelines section 4.3.1. /// protected PSHostRawUserInterface() { // do nothing } /// /// Gets or sets the color used to render characters on the screen buffer. Each character cell in the screen buffer can /// have a separate foreground color. /// /// /// public abstract ConsoleColor ForegroundColor { get; set; } /// /// Gets or sets the color used to render the background behind characters on the screen buffer. Each character cell in /// the screen buffer can have a separate background color. /// /// public abstract ConsoleColor BackgroundColor { get; set; } /// /// Gets or sets the cursor position in the screen buffer. The view window always adjusts it's location over the screen /// buffer such that the cursor is always visible. /// /// /// To write to the screen buffer without updating the cursor position, use /// or /// /// /// /// /// /// /// /// public abstract Coordinates CursorPosition { get; set; } /// /// Gets or sets position of the view window relative to the screen buffer, in characters. (0,0) is the upper left of the screen /// buffer. /// /// /// /// /// public abstract Coordinates WindowPosition { get; set; } /// /// Gets or sets the cursor size as a percentage 0..100. /// /// public abstract int CursorSize { get; set; } /// /// Gets or sets the current size of the screen buffer, measured in character cells. /// /// /// /// /// /// public abstract Size BufferSize { get; set; } /// /// Gets or sets the current view window size, measured in character cells. The window size cannot be larger than the /// dimensions returned by . /// /// /// /// /// /// public abstract Size WindowSize { get; set; } /// /// Gets the size of the largest window possible for the current buffer, current font, and current display hardware. /// The view window cannot be larger than the screen buffer or the current display (the display the window is rendered on). /// /// /// The largest dimensions the window can be resized to without resizing the screen buffer. /// /// /// Always returns a value less than or equal to /// . /// /// /// /// /// /// public abstract Size MaxWindowSize { get; } /// /// Gets the largest window possible for the current font and display hardware, ignoring the current buffer dimensions. In /// other words, the dimensions of the largest window that could be rendered in the current display, if the buffer was /// at least as large. /// /// /// To resize the window to this dimension, use /// to first check and, if necessary, adjust, the screen buffer size. /// /// /// /// /// /// public abstract Size MaxPhysicalWindowSize { get; } /// /// Reads a key stroke from the keyboard device, blocking until a keystroke is typed. /// Same as ReadKey(ReadKeyOptions.IncludeKeyDown) /// /// /// Key stroke when a key is pressed. /// /// /// /// $Host.UI.RawUI.ReadKey() /// /// /// /// /// /// public KeyInfo ReadKey() { return ReadKey(ReadKeyOptions.IncludeKeyDown); } /// /// Reads a key stroke from the keyboard device, blocking until a keystroke is typed. /// Either one of ReadKeyOptions.IncludeKeyDown and ReadKeyOptions.IncludeKeyUp or both must be specified. /// /// /// A bit mask of the options to be used to read the keyboard. Constants defined by /// /// /// /// Key stroke depending on the value of . /// /// /// Neither ReadKeyOptions.IncludeKeyDown nor ReadKeyOptions.IncludeKeyUp is specified. /// /// /// /// $option = [System.Management.Automation.Host.ReadKeyOptions]"IncludeKeyDown"; /// $host.UI.RawUI.ReadKey($option) /// /// /// /// /// /// /// public abstract KeyInfo ReadKey(ReadKeyOptions options); /// /// Resets the keyboard input buffer. /// /// /// /// public abstract void FlushInputBuffer(); /// /// A non-blocking call to examine if a keystroke is waiting in the input buffer. /// /// /// True if a keystroke is waiting in the input buffer, false if not. /// /// /// /// public abstract bool KeyAvailable { get; } /// /// Gets or sets the titlebar text of the current view window. /// public abstract string WindowTitle { get; set; } /// /// Copies the array into the screen buffer at the /// given origin, clipping such that cells in the array that would fall outside the screen buffer are ignored. /// /// /// The top left corner of the rectangular screen area to which is copied. /// /// /// A rectangle of objects to be copied to the /// screen buffer. /// /// /// /// /// /// /// /// /// public abstract void SetBufferContents(Coordinates origin, BufferCell[,] contents); /// /// Copies a given character to all of the character cells in the screen buffer with the indicated colors. /// /// /// The rectangle on the screen buffer to which is copied. /// If all elements are -1, the entire screen buffer will be copied with . /// /// /// The character and attributes used to fill . /// /// /// Provided for clearing regions -- less chatty than passing an array of cells. /// /// /// /// using System; /// using System.Management.Automation; /// using System.Management.Automation.Host; /// namespace Microsoft.Samples.Cmdlet /// { /// [Cmdlet("Clear","Screen")] /// public class ClearScreen : PSCmdlet /// { /// protected override void BeginProcessing() /// { /// Host.UI.RawUI.SetBufferContents(new Rectangle(-1, -1, -1, -1), /// new BufferCell(' ', Host.UI.RawUI.ForegroundColor, Host.UI.RawUI.BackgroundColor)) /// } /// } /// } /// /// /// /// /// /// /// /// /// /// public abstract void SetBufferContents(Rectangle rectangle, BufferCell fill); /// /// Extracts a rectangular region of the screen buffer. /// /// /// The rectangle on the screen buffer to extract. /// /// /// An array of objects extracted from /// the rectangular region of the screen buffer specified by /// /// /// If the rectangle is completely outside of the screen buffer, a BufferCell array of zero rows and column will be /// returned. /// /// If the rectangle is partially outside of the screen buffer, the area where the screen buffer and rectangle overlap /// will be read and returned. The size of the returned array is the same as that of r. Each BufferCell in the /// non-overlapping area of this array is set as follows: /// /// Character is the space (' ') /// ForegroundColor to the current foreground color, given by the ForegroundColor property of this class. /// BackgroundColor to the current background color, given by the BackgroundColor property of this class. /// /// The resulting array is organized in row-major order for performance reasons. The screen buffer, however, is /// organized in column-major order -- e.g. you specify the column index first, then the row index second, as in (x, y). /// This means that a cell at screen buffer position (x, y) is in the array element [y, x]. /// /// /// /// /// /// /// /// /// public abstract BufferCell[,] GetBufferContents(Rectangle rectangle); /// /// Scroll a region of the screen buffer. /// /// /// Indicates the region of the screen to be scrolled. /// /// /// Indicates the upper left coordinates of the region of the screen to receive the source region contents. The target /// region is the same size as the source region. /// /// /// Indicates the region of the screen to include in the operation. If a cell would be changed by the operation but /// does not fall within the clip region, it will be unchanged. /// /// /// The character and attributes to be used to fill any cells within the intersection of the source rectangle and /// clipping rectangle that are left "empty" by the move. /// /// /// /// /// /// /// /// /// public abstract void ScrollBufferContents ( Rectangle source, Coordinates destination, Rectangle clip, BufferCell fill ); /// /// Determines the number of BufferCells a substring of a string occupies. /// /// /// The string whose substring length we want to know. /// /// /// Offset where the substring begins in /// /// /// The default implementation calls method /// with the substring extracted from the string /// starting at the offset /// /// /// /// /// /// /// /// /// /// public virtual int LengthInBufferCells ( string source, int offset ) { if (source == null) { throw PSTraceSource.NewArgumentNullException(nameof(source)); } // this implementation is inefficient // it is here to help with backcompatibility // it preserves the old behavior from the times // when there was only Length(string) overload string substring = offset == 0 ? source : source.Substring(offset); return this.LengthInBufferCells(substring); } /// /// Determines the number of BufferCells a string occupies. /// /// /// The string whose length we want to know. /// /// /// The default implementation returns the length of /// /// /// /// /// /// /// /// /// /// public virtual int LengthInBufferCells ( string source ) { if (source == null) { throw PSTraceSource.NewArgumentNullException(nameof(source)); } return source.Length; } /// /// Determines the number of BufferCells a character occupies. /// /// /// The character whose length we want to know. /// /// /// The default implementation returns 1. /// /// /// /// /// /// /// /// /// public virtual int LengthInBufferCells ( char source ) { return 1; } /// /// Creates a two dimensional array of BufferCells by examining each character in . /// /// /// String array based on which the two dimensional array of BufferCells will be created. /// /// /// Foreground color of the buffer cells in the resulting array. /// /// /// Background color of the buffer cells in the resulting array. /// /// /// A two dimensional array of BufferCells whose characters are the same as those in /// and whose foreground and background colors set to and /// /// /// /// is null; /// Any string in is null or empty /// /// /// If a character C takes one BufferCell to display as determined by LengthInBufferCells, /// one BufferCell is allocated with its Character set to C and BufferCellType to BufferCell.Complete. /// On the other hand, if C takes two BufferCell, two adjacent BufferCells on a row in /// the returned array will be allocated: the first has Character set to C and BufferCellType to /// and the second /// Character set to (char)0 and Type to /// . Hence, the returned /// BufferCell array has .Length number of rows and number of columns /// equal to the largest number of cells a string in takes. The /// foreground and background colors of the cells are initialized to /// and , respectively. /// The resulting array is suitable for use with /// and . /// /// /// /// /// /// /// /// /// #pragma warning disable 56506 public BufferCell[,] NewBufferCellArray(string[] contents, ConsoleColor foregroundColor, ConsoleColor backgroundColor) { #pragma warning disable 56506 if (contents == null) { throw PSTraceSource.NewArgumentNullException(nameof(contents)); } byte[][] charLengths = new byte[contents.Length][]; int maxStringLengthInBufferCells = 0; for (int i = 0; i < contents.Length; i++) { if (string.IsNullOrEmpty(contents[i])) { continue; } int lengthInBufferCells = 0; charLengths[i] = new byte[contents[i].Length]; for (int j = 0; j < contents[i].Length; j++) { charLengths[i][j] = (byte)LengthInBufferCells(contents[i][j]); lengthInBufferCells += charLengths[i][j]; } if (maxStringLengthInBufferCells < lengthInBufferCells) { maxStringLengthInBufferCells = lengthInBufferCells; } } if (maxStringLengthInBufferCells <= 0) { throw PSTraceSource.NewArgumentException(nameof(contents), MshHostRawUserInterfaceStrings.AllNullOrEmptyStringsErrorTemplate); } BufferCell[,] results = new BufferCell[contents.Length, maxStringLengthInBufferCells]; for (int i = 0; i < contents.Length; i++) { int resultJ = 0; for (int j = 0; j < contents[i].Length; j++, resultJ++) { if (charLengths[i][j] == 1) { results[i, resultJ] = new BufferCell(contents[i][j], foregroundColor, backgroundColor, BufferCellType.Complete); } else if (charLengths[i][j] == 2) { results[i, resultJ] = new BufferCell(contents[i][j], foregroundColor, backgroundColor, BufferCellType.Leading); resultJ++; results[i, resultJ] = new BufferCell((char)0, foregroundColor, backgroundColor, BufferCellType.Trailing); } } while (resultJ < maxStringLengthInBufferCells) { results[i, resultJ] = new BufferCell(' ', foregroundColor, backgroundColor, BufferCellType.Complete); resultJ++; } } return results; #pragma warning restore 56506 } #pragma warning restore 56506 /// /// Creates a 2D array of BufferCells by examining .Character. /// /// /// /// The number of columns of the resulting array /// /// /// The number of rows of the resulting array /// /// /// The cell to be copied to each of the elements of the resulting array. /// /// /// A by array of BufferCells where each cell's value is /// based on /// /// /// /// is less than 1; /// is less than 1. /// /// /// If the character takes one BufferCell to display as determined by LengthInBufferCells, /// one BufferCell is allocated with its Character set to the character and BufferCellType to /// BufferCell.Complete. /// On the other hand, if it takes two BufferCells, two adjacent BufferCells on a row /// in the returned array will be allocated: the first has Character /// set to the character and BufferCellType to BufferCellType.Leading and the second Character /// set to (char)0 and BufferCellType to BufferCellType.Trailing. Moreover, if /// is odd, the last column will just contain the leading cell. /// .BufferCellType is not used in creating the array. /// The resulting array is suitable for use with the PSHostRawUserInterface.SetBufferContents method. /// /// /// /// /// /// /// /// /// public BufferCell[,] NewBufferCellArray(int width, int height, BufferCell contents) { if (width <= 0) { // "width" is not localizable throw PSTraceSource.NewArgumentOutOfRangeException(nameof(width), width, MshHostRawUserInterfaceStrings.NonPositiveNumberErrorTemplate, "width"); } if (height <= 0) { // "height" is not localizable throw PSTraceSource.NewArgumentOutOfRangeException(nameof(height), height, MshHostRawUserInterfaceStrings.NonPositiveNumberErrorTemplate, "height"); } BufferCell[,] buffer = new BufferCell[height, width]; int charLength = LengthInBufferCells(contents.Character); if (charLength == 1) { for (int r = 0; r < buffer.GetLength(0); ++r) { for (int c = 0; c < buffer.GetLength(1); ++c) { buffer[r, c] = contents; buffer[r, c].BufferCellType = BufferCellType.Complete; } } } else if (charLength == 2) { int normalizedWidth = width % 2 == 0 ? width : width - 1; for (int i = 0; i < height; i++) { for (int j = 0; j < normalizedWidth; j++) { buffer[i, j] = contents; buffer[i, j].BufferCellType = BufferCellType.Leading; j++; buffer[i, j] = new BufferCell((char)0, contents.ForegroundColor, contents.BackgroundColor, BufferCellType.Trailing); } if (normalizedWidth < width) { buffer[i, normalizedWidth] = contents; buffer[i, normalizedWidth].BufferCellType = BufferCellType.Leading; } } } return buffer; } /// /// Same as /// /// /// The width and height of the resulting array. /// /// /// The cell to be copied to each of the elements of the resulting array. /// /// /// An array of BufferCells whose size is and where each cell's value is /// based on /// /// /// If .Width or .Height is less than 1. /// /// /// /// /// /// /// /// /// public BufferCell[,] NewBufferCellArray(Size size, BufferCell contents) { return NewBufferCellArray(size.Width, size.Height, contents); } } }