this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix name

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+120 -3
+1 -1
build.zig.zon
··· 1 1 .{ 2 - .name = "salmon", 2 + .name = "vaxis", 3 3 // This is a [Semantic Version](https://semver.org/). 4 4 // In a future version of Zig it will be used for package deduplication. 5 5 .version = "0.0.0",
+3 -2
src/Image.zig
··· 76 76 .y_pixel = 1, 77 77 }, 78 78 .{ .path = "vaxis.png" }, 79 + 1, 79 80 ); 80 81 defer img.deinit(); 81 - try testing.expectEqual(1, img.cell_width); 82 - try testing.expectEqual(1, img.cell_height); 82 + try testing.expectEqual(200, img.cell_width); 83 + try testing.expectEqual(197, img.cell_height); 83 84 }
+74
src/image/Kitty.zig
··· 1 + const std = @import("std"); 2 + const math = std.math; 3 + const testing = std.testing; 4 + const zigimg = @import("zigimg"); 5 + const png = zigimg.png; 6 + 7 + const Window = @import("../Window.zig"); 8 + const Winsize = @import("../Tty.zig").Winsize; 9 + 10 + const Kitty = @This(); 11 + 12 + /// the decoded image 13 + img: zigimg.Image, 14 + 15 + /// unique identifier for this image 16 + id: u32, 17 + 18 + /// width of the image, in cells 19 + cell_width: usize, 20 + /// height of the image, in cells 21 + cell_height: usize, 22 + 23 + /// initialize a new image 24 + pub fn init( 25 + alloc: std.mem.Allocator, 26 + winsize: Winsize, 27 + src: []const u8, 28 + id: u32, 29 + ) !Kitty { 30 + const img = switch (src) { 31 + .path => |path| try zigimg.Image.fromFilePath(alloc, path), 32 + .mem => |bytes| try zigimg.Image.fromMemory(alloc, bytes), 33 + }; 34 + // cell geometry 35 + const pix_per_col = try math.divCeil(usize, winsize.x_pixel, winsize.cols); 36 + const pix_per_row = try math.divCeil(usize, winsize.y_pixel, winsize.rows); 37 + 38 + const cell_width = math.divCeil(usize, img.width, pix_per_col) catch 0; 39 + const cell_height = math.divCeil(usize, img.height, pix_per_row) catch 0; 40 + 41 + return Image{ 42 + .img = img, 43 + .cell_width = cell_width, 44 + .cell_height = cell_height, 45 + .id = id, 46 + }; 47 + } 48 + 49 + pub fn deinit(self: *Image) void { 50 + self.img.deinit(); 51 + } 52 + 53 + pub fn draw(self: *Image, win: Window, placement_id: u32) !void { 54 + try win.writeImage(win.x_off, win.y_off, self, placement_id); 55 + } 56 + 57 + test "image" { 58 + const alloc = testing.allocator; 59 + var img = try init( 60 + alloc, 61 + .{ 62 + .rows = 1, 63 + .cols = 1, 64 + .x_pixel = 1, 65 + .y_pixel = 1, 66 + }, 67 + .{ .path = "vaxis.png" }, 68 + 0, 69 + .kitty, 70 + ); 71 + defer img.deinit(); 72 + try testing.expectEqual(200, img.cell_width); 73 + try testing.expectEqual(197, img.cell_height); 74 + }
+42
src/image/image.zig
··· 1 + const std = @import("std"); 2 + const math = std.math; 3 + const testing = std.testing; 4 + const zigimg = @import("zigimg"); 5 + 6 + const Winsize = @import("../Tty.zig").Winsize; 7 + const Window = @import("../Window.zig"); 8 + 9 + const Kitty = @import("Kitty.zig"); 10 + 11 + pub const Image = union(enum) { 12 + kitty: Kitty, 13 + 14 + pub const Protocol = enum { 15 + kitty, 16 + // TODO: sixel, full block, half block, quad block 17 + }; 18 + 19 + /// initialize a new image 20 + pub fn init( 21 + alloc: std.mem.Allocator, 22 + winsize: Winsize, 23 + src: []const u8, 24 + id: u32, 25 + protocol: Protocol, 26 + ) !Image { 27 + switch (protocol) { 28 + .kitty => { 29 + const img = try Kitty.init(alloc, winsize, src, id); 30 + return .{ .kitty = img }; 31 + }, 32 + } 33 + } 34 + 35 + pub fn deinit(self: *Image) void { 36 + self.img.deinit(); 37 + } 38 + 39 + pub fn draw(self: *Image, win: Window, placement_id: u32) !void { 40 + try win.writeImage(win.x_off, win.y_off, self, placement_id); 41 + } 42 + };