| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- namespace Terminal.Gui.Drivers;
- /// <summary>
- /// Abstract base class to assist with implementing <see cref="IOutput"/>.
- /// </summary>
- public abstract class OutputBase
- {
- private CursorVisibility? _cachedCursorVisibility;
- // Last text style used, for updating style with EscSeqUtils.CSI_AppendTextStyleChange().
- private TextStyle _redrawTextStyle = TextStyle.None;
- /// <summary>
- /// Changes the visibility of the cursor in the terminal to the specified <paramref name="visibility"/> e.g.
- /// the flashing indicator, invisible, box indicator etc.
- /// </summary>
- /// <param name="visibility"></param>
- public abstract void SetCursorVisibility (CursorVisibility visibility);
- /// <inheritdoc cref="IOutput.Write(IOutputBuffer)"/>
- 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);
- }
- }
- // BUGBUG: The Sixel impl depends on the legacy static Application object
- // BUGBUG: Disabled for now
- //foreach (SixelToRender s in Application.Sixel)
- //{
- // if (!string.IsNullOrWhiteSpace (s.SixelData))
- // {
- // SetCursorPositionImpl (s.ScreenPosition.X, s.ScreenPosition.Y);
- // Console.Out.Write (s.SixelData);
- // }
- //}
- // DO NOT restore cursor visibility here - let ApplicationMainLoop.SetCursor() handle it
- // The old code was saving/restoring visibility which caused flickering because
- // it would restore to the old value even if the application wanted it hidden
- }
- /// <summary>
- /// Changes the color and text style of the console to the given <paramref name="attr"/> and
- /// <paramref name="redrawTextStyle"/>.
- /// If command can be buffered in line with other output (e.g. CSI sequence) then it should be appended to
- /// <paramref name="output"/>
- /// otherwise the relevant output state should be flushed directly (e.g. by calling relevant win 32 API method)
- /// </summary>
- /// <param name="output"></param>
- /// <param name="attr"></param>
- /// <param name="redrawTextStyle"></param>
- protected abstract void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle);
- /// <summary>
- /// When overriden in derived class, positions the terminal output cursor to the specified point on the screen.
- /// </summary>
- /// <param name="screenPositionX">Column to move cursor to</param>
- /// <param name="screenPositionY">Row to move cursor to</param>
- /// <returns></returns>
- protected abstract bool SetCursorPositionImpl (int screenPositionX, int screenPositionY);
- /// <summary>
- /// Output the contents of the <paramref name="output"/> to the console.
- /// </summary>
- /// <param name="output"></param>
- protected abstract void Write (StringBuilder output);
- /// <summary>
- /// Builds ANSI escape sequences for the specified rectangular region of the buffer.
- /// </summary>
- /// <param name="buffer">The output buffer to build ANSI for.</param>
- /// <param name="startRow">The starting row (inclusive).</param>
- /// <param name="endRow">The ending row (exclusive).</param>
- /// <param name="startCol">The starting column (inclusive).</param>
- /// <param name="endCol">The ending column (exclusive).</param>
- /// <param name="output">The StringBuilder to append ANSI sequences to.</param>
- /// <param name="lastAttr">The last attribute used, for optimization.</param>
- /// <param name="includeCellPredicate">Predicate to determine which cells to include. If null, includes all cells.</param>
- /// <param name="addNewlines">Whether to add newlines between rows.</param>
- protected void BuildAnsiForRegion (
- IOutputBuffer buffer,
- int startRow,
- int endRow,
- int startCol,
- int endCol,
- StringBuilder output,
- ref Attribute? lastAttr,
- Func<int, int, bool>? 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 ();
- }
- }
- }
- /// <summary>
- /// Appends ANSI sequences for a single cell to the output.
- /// </summary>
- /// <param name="cell">The cell to append ANSI for.</param>
- /// <param name="output">The StringBuilder to append to.</param>
- /// <param name="lastAttr">The last attribute used, updated if the cell's attribute is different.</param>
- /// <param name="redrawTextStyle">The current text style for optimization.</param>
- /// <param name="maxCol">The maximum column, used for wide character handling.</param>
- /// <param name="currentCol">The current column, updated for wide characters.</param>
- 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
- }
- }
- /// <summary>
- /// Generates an ANSI escape sequence string representation of the given <paramref name="buffer"/> contents.
- /// This is the same output that would be written to the terminal to recreate the current screen contents.
- /// </summary>
- /// <param name="buffer">The output buffer to convert to ANSI.</param>
- /// <returns>A string containing ANSI escape sequences representing the buffer contents.</returns>
- 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;
- }
- }
|