chor: upgrade to latest zig; remove zg dependency
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 55s
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 55s
This commit is contained in:
@@ -35,19 +35,12 @@ pub fn build(b: *std.Build) void {
|
|||||||
options.addOption(bool, "debug", debug_rendering);
|
options.addOption(bool, "debug", debug_rendering);
|
||||||
const options_module = options.createModule();
|
const options_module = options.createModule();
|
||||||
|
|
||||||
// dependencies
|
|
||||||
const zg = b.dependency("zg", .{
|
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
|
||||||
|
|
||||||
// library
|
// library
|
||||||
const lib = b.addModule("zterm", .{
|
const lib = b.addModule("zterm", .{
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_source_file = b.path("src/root.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
.{ .name = "code_point", .module = zg.module("code_point") },
|
|
||||||
.{ .name = "build_options", .module = options_module },
|
.{ .name = "build_options", .module = options_module },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -100,8 +93,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
.imports = &.{
|
.imports = &.{
|
||||||
.{ .name = "code_point", .module = zg.module("code_point") },
|
|
||||||
.{ .name = "DisplayWidth", .module = zg.module("DisplayWidth") },
|
|
||||||
.{ .name = "build_options", .module = options_module },
|
.{ .name = "build_options", .module = options_module },
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -35,12 +35,7 @@
|
|||||||
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
|
||||||
// Once all dependencies are fetched, `zig build` no longer requires
|
// Once all dependencies are fetched, `zig build` no longer requires
|
||||||
// internet connectivity.
|
// internet connectivity.
|
||||||
.dependencies = .{
|
.dependencies = .{},
|
||||||
.zg = .{
|
|
||||||
.url = "git+https://codeberg.org/atman/zg#9427a9e53aaa29ee071f4dcb35b809a699d75aa9",
|
|
||||||
.hash = "zg-0.14.1-oGqU3IQ_tALZIiBN026_NTaPJqU-Upm8P_C7QED2Rzm8",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
.paths = .{
|
.paths = .{
|
||||||
"LICENSE",
|
"LICENSE",
|
||||||
"build.zig",
|
"build.zig",
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
//! Application type for TUI-applications
|
//! Application type for TUI-applications
|
||||||
|
|
||||||
|
// FIX known issues:
|
||||||
|
// - reseting the terminal screen when stopping an `App` under `tmux`
|
||||||
|
// (outside of tmux it seems to work, and other applications can also do that)
|
||||||
|
|
||||||
/// Create the App Type with the associated user events _E_ which describes
|
/// Create the App Type with the associated user events _E_ which describes
|
||||||
/// an tagged union for all the user events that can be send through the
|
/// an tagged union for all the user events that can be send through the
|
||||||
/// applications event loop.
|
/// applications event loop.
|
||||||
@@ -376,8 +380,8 @@ pub fn App(comptime E: type) type {
|
|||||||
},
|
},
|
||||||
0x7f => .{ .cp = input.Backspace },
|
0x7f => .{ .cp = input.Backspace },
|
||||||
else => {
|
else => {
|
||||||
var iter = code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
var iter: std.unicode.Utf8Iterator = .{ .bytes = buf[0..read_bytes], .i = 0 };
|
||||||
while (iter.next()) |cp| this.postEvent(.{ .key = .{ .cp = cp.code } });
|
while (iter.nextCodepoint()) |cp| this.postEvent(.{ .key = .{ .cp = cp } });
|
||||||
continue;
|
continue;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -431,7 +435,6 @@ const fmt = std.fmt;
|
|||||||
const posix = std.posix;
|
const posix = std.posix;
|
||||||
const Thread = std.Thread;
|
const Thread = std.Thread;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const code_point = @import("code_point");
|
|
||||||
const event = @import("event.zig");
|
const event = @import("event.zig");
|
||||||
const input = @import("input.zig");
|
const input = @import("input.zig");
|
||||||
const terminal = @import("terminal.zig");
|
const terminal = @import("terminal.zig");
|
||||||
|
|||||||
12
src/cell.zig
12
src/cell.zig
@@ -29,10 +29,10 @@ test "ascii styled text" {
|
|||||||
.{ .cp = 's', .style = .{ .fg = .light_green, .bg = .black, .emphasis = &.{.underline} } },
|
.{ .cp = 's', .style = .{ .fg = .light_green, .bg = .black, .emphasis = &.{.underline} } },
|
||||||
};
|
};
|
||||||
|
|
||||||
var string = std.ArrayList(u8).init(std.testing.allocator);
|
var string = try std.ArrayList(u8).initCapacity(std.testing.allocator, 4);
|
||||||
defer string.deinit();
|
defer string.deinit(std.testing.allocator);
|
||||||
|
|
||||||
const writer = string.writer();
|
const writer = string.writer(std.testing.allocator);
|
||||||
for (cells) |cell| {
|
for (cells) |cell| {
|
||||||
try cell.value(writer);
|
try cell.value(writer);
|
||||||
}
|
}
|
||||||
@@ -51,10 +51,10 @@ test "utf-8 styled text" {
|
|||||||
.{ .cp = '┘', .style = .{ .fg = .light_green, .bg = .black, .emphasis = &.{.underline} } },
|
.{ .cp = '┘', .style = .{ .fg = .light_green, .bg = .black, .emphasis = &.{.underline} } },
|
||||||
};
|
};
|
||||||
|
|
||||||
var string = std.ArrayList(u8).init(std.testing.allocator);
|
var string = try std.ArrayList(u8).initCapacity(std.testing.allocator, 4);
|
||||||
defer string.deinit();
|
defer string.deinit(std.testing.allocator);
|
||||||
|
|
||||||
const writer = string.writer();
|
const writer = string.writer(std.testing.allocator);
|
||||||
for (cells) |cell| {
|
for (cells) |cell| {
|
||||||
try cell.value(writer);
|
try cell.value(writer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// FIX known issues:
|
||||||
|
// - hold fewer instances of the `Allocator`
|
||||||
|
|
||||||
/// Border configuration struct
|
/// Border configuration struct
|
||||||
pub const Border = packed struct {
|
pub const Border = packed struct {
|
||||||
/// Color to use for the border
|
/// Color to use for the border
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
//! Interface for Element's which describe the contents of a `Container`.
|
//! Interface for Element's which describe the contents of a `Container`.
|
||||||
|
|
||||||
|
// TODO following features need to be implemented here
|
||||||
|
// - support for dynamic layouts? it can already be done, but the way you do this is pretty annoying
|
||||||
|
// -> currently you need to swap out the entire `Container` accordingly before rendering
|
||||||
|
// -> this might be necessary for size depending layout options, etc.
|
||||||
|
// -> maybe this can be implemented / supported similarly as to how the scrollable `Element`'s are implemented
|
||||||
|
|
||||||
|
// FIX known issues:
|
||||||
|
// - hold fewer instances of the `Allocator`
|
||||||
|
|
||||||
pub fn Element(Event: type) type {
|
pub fn Element(Event: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
ptr: *anyopaque = undefined,
|
ptr: *anyopaque = undefined,
|
||||||
@@ -1292,13 +1301,15 @@ test "alignment center" {
|
|||||||
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
const aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
var aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
||||||
.rectangle = .{ .fill = .green },
|
.rectangle = .{ .fill = .green },
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 12, .y = 5 },
|
.dim = .{ .x = 12, .y = 5 },
|
||||||
.grow = .fixed,
|
.grow = .fixed,
|
||||||
},
|
},
|
||||||
}, .{});
|
}, .{});
|
||||||
|
defer aligned_container.deinit();
|
||||||
|
|
||||||
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .center);
|
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .center);
|
||||||
try container.append(try .init(allocator, .{}, alignment.element()));
|
try container.append(try .init(allocator, .{}, alignment.element()));
|
||||||
|
|
||||||
@@ -1316,13 +1327,15 @@ test "alignment left" {
|
|||||||
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
const aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
var aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
||||||
.rectangle = .{ .fill = .green },
|
.rectangle = .{ .fill = .green },
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 12, .y = 5 },
|
.dim = .{ .x = 12, .y = 5 },
|
||||||
.grow = .fixed,
|
.grow = .fixed,
|
||||||
},
|
},
|
||||||
}, .{});
|
}, .{});
|
||||||
|
defer aligned_container.deinit();
|
||||||
|
|
||||||
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
||||||
.h = .start,
|
.h = .start,
|
||||||
.v = .center,
|
.v = .center,
|
||||||
@@ -1343,13 +1356,15 @@ test "alignment right" {
|
|||||||
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
const aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
var aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
||||||
.rectangle = .{ .fill = .green },
|
.rectangle = .{ .fill = .green },
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 12, .y = 5 },
|
.dim = .{ .x = 12, .y = 5 },
|
||||||
.grow = .fixed,
|
.grow = .fixed,
|
||||||
},
|
},
|
||||||
}, .{});
|
}, .{});
|
||||||
|
defer aligned_container.deinit();
|
||||||
|
|
||||||
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
||||||
.h = .end,
|
.h = .end,
|
||||||
.v = .center,
|
.v = .center,
|
||||||
@@ -1370,13 +1385,15 @@ test "alignment top" {
|
|||||||
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
const aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
var aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
||||||
.rectangle = .{ .fill = .green },
|
.rectangle = .{ .fill = .green },
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 12, .y = 5 },
|
.dim = .{ .x = 12, .y = 5 },
|
||||||
.grow = .fixed,
|
.grow = .fixed,
|
||||||
},
|
},
|
||||||
}, .{});
|
}, .{});
|
||||||
|
defer aligned_container.deinit();
|
||||||
|
|
||||||
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
||||||
.h = .center,
|
.h = .center,
|
||||||
.v = .start,
|
.v = .start,
|
||||||
@@ -1397,13 +1414,15 @@ test "alignment bottom" {
|
|||||||
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
var container: Container(event.SystemEvent) = try .init(allocator, .{}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
const aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
var aligned_container: Container(event.SystemEvent) = try .init(allocator, .{
|
||||||
.rectangle = .{ .fill = .green },
|
.rectangle = .{ .fill = .green },
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 12, .y = 5 },
|
.dim = .{ .x = 12, .y = 5 },
|
||||||
.grow = .fixed,
|
.grow = .fixed,
|
||||||
},
|
},
|
||||||
}, .{});
|
}, .{});
|
||||||
|
defer aligned_container.deinit();
|
||||||
|
|
||||||
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
var alignment: Alignment(event.SystemEvent) = .init(aligned_container, .{
|
||||||
.h = .center,
|
.h = .center,
|
||||||
.v = .end,
|
.v = .end,
|
||||||
@@ -1424,7 +1443,7 @@ test "input element" {
|
|||||||
accept: []u21,
|
accept: []u21,
|
||||||
});
|
});
|
||||||
const testing = @import("testing.zig");
|
const testing = @import("testing.zig");
|
||||||
const Queue = @import("queue").Queue(Event, 256);
|
const Queue = @import("queue.zig").Queue(Event, 256);
|
||||||
|
|
||||||
var container: Container(Event) = try .init(allocator, .{}, .{});
|
var container: Container(Event) = try .init(allocator, .{}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
@@ -1435,38 +1454,39 @@ test "input element" {
|
|||||||
};
|
};
|
||||||
var queue: Queue = .{};
|
var queue: Queue = .{};
|
||||||
|
|
||||||
|
var input_element: Input(Event, Queue)(.accept) = .init(allocator, &queue, .init(.black));
|
||||||
|
defer input_element.deinit();
|
||||||
|
|
||||||
const input_container: Container(Event) = try .init(allocator, .{
|
const input_container: Container(Event) = try .init(allocator, .{
|
||||||
.rectangle = .{ .fill = .green },
|
.rectangle = .{ .fill = .green },
|
||||||
.size = .{
|
.size = .{
|
||||||
.dim = .{ .x = 12, .y = 2 },
|
.dim = .{ .x = 12, .y = 2 },
|
||||||
.grow = .fixed,
|
.grow = .fixed,
|
||||||
},
|
},
|
||||||
}, .{});
|
}, input_element.element());
|
||||||
var input_element: Input(Event, Queue)(.accept) = .init(input_container, &queue, .{.black});
|
|
||||||
defer input_element.deinit();
|
|
||||||
|
|
||||||
try container.append(try .init(allocator, .{}, input_element.element()));
|
try container.append(input_container);
|
||||||
|
|
||||||
var renderer: testing.Renderer = .init(allocator, size);
|
var renderer: testing.Renderer = .init(allocator, size);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
container.resize(size);
|
container.resize(size);
|
||||||
container.reposition(.{});
|
container.reposition(.{});
|
||||||
try renderer.render(Container(event.SystemEvent), &container);
|
try renderer.render(Container(Event), &container);
|
||||||
// try testing.expectEqualCells(.{}, renderer.size, @import("test/element/input.without.text.zon"), renderer.screen);
|
// try testing.expectEqualCells(.{}, renderer.size, @import("test/element/input.without.text.zon"), renderer.screen);
|
||||||
|
|
||||||
// press 'a' 15 times
|
// press 'a' 15 times
|
||||||
for (0..15) |_| try container.handle(.{
|
for (0..15) |_| try container.handle(.{
|
||||||
.key = .{ .cp = 'a' },
|
.key = .{ .cp = 'a' },
|
||||||
});
|
});
|
||||||
try renderer.render(Container(event.SystemEvent), &container);
|
try renderer.render(Container(Event), &container);
|
||||||
// try testing.expectEqualCells(.{}, renderer.size, @import("test/element/input.with.text.zon"), renderer.screen);
|
// try testing.expectEqualCells(.{}, renderer.size, @import("test/element/input.with.text.zon"), renderer.screen);
|
||||||
|
|
||||||
// press 'a' 15 times
|
// press 'a' 15 times
|
||||||
for (0..15) |_| try container.handle(.{
|
for (0..15) |_| try container.handle(.{
|
||||||
.key = .{ .cp = 'a' },
|
.key = .{ .cp = 'a' },
|
||||||
});
|
});
|
||||||
try renderer.render(Container(event.SystemEvent), &container);
|
try renderer.render(Container(Event), &container);
|
||||||
// try testing.expectEqualCells(.{}, renderer.size, @import("test/element/input.with.text.overflow.zon"), renderer.screen);
|
// try testing.expectEqualCells(.{}, renderer.size, @import("test/element/input.with.text.overflow.zon"), renderer.screen);
|
||||||
|
|
||||||
// test the accepting of the `Element`
|
// test the accepting of the `Element`
|
||||||
@@ -1494,7 +1514,7 @@ test "button" {
|
|||||||
accept,
|
accept,
|
||||||
});
|
});
|
||||||
const testing = @import("testing.zig");
|
const testing = @import("testing.zig");
|
||||||
const Queue = @import("queue").Queue(Event, 256);
|
const Queue = @import("queue.zig").Queue(Event, 256);
|
||||||
|
|
||||||
const size: Point = .{
|
const size: Point = .{
|
||||||
.x = 30,
|
.x = 30,
|
||||||
@@ -1543,12 +1563,13 @@ test "progress" {
|
|||||||
progress: u8,
|
progress: u8,
|
||||||
});
|
});
|
||||||
const testing = @import("testing.zig");
|
const testing = @import("testing.zig");
|
||||||
const Queue = @import("queue").Queue(Event, 256);
|
const Queue = @import("queue.zig").Queue(Event, 256);
|
||||||
|
|
||||||
const size: Point = .{
|
const size: Point = .{
|
||||||
.x = 30,
|
.x = 30,
|
||||||
.y = 20,
|
.y = 20,
|
||||||
};
|
};
|
||||||
|
var queue: Queue = .{};
|
||||||
|
|
||||||
var container: Container(Event) = try .init(allocator, .{
|
var container: Container(Event) = try .init(allocator, .{
|
||||||
.layout = .{
|
.layout = .{
|
||||||
@@ -1558,7 +1579,7 @@ test "progress" {
|
|||||||
}, .{});
|
}, .{});
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
var progress: Progress(Event, Queue)(.progress) = .init(&.{}, .{
|
var progress: Progress(Event, Queue)(.progress) = .init(&queue, .{
|
||||||
.percent = .{ .enabled = true },
|
.percent = .{ .enabled = true },
|
||||||
.fg = .green,
|
.fg = .green,
|
||||||
.bg = .grey,
|
.bg = .grey,
|
||||||
|
|||||||
@@ -215,7 +215,7 @@ fn sleepyPop(q: *Queue(u8, 2)) !void {
|
|||||||
// still full and the push in the other thread is still blocked
|
// still full and the push in the other thread is still blocked
|
||||||
// waiting for space.
|
// waiting for space.
|
||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
std.time.sleep(std.time.ns_per_s);
|
std.Thread.sleep(std.time.ns_per_s);
|
||||||
// Finally, let that other thread go.
|
// Finally, let that other thread go.
|
||||||
try testing.expectEqual(1, q.pop());
|
try testing.expectEqual(1, q.pop());
|
||||||
|
|
||||||
@@ -225,7 +225,7 @@ fn sleepyPop(q: *Queue(u8, 2)) !void {
|
|||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
// But we want to ensure that there's a second push waiting, so
|
// But we want to ensure that there's a second push waiting, so
|
||||||
// here's another sleep.
|
// here's another sleep.
|
||||||
std.time.sleep(std.time.ns_per_s / 2);
|
std.Thread.sleep(std.time.ns_per_s / 2);
|
||||||
|
|
||||||
// Another spurious wake...
|
// Another spurious wake...
|
||||||
q.not_full.signal();
|
q.not_full.signal();
|
||||||
@@ -233,7 +233,7 @@ fn sleepyPop(q: *Queue(u8, 2)) !void {
|
|||||||
// And another chance for the other thread to see that it's
|
// And another chance for the other thread to see that it's
|
||||||
// spurious and go back to sleep.
|
// spurious and go back to sleep.
|
||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
std.time.sleep(std.time.ns_per_s / 2);
|
std.Thread.sleep(std.time.ns_per_s / 2);
|
||||||
|
|
||||||
// Pop that thing and we're done.
|
// Pop that thing and we're done.
|
||||||
try testing.expectEqual(2, q.pop());
|
try testing.expectEqual(2, q.pop());
|
||||||
@@ -270,14 +270,14 @@ test "Fill, block, fill, block" {
|
|||||||
fn sleepyPush(q: *Queue(u8, 1)) !void {
|
fn sleepyPush(q: *Queue(u8, 1)) !void {
|
||||||
// Try to ensure the other thread has already started trying to pop.
|
// Try to ensure the other thread has already started trying to pop.
|
||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
std.time.sleep(std.time.ns_per_s / 2);
|
std.Thread.sleep(std.time.ns_per_s / 2);
|
||||||
|
|
||||||
// Spurious wake
|
// Spurious wake
|
||||||
q.not_full.signal();
|
q.not_full.signal();
|
||||||
q.not_empty.signal();
|
q.not_empty.signal();
|
||||||
|
|
||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
std.time.sleep(std.time.ns_per_s / 2);
|
std.Thread.sleep(std.time.ns_per_s / 2);
|
||||||
|
|
||||||
// Stick something in the queue so it can be popped.
|
// Stick something in the queue so it can be popped.
|
||||||
q.push(1);
|
q.push(1);
|
||||||
@@ -286,7 +286,7 @@ fn sleepyPush(q: *Queue(u8, 1)) !void {
|
|||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
// Give the other thread time to block again.
|
// Give the other thread time to block again.
|
||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
std.time.sleep(std.time.ns_per_s / 2);
|
std.Thread.sleep(std.time.ns_per_s / 2);
|
||||||
|
|
||||||
// Spurious wake
|
// Spurious wake
|
||||||
q.not_full.signal();
|
q.not_full.signal();
|
||||||
@@ -317,7 +317,7 @@ test "2 readers" {
|
|||||||
const t1 = try Thread.spawn(cfg, readerThread, .{&queue});
|
const t1 = try Thread.spawn(cfg, readerThread, .{&queue});
|
||||||
const t2 = try Thread.spawn(cfg, readerThread, .{&queue});
|
const t2 = try Thread.spawn(cfg, readerThread, .{&queue});
|
||||||
try Thread.yield();
|
try Thread.yield();
|
||||||
std.time.sleep(std.time.ns_per_s / 2);
|
std.Thread.sleep(std.time.ns_per_s / 2);
|
||||||
queue.push(1);
|
queue.push(1);
|
||||||
queue.push(1);
|
queue.push(1);
|
||||||
t1.join();
|
t1.join();
|
||||||
|
|||||||
@@ -231,7 +231,6 @@ const log = std.log.scoped(.terminal);
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const mem = std.mem;
|
const mem = std.mem;
|
||||||
const posix = std.posix;
|
const posix = std.posix;
|
||||||
const code_point = @import("code_point");
|
|
||||||
const ctlseqs = @import("ctlseqs.zig");
|
const ctlseqs = @import("ctlseqs.zig");
|
||||||
const input = @import("input.zig");
|
const input = @import("input.zig");
|
||||||
const Key = input.Key;
|
const Key = input.Key;
|
||||||
|
|||||||
@@ -116,6 +116,41 @@ pub fn expectContainerScreen(size: Point, container: *Container(event.SystemEven
|
|||||||
try expectEqualCells(.{}, renderer.size, expected, renderer.screen);
|
try expectEqualCells(.{}, renderer.size, expected, renderer.screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Taken from: https://codeberg.org/atman/zg/src/branch/master/src/DisplayWidth.zig
|
||||||
|
/// Owned by https://codeberg.org/atman licensed under MIT all credits for this function go to him
|
||||||
|
fn center(allocator: Allocator, str: []const u8, total_width: usize, pad: []const u8) ![]u8 {
|
||||||
|
if (str.len > total_width) return error.StrTooLong;
|
||||||
|
if (str.len == total_width) return try allocator.dupe(u8, str);
|
||||||
|
|
||||||
|
if (pad.len > total_width or str.len + pad.len > total_width) return error.PadTooLong;
|
||||||
|
|
||||||
|
const margin_width = @divFloor((total_width - str.len), 2);
|
||||||
|
if (pad.len > margin_width) return error.PadTooLong;
|
||||||
|
|
||||||
|
const extra_pad: usize = if (total_width % 2 != str.len % 2) 1 else 0;
|
||||||
|
const pads = @divFloor(margin_width, pad.len) * 2 + extra_pad;
|
||||||
|
|
||||||
|
var result = try allocator.alloc(u8, pads * pad.len + str.len);
|
||||||
|
var bytes_index: usize = 0;
|
||||||
|
var pads_index: usize = 0;
|
||||||
|
|
||||||
|
while (pads_index < pads / 2) : (pads_index += 1) {
|
||||||
|
@memcpy(result[bytes_index..][0..pad.len], pad);
|
||||||
|
bytes_index += pad.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
@memcpy(result[bytes_index..][0..str.len], str);
|
||||||
|
bytes_index += str.len;
|
||||||
|
|
||||||
|
pads_index = 0;
|
||||||
|
while (pads_index < pads / 2 + extra_pad) : (pads_index += 1) {
|
||||||
|
@memcpy(result[bytes_index..][0..pad.len], pad);
|
||||||
|
bytes_index += pad.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// This function is intended to be used only in tests. Test if the two
|
/// This function is intended to be used only in tests. Test if the two
|
||||||
/// provided cell arrays are identical. Usually the `Cell` slices are
|
/// provided cell arrays are identical. Usually the `Cell` slices are
|
||||||
/// the contents of a given screen from the `zterm.testing.Renderer`. See
|
/// the contents of a given screen from the `zterm.testing.Renderer`. See
|
||||||
@@ -127,28 +162,21 @@ pub fn expectEqualCells(origin: Point, size: Point, expected: []const Cell, actu
|
|||||||
try testing.expectEqual(expected.len, @as(usize, size.y) * @as(usize, size.x));
|
try testing.expectEqual(expected.len, @as(usize, size.y) * @as(usize, size.x));
|
||||||
|
|
||||||
var expected_cps = try std.ArrayList(Cell).initCapacity(allocator, size.x);
|
var expected_cps = try std.ArrayList(Cell).initCapacity(allocator, size.x);
|
||||||
defer expected_cps.deinit();
|
defer expected_cps.deinit(allocator);
|
||||||
|
|
||||||
var actual_cps = try std.ArrayList(Cell).initCapacity(allocator, size.x);
|
var actual_cps = try std.ArrayList(Cell).initCapacity(allocator, size.x);
|
||||||
defer actual_cps.deinit();
|
defer actual_cps.deinit(allocator);
|
||||||
|
|
||||||
var output = try std.ArrayList(u8).initCapacity(allocator, expected_cps.capacity * actual_cps.capacity + 5 * size.y);
|
var output = try std.ArrayList(u8).initCapacity(allocator, expected_cps.capacity * actual_cps.capacity + 5 * size.y);
|
||||||
defer output.deinit();
|
defer output.deinit(allocator);
|
||||||
|
|
||||||
var buffer = std.io.bufferedWriter(output.writer());
|
const writer = output.writer(allocator);
|
||||||
defer buffer.flush() catch {};
|
|
||||||
|
|
||||||
const writer = buffer.writer();
|
|
||||||
var differ = false;
|
var differ = false;
|
||||||
|
|
||||||
const dwd = try DisplayWidth.DisplayWidthData.init(allocator);
|
const expected_centered = try center(allocator, "Expected Screen", size.x, " ");
|
||||||
defer dwd.deinit();
|
|
||||||
const dw: DisplayWidth = .{ .data = &dwd };
|
|
||||||
|
|
||||||
const expected_centered = try dw.center(allocator, "Expected Screen", size.x, " ");
|
|
||||||
defer allocator.free(expected_centered);
|
defer allocator.free(expected_centered);
|
||||||
|
|
||||||
const actual_centered = try dw.center(allocator, "Actual Screen", size.x, " ");
|
const actual_centered = try center(allocator, "Actual Screen", size.x, " ");
|
||||||
defer allocator.free(actual_centered);
|
defer allocator.free(actual_centered);
|
||||||
|
|
||||||
try writer.print("Screens are not equivalent.\n{s} ┆ {s}\n", .{ expected_centered, actual_centered });
|
try writer.print("Screens are not equivalent.\n{s} ┆ {s}\n", .{ expected_centered, actual_centered });
|
||||||
@@ -164,8 +192,8 @@ pub fn expectEqualCells(origin: Point, size: Point, expected: []const Cell, actu
|
|||||||
|
|
||||||
if (!expected_cell.eql(actual_cell)) differ = true;
|
if (!expected_cell.eql(actual_cell)) differ = true;
|
||||||
|
|
||||||
try expected_cps.append(expected_cell);
|
try expected_cps.append(allocator, expected_cell);
|
||||||
try actual_cps.append(actual_cell);
|
try actual_cps.append(allocator, actual_cell);
|
||||||
}
|
}
|
||||||
|
|
||||||
// write screens both formatted to buffer
|
// write screens both formatted to buffer
|
||||||
@@ -178,13 +206,14 @@ pub fn expectEqualCells(origin: Point, size: Point, expected: []const Cell, actu
|
|||||||
if (!differ) return;
|
if (!differ) return;
|
||||||
|
|
||||||
// test failed
|
// test failed
|
||||||
try buffer.flush();
|
|
||||||
|
|
||||||
debug.lockStdErr();
|
debug.lockStdErr();
|
||||||
defer debug.unlockStdErr();
|
defer debug.unlockStdErr();
|
||||||
|
|
||||||
const std_writer = std.io.getStdErr().writer();
|
var stdout_buffer: [1024]u8 = undefined;
|
||||||
try std_writer.writeAll(output.items);
|
var stdout = std.fs.File.stdout().writer(&stdout_buffer);
|
||||||
|
const stdout_writer = &stdout.interface;
|
||||||
|
try stdout_writer.writeAll(output.items);
|
||||||
|
try stdout_writer.flush();
|
||||||
return error.TestExpectEqualCells;
|
return error.TestExpectEqualCells;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,5 +224,4 @@ const Allocator = std.mem.Allocator;
|
|||||||
const event = @import("event.zig");
|
const event = @import("event.zig");
|
||||||
const Container = @import("container.zig").Container;
|
const Container = @import("container.zig").Container;
|
||||||
const Cell = @import("cell.zig");
|
const Cell = @import("cell.zig");
|
||||||
const DisplayWidth = @import("DisplayWidth");
|
|
||||||
const Point = @import("point.zig").Point;
|
const Point = @import("point.zig").Point;
|
||||||
|
|||||||
Reference in New Issue
Block a user