namespace Terminal.Gui.Drivers;
///
/// Abstract base class to assist with implementing .
///
public abstract class OutputBase
{
private CursorVisibility? _cachedCursorVisibility;
// Last text style used, for updating style with EscSeqUtils.CSI_AppendTextStyleChange().
private TextStyle _redrawTextStyle = TextStyle.None;
///
/// Changes the visibility of the cursor in the terminal to the specified e.g.
/// the flashing indicator, invisible, box indicator etc.
///
///
public abstract void SetCursorVisibility (CursorVisibility visibility);
///
public virtual void Write (IOutputBuffer buffer)
{
var top = 0;
var left = 0;
int rows = buffer.Rows;
int cols = buffer.Cols;
var output = new StringBuilder ();
Attribute? redrawAttr = null;
int lastCol = -1;
CursorVisibility? savedVisibility = _cachedCursorVisibility;
SetCursorVisibility (CursorVisibility.Invisible);
for (int row = top; row < rows; row++)
{
if (!SetCursorPositionImpl (0, row))
{
return;
}
output.Clear ();
for (int col = left; col < cols; col++)
{
lastCol = -1;
var outputWidth = 0;
for (; col < cols; col++)
{
if (!buffer.Contents! [row, col].IsDirty)
{
if (output.Length > 0)
{
WriteToConsole (output, ref lastCol, row, ref outputWidth);
}
else if (lastCol == -1)
{
lastCol = col;
}
if (lastCol + 1 < cols)
{
lastCol++;
}
continue;
}
if (lastCol == -1)
{
lastCol = col;
}
Cell cell = buffer.Contents [row, col];
AppendCellAnsi (cell, output, ref redrawAttr, ref _redrawTextStyle, cols, ref col);
outputWidth++;
buffer.Contents [row, col].IsDirty = false;
}
}
if (output.Length > 0)
{
SetCursorPositionImpl (lastCol, row);
// Wrap URLs with OSC 8 hyperlink sequences using the new Osc8UrlLinker
StringBuilder processed = Osc8UrlLinker.WrapOsc8 (output);
Write (processed);
}
}
foreach (SixelToRender s in Application.Sixel)
{
if (!string.IsNullOrWhiteSpace (s.SixelData))
{
SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y);
Console.Out.Write (s.SixelData);
}
}
SetCursorVisibility (savedVisibility ?? CursorVisibility.Default);
_cachedCursorVisibility = savedVisibility;
}
///
/// Changes the color and text style of the console to the given and
/// .
/// If command can be buffered in line with other output (e.g. CSI sequence) then it should be appended to
///
/// otherwise the relevant output state should be flushed directly (e.g. by calling relevant win 32 API method)
///
///
///
///
protected abstract void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle);
///
/// When overriden in derived class, positions the terminal output cursor to the specified point on the screen.
///
/// Column to move cursor to
/// Row to move cursor to
///
protected abstract bool SetCursorPositionImpl (int screenPositionX, int screenPositionY);
///
/// Output the contents of the to the console.
///
///
protected abstract void Write (StringBuilder output);
///
/// Builds ANSI escape sequences for the specified rectangular region of the buffer.
///
/// The output buffer to build ANSI for.
/// The starting row (inclusive).
/// The ending row (exclusive).
/// The starting column (inclusive).
/// The ending column (exclusive).
/// The StringBuilder to append ANSI sequences to.
/// The last attribute used, for optimization.
/// Predicate to determine which cells to include. If null, includes all cells.
/// Whether to add newlines between rows.
protected void BuildAnsiForRegion (
IOutputBuffer buffer,
int startRow,
int endRow,
int startCol,
int endCol,
StringBuilder output,
ref Attribute? lastAttr,
Func? includeCellPredicate = null,
bool addNewlines = true
)
{
TextStyle redrawTextStyle = TextStyle.None;
for (int row = startRow; row < endRow; row++)
{
for (int col = startCol; col < endCol; col++)
{
if (includeCellPredicate != null && !includeCellPredicate (row, col))
{
continue;
}
Cell cell = buffer.Contents![row, col];
AppendCellAnsi (cell, output, ref lastAttr, ref redrawTextStyle, endCol, ref col);
}
// Add newline at end of row if requested
if (addNewlines)
{
output.AppendLine ();
}
}
}
///
/// Appends ANSI sequences for a single cell to the output.
///
/// The cell to append ANSI for.
/// The StringBuilder to append to.
/// The last attribute used, updated if the cell's attribute is different.
/// The current text style for optimization.
/// The maximum column, used for wide character handling.
/// The current column, updated for wide characters.
protected void AppendCellAnsi (Cell cell, StringBuilder output, ref Attribute? lastAttr, ref TextStyle redrawTextStyle, int maxCol, ref int currentCol)
{
Attribute? attribute = cell.Attribute;
// Add ANSI escape sequence for attribute change
if (attribute.HasValue && attribute.Value != lastAttr)
{
lastAttr = attribute.Value;
AppendOrWriteAttribute (output, attribute.Value, redrawTextStyle);
redrawTextStyle = attribute.Value.Style;
}
// Add the grapheme
string grapheme = cell.Grapheme;
output.Append (grapheme);
// Handle wide grapheme
if (grapheme.GetColumns () > 1 && currentCol + 1 < maxCol)
{
currentCol++; // Skip next cell for wide character
}
}
///
/// Generates an ANSI escape sequence string representation of the given contents.
/// This is the same output that would be written to the terminal to recreate the current screen contents.
///
/// The output buffer to convert to ANSI.
/// A string containing ANSI escape sequences representing the buffer contents.
public string ToAnsi (IOutputBuffer buffer)
{
var output = new StringBuilder ();
Attribute? lastAttr = null;
BuildAnsiForRegion (buffer, 0, buffer.Rows, 0, buffer.Cols, output, ref lastAttr);
return output.ToString ();
}
private void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth)
{
SetCursorPositionImpl (lastCol, row);
// Wrap URLs with OSC 8 hyperlink sequences using the new Osc8UrlLinker
StringBuilder processed = Osc8UrlLinker.WrapOsc8 (output);
Write (processed);
output.Clear ();
lastCol += outputWidth;
outputWidth = 0;
}
}