Configuration to enable scrollbar rendering for scrollable `Element`s. Currently only the fg `Color` of the scrollbar can be configured while the background uses the same fg `Color` but adds the emphasis `.dim` to make it obvious what the is the actual scrollbar. In the future it might be necessary to provide the user with more options to configure the representation of the scrollbar. Tests have been added to test the scrollbar rendering and placement accordingly.
156 lines
5.2 KiB
Zig
156 lines
5.2 KiB
Zig
const QuitText = struct {
|
|
const text = "Press ctrl+c to quit.";
|
|
|
|
pub fn element(this: *@This()) App.Element {
|
|
return .{ .ptr = this, .vtable = &.{ .content = content } };
|
|
}
|
|
|
|
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
|
|
_ = ctx;
|
|
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
|
|
|
const row = 2;
|
|
const col = size.x / 2 -| (text.len / 2);
|
|
const anchor = (row * size.x) + col;
|
|
|
|
for (text, 0..) |cp, idx| {
|
|
cells[anchor + idx].style.fg = .white;
|
|
cells[anchor + idx].style.bg = .black;
|
|
cells[anchor + idx].cp = cp;
|
|
|
|
// NOTE do not write over the contents of this `Container`'s `Size`
|
|
if (anchor + idx == cells.len - 1) break;
|
|
}
|
|
}
|
|
};
|
|
|
|
const TextStyles = struct {
|
|
const text = "Example";
|
|
|
|
pub fn element(this: *@This()) App.Element {
|
|
return .{ .ptr = this, .vtable = &.{ .content = content } };
|
|
}
|
|
|
|
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
|
|
@setEvalBranchQuota(50000);
|
|
_ = ctx;
|
|
std.debug.assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
|
|
|
|
var row: usize = 0;
|
|
var col: usize = 0;
|
|
|
|
// Color
|
|
inline for (std.meta.fields(zterm.Color)) |bg_field| {
|
|
if (comptime bg_field.value == 0) continue; // zterm.Color.default == 0 -> skip
|
|
|
|
inline for (std.meta.fields(zterm.Color)) |fg_field| {
|
|
if (comptime fg_field.value == 0) continue; // zterm.Color.default == 0 -> skip
|
|
if (comptime fg_field.value == bg_field.value) continue;
|
|
|
|
// witouth any emphasis
|
|
for (text) |cp| {
|
|
cells[(row * size.x) + col].style.bg = @enumFromInt(bg_field.value);
|
|
cells[(row * size.x) + col].style.fg = @enumFromInt(fg_field.value);
|
|
cells[(row * size.x) + col].cp = cp;
|
|
col += 1;
|
|
}
|
|
|
|
// emphasis (no combinations)
|
|
inline for (std.meta.fields(zterm.Style.Emphasis)) |emp_field| {
|
|
if (comptime emp_field.value == 0) continue; // zterm.Style.Emphasis.reset == 0 -> skip
|
|
const emphasis: zterm.Style.Emphasis = @enumFromInt(emp_field.value);
|
|
|
|
for (text) |cp| {
|
|
cells[(row * size.x) + col].style.bg = @enumFromInt(bg_field.value);
|
|
cells[(row * size.x) + col].style.fg = @enumFromInt(fg_field.value);
|
|
cells[(row * size.x) + col].style.emphasis = &.{emphasis};
|
|
cells[(row * size.x) + col].cp = cp;
|
|
col += 1;
|
|
}
|
|
}
|
|
row += 1;
|
|
col = 0;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
pub fn main() !void {
|
|
errdefer |err| log.err("Application Error: {any}", .{err});
|
|
|
|
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
|
|
defer if (gpa.deinit() == .leak) log.err("memory leak", .{});
|
|
|
|
const allocator = gpa.allocator();
|
|
|
|
var app: App = .init;
|
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
|
defer renderer.deinit();
|
|
|
|
var quit_text: QuitText = .{};
|
|
const element = quit_text.element();
|
|
|
|
var text_styles: TextStyles = .{};
|
|
|
|
var container = try App.Container.init(allocator, .{
|
|
.layout = .{
|
|
.gap = 2,
|
|
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
|
|
},
|
|
}, element);
|
|
defer container.deinit();
|
|
|
|
var box = try App.Container.init(allocator, .{
|
|
.layout = .{ .direction = .vertical },
|
|
.size = .{
|
|
.dim = .{
|
|
.x = std.meta.fields(zterm.Style.Emphasis).len * TextStyles.text.len,
|
|
.y = (std.meta.fields(zterm.Color).len - 1) * (std.meta.fields(zterm.Color).len - 2),
|
|
},
|
|
},
|
|
}, text_styles.element());
|
|
defer box.deinit();
|
|
|
|
var scrollable: App.Scrollable = .init(box, .disabled);
|
|
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
|
|
|
|
try app.start();
|
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
|
|
|
while (true) {
|
|
const event = app.nextEvent();
|
|
log.debug("received event: {s}", .{@tagName(event)});
|
|
|
|
// pre event handling
|
|
switch (event) {
|
|
.key => |key| if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(),
|
|
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
|
|
else => {},
|
|
}
|
|
|
|
container.handle(event) catch |err| app.postEvent(.{
|
|
.err = .{
|
|
.err = err,
|
|
.msg = "Container Event handling failed",
|
|
},
|
|
});
|
|
|
|
// post event handling
|
|
switch (event) {
|
|
.quit => break,
|
|
else => {},
|
|
}
|
|
|
|
container.resize(try renderer.resize());
|
|
container.reposition(.{});
|
|
try renderer.render(@TypeOf(container), &container);
|
|
try renderer.flush();
|
|
}
|
|
}
|
|
|
|
const log = std.log.scoped(.default);
|
|
|
|
const std = @import("std");
|
|
const zterm = @import("zterm");
|
|
const App = zterm.App(union(enum) {});
|