Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1pub const Colors = @import("colors.zig");
2const std = @import("std");
3const c = @cImport({
4 @cInclude("flanterm.h");
5 @cInclude("flanterm_backends/fb.h");
6});
7
8pub const Context = struct {
9 backing: ?*c.struct_flanterm_context,
10
11 const Self = @This();
12
13 /// Initialize the context
14 pub fn init(
15 args: struct {
16 // Malloc and Free
17 malloc: ?*const fn (usize) callconv(.c) ?*anyopaque = null,
18 free: ?*const fn (?*anyopaque, usize) callconv(.c) void = null,
19 // Framebuffer Info (required)
20 fb: [*]u32,
21 width: usize,
22 height: usize,
23 pitch: usize,
24 // Framebuffer Color Info
25 red_mask_size: u8,
26 red_mask_shift: u8,
27 green_mask_size: u8,
28 green_mask_shift: u8,
29 blue_mask_size: u8,
30 blue_mask_shift: u8,
31 // Canvas (scratch space)
32 canvas: ?[*]u32 = null,
33 // Color Pointers
34 ansi_colours: *Colors.Palette = &Colors.default_colors,
35 ansi_bright_colours: *Colors.Palette = &Colors.default_bold_colors,
36 // Default Color choices
37 default_bg: *u32 = &Colors.default_colors.black,
38 default_fg: *u32 = &Colors.default_colors.cyan,
39 default_bg_bright: *u32 = &Colors.default_bold_colors.black,
40 default_fg_bright: *u32 = &Colors.default_bold_colors.cyan,
41 // Font Information
42 font: ?*anyopaque = null,
43 font_width: usize = 0,
44 font_height: usize = 0,
45 font_spacing: usize = 1,
46 // Font Scale values
47 font_scale_x: usize = 0,
48 font_scale_y: usize = 0,
49 // Margin
50 margin: usize = 0,
51 },
52 ) Self {
53 const ansi_colours: [*]u32 = @ptrCast(args.ansi_colours);
54 const ansi_bright_colours: [*]u32 = @ptrCast(args.ansi_bright_colours);
55 const ctx = c.flanterm_fb_init(
56 args.malloc,
57 args.free,
58 args.fb,
59 args.width,
60 args.height,
61 args.pitch,
62 args.red_mask_size,
63 args.red_mask_shift,
64 args.green_mask_size,
65 args.green_mask_shift,
66 args.blue_mask_size,
67 args.blue_mask_shift,
68 args.canvas,
69 ansi_colours,
70 ansi_bright_colours,
71 args.default_bg,
72 args.default_fg,
73 args.default_bg_bright,
74 args.default_fg_bright,
75 args.font,
76 args.font_width,
77 args.font_height,
78 args.font_spacing,
79 args.font_scale_x,
80 args.font_scale_y,
81 args.margin,
82 );
83 return .{
84 .backing = ctx,
85 };
86 }
87
88 /// Write to the console
89 pub fn write_slice(self: *Self, buf: []u8) void {
90 c.flanterm_write(self.backing, buf.ptr, buf.len);
91 }
92};