add(examples/styles): color palette to showcase all available colors to render (except for .default)
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 54s
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 54s
This commit is contained in:
@@ -136,15 +136,15 @@ the primary use-case for myself to create this library in the first place.
|
|||||||
- [x] Scrollable Content (i.e. show long text of an except of something and other smaller `Container`)
|
- [x] Scrollable Content (i.e. show long text of an except of something and other smaller `Container`)
|
||||||
- [x] min size
|
- [x] min size
|
||||||
- [x] mouse scrolling aware of mouse position (i.e. through multiple different scrollable `Container`)
|
- [x] mouse scrolling aware of mouse position (i.e. through multiple different scrollable `Container`)
|
||||||
- [ ] Launch sub-applications (not inside of a `Container` but during the application workflow, like an editor)
|
|
||||||
- [ ] Styles
|
- [ ] Styles
|
||||||
- [ ] Text styles
|
- [ ] Text styles
|
||||||
- [ ] Color palette
|
- [x] Color palette
|
||||||
- [ ] Error Handling
|
- [ ] Error Handling
|
||||||
- [ ] log and show error's without crashing the application
|
- [ ] log and show error's without crashing the application
|
||||||
- [ ] show application error that will cause a crash
|
- [ ] show application error that will cause a crash
|
||||||
- [ ] Demo
|
- [ ] Demo
|
||||||
- [ ] use another tui application to launch and come back to (showcase the interrupt behavior)
|
- [ ] use another tui application to launch and come back to (showcase the interrupt behavior)
|
||||||
|
- [ ] Launch sub-applications (not inside of a `Container` but during the application workflow, like an editor)
|
||||||
- [ ] Testability
|
- [ ] Testability
|
||||||
- [ ] snapshot ability to safe current screen (from `Renderer`) to test against
|
- [ ] snapshot ability to safe current screen (from `Renderer`) to test against
|
||||||
- [ ] try to integrate them into the library itself such that they also serve as examples on how to test
|
- [ ] try to integrate them into the library itself such that they also serve as examples on how to test
|
||||||
|
|||||||
11
build.zig
11
build.zig
@@ -15,6 +15,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
grid,
|
grid,
|
||||||
mixed,
|
mixed,
|
||||||
// styles:
|
// styles:
|
||||||
|
palette,
|
||||||
};
|
};
|
||||||
|
|
||||||
const example = b.option(Examples, "example", "Example to build and/or run. (default: vertical)") orelse .vertical;
|
const example = b.option(Examples, "example", "Example to build and/or run. (default: vertical)") orelse .vertical;
|
||||||
@@ -96,6 +97,15 @@ pub fn build(b: *std.Build) void {
|
|||||||
});
|
});
|
||||||
mixed.root_module.addImport("zterm", lib);
|
mixed.root_module.addImport("zterm", lib);
|
||||||
|
|
||||||
|
// styles:
|
||||||
|
const palette = b.addExecutable(.{
|
||||||
|
.name = "palette",
|
||||||
|
.root_source_file = b.path("examples/styles/palette.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
palette.root_module.addImport("zterm", lib);
|
||||||
|
|
||||||
// mapping of user selected example to compile step
|
// mapping of user selected example to compile step
|
||||||
const exe = switch (example) {
|
const exe = switch (example) {
|
||||||
// elements:
|
// elements:
|
||||||
@@ -108,6 +118,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
.grid => grid,
|
.grid => grid,
|
||||||
.mixed => mixed,
|
.mixed => mixed,
|
||||||
// styles:
|
// styles:
|
||||||
|
.palette => palette,
|
||||||
};
|
};
|
||||||
b.installArtifact(exe);
|
b.installArtifact(exe);
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ pub fn main() !void {
|
|||||||
const element = quit_text.element();
|
const element = quit_text.element();
|
||||||
|
|
||||||
var container = try App.Container.init(allocator, .{
|
var container = try App.Container.init(allocator, .{
|
||||||
.border = .{},
|
|
||||||
.layout = .{
|
.layout = .{
|
||||||
.gap = 2,
|
.gap = 2,
|
||||||
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
|
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
|
||||||
|
|||||||
97
examples/styles/palette.zig
Normal file
97
examples/styles/palette.zig
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const zterm = @import("zterm");
|
||||||
|
|
||||||
|
const App = zterm.App(union(enum) {});
|
||||||
|
|
||||||
|
const log = std.log.scoped(.default);
|
||||||
|
|
||||||
|
const QuitText = struct {
|
||||||
|
const text = "Press ctrl+c to quit.";
|
||||||
|
|
||||||
|
pub fn element(this: *@This()) App.Element {
|
||||||
|
return .{ .ptr = this, .vtable = &.{ .content = content } };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Size) !void {
|
||||||
|
_ = ctx;
|
||||||
|
std.debug.assert(cells.len == @as(usize, size.cols) * @as(usize, size.rows));
|
||||||
|
|
||||||
|
const row = 2;
|
||||||
|
const col = size.cols / 2 -| (text.len / 2);
|
||||||
|
const anchor = (row * size.cols) + 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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 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 = .horizontal },
|
||||||
|
.min_size = .{ .cols = 3 * std.meta.fields(zterm.Color).len }, // ensure enough columns to render all colors -> scrollable otherwise
|
||||||
|
}, .{});
|
||||||
|
defer box.deinit();
|
||||||
|
|
||||||
|
inline for (std.meta.fields(zterm.Color)) |field| {
|
||||||
|
if (comptime field.value == 0) continue; // zterm.Color.default == 0 -> skip
|
||||||
|
const color = std.meta.stringToEnum(zterm.Color, field.name).?;
|
||||||
|
try box.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = color } }, .{}));
|
||||||
|
}
|
||||||
|
var scrollable: App.Scrollable = .init(box);
|
||||||
|
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)});
|
||||||
|
|
||||||
|
switch (event) {
|
||||||
|
.init => continue,
|
||||||
|
.quit => break,
|
||||||
|
.resize => |size| try renderer.resize(size),
|
||||||
|
.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",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
try renderer.render(@TypeOf(container), &container);
|
||||||
|
try renderer.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user