-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathrenderer.zig
More file actions
1105 lines (911 loc) · 43.2 KB
/
renderer.zig
File metadata and controls
1105 lines (911 loc) · 43.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const Allocator = std.mem.Allocator;
const ansi = @import("ansi.zig");
const buf = @import("buffer.zig");
const gp = @import("grapheme.zig");
const Terminal = @import("terminal.zig");
const logger = @import("logger.zig");
pub const RGBA = ansi.RGBA;
pub const OptimizedBuffer = buf.OptimizedBuffer;
pub const TextAttributes = ansi.TextAttributes;
pub const CursorStyle = Terminal.CursorStyle;
const CLEAR_CHAR = '\u{0a00}';
const MAX_STAT_SAMPLES = 30;
const STAT_SAMPLE_CAPACITY = 30;
const COLOR_EPSILON_DEFAULT: f32 = 0.00001;
const OUTPUT_BUFFER_SIZE = 1024 * 1024 * 2; // 2MB
pub const RendererError = error{
OutOfMemory,
InvalidDimensions,
ThreadingFailed,
WriteFailed,
};
fn rgbaComponentToU8(component: f32) u8 {
if (!std.math.isFinite(component)) return 0;
const clamped = std.math.clamp(component, 0.0, 1.0);
return @intFromFloat(@round(clamped * 255.0));
}
pub const DebugOverlayCorner = enum {
topLeft,
topRight,
bottomLeft,
bottomRight,
};
pub const CliRenderer = struct {
width: u32,
height: u32,
currentRenderBuffer: *OptimizedBuffer,
nextRenderBuffer: *OptimizedBuffer,
pool: *gp.GraphemePool,
backgroundColor: RGBA,
renderOffset: u32,
terminal: Terminal,
testing: bool = false,
useAlternateScreen: bool = true,
terminalSetup: bool = false,
renderStats: struct {
lastFrameTime: f64,
averageFrameTime: f64,
frameCount: u64,
fps: u32,
cellsUpdated: u32,
renderTime: ?f64,
overallFrameTime: ?f64,
bufferResetTime: ?f64,
stdoutWriteTime: ?f64,
heapUsed: u32,
heapTotal: u32,
arrayBuffers: u32,
frameCallbackTime: ?f64,
},
statSamples: struct {
lastFrameTime: std.ArrayList(f64),
renderTime: std.ArrayList(f64),
overallFrameTime: std.ArrayList(f64),
bufferResetTime: std.ArrayList(f64),
stdoutWriteTime: std.ArrayList(f64),
cellsUpdated: std.ArrayList(u32),
frameCallbackTime: std.ArrayList(f64),
},
lastRenderTime: i64,
allocator: Allocator,
renderThread: ?std.Thread = null,
stdoutWriter: std.io.BufferedWriter(4096, std.fs.File.Writer),
debugOverlay: struct {
enabled: bool,
corner: DebugOverlayCorner,
} = .{
.enabled = false,
.corner = .bottomRight,
},
// Threading
useThread: bool = false,
renderMutex: std.Thread.Mutex = .{},
renderCondition: std.Thread.Condition = .{},
renderRequested: bool = false,
shouldTerminate: bool = false,
renderInProgress: bool = false,
currentOutputBuffer: []u8 = &[_]u8{},
currentOutputLen: usize = 0,
currentHitGrid: []u32,
nextHitGrid: []u32,
hitGridWidth: u32,
hitGridHeight: u32,
lastCursorStyleTag: ?u8 = null,
lastCursorBlinking: ?bool = null,
lastCursorColorRGB: ?[3]u8 = null,
// Preallocated output buffer
var outputBuffer: [OUTPUT_BUFFER_SIZE]u8 = undefined;
var outputBufferLen: usize = 0;
var outputBufferB: [OUTPUT_BUFFER_SIZE]u8 = undefined;
var outputBufferBLen: usize = 0;
var activeBuffer: enum { A, B } = .A;
const OutputBufferWriter = struct {
pub fn write(_: void, data: []const u8) !usize {
const bufferLen = if (activeBuffer == .A) &outputBufferLen else &outputBufferBLen;
const buffer = if (activeBuffer == .A) &outputBuffer else &outputBufferB;
if (bufferLen.* + data.len > buffer.len) {
// TODO: Resize buffer when necessary
return error.BufferFull;
}
@memcpy(buffer.*[bufferLen.*..][0..data.len], data);
bufferLen.* += data.len;
return data.len;
}
pub fn writer() std.io.Writer(void, error{BufferFull}, write) {
return .{ .context = {} };
}
};
pub fn create(allocator: Allocator, width: u32, height: u32, pool: *gp.GraphemePool, testing: bool) !*CliRenderer {
const self = try allocator.create(CliRenderer);
const currentBuffer = try OptimizedBuffer.init(allocator, width, height, .{ .pool = pool, .width_method = .unicode, .id = "current buffer" });
const nextBuffer = try OptimizedBuffer.init(allocator, width, height, .{ .pool = pool, .width_method = .unicode, .id = "next buffer" });
const stdoutWriter = if (testing) blk: {
// In testing mode, use /dev/null to discard output
const devnull = std.fs.openFileAbsolute("/dev/null", .{ .mode = .write_only }) catch {
// Fallback to stdout if /dev/null can't be opened
logger.warn("Failed to open /dev/null, falling back to stdout\n", .{});
break :blk std.io.BufferedWriter(4096, std.fs.File.Writer){ .unbuffered_writer = std.io.getStdOut().writer() };
};
break :blk std.io.BufferedWriter(4096, std.fs.File.Writer){ .unbuffered_writer = devnull.writer() };
} else blk: {
const stdout = std.io.getStdOut();
break :blk std.io.BufferedWriter(4096, std.fs.File.Writer){ .unbuffered_writer = stdout.writer() };
};
// stat sample arrays
var lastFrameTime = std.ArrayList(f64).init(allocator);
var renderTime = std.ArrayList(f64).init(allocator);
var overallFrameTime = std.ArrayList(f64).init(allocator);
var bufferResetTime = std.ArrayList(f64).init(allocator);
var stdoutWriteTime = std.ArrayList(f64).init(allocator);
var cellsUpdated = std.ArrayList(u32).init(allocator);
var frameCallbackTimes = std.ArrayList(f64).init(allocator);
try lastFrameTime.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
try renderTime.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
try overallFrameTime.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
try bufferResetTime.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
try stdoutWriteTime.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
try cellsUpdated.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
try frameCallbackTimes.ensureTotalCapacity(STAT_SAMPLE_CAPACITY);
const hitGridSize = width * height;
const currentHitGrid = try allocator.alloc(u32, hitGridSize);
const nextHitGrid = try allocator.alloc(u32, hitGridSize);
@memset(currentHitGrid, 0); // Initialize with 0 (no renderable)
@memset(nextHitGrid, 0);
self.* = .{
.width = width,
.height = height,
.currentRenderBuffer = currentBuffer,
.nextRenderBuffer = nextBuffer,
.pool = pool,
.backgroundColor = .{ 0.0, 0.0, 0.0, 0.0 },
.renderOffset = 0,
.terminal = Terminal.init(.{}),
.testing = testing,
.lastCursorStyleTag = null,
.lastCursorBlinking = null,
.lastCursorColorRGB = null,
.renderStats = .{
.lastFrameTime = 0,
.averageFrameTime = 0,
.frameCount = 0,
.fps = 0,
.cellsUpdated = 0,
.renderTime = null,
.overallFrameTime = null,
.bufferResetTime = null,
.stdoutWriteTime = null,
.heapUsed = 0,
.heapTotal = 0,
.arrayBuffers = 0,
.frameCallbackTime = null,
},
.statSamples = .{
.lastFrameTime = lastFrameTime,
.renderTime = renderTime,
.overallFrameTime = overallFrameTime,
.bufferResetTime = bufferResetTime,
.stdoutWriteTime = stdoutWriteTime,
.cellsUpdated = cellsUpdated,
.frameCallbackTime = frameCallbackTimes,
},
.lastRenderTime = std.time.microTimestamp(),
.allocator = allocator,
.stdoutWriter = stdoutWriter,
.currentHitGrid = currentHitGrid,
.nextHitGrid = nextHitGrid,
.hitGridWidth = width,
.hitGridHeight = height,
};
try currentBuffer.clear(.{ self.backgroundColor[0], self.backgroundColor[1], self.backgroundColor[2], self.backgroundColor[3] }, CLEAR_CHAR);
try nextBuffer.clear(.{ self.backgroundColor[0], self.backgroundColor[1], self.backgroundColor[2], self.backgroundColor[3] }, null);
return self;
}
pub fn destroy(self: *CliRenderer) void {
self.renderMutex.lock();
while (self.renderInProgress) {
self.renderCondition.wait(&self.renderMutex);
}
self.shouldTerminate = true;
self.renderRequested = true;
self.renderCondition.signal();
self.renderMutex.unlock();
if (self.renderThread) |thread| {
thread.join();
}
self.performShutdownSequence();
self.currentRenderBuffer.deinit();
self.nextRenderBuffer.deinit();
// Free stat sample arrays
self.statSamples.lastFrameTime.deinit();
self.statSamples.renderTime.deinit();
self.statSamples.overallFrameTime.deinit();
self.statSamples.bufferResetTime.deinit();
self.statSamples.stdoutWriteTime.deinit();
self.statSamples.cellsUpdated.deinit();
self.statSamples.frameCallbackTime.deinit();
self.allocator.free(self.currentHitGrid);
self.allocator.free(self.nextHitGrid);
self.allocator.destroy(self);
}
pub fn setupTerminal(self: *CliRenderer, useAlternateScreen: bool) void {
self.useAlternateScreen = useAlternateScreen;
self.terminalSetup = true;
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
self.terminal.queryTerminalSend(writer) catch {
logger.warn("Failed to query terminal capabilities", .{});
};
self.setupTerminalWithoutDetection(useAlternateScreen);
}
fn setupTerminalWithoutDetection(self: *CliRenderer, useAlternateScreen: bool) void {
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
writer.writeAll(ansi.ANSI.saveCursorState) catch {};
if (useAlternateScreen) {
self.terminal.enterAltScreen(writer) catch {};
} else {
ansi.ANSI.makeRoomForRendererOutput(writer, @max(self.height, 1)) catch {};
}
self.terminal.setCursorPosition(1, 1, false);
const useKitty = self.terminal.opts.kitty_keyboard_flags > 0;
self.terminal.enableDetectedFeatures(writer, useKitty) catch {};
bufferedWriter.flush() catch {};
}
pub fn suspendRenderer(self: *CliRenderer) void {
if (!self.terminalSetup) return;
self.performShutdownSequence();
}
pub fn resumeRenderer(self: *CliRenderer) void {
if (!self.terminalSetup) return;
self.setupTerminalWithoutDetection(self.useAlternateScreen);
}
pub fn performShutdownSequence(self: *CliRenderer) void {
if (!self.terminalSetup) return;
const direct = self.stdoutWriter.writer();
self.terminal.resetState(direct) catch {
logger.warn("Failed to reset terminal state", .{});
};
if (self.useAlternateScreen) {
self.stdoutWriter.flush() catch {};
} else if (self.renderOffset == 0) {
direct.writeAll("\x1b[H\x1b[J") catch {};
self.stdoutWriter.flush() catch {};
} else if (self.renderOffset > 0) {
// Currently still handled in typescript
// const consoleEndLine = self.height - self.renderOffset;
// ansi.ANSI.moveToOutput(direct, 1, consoleEndLine) catch {};
}
// NOTE: This messes up state after shutdown, but might be necessary for windows?
// direct.writeAll(ansi.ANSI.restoreCursorState) catch {};
direct.writeAll(ansi.ANSI.resetCursorColorFallback) catch {};
direct.writeAll(ansi.ANSI.resetCursorColor) catch {};
direct.writeAll(ansi.ANSI.defaultCursorStyle) catch {};
// Workaround for Ghostty not showing the cursor after shutdown for some reason
direct.writeAll(ansi.ANSI.showCursor) catch {};
self.stdoutWriter.flush() catch {};
std.time.sleep(10 * std.time.ns_per_ms);
direct.writeAll(ansi.ANSI.showCursor) catch {};
self.stdoutWriter.flush() catch {};
std.time.sleep(10 * std.time.ns_per_ms);
}
fn addStatSample(comptime T: type, samples: *std.ArrayList(T), value: T) void {
samples.append(value) catch return;
if (samples.items.len > MAX_STAT_SAMPLES) {
_ = samples.orderedRemove(0);
}
}
fn getStatAverage(comptime T: type, samples: *const std.ArrayList(T)) T {
if (samples.items.len == 0) {
return 0;
}
var sum: T = 0;
for (samples.items) |value| {
sum += value;
}
if (@typeInfo(T) == .float) {
return sum / @as(T, @floatFromInt(samples.items.len));
} else {
return sum / @as(T, @intCast(samples.items.len));
}
}
pub fn setUseThread(self: *CliRenderer, useThread: bool) void {
if (self.useThread == useThread) return;
if (useThread) {
if (self.renderThread == null) {
self.renderThread = std.Thread.spawn(.{}, renderThreadFn, .{self}) catch |err| {
std.log.warn("Failed to spawn render thread: {}, falling back to non-threaded mode", .{err});
self.useThread = false;
return;
};
}
} else {
if (self.renderThread) |thread| {
// Signal the render thread to terminate (same pattern as destroy)
self.renderMutex.lock();
while (self.renderInProgress) {
self.renderCondition.wait(&self.renderMutex);
}
self.shouldTerminate = true;
self.renderRequested = true;
self.renderCondition.signal();
self.renderMutex.unlock();
thread.join();
self.renderThread = null;
// Reset termination flag so thread can be re-enabled later
self.shouldTerminate = false;
}
}
self.useThread = useThread;
}
pub fn updateStats(self: *CliRenderer, time: f64, fps: u32, frameCallbackTime: f64) void {
self.renderStats.overallFrameTime = time;
self.renderStats.fps = fps;
self.renderStats.frameCallbackTime = frameCallbackTime;
addStatSample(f64, &self.statSamples.overallFrameTime, time);
addStatSample(f64, &self.statSamples.frameCallbackTime, frameCallbackTime);
}
pub fn updateMemoryStats(self: *CliRenderer, heapUsed: u32, heapTotal: u32, arrayBuffers: u32) void {
self.renderStats.heapUsed = heapUsed;
self.renderStats.heapTotal = heapTotal;
self.renderStats.arrayBuffers = arrayBuffers;
}
pub fn resize(self: *CliRenderer, width: u32, height: u32) !void {
if (self.width == width and self.height == height) return;
self.width = width;
self.height = height;
try self.currentRenderBuffer.resize(width, height);
try self.nextRenderBuffer.resize(width, height);
try self.currentRenderBuffer.clear(.{ 0.0, 0.0, 0.0, 1.0 }, CLEAR_CHAR);
try self.nextRenderBuffer.clear(.{ self.backgroundColor[0], self.backgroundColor[1], self.backgroundColor[2], self.backgroundColor[3] }, null);
const newHitGridSize = width * height;
const currentHitGridSize = self.hitGridWidth * self.hitGridHeight;
if (newHitGridSize > currentHitGridSize) {
const newCurrentHitGrid = try self.allocator.alloc(u32, newHitGridSize);
const newNextHitGrid = try self.allocator.alloc(u32, newHitGridSize);
@memset(newCurrentHitGrid, 0);
@memset(newNextHitGrid, 0);
self.allocator.free(self.currentHitGrid);
self.allocator.free(self.nextHitGrid);
self.currentHitGrid = newCurrentHitGrid;
self.nextHitGrid = newNextHitGrid;
self.hitGridWidth = width;
self.hitGridHeight = height;
}
const cursor = self.terminal.getCursorPosition();
self.terminal.setCursorPosition(@min(cursor.x, width), @min(cursor.y, height), cursor.visible);
}
pub fn setBackgroundColor(self: *CliRenderer, rgba: RGBA) void {
self.backgroundColor = rgba;
}
pub fn setRenderOffset(self: *CliRenderer, offset: u32) void {
self.renderOffset = offset;
}
fn renderThreadFn(self: *CliRenderer) void {
while (true) {
self.renderMutex.lock();
while (!self.renderRequested and !self.shouldTerminate) {
self.renderCondition.wait(&self.renderMutex);
}
if (self.shouldTerminate and !self.renderRequested) {
self.renderMutex.unlock();
break;
}
self.renderRequested = false;
const outputData = self.currentOutputBuffer;
const outputLen = self.currentOutputLen;
const writeStart = std.time.microTimestamp();
if (outputLen > 0) {
var bufferedWriter = &self.stdoutWriter;
bufferedWriter.writer().writeAll(outputData[0..outputLen]) catch {};
bufferedWriter.flush() catch {};
}
// Signal that rendering is complete
self.renderStats.stdoutWriteTime = @as(f64, @floatFromInt(std.time.microTimestamp() - writeStart));
self.renderInProgress = false;
self.renderCondition.signal();
self.renderMutex.unlock();
}
}
// Render once with current state
pub fn render(self: *CliRenderer, force: bool) void {
const now = std.time.microTimestamp();
const deltaTimeMs = @as(f64, @floatFromInt(now - self.lastRenderTime));
const deltaTime = deltaTimeMs / 1000.0; // Convert to seconds
self.lastRenderTime = now;
self.renderDebugOverlay();
self.prepareRenderFrame(force);
if (self.useThread) {
self.renderMutex.lock();
while (self.renderInProgress) {
self.renderCondition.wait(&self.renderMutex);
}
if (activeBuffer == .A) {
activeBuffer = .B;
self.currentOutputBuffer = &outputBuffer;
self.currentOutputLen = outputBufferLen;
} else {
activeBuffer = .A;
self.currentOutputBuffer = &outputBufferB;
self.currentOutputLen = outputBufferBLen;
}
self.renderRequested = true;
self.renderInProgress = true;
self.renderCondition.signal();
self.renderMutex.unlock();
} else {
const writeStart = std.time.microTimestamp();
var bufferedWriter = &self.stdoutWriter;
bufferedWriter.writer().writeAll(outputBuffer[0..outputBufferLen]) catch {};
bufferedWriter.flush() catch {};
self.renderStats.stdoutWriteTime = @as(f64, @floatFromInt(std.time.microTimestamp() - writeStart));
}
self.renderStats.lastFrameTime = deltaTime * 1000.0;
self.renderStats.frameCount += 1;
addStatSample(f64, &self.statSamples.lastFrameTime, deltaTime * 1000.0);
if (self.renderStats.renderTime) |rt| {
addStatSample(f64, &self.statSamples.renderTime, rt);
}
if (self.renderStats.bufferResetTime) |brt| {
addStatSample(f64, &self.statSamples.bufferResetTime, brt);
}
if (self.renderStats.stdoutWriteTime) |swt| {
addStatSample(f64, &self.statSamples.stdoutWriteTime, swt);
}
addStatSample(u32, &self.statSamples.cellsUpdated, self.renderStats.cellsUpdated);
}
pub fn getNextBuffer(self: *CliRenderer) *OptimizedBuffer {
return self.nextRenderBuffer;
}
pub fn getCurrentBuffer(self: *CliRenderer) *OptimizedBuffer {
return self.currentRenderBuffer;
}
fn prepareRenderFrame(self: *CliRenderer, force: bool) void {
const renderStartTime = std.time.microTimestamp();
var cellsUpdated: u32 = 0;
if (activeBuffer == .A) {
outputBufferLen = 0;
} else {
outputBufferBLen = 0;
}
var writer = OutputBufferWriter.writer();
writer.writeAll(ansi.ANSI.syncSet) catch {};
writer.writeAll(ansi.ANSI.hideCursor) catch {};
var currentFg: ?RGBA = null;
var currentBg: ?RGBA = null;
var currentAttributes: i16 = -1;
var utf8Buf: [4]u8 = undefined;
const colorEpsilon: f32 = COLOR_EPSILON_DEFAULT;
for (0..self.height) |uy| {
const y = @as(u32, @intCast(uy));
var runStart: i64 = -1;
var runLength: u32 = 0;
for (0..self.width) |ux| {
const x = @as(u32, @intCast(ux));
const currentCell = self.currentRenderBuffer.get(x, y);
const nextCell = self.nextRenderBuffer.get(x, y);
if (currentCell == null or nextCell == null) continue;
if (!force) {
const charEqual = currentCell.?.char == nextCell.?.char;
const attrEqual = currentCell.?.attributes == nextCell.?.attributes;
if (charEqual and attrEqual and
buf.rgbaEqual(currentCell.?.fg, nextCell.?.fg, colorEpsilon) and
buf.rgbaEqual(currentCell.?.bg, nextCell.?.bg, colorEpsilon))
{
if (runLength > 0) {
writer.writeAll(ansi.ANSI.reset) catch {};
runStart = -1;
runLength = 0;
}
continue;
}
}
const cell = nextCell.?;
const fgMatch = currentFg != null and buf.rgbaEqual(currentFg.?, cell.fg, colorEpsilon);
const bgMatch = currentBg != null and buf.rgbaEqual(currentBg.?, cell.bg, colorEpsilon);
const sameAttributes = fgMatch and bgMatch and @as(i16, cell.attributes) == currentAttributes;
if (!sameAttributes or runStart == -1) {
if (runLength > 0) {
writer.writeAll(ansi.ANSI.reset) catch {};
}
runStart = @intCast(x);
runLength = 0;
currentFg = cell.fg;
currentBg = cell.bg;
currentAttributes = @intCast(cell.attributes);
ansi.ANSI.moveToOutput(writer, x + 1, y + 1 + self.renderOffset) catch {};
const fgR = rgbaComponentToU8(cell.fg[0]);
const fgG = rgbaComponentToU8(cell.fg[1]);
const fgB = rgbaComponentToU8(cell.fg[2]);
const bgR = rgbaComponentToU8(cell.bg[0]);
const bgG = rgbaComponentToU8(cell.bg[1]);
const bgB = rgbaComponentToU8(cell.bg[2]);
const bgA = cell.bg[3];
ansi.ANSI.fgColorOutput(writer, fgR, fgG, fgB) catch {};
// If alpha is 0 (transparent), use terminal default background instead of black
if (bgA < 0.001) {
writer.writeAll("\x1b[49m") catch {};
} else {
ansi.ANSI.bgColorOutput(writer, bgR, bgG, bgB) catch {};
}
ansi.TextAttributes.applyAttributesOutputWriter(writer, cell.attributes) catch {};
}
// Handle grapheme characters
if (gp.isGraphemeChar(cell.char)) {
const gid: u32 = gp.graphemeIdFromChar(cell.char);
const bytes = self.pool.get(gid) catch |err| {
self.performShutdownSequence();
std.debug.panic("Fatal: no grapheme bytes in pool for gid {d}: {}", .{ gid, err });
};
if (bytes.len > 0) {
const capabilities = self.terminal.getCapabilities();
if (capabilities.explicit_width) {
const graphemeWidth = gp.charRightExtent(cell.char) + 1;
ansi.ANSI.explicitWidthOutput(writer, graphemeWidth, bytes) catch {};
} else {
writer.writeAll(bytes) catch {};
}
}
} else if (gp.isContinuationChar(cell.char)) {
// Write a space for continuation cells to clear any previous content
writer.writeByte(' ') catch {};
} else {
const len = std.unicode.utf8Encode(@intCast(cell.char), &utf8Buf) catch 1;
writer.writeAll(utf8Buf[0..len]) catch {};
}
runLength += 1;
// Update the current buffer with the new cell
self.currentRenderBuffer.setRaw(x, y, nextCell.?);
// If this is a grapheme start, also update all continuation cells
if (gp.isGraphemeChar(nextCell.?.char)) {
const rightExtent = gp.charRightExtent(nextCell.?.char);
var k: u32 = 1;
while (k <= rightExtent and x + k < self.width) : (k += 1) {
if (self.nextRenderBuffer.get(x + k, y)) |contCell| {
self.currentRenderBuffer.setRaw(x + k, y, contCell);
}
}
}
cellsUpdated += 1;
}
}
writer.writeAll(ansi.ANSI.reset) catch {};
const cursorPos = self.terminal.getCursorPosition();
const cursorStyle = self.terminal.getCursorStyle();
const cursorColor = self.terminal.getCursorColor();
if (cursorPos.visible) {
var cursorStyleCode: []const u8 = undefined;
switch (cursorStyle.style) {
.block => {
cursorStyleCode = if (cursorStyle.blinking)
ansi.ANSI.cursorBlockBlink
else
ansi.ANSI.cursorBlock;
},
.line => {
cursorStyleCode = if (cursorStyle.blinking)
ansi.ANSI.cursorLineBlink
else
ansi.ANSI.cursorLine;
},
.underline => {
cursorStyleCode = if (cursorStyle.blinking)
ansi.ANSI.cursorUnderlineBlink
else
ansi.ANSI.cursorUnderline;
},
}
const cursorR = rgbaComponentToU8(cursorColor[0]);
const cursorG = rgbaComponentToU8(cursorColor[1]);
const cursorB = rgbaComponentToU8(cursorColor[2]);
const styleTag: u8 = @intFromEnum(cursorStyle.style);
const styleChanged = (self.lastCursorStyleTag == null or self.lastCursorStyleTag.? != styleTag) or
(self.lastCursorBlinking == null or self.lastCursorBlinking.? != cursorStyle.blinking);
const colorChanged = (self.lastCursorColorRGB == null or self.lastCursorColorRGB.?[0] != cursorR or self.lastCursorColorRGB.?[1] != cursorG or self.lastCursorColorRGB.?[2] != cursorB);
if (colorChanged) {
ansi.ANSI.cursorColorOutputWriter(writer, cursorR, cursorG, cursorB) catch {};
self.lastCursorColorRGB = .{ cursorR, cursorG, cursorB };
}
if (styleChanged) {
writer.writeAll(cursorStyleCode) catch {};
self.lastCursorStyleTag = styleTag;
self.lastCursorBlinking = cursorStyle.blinking;
}
ansi.ANSI.moveToOutput(writer, cursorPos.x, cursorPos.y + self.renderOffset) catch {};
writer.writeAll(ansi.ANSI.showCursor) catch {};
} else {
writer.writeAll(ansi.ANSI.hideCursor) catch {};
self.lastCursorStyleTag = null;
self.lastCursorBlinking = null;
self.lastCursorColorRGB = null;
}
writer.writeAll(ansi.ANSI.syncReset) catch {};
const renderEndTime = std.time.microTimestamp();
const renderTime = @as(f64, @floatFromInt(renderEndTime - renderStartTime));
self.renderStats.cellsUpdated = cellsUpdated;
self.renderStats.renderTime = renderTime;
self.nextRenderBuffer.clear(.{ self.backgroundColor[0], self.backgroundColor[1], self.backgroundColor[2], self.backgroundColor[3] }, null) catch {};
const temp = self.currentHitGrid;
self.currentHitGrid = self.nextHitGrid;
self.nextHitGrid = temp;
@memset(self.nextHitGrid, 0);
}
pub fn setDebugOverlay(self: *CliRenderer, enabled: bool, corner: DebugOverlayCorner) void {
self.debugOverlay.enabled = enabled;
self.debugOverlay.corner = corner;
}
pub fn clearTerminal(self: *CliRenderer) void {
var bufferedWriter = &self.stdoutWriter;
bufferedWriter.writer().writeAll(ansi.ANSI.clearAndHome) catch {};
bufferedWriter.flush() catch {};
}
pub fn addToHitGrid(self: *CliRenderer, x: i32, y: i32, width: u32, height: u32, id: u32) void {
const startX = @max(0, x);
const startY = @max(0, y);
const endX = @min(@as(i32, @intCast(self.hitGridWidth)), x + @as(i32, @intCast(width)));
const endY = @min(@as(i32, @intCast(self.hitGridHeight)), y + @as(i32, @intCast(height)));
if (startX >= endX or startY >= endY) return;
const uStartX: u32 = @intCast(startX);
const uStartY: u32 = @intCast(startY);
const uEndX: u32 = @intCast(endX);
const uEndY: u32 = @intCast(endY);
for (uStartY..uEndY) |row| {
const rowStart = row * self.hitGridWidth;
const startIdx = rowStart + uStartX;
const endIdx = rowStart + uEndX;
@memset(self.nextHitGrid[startIdx..endIdx], id);
}
}
pub fn checkHit(self: *CliRenderer, x: u32, y: u32) u32 {
if (x >= self.hitGridWidth or y >= self.hitGridHeight) {
return 0;
}
const index = y * self.hitGridWidth + x;
return self.currentHitGrid[index];
}
pub fn dumpHitGrid(self: *CliRenderer) void {
const timestamp = std.time.timestamp();
var filename_buf: [64]u8 = undefined;
const filename = std.fmt.bufPrint(&filename_buf, "hitgrid_{d}.txt", .{timestamp}) catch return;
const file = std.fs.cwd().createFile(filename, .{}) catch return;
defer file.close();
const writer = file.writer();
for (0..self.hitGridHeight) |y| {
for (0..self.hitGridWidth) |x| {
const index = y * self.hitGridWidth + x;
const id = self.currentHitGrid[index];
const char = if (id == 0) '.' else ('0' + @as(u8, @intCast(id % 10)));
writer.writeByte(char) catch return;
}
writer.writeByte('\n') catch return;
}
}
fn dumpSingleBuffer(self: *CliRenderer, buffer: *OptimizedBuffer, buffer_name: []const u8, timestamp: i64) void {
std.fs.cwd().makeDir("buffer_dump") catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return,
};
var filename_buf: [128]u8 = undefined;
const filename = std.fmt.bufPrint(&filename_buf, "buffer_dump/{s}_buffer_{d}.txt", .{ buffer_name, timestamp }) catch return;
const file = std.fs.cwd().createFile(filename, .{}) catch return;
defer file.close();
const writer = file.writer();
writer.print("{s} Buffer ({d}x{d}):\n", .{ buffer_name, self.width, self.height }) catch return;
writer.writeAll("Characters:\n") catch return;
for (0..self.height) |y| {
for (0..self.width) |x| {
const cell = buffer.get(@intCast(x), @intCast(y));
if (cell) |c| {
if (gp.isContinuationChar(c.char)) {
// skip
} else if (gp.isGraphemeChar(c.char)) {
const gid: u32 = gp.graphemeIdFromChar(c.char);
const bytes = self.pool.get(gid) catch &[_]u8{};
if (bytes.len > 0) writer.writeAll(bytes) catch return;
} else {
var utf8Buf: [4]u8 = undefined;
const len = std.unicode.utf8Encode(@intCast(c.char), &utf8Buf) catch 1;
writer.writeAll(utf8Buf[0..len]) catch return;
}
} else {
writer.writeByte(' ') catch return;
}
}
writer.writeByte('\n') catch return;
}
}
pub fn dumpStdoutBuffer(self: *CliRenderer, timestamp: i64) void {
_ = self;
std.fs.cwd().makeDir("buffer_dump") catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return,
};
var filename_buf: [128]u8 = undefined;
const filename = std.fmt.bufPrint(&filename_buf, "buffer_dump/stdout_buffer_{d}.txt", .{timestamp}) catch return;
const file = std.fs.cwd().createFile(filename, .{}) catch return;
defer file.close();
const writer = file.writer();
writer.print("Stdout Buffer Output (timestamp: {d}):\n", .{timestamp}) catch return;
writer.writeAll("Last Rendered ANSI Output:\n") catch return;
writer.writeAll("================\n") catch return;
const lastBuffer = if (activeBuffer == .A) &outputBufferB else &outputBuffer;
const lastLen = if (activeBuffer == .A) outputBufferBLen else outputBufferLen;
if (lastLen > 0) {
writer.writeAll(lastBuffer.*[0..lastLen]) catch return;
} else {
writer.writeAll("(no output rendered yet)\n") catch return;
}
writer.writeAll("\n================\n") catch return;
writer.print("Buffer size: {d} bytes\n", .{lastLen}) catch return;
writer.print("Active buffer: {s}\n", .{if (activeBuffer == .A) "A" else "B"}) catch return;
}
pub fn dumpBuffers(self: *CliRenderer, timestamp: i64) void {
self.dumpSingleBuffer(self.currentRenderBuffer, "current", timestamp);
self.dumpSingleBuffer(self.nextRenderBuffer, "next", timestamp);
self.dumpStdoutBuffer(timestamp);
}
pub fn enableMouse(self: *CliRenderer, enableMovement: bool) void {
_ = enableMovement; // TODO: Use this to control motion tracking levels
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
self.terminal.setMouseMode(writer, true) catch {};
bufferedWriter.flush() catch {};
}
pub fn queryPixelResolution(self: *CliRenderer) void {
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
writer.writeAll(ansi.ANSI.queryPixelSize) catch {};
bufferedWriter.flush() catch {};
}
pub fn disableMouse(self: *CliRenderer) void {
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
self.terminal.setMouseMode(writer, false) catch {};
bufferedWriter.flush() catch {};
}
pub fn enableKittyKeyboard(self: *CliRenderer, flags: u8) void {
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
self.terminal.setKittyKeyboard(writer, true, flags) catch {};
bufferedWriter.flush() catch {};
}
pub fn disableKittyKeyboard(self: *CliRenderer) void {
var bufferedWriter = &self.stdoutWriter;
const writer = bufferedWriter.writer();
self.terminal.setKittyKeyboard(writer, false, 0) catch {};
bufferedWriter.flush() catch {};
}
pub fn getTerminalCapabilities(self: *CliRenderer) Terminal.Capabilities {
return self.terminal.getCapabilities();
}
pub fn processCapabilityResponse(self: *CliRenderer, response: []const u8) void {
self.terminal.processCapabilityResponse(response);
const writer = self.stdoutWriter.writer();
const did_send = self.terminal.sendPendingGraphicsQuery(writer) catch |err| blk: {
logger.warn("Failed to send kitty graphics query: {}", .{err});
break :blk false;
};
if (did_send) {
self.stdoutWriter.flush() catch |err| {
logger.warn("Failed to flush kitty graphics query: {}", .{err});
};
}
const useKitty = self.terminal.opts.kitty_keyboard_flags > 0;
self.terminal.enableDetectedFeatures(writer, useKitty) catch {};
}
pub fn setCursorPosition(self: *CliRenderer, x: u32, y: u32, visible: bool) void {
self.terminal.setCursorPosition(x, y, visible);
}
pub fn setCursorStyle(self: *CliRenderer, style: Terminal.CursorStyle, blinking: bool) void {
self.terminal.setCursorStyle(style, blinking);
}
pub fn setCursorColor(self: *CliRenderer, color: [4]f32) void {
self.terminal.setCursorColor(color);
}
pub fn setKittyKeyboardFlags(self: *CliRenderer, flags: u8) void {
self.terminal.setKittyKeyboardFlags(flags);
}
pub fn getKittyKeyboardFlags(self: *CliRenderer) u8 {
return self.terminal.opts.kitty_keyboard_flags;
}
fn renderDebugOverlay(self: *CliRenderer) void {
if (!self.debugOverlay.enabled) return;
const width: u32 = 40;
const height: u32 = 11;
var x: u32 = 0;
var y: u32 = 0;
if (self.width < width + 2 or self.height < height + 2) return;
switch (self.debugOverlay.corner) {
.topLeft => {