فهرست منبع

WindowsDriver prototype sixel support

tznind 10 ماه پیش
والد
کامیت
5e356c2b24
3فایلهای تغییر یافته به همراه31 افزوده شده و 2 حذف شده
  1. 18 0
      Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
  2. 9 1
      Terminal.Gui/Drawing/Quant/ColorQuantizer.cs
  3. 4 1
      UICatalog/Scenarios/Images.cs

+ 18 - 0
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -37,6 +37,7 @@ internal class WindowsConsole
     private CursorVisibility? _currentCursorVisibility;
     private CursorVisibility? _pendingCursorVisibility;
     private readonly StringBuilder _stringBuilder = new (256 * 1024);
+    private string _lastWrite = string.Empty;
 
     public WindowsConsole ()
     {
@@ -116,7 +117,24 @@ internal class WindowsConsole
 
             var s = _stringBuilder.ToString ();
 
+            // TODO: requires extensive testing if we go down this route
+            // If console output has changed
+            if (s != _lastWrite)
+            {
+                // supply console with the new content
+                result = WriteConsole (_screenBuffer, s, (uint)s.Length, out uint _, nint.Zero);
+            }
+
             result = WriteConsole (_screenBuffer, s, (uint)s.Length, out uint _, nint.Zero);
+
+            _lastWrite = s;
+
+            foreach (var sixel in Application.Sixel)
+            {
+                SetCursorPosition (new Coord ((short)sixel.ScreenPosition.X, (short)sixel.ScreenPosition.Y));
+                WriteConsole (_screenBuffer, sixel.SixelData, (uint)sixel.SixelData.Length, out uint _, nint.Zero);
+
+            }
         }
 
         if (!result)

+ 9 - 1
Terminal.Gui/Drawing/Quant/ColorQuantizer.cs

@@ -1,4 +1,4 @@
-
+using System.Collections.Concurrent;
 
 namespace Terminal.Gui;
 
@@ -30,6 +30,8 @@ public class ColorQuantizer
     /// </summary>
     public IPaletteBuilder PaletteBuildingAlgorithm { get; set; } = new PopularityPaletteWithThreshold (new EuclideanColorDistance (),5) ;
 
+    private readonly ConcurrentDictionary<Color, int> _nearestColorCache = new ();
+
     public void BuildPalette (Color [,] pixels)
     {
         List<Color> allColors = new List<Color> ();
@@ -49,6 +51,11 @@ public class ColorQuantizer
 
     public int GetNearestColor (Color toTranslate)
     {
+        if (_nearestColorCache.TryGetValue (toTranslate, out var cachedAnswer))
+        {
+            return cachedAnswer;
+        }
+
         // Simple nearest color matching based on DistanceAlgorithm
         double minDistance = double.MaxValue;
         int nearestIndex = 0;
@@ -65,6 +72,7 @@ public class ColorQuantizer
             }
         }
 
+        _nearestColorCache.TryAdd (toTranslate, nearestIndex);
         return nearestIndex;
     }
 }

+ 4 - 1
UICatalog/Scenarios/Images.cs

@@ -120,7 +120,10 @@ public class Images : Scenario
                                 {
                                     fire.AdvanceFrame ();
                                     counter++;
-                                    if (counter % 5 != 0)
+
+                                    // Control frame rate by adjusting this
+                                    // Lower number means more FPS
+                                    if (counter % 2 != 0)
                                     {
                                         return true;
                                     }