// Copyright (c) Microsoft. All rights reserved.
namespace Harness.ConsoleReactiveComponents;
///
/// Provides descriptive helpers for common ANSI/VT100 escape sequences used
/// in the split-console layout (DECSTBM scroll regions, cursor movement, line erasure).
///
public static class AnsiEscapes
{
///
/// Sets the scrollable region to rows 1 through (DECSTBM).
/// Content outside this region will not scroll.
///
public static string SetScrollRegion(int bottom) => $"\x1b[1;{bottom}r";
///
/// Resets the scroll region to the full terminal height (DECSTBM reset).
///
public static string ResetScrollRegion => "\x1b[r";
///
/// Moves the cursor to the specified 1-based and (CUP).
///
public static string MoveCursor(int row, int column) => $"\x1b[{row};{column}H";
///
/// Erases the current line from the cursor position to the end of the line (EL 0).
///
public static string EraseToEndOfLine => "\x1b[0K";
///
/// Erases the entire current line (EL 2).
///
public static string EraseEntireLine => "\x1b[2K";
///
/// Erases the entire screen.
///
public static string EraseEntireScreen => "\x1b[2J";
///
/// Erases the scrollback buffer (ESC[3J). Use alongside
/// to fully clear both the visible screen and the scroll history.
///
public static string EraseScrollbackBuffer => "\x1b[3J";
///
/// Saves the current cursor position (DECSC / SCP).
/// Note: most terminals have a single save slot — nested saves are not supported.
///
public static string SaveCursor => "\x1b[s";
///
/// Restores the previously saved cursor position (DECRC / RCP).
///
public static string RestoreCursor => "\x1b[u";
///
/// Moves the cursor to the specified 1-based at column 1, then erases the entire line.
/// Convenience combination of and .
///
public static string MoveAndEraseLine(int row) => $"\x1b[{row};1H\x1b[2K";
///
/// Sets the foreground text color using a value.
///
public static string SetForegroundColor(ConsoleColor color) => $"\x1b[{ConsoleColorToAnsi(color)}m";
///
/// Resets all text attributes (color, bold, etc.) to their defaults.
///
public static string ResetAttributes => "\x1b[0m";
private static int ConsoleColorToAnsi(ConsoleColor color) => color switch
{
ConsoleColor.Black => 30,
ConsoleColor.DarkRed => 31,
ConsoleColor.DarkGreen => 32,
ConsoleColor.DarkYellow => 33,
ConsoleColor.DarkBlue => 34,
ConsoleColor.DarkMagenta => 35,
ConsoleColor.DarkCyan => 36,
ConsoleColor.Gray => 37,
ConsoleColor.DarkGray => 90,
ConsoleColor.Red => 91,
ConsoleColor.Green => 92,
ConsoleColor.Yellow => 93,
ConsoleColor.Blue => 94,
ConsoleColor.Magenta => 95,
ConsoleColor.Cyan => 96,
ConsoleColor.White => 97,
_ => 37
};
}