a modern tui library written in zig
1const std = @import("std");
2const fmt = std.fmt;
3const math = std.math;
4const testing = std.testing;
5const base64 = std.base64.standard.Encoder;
6const zigimg = @import("zigimg");
7
8const Window = @import("Window.zig");
9
10const log = std.log.scoped(.image);
11
12const Image = @This();
13
14const transmit_opener = "\x1b_Gf=32,i={d},s={d},v={d},m={d};";
15
16pub const Source = union(enum) {
17 path: []const u8,
18 mem: []const u8,
19};
20
21pub const Placement = struct {
22 img_id: u32,
23 z_index: i32,
24 size: ?CellSize = null,
25};
26
27pub const CellSize = struct {
28 rows: usize,
29 cols: usize,
30};
31
32/// unique identifier for this image. This will be managed by the screen.
33id: u32,
34
35// width in pixels
36width: usize,
37// height in pixels
38height: usize,
39
40pub fn draw(self: Image, win: Window, scale: bool, z_index: i32) void {
41 const p = Placement{
42 .img_id = self.id,
43 .z_index = z_index,
44 .size = sz: {
45 if (!scale) break :sz null;
46 break :sz CellSize{
47 .rows = win.height,
48 .cols = win.width,
49 };
50 },
51 };
52 win.writeCell(0, 0, .{ .image = p });
53}
54
55pub fn cellSize(self: Image, win: Window) !CellSize {
56 // cell geometry
57 const x_pix = win.screen.width_pix;
58 const y_pix = win.screen.height_pix;
59 const w = win.screen.width;
60 const h = win.screen.height;
61
62 const pix_per_col = try std.math.divCeil(usize, x_pix, w);
63 const pix_per_row = try std.math.divCeil(usize, y_pix, h);
64
65 const cell_width = std.math.divCeil(usize, self.width, pix_per_col) catch 0;
66 const cell_height = std.math.divCeil(usize, self.height, pix_per_row) catch 0;
67 return .{
68 .rows = cell_height,
69 .cols = cell_width,
70 };
71}