1
1
using Sixel . Terminal . Models ;
2
+ using System . Diagnostics ;
2
3
3
4
namespace Sixel . Terminal ;
4
5
@@ -9,6 +10,11 @@ public static class Compatibility
9
10
/// </summary>
10
11
private static bool ? _terminalSupportsSixel ;
11
12
13
+ /// <summary>
14
+ /// Check if the terminal supports kitty graphics
15
+ /// </summary>
16
+ private static bool ? _terminalSupportsKitty ;
17
+
12
18
/// <summary>
13
19
/// Memory-caches the result of the terminal cell size.
14
20
/// </summary>
@@ -17,8 +23,12 @@ public static class Compatibility
17
23
/// <summary>
18
24
/// get the terminal info
19
25
/// </summary>
20
-
21
26
private static TerminalInfo ? _terminalInfo ;
27
+
28
+ private static WindowSizePixels ? _windowSizePixels ;
29
+
30
+ private static WindowSizeCharacters ? _windowSizeCharacters ;
31
+
22
32
/// <summary>
23
33
/// Get the cell size of the terminal in pixel-sixel size.
24
34
/// The response to the command will look like [6;20;10t where the 20 is height and 10 is width.
@@ -52,9 +62,55 @@ public static CellSize GetCellSize()
52
62
PixelHeight = 20
53
63
} ;
54
64
}
55
-
56
65
return _cellSize ;
57
66
}
67
+ public static WindowSizePixels GetWindowSizePixels ( )
68
+ {
69
+ // this class should be able to re-run, people can resize the terminal
70
+ // so should not cache the result.. hopefully this is not too slow
71
+ var response14 = GetControlSequenceResponse ( "[14t" ) ;
72
+ try
73
+ {
74
+ var parts14 = response14 . Split ( ';' , 't' ) ;
75
+ _windowSizePixels = new WindowSizePixels
76
+ {
77
+ PixelWidth = int . Parse ( parts14 [ 2 ] ) ,
78
+ PixelHeight = int . Parse ( parts14 [ 1 ] ) ,
79
+ } ;
80
+ }
81
+ catch
82
+ {
83
+ _windowSizePixels = new WindowSizePixels
84
+ {
85
+ PixelWidth = 0 ,
86
+ PixelHeight = 0
87
+ } ;
88
+ }
89
+ return _windowSizePixels ;
90
+ }
91
+ public static WindowSizeCharacters GetWindowSizeCharacters ( )
92
+ {
93
+ // this class should be able to re-run, people can resize the terminal
94
+ // so should not cache the result.. hopefully this is not too slow
95
+ var response18 = GetControlSequenceResponse ( "[18t" ) ;
96
+ try
97
+ {
98
+ var parts18 = response18 . Split ( ';' , 't' ) ;
99
+ _windowSizeCharacters = new WindowSizeCharacters
100
+ {
101
+ CharacterWidth = int . Parse ( parts18 [ 2 ] ) ,
102
+ CharacterHeight = int . Parse ( parts18 [ 1 ] ) ,
103
+ } ;
104
+ }
105
+ catch {
106
+ _windowSizeCharacters = new WindowSizeCharacters
107
+ {
108
+ CharacterWidth = 0 ,
109
+ CharacterHeight = 0
110
+ } ;
111
+ }
112
+ return _windowSizeCharacters ;
113
+ }
58
114
59
115
/// <summary>
60
116
/// Check if the terminal supports sixel graphics.
@@ -69,17 +125,28 @@ public static bool TerminalSupportsSixel()
69
125
{
70
126
return _terminalSupportsSixel . Value ;
71
127
}
72
-
73
128
_terminalSupportsSixel = GetControlSequenceResponse ( "[c" ) . Contains ( ";4;" ) ;
74
-
75
129
return _terminalSupportsSixel . Value ;
76
130
}
77
131
78
132
/// <summary>
79
- /// Send a control sequence to the terminal and read back the response from STDIN.
133
+ /// Check if the terminal supports kitty graphics.
134
+ /// https://sw.kovidgoyal.net/kitty/graphics-protocol/
135
+ // response: ␛_Gi=31;OK␛\␛[?62;c
80
136
/// </summary>
81
- /// <param name="controlSequence"></param>
82
- /// <returns>The response from the terminal.</returns>
137
+ /// <returns>True if the terminal supports sixel graphics, false otherwise.</returns>
138
+ public static bool TerminalSupportsKitty ( )
139
+ {
140
+ if ( _terminalSupportsKitty . HasValue )
141
+ {
142
+ return _terminalSupportsKitty . Value ;
143
+ }
144
+ // string kittyTest = $"_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA{Constants.ESC}\\{Constants.ESC}[c";
145
+ string kittyTest = $ "_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA{ Constants . ESC } \\ ";
146
+ _terminalSupportsKitty = GetControlSequenceResponse ( kittyTest ) . Contains ( ";OK" ) ;
147
+ return _terminalSupportsKitty . Value ;
148
+ }
149
+
83
150
private static string GetControlSequenceResponse ( string controlSequence )
84
151
{
85
152
char ? c ;
0 commit comments