Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)

Add support for Zig and the Zig build system

pci.express 8071c825 55d228ff

verified
+2 -1
.gitignore
···
*.d
-
*.o
+
*.o
+
.zig-cache
+17
README.zig.md
···
+
# Flanterm
+
Adapted from [https://codeberg.org/Mintsuki/Flanterm](https://codeberg.org/Mintsuki/Flanterm)
+
for Zig. Read the C code and original README for more information.
+
+
## Usage
+
First, add the package to your build.zig.zon:
+
`zig fetch --save git+https://tangled.sh/@sydney.blue/flanterm.zig#trunk`
+
Then, add the following to your build.zig:
+
```zig
+
const flanterm_dep = b.dependency("flanterm", .{});
+
const flanterm_mod = flanterm.module("flanterm");
+
exe.root_module.addImport("flanterm", flanterm_mod);
+
```
+
+
Now, you can import the `flanterm` module and use the helpers. More
+
bindings will be added as needed, feel free to contribute anything
+
you would find useful.
+17
build.zig
···
+
const std = @import("std");
+
+
pub fn build(b: *std.Build) void {
+
// Create the exported module
+
const mod = b.addModule("flanterm", .{
+
.root_source_file = b.path("src/root.zig"),
+
});
+
+
// Add the C source files and includes
+
mod.addIncludePath(b.path("src"));
+
mod.addCSourceFiles(.{
+
.files = &.{
+
"src/flanterm.c",
+
"src/flanterm_backends/fb.c",
+
},
+
});
+
}
+15
build.zig.zon
···
+
.{
+
.name = .flanterm,
+
.version = "2.0.0",
+
.fingerprint = 0x5e517d869e9f7b42, // Changing this has security and trust implications.
+
.minimum_zig_version = "0.15.1",
+
.dependencies = .{},
+
.paths = .{
+
"build.zig",
+
"build.zig.zon",
+
"src",
+
"LICENSE",
+
"README.md",
+
".gitignore",
+
},
+
}
+60
src/colors.zig
···
+
const std = @import("std");
+
+
pub const Palette = extern struct {
+
black: u32,
+
red: u32,
+
green: u32,
+
yellow: u32,
+
blue: u32,
+
magenta: u32,
+
cyan: u32,
+
white: u32,
+
};
+
+
/// Used for escape sequences
+
pub const Color = enum(u8) {
+
black = 30,
+
red = 31,
+
green = 32,
+
yellow = 33,
+
blue = 34,
+
magenta = 35,
+
cyan = 36,
+
white = 37,
+
default = 39,
+
bright_black = 90,
+
bright_red = 91,
+
bright_green = 92,
+
bright_yellow = 93,
+
bright_blue = 94,
+
bright_magenta = 95,
+
bright_cyan = 96,
+
bright_white = 97,
+
+
pub fn esc_seq(comptime self: Color) []const u8 {
+
if (self == .default) return "\x1b[0m";
+
return std.fmt.comptimePrint("\x1b[{}m", .{@intFromEnum(self)});
+
}
+
};
+
+
pub var default_colors: Palette = .{
+
.black = 0x000000,
+
.red = 0xff0000,
+
.green = 0x37dd21,
+
.yellow = 0xfee409,
+
.blue = 0x1460d2,
+
.magenta = 0xff005d,
+
.cyan = 0x00bbbb,
+
.white = 0xbbbbbb,
+
};
+
+
pub var default_bold_colors: Palette = .{
+
.black = 0x545454,
+
.red = 0xf40d17,
+
.green = 0x3bcf1d,
+
.yellow = 0xecc809,
+
.blue = 0x5555ff,
+
.magenta = 0xff55ff,
+
.cyan = 0x6ae3f9,
+
.white = 0xffffff,
+
};
+92
src/root.zig
···
+
pub const Colors = @import("colors.zig");
+
const std = @import("std");
+
const c = @cImport({
+
@cInclude("flanterm.h");
+
@cInclude("flanterm_backends/fb.h");
+
});
+
+
pub const Context = struct {
+
backing: ?*c.struct_flanterm_context,
+
+
const Self = @This();
+
+
/// Initialize the context
+
pub fn init(
+
args: struct {
+
// Malloc and Free
+
malloc: ?*const fn (usize) callconv(.c) ?*anyopaque = null,
+
free: ?*const fn (?*anyopaque, usize) callconv(.c) void = null,
+
// Framebuffer Info (required)
+
fb: [*]u32,
+
width: usize,
+
height: usize,
+
pitch: usize,
+
// Framebuffer Color Info
+
red_mask_size: u8,
+
red_mask_shift: u8,
+
green_mask_size: u8,
+
green_mask_shift: u8,
+
blue_mask_size: u8,
+
blue_mask_shift: u8,
+
// Canvas (scratch space)
+
canvas: ?[*]u32 = null,
+
// Color Pointers
+
ansi_colours: *Colors.Palette = &Colors.default_colors,
+
ansi_bright_colours: *Colors.Palette = &Colors.default_bold_colors,
+
// Default Color choices
+
default_bg: *u32 = &Colors.default_colors.black,
+
default_fg: *u32 = &Colors.default_colors.cyan,
+
default_bg_bright: *u32 = &Colors.default_bold_colors.black,
+
default_fg_bright: *u32 = &Colors.default_bold_colors.cyan,
+
// Font Information
+
font: ?*anyopaque = null,
+
font_width: usize = 0,
+
font_height: usize = 0,
+
font_spacing: usize = 1,
+
// Font Scale values
+
font_scale_x: usize = 0,
+
font_scale_y: usize = 0,
+
// Margin
+
margin: usize = 0,
+
},
+
) Self {
+
const ansi_colours: [*]u32 = @ptrCast(args.ansi_colours);
+
const ansi_bright_colours: [*]u32 = @ptrCast(args.ansi_bright_colours);
+
const ctx = c.flanterm_fb_init(
+
args.malloc,
+
args.free,
+
args.fb,
+
args.width,
+
args.height,
+
args.pitch,
+
args.red_mask_size,
+
args.red_mask_shift,
+
args.green_mask_size,
+
args.green_mask_shift,
+
args.blue_mask_size,
+
args.blue_mask_shift,
+
args.canvas,
+
ansi_colours,
+
ansi_bright_colours,
+
args.default_bg,
+
args.default_fg,
+
args.default_bg_bright,
+
args.default_fg_bright,
+
args.font,
+
args.font_width,
+
args.font_height,
+
args.font_spacing,
+
args.font_scale_x,
+
args.font_scale_y,
+
args.margin,
+
);
+
return .{
+
.backing = ctx,
+
};
+
}
+
+
/// Write to the console
+
pub fn write_slice(self: *Self, buf: []u8) void {
+
c.flanterm_write(self.backing, buf.ptr, buf.len);
+
}
+
};