using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
namespace Terminal.Gui;
///
/// Uses ANSII escape sequences to detect whether sixel is supported
/// by the terminal.
///
public class SixelSupportDetector
{
public SixelSupport Detect ()
{
var result = new SixelSupport ();
result.IsSupported =
AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_SendDeviceAttributes, out AnsiEscapeSequenceResponse darResponse)
? darResponse.Response.Split (';').Contains ("4")
: false;
if (result.IsSupported)
{
// Expect something like:
//[6;20;10t
bool gotResolutionDirectly = false;
if (AnsiEscapeSequenceRequest.TryExecuteAnsiRequest (EscSeqUtils.CSI_RequestSixelResolution, out var resolution))
{
// Terminal supports directly responding with resolution
var match = Regex.Match (resolution.Response, @"\[\d+;(\d+);(\d+)t$");
if (match.Success)
{
if (int.TryParse (match.Groups [1].Value, out var ry) &&
int.TryParse (match.Groups [2].Value, out var rx))
{
result.Resolution = new Size (rx, ry);
gotResolutionDirectly = true;
}
}
}
if (!gotResolutionDirectly)
{
// TODO: Try pixel/window resolution getting
}
}
return result;
}
}
public class SixelSupport
{
///
/// Whether the current driver supports sixel graphic format.
/// Defaults to false.
///
public bool IsSupported { get; set; }
///
/// The number of pixels of sixel that corresponds to each Col ()
/// and each Row (. Defaults to 10x20.
///
public Size Resolution { get; set; } = new (10, 20);
///
/// The maximum number of colors that can be included in a sixel image. Defaults
/// to 256.
///
public int MaxPaletteColors { get; set; } = 256;
}