/// <summary> /// Sets the background. /// </summary> /// <param name="r">The red component value. Valid values are 0 through 255.</param> /// <param name="g">The green component value. Valid values are 0 through 255.</param> /// <param name="b">The blue component value. Valid values are 0 through 255.</param> /// <param name="convertToFallback">true if also convert to fallback console color.</param> public void SetBackground(byte r, byte g, byte b, bool convertToFallback = false) { var color = Color.FromArgb(r, g, b); BackgroundRgbColor = color; if (convertToFallback) { BackgroundConsoleColor = AnsiCodeGenerator.ToConsoleColor(color); } }
/// <summary> /// Removes the specific area. /// </summary> /// <param name="area">The area to remove.</param> public void Clear(RelativeAreas area) { Flush(); var h = Handler; if (h != null) { h.Remove(area, context); Cleared?.Invoke(this, new DataEventArgs<RelativeAreas>(area)); return; } switch (Mode) { case Modes.Cmd: try { ClearInCmd(area); break; } catch (IOException) { } catch (InvalidOperationException) { } catch (NotSupportedException) { } WriteImmediately(AnsiCodeGenerator.Clear(area)); break; case Modes.Text: switch (area) { case RelativeAreas.ToEndOfScreen: case RelativeAreas.ToEndOfLine: col.Add(new ConsoleText(" \b")); break; case RelativeAreas.None: break; default: Write("\b \b"); break; } break; default: WriteImmediately(AnsiCodeGenerator.Clear(area)); break; } Cleared?.Invoke(this, new DataEventArgs<RelativeAreas>(area)); }
/// <summary> /// Initializes a new instance of the ConsoleTextStyle class. /// </summary> /// <param name="foreground">The foreground color.</param> /// <param name="background">The background color.</param> /// <param name="nullConsoleColors">true if set console colors as null; otherwise, false.</param> public ConsoleTextStyle(Color?foreground, Color?background = null, bool nullConsoleColors = false) { ForegroundRgbColor = foreground; BackgroundRgbColor = background; if (nullConsoleColors) { return; } if (foreground.HasValue) { ForegroundConsoleColor = AnsiCodeGenerator.ToConsoleColor(foreground.Value); } if (background.HasValue) { BackgroundConsoleColor = AnsiCodeGenerator.ToConsoleColor(background.Value); } }
private void TestMode() { if (Mode != 0 || Handler is not null) return; #if NET5_0_OR_GREATER if (!OperatingSystem.IsWindows()) { Mode = OperatingSystem.IsBrowser() ? Modes.Text : Modes.Ansi; return; } #elif NETCOREAPP || NETSTANDARD switch (Environment.OSVersion.Platform) { case PlatformID.Unix: case PlatformID.MacOSX: Mode = Modes.Ansi; return; } #endif var left = -1; try { left = CursorLeft; } catch (ArgumentException) { } catch (InvalidOperationException) { } catch (NotSupportedException) { } catch (NullReferenceException) { } catch (IOException) { } catch (NotImplementedException) { } catch (SecurityException) { } catch (System.Runtime.InteropServices.ExternalException) { } if (left < 0) { Mode = Modes.Text; return; } var s = AnsiCodeGenerator.Background(true); Console.Write(s); if (left == CursorLeft) { Mode = Modes.Ansi; return; } try { Console.CursorLeft = left; Mode = Modes.Cmd; var sb = new StringBuilder(); sb.Append('\b', s.Length); sb.Append(' ', s.Length); sb.Append('\b', s.Length); Console.Write(sb.ToString()); return; } catch (ArgumentException) { } catch (InvalidOperationException) { } catch (NotSupportedException) { } catch (NullReferenceException) { } catch (IOException) { } catch (NotImplementedException) { } catch (SecurityException) { } catch (System.Runtime.InteropServices.ExternalException) { } Console.WriteLine(); Mode = Modes.Text; }
/// <summary> /// Moves cursor at a specific position in buffer. /// </summary> /// <param name="x">Column (zero-based), the left from the edge of buffer.</param> /// <param name="y">Row (zero-based) the top from the edge of buffer; or -1 if do not move.</param> public void MoveCursorTo(int x, int y) { var h = Handler; if (h != null) { h.MoveCursorTo(x, y, context); CursorMovedTo?.Invoke(this, new Maths.IntPoint2D.DataEventArgs(x, y)); return; } TestMode(); switch (Mode) { case Modes.Cmd: try { if (y >= 0) Console.CursorTop = y; } catch (InvalidOperationException) { } catch (IOException) { } catch (SecurityException) { } catch (ArgumentOutOfRangeException) { Console.CursorTop = Math.Min(y, BufferHeight - 1); WriteImmediately(Environment.NewLine); } catch (ArgumentException) { } catch (NotSupportedException) { } try { if (x < 0) { x = BufferWidth + x; if (x < 0) x = 0; } Console.CursorLeft = x; } catch (InvalidOperationException) { } catch (IOException) { } catch (SecurityException) { } catch (ArgumentOutOfRangeException) { if (BufferWidth < 8) throw; Console.CursorLeft = Math.Min(x, BufferWidth - 1); } catch (ArgumentException) { } catch (NotSupportedException) { } break; case Modes.Text: break; default: WriteImmediately(AnsiCodeGenerator.MoveCursorTo(x, y)); break; } CursorMovedTo?.Invoke(this, new Maths.IntPoint2D.DataEventArgs(x, y)); }
/// <summary> /// Moves cursor by a specific relative position. /// </summary> /// <param name="x">The horizontal translation size.</param> /// <param name="y">The vertical translation size.</param> public void MoveCursorBy(int x, int y = 0) { var h = Handler; if (h != null) { h.MoveCursorBy(x, y, context); CursorMovedBy?.Invoke(this, new Maths.IntPoint2D.DataEventArgs(x, y)); return; } TestMode(); switch (Mode) { case Modes.Cmd: if (y != 0) { try { Console.CursorTop += y; } catch (InvalidOperationException) { } catch (IOException) { } catch (SecurityException) { } catch (ArgumentOutOfRangeException) { if (y > 0) { Console.CursorTop = Math.Min(CursorTop + y, BufferHeight - 1); WriteImmediately(Environment.NewLine); } else { Console.CursorTop = Math.Max(CursorTop + y, 0); } } catch (ArgumentException) { } catch (NotSupportedException) { } } if (x != 0) { try { Console.CursorLeft += x; } catch (InvalidOperationException) { } catch (IOException) { } catch (SecurityException) { } catch (ArgumentOutOfRangeException) { if (BufferWidth < 8) throw; Console.CursorLeft = x > 0 ? Math.Min(CursorLeft + x, BufferWidth - 1) : Math.Max(CursorLeft + x, 0); } catch (ArgumentException) { } catch (NotSupportedException) { } } break; case Modes.Text: if (y > 0 && col.LastOrDefault()?.Content?.ToString() != Environment.NewLine) col.Add(new ConsoleText(Environment.NewLine)); break; default: WriteImmediately(AnsiCodeGenerator.MoveCursorBy(x, y)); break; } CursorMovedBy?.Invoke(this, new Maths.IntPoint2D.DataEventArgs(x, y)); }