59 Commits

Author SHA1 Message Date
bfbe75f8d3 feat(inline): rendering without alternate screen
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (pull_request) Successful in 1m10s
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 55s
Fix issue with growth resize for containers with corresponding
borders, padding and gaps.
2026-01-20 23:09:16 +01:00
89517b2546 fix: add newly introduced parameter for App.start function to all corresponding usages 2026-01-20 15:16:10 +01:00
a71d808250 add: .line core event for line contents in non *raw mode* instead of .key core events
The end of the `.line` event received contents is highlighted by a trailing newline
(which cannot occur before, as that triggers the line event itself).

Add signal handler for SIGCONT which forces a `.resize` event that should re-draw the
contents after continuing a suspended application (i.e. ctrl+z followed by `fg`).
2026-01-20 15:11:19 +01:00
97a240c54d mod: example shows how dynamic sizing is achived that can be independent form the reported terminal size
Setting the cursor with the `Direct` handler will cause the rendering
to halt at that point and leave the cursor at point.

Due to not enabling *raw mode* with the newly introduced `App.start`
configuration options corresponding inputs are only visible to `zterm`
once the input has been completed with a newline. With this it is not
necessary for the renderer to know nothing more than the width of the
terminal (which is implied through the `Container` sizes). Making it
very trivial to implement.
2026-01-20 13:57:55 +01:00
c29c60bd89 WIP make example interactive
Handle inputs as per usual (which however is a bit weak, is it goes through
key by key and not the entire line), batch all events such that all events
are handled before the next frame is rendered. For this the `App.start`
function needs to become configurable, such that it changes the termios as
configured (hence it is currently all commented out for testing).
2026-01-20 11:54:28 +01:00
4a3bec3edc add: Direct Renderer implementation rendering the fixed size of the root Container
A corresponding example has been added, which is very minimalistic as of now,
but will add further functionality to test the corresponding workflow that is
usual in `zterm` such that it in the best case only a swap in the renderer to
switch from alternate mode drawing to direct drawing.
2026-01-20 11:28:37 +01:00
836d7669e5 feat: introduce growth options .horizontal_only and .vertical_only
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m5s
Growing containers only in one specific direction while fixing the
other dimension (which is then required to be provided - just like
before).
2026-01-18 13:36:03 +01:00
1621715ad8 lint: correct typo
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m7s
2026-01-17 12:29:42 +01:00
4874252e8c mod: adapt implementation to zig version 0.15.2
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 54s
2026-01-17 12:05:20 +01:00
e1e8907848 doc: remove hint as builds now use latest zig version 2026-01-17 12:04:52 +01:00
1cb7fca701 mod(ci): change target zig version to *latest* 2026-01-17 12:04:19 +01:00
19d1602d3b ref: omit capture instead of dropping
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 49s
2026-01-12 23:28:30 +01:00
88c7eea356 feat(unicode): accept unicode characters through the app's input handler to be handled correctly; key introduce isUnicode method
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m21s
With the `isUnicode` method the read unicode characters from the user
can be checked against displayable text and rendered on the screen accordingly.
2026-01-12 22:47:20 +01:00
b1a0d60ae3 mod: bump zig master version
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m9s
2026-01-06 22:58:10 +01:00
c49c2a5c6d mod(app): export TextField from App to be used in other Element implementations similar to App.Input
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m1s
2025-12-27 19:56:44 +01:00
8b5d3757fc mod(element/Input): split into TextField without associated event to automatically trigger
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m9s
2025-12-27 17:06:07 +01:00
06ab32bdde mod(container): Size property handling for .fixed, .horizontal and .vertical check with assertions required values
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m0s
2025-12-17 22:58:58 +01:00
8e3b43fa61 mod(container): .horizontal and .vertical size.grow respect minSize value if no size.dim is used
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m0s
2025-12-17 22:48:02 +01:00
6b2797cd8c mod(container): .fixed sizing behavior with minSize Container / Element function
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m1s
2025-12-17 22:04:06 +01:00
e972a2ea0f mod(container): integrate minSize call when fit_resize is calculated
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 57s
2025-11-28 15:31:45 +01:00
67bfd90a2c WIP(container): fit_size call minSize for determining the minimal required size of a given Container
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 57s
2025-11-28 15:25:10 +01:00
40fa080af6 WIP(container): use another approach
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 57s
2025-11-28 15:08:58 +01:00
d92f562a57 WIP(container): propagate individual child sizes correctly
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 57s
2025-11-28 15:03:50 +01:00
f7025a0fc2 fix(container): minSize should not add children's sizes, but take max instead
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 57s
2025-11-28 14:34:32 +01:00
41229c13d3 mod(container): provide minSize function for Element implementation to refer for nested structures
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m6s
2025-11-28 14:27:41 +01:00
855594a8c8 mod(element): introduce deinit interface for deinitializing Elements automatically with the Container
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m34s
2025-11-27 21:13:03 +01:00
9488d0b64d feat(terminal/cursor): add support for cursor shape configuration
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m5s
2025-11-20 18:46:58 +01:00
424740d350 feat(terminal/osc12): define cursor color through style of cell that describes the cursor position
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m33s
The `.default` color will reset the cursor color to the terminal's default color
2025-11-19 18:42:13 +01:00
28c733352e feat(event): introduce .bell system event
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 16m37s
2025-11-17 18:30:31 +01:00
3b5507ec1e fix(lint): correct typo
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m4s
2025-11-10 17:18:36 +01:00
38d31fae72 feat(element): parameter *const App.Model
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 55s
The renderer will provide `resize`, `reposition` and `minSize` (for
the `Scrollable` `Element`) with a read-only pointer to the model of
the application (similar to how it is already done for `handle` and
`content`). Every interface function now has the same data that it can
each use for implementing its corresponding task based on local and
shared variables through the element instance and model pointer.
2025-11-10 17:09:29 +01:00
79a0d17a66 feat(io): introduce std.Io parameter for App.init
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m22s
The provided `std.Io` is used for running the input reading and the
rendering asynch. The user can provide their desired `std.Io`
implementation as they wish to use. The examples use `std.Io.Threaded`
as a simple threaded solution.
2025-11-10 16:44:02 +01:00
accbb4c97d mod: bump action dependencies
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 56s
2025-11-05 22:45:56 +01:00
ad32e46bc9 feat(element/scrollable): mouse support for scrollbar interaction
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 55s
Fix an issue with the scrollbar contents to work proper with statically
defined `Container` sizes through its `Properties` sizing option.
2025-11-05 22:08:35 +01:00
8ebab702ac fix(element/scrollable): reduce the number of minSize calls and correctly resize scrollable Container
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m26s
2025-11-05 18:33:16 +01:00
e53bb7880b mod: cleanup TODO and outdated comments 2025-11-05 18:32:49 +01:00
a83e86f8d9 feat(element/scrollable): background configuration
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m30s
2025-11-02 21:00:17 +01:00
4567963ff2 mod(example/text): improve event loop; add stick table heading for used emphasis
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 59s
This should be the way it should be done all the time, such that you
are not rendering for every input, but instead handle all `App.Event`s
that happened between the last render and the current. This shares
similarities with the continuous event loops, which also batches
the events only with the exception that it instead blocks (see
`App.Queue.poll`).
2025-11-02 16:10:11 +01:00
d4cc520826 fix(element/scrollable): display and user interaction
Fix initial render to show scrollbar immediately if required. Show and
hide scrollbar correctly when content size or terminal size changes. Add
keybindings for scrolling similar to pager keybindings.
2025-11-02 16:05:53 +01:00
b3dc8096d7 mod(event): do not care about enum declarations
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 56s
The union type can not be constructed with declarations anyway:
`error: reified unions must have no decls`; meaning there is no point in
even trying to do so.
2025-11-01 21:44:53 +01:00
7cd1fb139f mod(element/scrollable): ensure minSize returns correct dimensions
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 56s
Enforce that `minSize` returns a `Point` with corresponding minimal
dimensions with respect to the provided available size. This means that
an `Element` implementation that provides a `minSize` function may even
return a too small dimension, which would automatically be resized to be
at least as big as the provided size.
2025-11-01 14:37:14 +01:00
f70ea05244 mod(element/scrollable): introduce minSize function for Element
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 3m24s
The minimal required size can the queried from the containing `Element`
of the `Container` that is provided to the `Scrollable` to dynamically
adjust its size.

Currently abritrary nesting is not supported / tested.
2025-11-01 13:55:16 +01:00
c645c2efee fix: changes from newest zig version
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m3s
2025-11-01 00:03:04 +01:00
89aeac1e96 doc: correct alert blocks
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m0s
2025-10-26 21:46:00 +01:00
feae9fa1a4 feat(model): implement Elm architecture
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m2s
Now the `App` contains a state which is a user-defined `struct` which
is passed to the `handle` and `contents` callbacks for `Container`'s and
`Element`'s. Built-in `Element`'s shall not access the `App.Model` and
should therefore never cause any side-effects.

User-defined events shall be used to act as *messages* to cause
potential side-effects for the model. This is the reason why only
the `handle` callback has a non-const pointer to the `App.Model`. The
`contents` callback can only access the `App.Model` read-only to use for
generating the *view* (in context of the elm architecture).
2025-10-26 15:58:07 +01:00
8f90f57f44 fix(terminal): correctly restore termios and screen's during panic_handler
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m1s
2025-10-19 11:44:17 +02:00
fee3c796d9 doc(terminal): remove resolved issue
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m0s
2025-10-18 23:31:49 +02:00
547f553404 fix(terminal): restore screen correctly with alt screen
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m3s
2025-10-18 23:30:15 +02:00
1f6bbcc45e mod(examples/progress): show multiple different configuration options
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 59s
2025-10-01 11:14:22 +02:00
832fc45c3e chor: use new Writer interface for terminal's Writer; fix test cases
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 3m38s
2025-10-01 10:59:29 +02:00
aa17e13b99 lint: correct typo
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 58s
2025-09-29 23:11:28 +02:00
cba07b119c 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
2025-09-29 23:09:42 +02:00
f256a79da0 chor: bumb zig version to 0.16.0-dev
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m41s
2025-08-27 12:17:20 +02:00
c50b10f32d fix: zig update build errors
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m35s
2025-08-06 16:14:22 +02:00
4f6cabf898 chor: update zig version
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m3s
2025-08-04 21:09:56 +02:00
853f4d9769 mod(event): documentation of unsupported creation of declarations during comptime
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 56s
The functionality remains in the code, but will cause an compilation
error when the user provided tagged union for the application events
has declarations. This way it is at least explicit that no declarations
are supported (even if this is actually a nice to have).

For details see this [issue](https://github.com/ziglang/zig/issues/6709).
2025-07-17 23:15:45 +02:00
edbca39c38 mod(event): merge the declarations of the tagged unions alongside the fields
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 55s
2025-07-17 22:27:02 +02:00
0de50e7016 feat(element/selection): Element implementation for selecting an enum variant
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m35s
2025-07-17 21:00:00 +02:00
088e1a9246 add(element/radio-button): RadioButton Element implementation
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 2m6s
This can be used to visualize the values of `bool`'s, which is relevant
when creating form's based on `struct`'s automatically.
2025-07-13 21:02:28 +02:00
35 changed files with 2276 additions and 891 deletions

View File

@@ -15,9 +15,15 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup zig installation
uses: mlugg/setup-zig@v2
uses: https://codeberg.org/mlugg/setup-zig@v2
with:
version: master
version: latest
- name: Lint check
run: zig fmt --check .
- name: Spell checking
uses: crate-ci/typos@v1.39.0
with:
config: ./.typos-config
- name: Run tests
run: zig build --release=fast
- name: Release build artifacts

View File

@@ -14,13 +14,13 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup zig installation
uses: mlugg/setup-zig@v2
uses: https://codeberg.org/mlugg/setup-zig@v2
with:
version: master
version: latest
- name: Lint check
run: zig fmt --check --exclude src/test .
- name: Spell checking
uses: crate-ci/typos@v1.25.0
uses: crate-ci/typos@v1.39.0
with:
config: ./.typos-config
- name: Run tests

View File

@@ -2,9 +2,6 @@
`zterm` is a terminal user interface library (*tui*) to implement terminal (fullscreen or inline) applications.
> [!CAUTION]
> Only builds using the zig master version are tested to work.
## Demo
Clone this repository and run `zig build --help` to see the available examples. Run a given example as follows:
@@ -13,7 +10,7 @@ Clone this repository and run `zig build --help` to see the available examples.
zig build --release=safe -Dexample=demo run
```
> [!TIP]
> [!tip]
> Every example application can be quit using `ctrl+c`.
See the [wiki](https://gitea.yves-biener.de/yves-biener/zterm/wiki) for a showcase of the examples and the further details.
@@ -33,8 +30,6 @@ const zterm: *Dependency = b.dependency("zterm", .{
.target = target,
.optimize = optimize,
});
// ...
exe.root_module.addImport("zterm", zterm.module("zterm"));
```
### Documentation

View File

@@ -11,7 +11,9 @@ pub fn build(b: *std.Build) void {
button,
input,
progress,
radio_button,
scrollable,
selection,
// layouts:
vertical,
horizontal,
@@ -22,6 +24,8 @@ pub fn build(b: *std.Build) void {
palette,
// error handling
errors,
// non alternate screen applications
direct,
};
const example = b.option(Examples, "example", "Example to build and/or run. (default: all)") orelse .all;
@@ -33,20 +37,15 @@ pub fn build(b: *std.Build) void {
options.addOption(bool, "debug", debug_rendering);
const options_module = options.createModule();
// dependencies
const zg = b.dependency("zg", .{
.target = target,
.optimize = optimize,
});
// library
const lib = b.addModule("zterm", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "build_options", .module = options_module },
},
});
lib.addImport("code_point", zg.module("code_point"));
lib.addImport("build_options", options_module);
//--- Examples ---
const examples = std.meta.fields(Examples);
@@ -54,45 +53,54 @@ pub fn build(b: *std.Build) void {
if (@as(Examples, @enumFromInt(e.value)) == .all) continue; // skip `.all` entry
const demo = b.addExecutable(.{
.name = e.name,
.root_source_file = b.path(switch (@as(Examples, @enumFromInt(e.value))) {
.demo => "examples/demo.zig",
.continuous => "examples/continuous.zig",
// elements:
.alignment => "examples/elements/alignment.zig",
.button => "examples/elements/button.zig",
.input => "examples/elements/input.zig",
.progress => "examples/elements/progress.zig",
.scrollable => "examples/elements/scrollable.zig",
// layouts:
.vertical => "examples/layouts/vertical.zig",
.horizontal => "examples/layouts/horizontal.zig",
.grid => "examples/layouts/grid.zig",
.mixed => "examples/layouts/mixed.zig",
// styles:
.text => "examples/styles/text.zig",
.palette => "examples/styles/palette.zig",
// error handling
.errors => "examples/errors.zig",
.all => unreachable, // should never happen
.root_module = b.createModule(.{
.root_source_file = b.path(switch (@as(Examples, @enumFromInt(e.value))) {
.demo => "examples/demo.zig",
.continuous => "examples/continuous.zig",
// elements:
.alignment => "examples/elements/alignment.zig",
.button => "examples/elements/button.zig",
.input => "examples/elements/input.zig",
.progress => "examples/elements/progress.zig",
.radio_button => "examples/elements/radio-button.zig",
.scrollable => "examples/elements/scrollable.zig",
.selection => "examples/elements/selection.zig",
// layouts:
.vertical => "examples/layouts/vertical.zig",
.horizontal => "examples/layouts/horizontal.zig",
.grid => "examples/layouts/grid.zig",
.mixed => "examples/layouts/mixed.zig",
// styles:
.text => "examples/styles/text.zig",
.palette => "examples/styles/palette.zig",
// error handling
.errors => "examples/errors.zig",
// non-alternate screen
.direct => "examples/direct.zig",
.all => unreachable, // should never happen
}),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "zterm", .module = lib },
},
}),
.target = target,
.optimize = optimize,
});
// import dependencies
demo.root_module.addImport("zterm", lib);
// mapping of user selected example to compile step
if (@intFromEnum(example) == e.value or example == .all) b.installArtifact(demo);
}
// zig build test
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "build_options", .module = options_module },
},
}),
});
lib_unit_tests.root_module.addImport("code_point", zg.module("code_point"));
lib_unit_tests.root_module.addImport("DisplayWidth", zg.module("DisplayWidth"));
lib_unit_tests.root_module.addImport("build_options", options_module);
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

View File

@@ -35,12 +35,7 @@
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.zg = .{
.url = "git+https://codeberg.org/atman/zg#9427a9e53aaa29ee071f4dcb35b809a699d75aa9",
.hash = "zg-0.14.1-oGqU3IQ_tALZIiBN026_NTaPJqU-Upm8P_C7QED2Rzm8",
},
},
.dependencies = .{},
.paths = .{
"LICENSE",
"build.zig",

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -41,7 +41,7 @@ const Spinner = struct {
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -57,18 +57,20 @@ const Spinner = struct {
};
const InputField = struct {
allocator: std.mem.Allocator,
input: std.ArrayList(u21),
queue: *App.Queue,
pub fn init(allocator: std.mem.Allocator, queue: *App.Queue) @This() {
return .{
.input = .init(allocator),
.allocator = allocator,
.input = std.ArrayList(u21).initCapacity(allocator, 8) catch unreachable,
.queue = queue,
};
}
pub fn deinit(this: @This()) void {
this.input.deinit();
pub fn deinit(this: *@This()) void {
this.input.deinit(this.allocator);
}
pub fn element(this: *@This()) App.Element {
@@ -81,14 +83,14 @@ const InputField = struct {
};
}
fn handle(ctx: *anyopaque, event: App.Event) !void {
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.key => |key| {
if (key.isAscii()) try this.input.append(key.cp);
if (key.isUnicode()) try this.input.append(this.allocator, key.cp);
if (key.eql(.{ .cp = zterm.input.Enter }) or key.eql(.{ .cp = zterm.input.KpEnter }))
this.queue.push(.{ .accept = try this.input.toOwnedSlice() });
this.queue.push(.{ .accept = try this.input.toOwnedSlice(this.allocator) });
if (key.eql(.{ .cp = zterm.input.Backspace }))
_ = this.input.pop();
@@ -100,7 +102,7 @@ const InputField = struct {
}
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -131,7 +133,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -151,7 +153,7 @@ pub fn main() !void {
defer container.deinit();
try container.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_grey },
.rectangle = .{ .fill = .lightgrey },
.size = .{
.grow = .horizontal,
.dim = .{ .y = 10 },
@@ -159,11 +161,11 @@ pub fn main() !void {
}, input_field.element()));
const nested_container: App.Container = try .init(allocator, .{
.rectangle = .{ .fill = .light_grey },
.rectangle = .{ .fill = .lightgrey },
}, spinner.element());
try container.append(nested_container);
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
var framerate: u64 = 60;
@@ -176,7 +178,7 @@ pub fn main() !void {
if (now_ms >= next_frame_ms) {
next_frame_ms = now_ms + tick_ms;
} else {
time.sleep((next_frame_ms - now_ms) * time.ns_per_ms);
std.Thread.sleep((next_frame_ms - now_ms) * time.ns_per_ms);
next_frame_ms += tick_ms;
}
@@ -212,7 +214,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -226,9 +228,9 @@ pub fn main() !void {
}
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -240,6 +242,6 @@ const std = @import("std");
const time = std.time;
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {
const App = zterm.App(struct {}, union(enum) {
accept: []u21,
});

View File

@@ -1,11 +1,11 @@
const QuitText = struct {
const text = "Press ctrl+c to quit. Press ctrl+n to launch helix.";
const text = "Press ctrl+c to quit. Press ctrl+n to launch `vim`.";
pub fn element(this: *@This()) App.Element {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
pub fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
pub fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -27,13 +27,12 @@ const QuitText = struct {
pub fn main() !void {
errdefer |err| log.err("Application Error: {any}", .{err});
// TODO maybe create own allocator as some sort of arena allocator to have consistent memory usage
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer if (gpa.deinit() == .leak) log.err("memory leak", .{});
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -57,15 +56,14 @@ pub fn main() !void {
},
}, .{});
try box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
.rectangle = .{ .fill = .lightgreen },
}, .{}));
try box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
.rectangle = .{ .fill = .lightgreen },
}, .{}));
try box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
.rectangle = .{ .fill = .lightgreen },
}, .{}));
defer box.deinit();
var scrollable: App.Scrollable = .init(box, .disabled);
@@ -92,7 +90,7 @@ pub fn main() !void {
.direction = .vertical,
},
.border = .{
.color = .light_blue,
.color = .lightblue,
.sides = .all,
},
}, .{});
@@ -135,7 +133,7 @@ pub fn main() !void {
}, .{}));
defer container.deinit(); // also de-initializes the children
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -151,8 +149,8 @@ pub fn main() !void {
if (key.eql(.{ .cp = 'n', .mod = .{ .ctrl = true } })) {
try app.interrupt();
renderer.size = .{}; // reset size, such that next resize will cause a full re-draw!
defer app.start() catch @panic("could not start app event loop");
var child = std.process.Child.init(&.{"hx"}, allocator);
defer app.start(.full) catch @panic("could not start app event loop");
var child = std.process.Child.init(&.{"vim"}, allocator);
_ = child.spawnAndWait() catch |err| app.postEvent(.{
.err = .{
.err = err,
@@ -168,7 +166,7 @@ pub fn main() !void {
}
// NOTE returned errors should be propagated back to the application
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -181,9 +179,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -195,4 +193,4 @@ const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const input = zterm.input;
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

191
examples/direct.zig Normal file
View File

@@ -0,0 +1,191 @@
const QuitText = struct {
const text = "Press ctrl+c to quit.";
pub fn element(this: *@This()) App.Element {
return .{
.ptr = this,
.vtable = &.{
.minSize = minSize,
.content = content,
},
};
}
fn minSize(_: *anyopaque, _: *const App.Model, size: zterm.Point) zterm.Point {
return .{ .x = size.x, .y = 10 }; // this includes the border
}
pub fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
const y = 0;
const x = 2;
const anchor = (y * size.x) + x;
for (text, 0..) |cp, idx| {
cells[anchor + idx].style.fg = .white;
cells[anchor + idx].style.emphasis = &.{ .bold, .underline };
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 Prompt = struct {
len: u16 = 3,
pub fn element(this: *@This()) App.Element {
return .{
.ptr = this,
.vtable = &.{
// .minSize = minSize,
.content = content,
},
};
}
// NOTE size hint is not required as the `.size = .{ .dim = .{..} }` property is set accordingly which denotes the minimal size
// fn minSize(ctx: *anyopaque, _: *const App.Model, _: zterm.Point) zterm.Point {
// const this: *@This() = @ptrCast(@alignCast(ctx));
// return .{ .x = this.len, .y = 1 };
// }
pub fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
assert(cells.len > 2); // expect at least two cells
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
for (0..this.len) |idx| {
cells[idx].style.bg = .blue;
cells[idx].style.fg = .black;
cells[idx].style.emphasis = &.{.bold};
}
cells[1].cp = '>';
// leave one clear whitespace after the prompt
cells[this.len].style.bg = .default;
cells[this.len].style.cursor = true; // marks the actual end of the rendering!
}
};
pub fn main() !void {
errdefer |err| log.err("Application Error: {any}", .{err});
var allocator: std.heap.DebugAllocator(.{}) = .init;
defer if (allocator.deinit() == .leak) log.err("memory leak", .{});
const gpa = allocator.allocator();
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Direct.init(gpa);
defer renderer.deinit();
var container: App.Container = try .init(gpa, .{
.layout = .{
.direction = .vertical,
.gap = 1, // show empty line between elements to allow navigation through paragraph jumping
},
.size = .{
.grow = .horizontal_only,
},
}, .{});
defer container.deinit();
var quit_text: QuitText = .{};
var intermediate: App.Container = try .init(gpa, .{
.border = .{
.sides = .{ .left = true },
.color = .grey,
},
.layout = .{
.direction = .horizontal,
.padding = .{ .left = 1, .top = 1 },
},
}, quit_text.element());
try intermediate.append(try .init(gpa, .{
.rectangle = .{ .fill = .blue },
}, .{}));
try intermediate.append(try .init(gpa, .{
.rectangle = .{ .fill = .green },
}, .{}));
var padding_container: App.Container = try .init(gpa, .{
.layout = .{
.padding = .horizontal(1),
},
}, .{});
try padding_container.append(intermediate);
try container.append(padding_container);
var prompt: Prompt = .{};
try container.append(try .init(gpa, .{
.rectangle = .{ .fill = .grey },
.size = .{
.dim = .{ .y = 1 },
},
}, prompt.element()));
try app.start(.direct); // needs to become configurable, as what should be enabled / disabled (i.e. show cursor, hide cursor, use alternate screen, etc.)
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
event: while (true) {
// batch events since last iteration
const len = blk: {
app.queue.poll();
app.queue.lock();
defer app.queue.unlock();
break :blk app.queue.len();
};
// handle events
for (0..len) |_| {
const event = app.queue.pop();
log.debug("received event: {s}", .{@tagName(event)});
// pre event handling
switch (event) {
// NOTE maybe I want to decouple the `key`s from the user input too? i.e. this only makes sense if the output is not echoed!
// otherwise just use gnu's `readline`?
.line => |line| {
log.debug("{s}", .{line});
},
// NOTE errors could be displayed in another container in case one was received, etc. to provide the user with feedback
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
else => {},
}
// NOTE returned errors should be propagated back to the application
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
},
});
// post event handling
switch (event) {
.quit => break :event,
else => {},
}
}
// if there are more events to process continue handling them otherwise I can render the next frame
if (app.queue.len() > 0) continue :event;
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
pub const panic = App.panic_handler;
const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const input = zterm.input;
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -32,7 +32,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -55,7 +55,7 @@ pub fn main() !void {
var alignment: App.Alignment = .init(quit_container, .center);
try container.append(try .init(allocator, .{}, alignment.element()));
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -70,7 +70,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -83,9 +83,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -96,4 +96,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -40,7 +40,7 @@ const Clickable = struct {
};
}
fn handle(ctx: *anyopaque, event: App.Event) !void {
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.mouse => |mouse| if (mouse.button == .left and mouse.kind == .release) {
@@ -55,7 +55,7 @@ const Clickable = struct {
}
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -82,7 +82,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -99,10 +99,10 @@ pub fn main() !void {
}, quit_text.element());
defer container.deinit();
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .light_grey } }, element));
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .lightgrey } }, element));
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .black } }, button.element()));
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -119,7 +119,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -132,9 +132,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -145,7 +145,10 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {
click: [:0]const u8,
accept,
});
const App = zterm.App(
struct {},
union(enum) {
click: [:0]const u8,
accept,
},
);

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -37,7 +37,7 @@ const MouseDraw = struct {
};
}
fn handle(ctx: *anyopaque, event: App.Event) !void {
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.mouse => |mouse| this.position = .{ .x = mouse.x, .y = mouse.y },
@@ -45,7 +45,7 @@ const MouseDraw = struct {
}
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
const this: *@This() = @ptrCast(@alignCast(ctx));
@@ -65,13 +65,11 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
var input_field: App.Input(.accept) = .init(allocator, &app.queue, .init(.black));
defer input_field.deinit();
var input_field: App.Input(.accept) = .init(allocator, &app.queue, .init(.black, .blue));
var mouse_draw: MouseDraw = .{};
var second_mouse_draw: MouseDraw = .{};
var quit_text: QuitText = .{};
@@ -86,7 +84,7 @@ pub fn main() !void {
defer container.deinit();
try container.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_grey },
.rectangle = .{ .fill = .lightgrey },
.size = .{
.grow = .horizontal,
.dim = .{ .y = 1 },
@@ -98,7 +96,7 @@ pub fn main() !void {
.sides = .all,
.color = .black,
},
.rectangle = .{ .fill = .light_grey },
.rectangle = .{ .fill = .lightgrey },
.layout = .{
.separator = .{
.enabled = true,
@@ -107,14 +105,14 @@ pub fn main() !void {
},
}, .{});
try nested_container.append(try .init(allocator, .{
.rectangle = .{ .fill = .light_grey },
.rectangle = .{ .fill = .lightgrey },
}, mouse_draw.element()));
try nested_container.append(try .init(allocator, .{
.rectangle = .{ .fill = .light_grey },
.rectangle = .{ .fill = .lightgrey },
}, second_mouse_draw.element()));
try container.append(nested_container);
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -133,7 +131,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -146,9 +144,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -161,6 +159,9 @@ const assert = std.debug.assert;
const zterm = @import("zterm");
const Color = zterm.Color;
const App = zterm.App(union(enum) {
accept: []u8,
});
const App = zterm.App(
struct {},
union(enum) {
accept: []u8,
},
);

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -32,26 +32,60 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
var progress_percent: u8 = 0;
var progress: App.Progress(.progress) = .init(&app.queue, .{
.percent = .{ .enabled = true },
.fg = .green,
.bg = .grey,
});
var quit_text: QuitText = .{};
var container = try App.Container.init(allocator, .{
.layout = .{ .padding = .all(5) },
.layout = .{ .padding = .all(5), .direction = .vertical },
}, quit_text.element());
defer container.deinit();
try container.append(try App.Container.init(allocator, .{}, progress.element()));
try app.start();
{
var progress: App.Progress(.progress) = .init(&app.queue, .{
.percent = .{
.enabled = true,
.alignment = .left,
},
.fg = .blue,
.bg = .grey,
});
try container.append(try App.Container.init(allocator, .{}, progress.element()));
}
{
var progress: App.Progress(.progress) = .init(&app.queue, .{
.percent = .{
.enabled = true,
.alignment = .middle, // default
},
.fg = .red,
.bg = .grey,
});
try container.append(try App.Container.init(allocator, .{}, progress.element()));
}
{
var progress: App.Progress(.progress) = .init(&app.queue, .{
.percent = .{
.enabled = true,
.alignment = .right,
},
.fg = .green,
.bg = .grey,
});
try container.append(try App.Container.init(allocator, .{}, progress.element()));
}
{
var progress: App.Progress(.progress) = .init(&app.queue, .{
.percent = .{ .enabled = false },
.fg = .default,
.bg = .grey,
});
try container.append(try App.Container.init(allocator, .{}, progress.element()));
}
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
var framerate: u64 = 60;
@@ -67,7 +101,7 @@ pub fn main() !void {
if (now_ms >= next_frame_ms) {
next_frame_ms = now_ms + tick_ms;
} else {
time.sleep((next_frame_ms - now_ms) * time.ns_per_ms);
std.Thread.sleep((next_frame_ms - now_ms) * time.ns_per_ms);
next_frame_ms += tick_ms;
}
@@ -104,7 +138,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -118,9 +152,9 @@ pub fn main() !void {
}
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -132,6 +166,9 @@ const std = @import("std");
const time = std.time;
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {
progress: u8,
});
const App = zterm.App(
struct {},
union(enum) {
progress: u8,
},
);

View File

@@ -0,0 +1,98 @@
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, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
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;
}
}
};
pub fn main() !void {
errdefer |err| log.err("Application Error: {any}", .{err});
var gpa: std.heap.DebugAllocator(.{}) = .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 radiobutton: App.RadioButton = .init(false, .{
.label = "Test Radio Button",
.style = .squared,
});
var button: App.Button(.accept) = .init(&app.queue, .init(.default, "Button"));
var quit_text: QuitText = .{};
var container = try App.Container.init(allocator, .{
.rectangle = .{ .fill = .grey },
.layout = .{ .padding = .all(5) },
}, quit_text.element());
defer container.deinit();
try container.append(try .init(allocator, .{}, radiobutton.element()));
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .black } }, button.element()));
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
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(),
.accept => log.info("Clicked built-in button using the mouse", .{}),
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
else => {},
}
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
},
});
// post event handling
switch (event) {
.quit => break,
else => {},
}
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
pub const panic = App.panic_handler;
const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(struct {}, union(enum) {
accept,
});

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -34,7 +34,7 @@ const HelloWorldText = packed struct {
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -56,7 +56,6 @@ const HelloWorldText = packed struct {
pub fn main() !void {
errdefer |err| log.err("Application Error: {any}", .{err});
// TODO maybe create own allocator as some sort of arena allocator to have consistent memory usage
var gpa: std.heap.DebugAllocator(.{}) = .init;
defer {
const deinit_status = gpa.deinit();
@@ -66,7 +65,7 @@ pub fn main() !void {
}
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -87,24 +86,23 @@ pub fn main() !void {
},
}, .{});
try top_box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
.rectangle = .{ .fill = .lightgreen },
.size = .{
.dim = .{ .y = 30 },
},
}, .{}));
try top_box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
.rectangle = .{ .fill = .lightgreen },
.size = .{
.dim = .{ .y = 5 },
},
}, element));
try top_box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .light_green },
.rectangle = .{ .fill = .lightgreen },
.size = .{
.dim = .{ .y = 2 },
},
}, .{}));
defer top_box.deinit();
var bottom_box = try App.Container.init(allocator, .{
.border = .{
@@ -132,7 +130,6 @@ pub fn main() !void {
try bottom_box.append(try App.Container.init(allocator, .{
.rectangle = .{ .fill = .grey },
}, .{}));
defer bottom_box.deinit();
var container = try App.Container.init(allocator, .{
.layout = .{
@@ -148,13 +145,13 @@ pub fn main() !void {
defer container.deinit();
// place empty container containing the element of the scrollable Container.
var scrollable_top: App.Scrollable = .init(top_box, .enabled(.grey));
var scrollable_top: App.Scrollable = .init(top_box, .enabled(.default, false));
try container.append(try App.Container.init(allocator, .{}, scrollable_top.element()));
var scrollable_bottom: App.Scrollable = .init(bottom_box, .enabled(.white));
var scrollable_bottom: App.Scrollable = .init(bottom_box, .enabled(.white, true));
try container.append(try App.Container.init(allocator, .{}, scrollable_bottom.element()));
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -169,7 +166,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -182,9 +179,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -196,4 +193,4 @@ const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const input = zterm.input;
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -0,0 +1,97 @@
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, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
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;
}
}
};
pub fn main() !void {
errdefer |err| log.err("Application Error: {any}", .{err});
var gpa: std.heap.DebugAllocator(.{}) = .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 selection: App.Selection(enum {
one,
two,
three,
}) = .init(.{
.label = "Selection",
});
var quit_text: QuitText = .{};
var container = try App.Container.init(allocator, .{
.rectangle = .{ .fill = .grey },
.layout = .{ .padding = .all(5) },
}, quit_text.element());
defer container.deinit();
try container.append(try .init(allocator, .{}, selection.element()));
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
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(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
},
});
// post event handling
switch (event) {
.quit => break,
else => {},
}
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
pub const panic = App.panic_handler;
const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const Error = zterm.Error;
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -31,7 +31,7 @@ const InfoText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -57,7 +57,7 @@ const ErrorNotification = struct {
return .{ .ptr = this, .vtable = &.{ .handle = handle, .content = content } };
}
fn handle(ctx: *anyopaque, event: App.Event) !void {
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.key => |key| if (!key.isAscii()) return zterm.Error.TooSmall,
@@ -66,7 +66,7 @@ const ErrorNotification = struct {
}
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -97,7 +97,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -116,7 +116,7 @@ pub fn main() !void {
try container.append(try App.Container.init(allocator, .{}, info_text.element()));
try container.append(try App.Container.init(allocator, .{}, error_notification.element()));
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
while (true) {
@@ -130,7 +130,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -143,9 +143,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -156,4 +156,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -8,7 +8,7 @@ const QuitText = struct {
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -35,7 +35,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -69,7 +69,7 @@ pub fn main() !void {
}
defer container.deinit(); // also de-initializes the children
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -86,7 +86,7 @@ pub fn main() !void {
}
// NOTE returned errors should be propagated back to the application
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -99,9 +99,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -112,4 +112,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -8,7 +8,7 @@ const QuitText = struct {
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -35,7 +35,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -61,7 +61,7 @@ pub fn main() !void {
}, .{}));
defer container.deinit(); // also de-initializes the children
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -78,7 +78,7 @@ pub fn main() !void {
}
// NOTE returned errors should be propagated back to the application
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -91,9 +91,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -104,4 +104,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -8,7 +8,7 @@ const QuitText = struct {
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -35,7 +35,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -77,7 +77,7 @@ pub fn main() !void {
}
defer container.deinit(); // also de-initializes the children
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -94,7 +94,7 @@ pub fn main() !void {
}
// NOTE returned errors should be propagated back to the application
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -107,9 +107,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -120,4 +120,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -8,7 +8,7 @@ const QuitText = struct {
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -35,7 +35,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -60,7 +60,7 @@ pub fn main() !void {
}, .{}));
defer container.deinit(); // also de-initializes the children
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
// event loop
@@ -77,7 +77,7 @@ pub fn main() !void {
}
// NOTE returned errors should be propagated back to the application
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -90,9 +90,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -103,4 +103,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -32,7 +32,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -50,7 +50,6 @@ pub fn main() !void {
var box = try App.Container.init(allocator, .{
.layout = .{ .direction = .horizontal },
}, .{});
defer box.deinit();
inline for (std.meta.fields(zterm.Color)) |field| {
if (field.value == 0) continue; // zterm.Color.default == 0 -> skip
@@ -59,7 +58,7 @@ pub fn main() !void {
var scrollable: App.Scrollable = .init(box, .disabled);
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
try app.start();
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
while (true) {
@@ -73,7 +72,7 @@ pub fn main() !void {
else => {},
}
container.handle(event) catch |err| app.postEvent(.{
container.handle(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -86,9 +85,9 @@ pub fn main() !void {
else => {},
}
container.resize(try renderer.resize());
container.reposition(.{});
try renderer.render(@TypeOf(container), &container);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -99,4 +98,4 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -5,7 +5,7 @@ const QuitText = struct {
return .{ .ptr = this, .vtable = &.{ .content = content } };
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -24,14 +24,109 @@ const QuitText = struct {
}
};
const TableText = struct {
pub fn element(this: *@This()) App.Element {
return .{
.ptr = this,
.vtable = &.{
.content = content,
},
};
}
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
_ = ctx;
_ = size;
var idx: usize = 0;
{
const text = "Normal ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Bold ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Dim ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Italic ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Underl ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Blink ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Invert ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Hidden ";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
{
const text = "Strikethrough";
for (text) |cp| {
cells[idx].cp = cp;
idx += 1;
}
}
}
};
const TextStyles = struct {
const text = "Example";
pub fn element(this: *@This()) App.Element {
return .{ .ptr = this, .vtable = &.{ .content = content } };
return .{
.ptr = this,
.vtable = &.{
.minSize = minSize,
.content = content,
},
};
}
fn content(ctx: *anyopaque, cells: []zterm.Cell, size: zterm.Point) !void {
fn minSize(ctx: *anyopaque, _: *const App.Model, size: zterm.Point) zterm.Point {
_ = ctx;
_ = size;
return .{
.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),
};
}
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
@setEvalBranchQuota(10000);
_ = ctx;
assert(cells.len == @as(usize, size.x) * @as(usize, size.y));
@@ -83,7 +178,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
var app: App = .init;
var app: App = .init(.{}, .{});
var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit();
@@ -94,56 +189,67 @@ pub fn main() !void {
var container = try App.Container.init(allocator, .{
.layout = .{
.gap = 2,
// .gap = 2,
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
.direction = .vertical,
},
}, element);
defer container.deinit();
var box = try App.Container.init(allocator, .{
.layout = .{ .direction = .vertical },
var table_head: TableText = .{};
try container.append(try .init(allocator, .{
.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),
},
.dim = .{ .y = 1 },
.grow = .horizontal,
},
}, text_styles.element());
defer box.deinit();
}, table_head.element()));
var scrollable: App.Scrollable = .init(box, .disabled);
var scrollable: App.Scrollable = .init(try .init(allocator, .{
.layout = .{ .direction = .vertical },
}, text_styles.element()), .enabled(.white, true));
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
try app.start();
try app.start(.full);
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)});
// event loop
loop: while (true) {
// batch events since last iteration
const len = blk: {
app.queue.poll();
app.queue.lock();
defer app.queue.unlock();
break :blk app.queue.len();
};
// 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 => {},
// handle events
for (0..len) |_| {
const event = app.queue.pop();
log.debug("handling 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(&app.model, event) catch |err| app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
},
});
// post event handling
switch (event) {
.quit => break :loop,
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);
container.resize(&app.model, try renderer.resize());
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
}
}
@@ -154,4 +260,5 @@ const log = std.log.scoped(.default);
const std = @import("std");
const assert = std.debug.assert;
const zterm = @import("zterm");
const App = zterm.App(union(enum) {});
const App = zterm.App(struct {}, union(enum) {});

View File

@@ -12,75 +12,116 @@
/// ```zig
/// const zterm = @import("zterm");
/// const App = zterm.App(
/// union(enum) {},
/// struct {}, // empty model
/// union(enum) {}, // no additional user event's
/// );
/// // later on create an `App` instance and start the event loop
/// var app: App = .init;
/// try app.start();
/// defer app.stop() catch unreachable;
/// var app: App = .init(io, .{}); // provide instance of the `std.Io` and `App` model that shall be used
/// try app.start(.full);
/// defer app.stop() catch unreachable; // does not clean-up the resources used in the model
/// ```
pub fn App(comptime E: type) type {
if (!isTaggedUnion(E)) {
@compileError("Provided user event `E` for `App(comptime E: type)` is not of type `union(enum)`.");
}
pub fn App(comptime M: type, comptime E: type) type {
if (!isStruct(M)) @compileError("Provided model `M` for `App(comptime M: type, comptime E: type)` is not of type `struct`");
if (!isTaggedUnion(E)) @compileError("Provided user event `E` for `App(comptime M: type, comptime E: type)` is not of type `union(enum)`.");
return struct {
io: std.Io,
model: Model,
queue: Queue,
thread: ?Thread = null,
quit_event: Thread.ResetEvent,
termios: ?posix.termios = null,
winch_registered: bool = false,
handler_registered: bool = false,
config: TerminalConfiguration,
pub const TerminalConfiguration = struct {
altScreen: bool,
saveScreen: bool,
rawMode: bool,
hideCursor: bool,
pub const full: @This() = .{
.altScreen = true,
.saveScreen = true,
.rawMode = true,
.hideCursor = true,
};
pub const direct: @This() = .{
.altScreen = false,
.saveScreen = false,
.rawMode = false,
.hideCursor = false,
};
};
// global variable for the registered handler for WINCH
var handler_ctx: *anyopaque = undefined;
/// registered WINCH handler to report resize events
fn handleWinch(_: c_int) callconv(.C) void {
fn handleWinch(_: i32) callconv(.c) void {
const this: *@This() = @ptrCast(@alignCast(handler_ctx));
// NOTE this does not have to be done if in-band resize events are supported
// -> the signal might not work correctly when hosting the application over ssh!
this.postEvent(.resize);
}
/// registered CONT handler to force a complete redraw
fn handleCont(_: i32) callconv(.c) void {
const this: *@This() = @ptrCast(@alignCast(handler_ctx));
// NOTE this does not have to be done if in-band resize events are supported
// -> the signal might not work correctly when hosting the application over ssh!
this.postEvent(.resize);
}
pub const init: @This() = .{
.queue = .{},
.quit_event = .{},
};
pub fn init(io: std.Io, model: Model) @This() {
return .{
.io = io,
.model = model,
.queue = .{},
.quit_event = .{},
.config = undefined,
};
}
pub fn start(this: *@This()) !void {
pub fn start(this: *@This(), config: TerminalConfiguration) !void {
this.config = config;
if (this.thread) |_| return;
// post init event (as the very first element to be in the queue - event loop)
this.postEvent(.init);
if (!this.winch_registered) {
if (!this.handler_registered) {
handler_ctx = this;
var act = posix.Sigaction{
posix.sigaction(posix.SIG.WINCH, &.{
.handler = .{ .handler = handleWinch },
.mask = posix.sigemptyset(),
.flags = 0,
};
posix.sigaction(posix.SIG.WINCH, &act, null);
this.winch_registered = true;
}, null);
posix.sigaction(posix.SIG.CONT, &.{
.handler = .{ .handler = handleCont },
.mask = posix.sigemptyset(),
.flags = 0,
}, null);
this.handler_registered = true;
}
this.quit_event.reset();
this.thread = try Thread.spawn(.{}, @This().run, .{this});
var termios: posix.termios = undefined;
try terminal.enableRawMode(&termios);
if (this.config.rawMode) try terminal.enableRawMode(&termios);
if (this.termios) |_| {} else this.termios = termios;
try terminal.saveScreen();
try terminal.enterAltScreen();
try terminal.hideCursor();
try terminal.enableMouseSupport();
if (this.config.altScreen) try terminal.enterAltScreen();
if (this.config.saveScreen) try terminal.saveScreen();
if (this.config.hideCursor) try terminal.hideCursor();
if (this.config.altScreen and this.config.rawMode) try terminal.enableMouseSupport();
}
pub fn interrupt(this: *@This()) !void {
this.quit_event.set();
try terminal.disableMouseSupport();
try terminal.exitAltScreen();
try terminal.restoreScreen();
if (this.thread) |thread| {
if (this.config.altScreen and this.config.rawMode) try terminal.disableMouseSupport();
if (this.config.saveScreen) try terminal.restoreScreen();
if (this.config.altScreen) try terminal.exitAltScreen();
if (this.thread) |*thread| {
thread.join();
this.thread = null;
}
@@ -88,18 +129,16 @@ pub fn App(comptime E: type) type {
pub fn stop(this: *@This()) !void {
try this.interrupt();
if (this.termios) |*termios| {
try terminal.disableMouseSupport();
try terminal.showCursor();
try terminal.exitAltScreen();
try terminal.disableRawMode(termios);
try terminal.restoreScreen();
if (this.config.hideCursor) try terminal.showCursor();
if (this.config.saveScreen) try terminal.resetCursor();
if (this.termios) |termios| {
if (this.config.rawMode) try terminal.disableRawMode(&termios);
this.termios = null;
}
this.termios = null;
}
/// Quit the application loop.
/// This will stop the internal input thread and post a **.quit** `Event`.
/// This will cancel the internal input thread and post a **.quit** `Event`.
pub fn quit(this: *@This()) void {
this.quit_event.set();
this.postEvent(.quit);
@@ -117,13 +156,42 @@ pub fn App(comptime E: type) type {
fn run(this: *@This()) !void {
// thread to read user inputs
var buf: [256]u8 = undefined;
var buf: [512]u8 = undefined;
// NOTE set the `NONBLOCK` option for the stdin file, such that reading is not blocking!
{
// TODO is there a better way to do this through the `std.Io` interface?
var fl_flags = posix.fcntl(posix.STDIN_FILENO, posix.F.GETFL, 0) catch |err| switch (err) {
error.FileBusy => unreachable,
error.Locked => unreachable,
error.PermissionDenied => unreachable,
error.DeadLock => unreachable,
error.LockedRegionLimitExceeded => unreachable,
else => |e| return e,
};
fl_flags |= 1 << @bitOffsetOf(posix.system.O, "NONBLOCK");
_ = posix.fcntl(posix.STDIN_FILENO, posix.F.SETFL, fl_flags) catch |err| switch (err) {
error.FileBusy => unreachable,
error.Locked => unreachable,
error.PermissionDenied => unreachable,
error.DeadLock => unreachable,
error.LockedRegionLimitExceeded => unreachable,
else => |e| return e,
};
}
while (true) {
// FIX I still think that there is a race condition (I'm just waiting 'long' enough)
this.quit_event.timedWait(20 * std.time.ns_per_ms) catch {
// FIX in case the queue is full -> the next user input should panic and quit the application? because something seems to clock up the event queue
const read_bytes = try terminal.read(buf[0..]);
// TODO `break` should not terminate the reading of the user inputs, but instead only the received faulty input!
var remaining_bytes: usize = 0;
// non-blocking read
const read_bytes = terminal.read(buf[remaining_bytes..]) catch |err| switch (err) {
error.WouldBlock => {
// wait a bit
std.Thread.sleep(20);
continue;
},
else => return err,
} + remaining_bytes;
// escape key presses
if (buf[0] == 0x1b and read_bytes > 1) {
switch (buf[1]) {
@@ -150,9 +218,9 @@ pub fn App(comptime E: type) type {
if (read_bytes < 3) continue;
// We start iterating at index 2 to get past the '['
const sequence = for (buf[2..], 2..) |b, i| {
const sequence: []u8 = blk: for (buf[2..], 2..) |b, i| {
switch (b) {
0x40...0xFF => break buf[0 .. i + 1],
0x40...0xFF => break :blk buf[0 .. i + 1],
else => continue,
}
} else continue;
@@ -283,18 +351,19 @@ pub fn App(comptime E: type) type {
// const alt = button_mask & mouse_bits.alt > 0;
// const ctrl = button_mask & mouse_bits.ctrl > 0;
const mouse: Mouse = .{
.button = button,
.x = px -| 1,
.y = py -| 1,
.kind = blk: {
if (motion and button != Mouse.Button.none) break :blk .drag;
if (motion and button == Mouse.Button.none) break :blk .motion;
if (sequence[sequence.len - 1] == 'm') break :blk .release;
break :blk .press;
this.postEvent(.{
.mouse = .{
.button = button,
.x = px -| 1,
.y = py -| 1,
.kind = blk: {
if (motion and button != Mouse.Button.none) break :blk .drag;
if (motion and button == Mouse.Button.none) break :blk .motion;
if (sequence[sequence.len - 1] == 'm') break :blk .release;
break :blk .press;
},
},
};
this.postEvent(.{ .mouse = mouse });
});
},
'c' => {
// Primary DA (CSI ? Pm c)
@@ -376,9 +445,17 @@ pub fn App(comptime E: type) type {
},
0x7f => .{ .cp = input.Backspace },
else => {
var iter = code_point.Iterator{ .bytes = buf[0..read_bytes] };
while (iter.next()) |cp| this.postEvent(.{ .key = .{ .cp = cp.code } });
continue;
var len = read_bytes;
while (!std.unicode.utf8ValidateSlice(buf[0..len])) len -= 1;
remaining_bytes = read_bytes - len;
if (this.config.rawMode) {
var iter: std.unicode.Utf8Iterator = .{ .bytes = buf[0..len], .i = 0 };
while (iter.nextCodepoint()) |cp| this.postEvent(.{ .key = .{ .cp = cp } });
continue;
} else {
this.postEvent(.{ .line = buf[0..len] });
continue;
}
},
};
this.postEvent(.{ .key = key });
@@ -391,9 +468,10 @@ pub fn App(comptime E: type) type {
pub fn panic_handler(msg: []const u8, _: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
terminal.disableMouseSupport() catch {};
terminal.exitAltScreen() catch {};
terminal.showCursor() catch {};
var termios: posix.termios = .{
terminal.resetCursor() catch {};
terminal.restoreScreen() catch {};
terminal.disableRawMode(&.{
.iflag = .{},
.lflag = .{},
.cflag = .{},
@@ -402,22 +480,25 @@ pub fn App(comptime E: type) type {
.line = 0,
.ispeed = undefined,
.ospeed = undefined,
};
terminal.disableRawMode(&termios) catch {};
terminal.restoreScreen() catch {};
}) catch {};
terminal.exitAltScreen() catch {};
std.debug.defaultPanic(msg, ret_addr);
}
const element = @import("element.zig");
pub const Model = M;
pub const Event = mergeTaggedUnions(event.SystemEvent, E);
pub const Container = @import("container.zig").Container(Event);
pub const Element = element.Element(Event);
pub const Alignment = element.Alignment(Event);
pub const Button = element.Button(Event, Queue);
pub const Input = element.Input(Event, Queue);
pub const Progress = element.Progress(Event, Queue);
pub const Scrollable = element.Scrollable(Event);
pub const Queue = queue.Queue(Event, 256);
pub const Container = @import("container.zig").Container(Model, Event);
pub const Element = element.Element(Model, Event);
pub const Alignment = element.Alignment(Model, Event);
pub const Button = element.Button(Model, Event, Queue);
pub const TextField = element.TextField(Model, Event);
pub const Input = element.Input(Model, Event, Queue);
pub const Progress = element.Progress(Model, Event, Queue);
pub const RadioButton = element.RadioButton(Model, Event);
pub const Scrollable = element.Scrollable(Model, Event);
pub const Selection = element.Selection(Model, Event);
pub const Queue = queue.Queue(Event, 512);
};
}
@@ -429,13 +510,13 @@ const fmt = std.fmt;
const posix = std.posix;
const Thread = std.Thread;
const assert = std.debug.assert;
const code_point = @import("code_point");
const event = @import("event.zig");
const input = @import("input.zig");
const terminal = @import("terminal.zig");
const queue = @import("queue.zig");
const mergeTaggedUnions = event.mergeTaggedUnions;
const isTaggedUnion = event.isTaggedUnion;
const isStruct = event.isStruct;
const Mouse = input.Mouse;
const Key = input.Key;
const Point = @import("point.zig").Point;

View File

@@ -1,6 +1,5 @@
//! Cell type containing content and formatting for each character in the terminal screen.
// TODO embrace `zg` dependency more due to utf-8 encoding
cp: u21 = ' ',
style: Style = .{ .emphasis = &.{} },
@@ -13,7 +12,7 @@ pub fn reset(this: *Cell) void {
this.cp = ' ';
}
pub fn value(this: Cell, writer: anytype) !void {
pub fn value(this: Cell, writer: *std.Io.Writer) !void {
try this.style.value(writer, this.cp);
}
@@ -26,20 +25,18 @@ test "ascii styled text" {
.{ .cp = 'Y', .style = .{ .fg = .green, .bg = .grey, .emphasis = &.{} } },
.{ .cp = 'v', .style = .{ .emphasis = &.{ .bold, .underline } } },
.{ .cp = 'e', .style = .{ .emphasis = &.{.italic} } },
.{ .cp = 's', .style = .{ .fg = .light_green, .bg = .black, .emphasis = &.{.underline} } },
.{ .cp = 's', .style = .{ .fg = .lightgreen, .bg = .black, .emphasis = &.{.underline} } },
};
var string = std.ArrayList(u8).init(std.testing.allocator);
defer string.deinit();
const writer = string.writer();
var writer = std.Io.Writer.Allocating.init(std.testing.allocator);
defer writer.deinit();
for (cells) |cell| {
try cell.value(writer);
try cell.value(&writer.writer);
}
try std.testing.expectEqualSlices(
u8,
"\x1b[38;5;10;48;5;8;59mY\x1b[0m\x1b[39;49;59;1;4mv\x1b[0m\x1b[39;49;59;3me\x1b[0m\x1b[38;5;2;48;5;16;59;4ms\x1b[0m",
string.items,
writer.writer.buffer[0..writer.writer.end],
);
}
@@ -48,19 +45,17 @@ test "utf-8 styled text" {
.{ .cp = '╭', .style = .{ .fg = .green, .bg = .grey, .emphasis = &.{} } },
.{ .cp = '─', .style = .{ .emphasis = &.{} } },
.{ .cp = '┄', .style = .{ .emphasis = &.{} } },
.{ .cp = '┘', .style = .{ .fg = .light_green, .bg = .black, .emphasis = &.{.underline} } },
.{ .cp = '┘', .style = .{ .fg = .lightgreen, .bg = .black, .emphasis = &.{.underline} } },
};
var string = std.ArrayList(u8).init(std.testing.allocator);
defer string.deinit();
const writer = string.writer();
var writer = std.Io.Writer.Allocating.init(std.testing.allocator);
defer writer.deinit();
for (cells) |cell| {
try cell.value(writer);
try cell.value(&writer.writer);
}
try std.testing.expectEqualSlices(
u8,
"\x1b[38;5;10;48;5;8;59m╭\x1b[0m\x1b[39;49;59m─\x1b[0m\x1b[39;49;59m┄\x1b[0m\x1b[38;5;2;48;5;16;59;4m┘\x1b[0m",
string.items,
writer.writer.buffer[0..writer.writer.end],
);
}

View File

@@ -1,13 +1,13 @@
pub const Color = enum(u8) {
default = 0,
black = 16,
light_red = 1,
light_green,
light_yellow,
light_blue,
light_magenta,
light_cyan,
light_grey,
lightred = 1,
lightgreen,
lightyellow,
lightblue,
lightmagenta,
lightcyan,
lightgrey,
grey,
red,
green,
@@ -18,24 +18,21 @@ pub const Color = enum(u8) {
white,
// TODO add further colors as described in https://gist.github.com/ConnerWill/d4b6c776b509add763e17f9f113fd25b # Color / Graphics Mode - 256 Colors
// TODO might be useful to use the std.ascii stuff!
pub inline fn write(this: Color, writer: anytype, comptime coloring: enum { fg, bg, ul }) !void {
pub inline fn write(this: Color, writer: *std.Io.Writer, comptime coloring: enum { fg, bg, ul }) !void {
if (this == .default) {
switch (coloring) {
.fg => try format(writer, "39", .{}),
.bg => try format(writer, "49", .{}),
.ul => try format(writer, "59", .{}),
.fg => try writer.printAscii("39", .{}),
.bg => try writer.printAscii("49", .{}),
.ul => try writer.printAscii("59", .{}),
}
} else {
switch (coloring) {
.fg => try format(writer, "38;5;{d}", .{@intFromEnum(this)}),
.bg => try format(writer, "48;5;{d}", .{@intFromEnum(this)}),
.ul => try format(writer, "58;5;{d}", .{@intFromEnum(this)}),
.fg => try writer.print("38;5;{d}", .{@intFromEnum(this)}),
.bg => try writer.print("48;5;{d}", .{@intFromEnum(this)}),
.ul => try writer.print("58;5;{d}", .{@intFromEnum(this)}),
}
}
}
};
const std = @import("std");
const format = std.fmt.format;

View File

@@ -1,3 +1,6 @@
// FIX known issues:
// - hold fewer instances of the `Allocator`
/// Border configuration struct
pub const Border = packed struct {
/// Color to use for the border
@@ -77,8 +80,9 @@ pub const Border = packed struct {
test "all sides" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .green,
.sides = .all,
@@ -89,14 +93,15 @@ pub const Border = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/border.all.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/border.all.zon"));
}
test "vertical sides" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .green,
.sides = .vertical,
@@ -107,14 +112,15 @@ pub const Border = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/border.vertical.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/border.vertical.zon"));
}
test "horizontal sides" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .green,
.sides = .horizontal,
@@ -125,7 +131,7 @@ pub const Border = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/border.horizontal.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/border.horizontal.zon"));
}
};
@@ -157,8 +163,9 @@ pub const Rectangle = packed struct {
test "fill color overwrite parent fill" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.rectangle = .{ .fill = .green },
}, .{});
try container.append(try .init(std.testing.allocator, .{
@@ -170,14 +177,15 @@ pub const Rectangle = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/rectangle_with_parent_fill_without_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/rectangle_with_parent_fill_without_padding.zon"));
}
test "fill color padding to show parent fill" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.layout = .{
.padding = .all(2),
},
@@ -192,14 +200,15 @@ pub const Rectangle = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/rectangle_with_parent_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/rectangle_with_parent_padding.zon"));
}
test "fill color padding to show parent fill (negative padding)" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.layout = .{
.padding = .{
.top = -18,
@@ -219,14 +228,15 @@ pub const Rectangle = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/rectangle_with_parent_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/rectangle_with_parent_padding.zon"));
}
test "fill color spacer with padding" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.rectangle = .{
.fill = .black,
},
@@ -247,14 +257,15 @@ pub const Rectangle = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/rectangle_with_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/rectangle_with_padding.zon"));
}
test "fill color with gap" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.rectangle = .{
.fill = .black,
},
@@ -278,14 +289,15 @@ pub const Rectangle = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/rectangle_with_gap.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/rectangle_with_gap.zon"));
}
test "fill color with separator" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.rectangle = .{
.fill = .black,
},
@@ -310,7 +322,7 @@ pub const Rectangle = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/rectangle_with_separator.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/rectangle_with_separator.zon"));
}
};
@@ -400,8 +412,9 @@ pub const Layout = packed struct {
test "separator without gaps" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.layout = .{
.separator = .{
.enabled = true,
@@ -415,14 +428,15 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_no_gaps.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_no_gaps.zon"));
}
test "separator without gaps with padding" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.layout = .{
.padding = .all(1),
.separator = .{
@@ -437,14 +451,15 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_no_gaps_with_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_no_gaps_with_padding.zon"));
}
test "separator(2x) without gaps" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.layout = .{
.direction = .vertical,
.separator = .{
@@ -461,14 +476,15 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_2x_no_gaps.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_2x_no_gaps.zon"));
}
test "separator(2x) with border(all)" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .red,
.sides = .all,
@@ -488,14 +504,15 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_2x_no_gaps_with_border.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_2x_no_gaps_with_border.zon"));
}
test "separator(2x) with border(all) and padding(all(1))" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .red,
.sides = .all,
@@ -516,14 +533,15 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_2x_no_gaps_with_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_2x_no_gaps_with_padding.zon"));
}
test "separator(2x) with border(all) and gap" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .red,
.sides = .all,
@@ -544,14 +562,15 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_2x_with_gaps_with_border.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_2x_with_gaps_with_border.zon"));
}
test "separator(2x) with border(all) and gap and padding" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.border = .{
.color = .red,
.sides = .all,
@@ -573,25 +592,27 @@ pub const Layout = packed struct {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/separator_2x_with_gaps_with_border_with_padding.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/separator_2x_with_gaps_with_border_with_padding.zon"));
}
};
/// Sizing options which should be used by the `Container`
pub const Size = packed struct {
dim: Point = .{},
grow: enum(u2) {
grow: enum(u3) {
both,
fixed,
vertical_only,
horizontal_only,
vertical,
horizontal,
} = .both,
};
pub fn Container(comptime Event: type) type {
pub fn Container(Model: type, Event: type) type {
if (!isTaggedUnion(Event)) @compileError("Provided user event `Event` for `Container(comptime Event: type)`");
const Element = @import("element.zig").Element(Event);
const Element = @import("element.zig").Element(Model, Event);
return struct {
allocator: Allocator,
origin: Point,
@@ -622,23 +643,24 @@ pub fn Container(comptime Event: type) type {
.size = .{},
.properties = properties,
.element = element,
.elements = std.ArrayList(@This()).init(allocator),
.elements = try std.ArrayList(@This()).initCapacity(allocator, 2),
};
}
pub fn deinit(this: *const @This()) void {
pub fn deinit(this: *@This()) void {
for (this.elements.items) |*element| element.deinit();
this.elements.deinit();
this.elements.deinit(this.allocator);
this.element.deinit();
}
pub fn append(this: *@This(), element: @This()) !void {
try this.elements.append(element);
try this.elements.append(this.allocator, element);
}
pub fn reposition(this: *@This(), origin: Point) void {
pub fn reposition(this: *@This(), model: *const Model, origin: Point) void {
const layout = this.properties.layout;
this.origin = origin;
this.element.reposition(origin);
this.element.reposition(model, origin);
var offset = origin.add(.{
.x = Layout.getAbsolutePadding(layout.padding.left, this.size.x),
@@ -650,7 +672,7 @@ pub fn Container(comptime Event: type) type {
if (sides.top) offset.y += 1;
for (this.elements.items) |*child| {
child.reposition(offset);
child.reposition(model, offset);
switch (layout.direction) {
.horizontal => offset.x += child.size.x + layout.gap,
@@ -665,7 +687,7 @@ pub fn Container(comptime Event: type) type {
}
/// Resize all fit sized `Containers` to the necessary size required by its child elements.
fn fit_resize(this: *@This()) Point {
fn fit_resize(this: *@This(), model: *const Model) Point {
// NOTE this is supposed to be a simple and easy to understand algorithm, there are currently no optimizations done
const layout = this.properties.layout;
var size: Point = switch (layout.direction) {
@@ -690,7 +712,7 @@ pub fn Container(comptime Event: type) type {
};
for (this.elements.items) |*child| {
const child_size = child.fit_resize();
const child_size = child.fit_resize(model);
switch (layout.direction) {
.horizontal => {
size.x += child_size.x;
@@ -703,24 +725,29 @@ pub fn Container(comptime Event: type) type {
}
}
size = this.minSize(model, size);
// assign currently calculated size
this.size = switch (this.properties.size.grow) {
.both => Point.max(size, this.properties.size.dim),
.fixed => this.properties.size.dim,
.horizontal => .{
.x = @max(size.x, this.properties.size.dim.x),
.y = this.properties.size.dim.y,
.both => .max(size, this.properties.size.dim),
.fixed => blk: {
assert(this.properties.size.dim.x > 0 or size.x > 0);
assert(this.properties.size.dim.y > 0 or size.y > 0);
break :blk .max(size, this.properties.size.dim);
},
.vertical => .{
.x = this.properties.size.dim.x,
.y = @max(size.y, this.properties.size.dim.y),
.horizontal, .horizontal_only => blk: {
assert(this.properties.size.dim.y > 0 or size.y > 0);
break :blk .max(size, this.properties.size.dim);
},
.vertical, .vertical_only => blk: {
assert(this.properties.size.dim.x > 0 or size.x > 0);
break :blk .max(size, this.properties.size.dim);
},
};
return this.size;
}
/// growable implicitly requires the root `Container` to have a set a size property to the size of the available terminal screen
fn grow_resize(this: *@This(), max_size: Point) void {
fn grow_resize(this: *@This(), model: *const Model, max_size: Point) void {
const layout = this.properties.layout;
var remainder = switch (layout.direction) {
.horizontal => max_size.x -| (Layout.getAbsolutePadding(layout.padding.left, this.size.x) + Layout.getAbsolutePadding(layout.padding.right, this.size.x)),
@@ -737,23 +764,31 @@ pub fn Container(comptime Event: type) type {
const sides = this.properties.border.sides;
switch (layout.direction) {
.horizontal => {
.vertical => {
if (sides.top) {
available -|= 1;
remainder -|= 1;
}
if (sides.bottom) {
available -|= 1;
remainder -|= 1;
}
},
.vertical => {
if (sides.left) {
available -|= 1;
remainder -|= 1;
}
if (sides.right) {
available -|= 1;
}
},
.horizontal => {
if (sides.top) {
available -|= 1;
}
if (sides.bottom) {
available -|= 1;
}
if (sides.left) {
remainder -|= 1;
}
if (sides.right) {
remainder -|= 1;
}
},
@@ -774,21 +809,21 @@ pub fn Container(comptime Event: type) type {
if (growable_children == 0) first_growable_child = child;
growable_children += 1;
},
.horizontal => if (layout.direction == .horizontal) {
.horizontal, .horizontal_only => if (layout.direction == .horizontal) {
if (growable_children == 0) first_growable_child = child;
growable_children += 1;
},
.vertical => if (layout.direction == .vertical) {
.vertical, .vertical_only => if (layout.direction == .vertical) {
if (growable_children == 0) first_growable_child = child;
growable_children += 1;
},
}
// non layout direction side growth
switch (layout.direction) {
.horizontal => if (child.properties.size.grow == .vertical or child.properties.size.grow == .both) {
.horizontal => if (child.properties.size.grow == .vertical or child.properties.size.grow == .vertical_only or child.properties.size.grow == .both) {
child.size.y = available;
},
.vertical => if (child.properties.size.grow == .horizontal or child.properties.size.grow == .both) {
.vertical => if (child.properties.size.grow == .horizontal or child.properties.size.grow == .horizontal_only or child.properties.size.grow == .both) {
child.size.x = available;
},
}
@@ -806,8 +841,8 @@ pub fn Container(comptime Event: type) type {
if (child.properties.size.grow == .fixed) continue;
switch (layout.direction) {
.horizontal => if (child.properties.size.grow == .vertical) continue,
.vertical => if (child.properties.size.grow == .horizontal) continue,
.horizontal => if (child.properties.size.grow == .vertical or child.properties.size.grow == .vertical_only) continue,
.vertical => if (child.properties.size.grow == .horizontal or child.properties.size.grow == .horizontal_only) continue,
}
const size = switch (layout.direction) {
@@ -835,23 +870,23 @@ pub fn Container(comptime Event: type) type {
};
if (child.properties.size.grow != .fixed and child_size == smallest_size) {
switch (layout.direction) {
.horizontal => if (child.properties.size.grow != .vertical) {
.horizontal => if (child.properties.size.grow != .vertical and child.properties.size.grow != .vertical_only) {
child.size.x += size_to_correct;
remainder -|= size_to_correct;
},
.vertical => if (child.properties.size.grow != .horizontal) {
.vertical => if (child.properties.size.grow != .horizontal and child.properties.size.grow != .horizontal_only) {
child.size.y += size_to_correct;
remainder -|= size_to_correct;
},
}
if (overflow > 0) {
switch (layout.direction) {
.horizontal => if (child.properties.size.grow != .vertical) {
.horizontal => if (child.properties.size.grow != .vertical and child.properties.size.grow != .vertical_only) {
child.size.x += 1;
overflow -|= 1;
remainder -|= 1;
},
.vertical => if (child.properties.size.grow != .horizontal) {
.vertical => if (child.properties.size.grow != .horizontal and child.properties.size.grow != .horizontal_only) {
child.size.y += 1;
overflow -|= 1;
remainder -|= 1;
@@ -862,31 +897,57 @@ pub fn Container(comptime Event: type) type {
}
}
this.element.resize(this.size);
for (this.elements.items) |*child| child.grow_resize(child.size);
this.element.resize(model, this.size);
for (this.elements.items) |*child| child.grow_resize(model, child.size);
}
pub fn resize(this: *@This(), size: Point) void {
pub fn resize(this: *@This(), model: *const Model, size: Point) void {
// NOTE assume that this function is only called for the root `Container`
this.size = size;
const fit_size = this.fit_resize();
// if (fit_size.y > size.y or fit_size.x > size.x) @panic("error: cannot render in available space");
switch (this.properties.size.grow) {
.both => this.size = Point.max(size, fit_size),
.fixed => {},
.horizontal => this.size = .{
const fit_size = this.fit_resize(model);
this.size = switch (this.properties.size.grow) {
.both => .max(size, fit_size),
.fixed => fit_size,
.horizontal_only => .{
.x = size.x,
.y = fit_size.y,
},
.vertical_only => .{
.x = fit_size.x,
.y = size.y,
},
.horizontal => .{
.x = @max(size.x, fit_size.x),
.y = size.y,
},
.vertical => this.size = .{
.vertical => .{
.x = size.x,
.y = @max(size.y, fit_size.y),
},
}
this.grow_resize(this.size);
};
this.grow_resize(model, this.size);
}
pub fn handle(this: *const @This(), event: Event) !void {
pub fn minSize(this: *const @This(), model: *const Model, size: Point) Point {
var min_size: Point = .{};
for (this.elements.items) |*child| {
const child_size = child.minSize(model, child.properties.size.dim);
min_size = switch (this.properties.layout.direction) {
.horizontal => .{
.x = child_size.x + min_size.x,
.y = @max(child_size.y, min_size.y),
},
.vertical => .{
.x = @max(child_size.x, min_size.x),
.y = child_size.y + min_size.y,
},
};
}
const element_size = this.element.minSize(model, size);
return .max(element_size, min_size);
}
pub fn handle(this: *const @This(), model: *Model, event: Event) !void {
switch (event) {
.mouse => |mouse| if (mouse.in(this.origin, this.size)) {
// the element receives the mouse event with relative position
@@ -894,17 +955,19 @@ pub fn Container(comptime Event: type) type {
var relative_mouse: input.Mouse = mouse;
relative_mouse.x -= this.origin.x;
relative_mouse.y -= this.origin.y;
try this.element.handle(.{ .mouse = relative_mouse });
for (this.elements.items) |*element| try element.handle(event);
try this.element.handle(model, .{ .mouse = relative_mouse });
},
else => {
try this.element.handle(event);
for (this.elements.items) |*element| try element.handle(event);
.bell => {
// ring the terminal bell and do not propagate the event any further
_ = try terminal.ringBell();
return;
},
else => try this.element.handle(model, event),
}
for (this.elements.items) |*element| try element.handle(model, event);
}
pub fn content(this: *const @This()) ![]Cell {
pub fn content(this: *const @This(), model: *const Model) ![]Cell {
if (this.size.x == 0 or this.size.y == 0) return Error.TooSmall;
const cells = try this.allocator.alloc(Cell, @as(usize, this.size.x) * @as(usize, this.size.y));
@@ -915,7 +978,7 @@ pub fn Container(comptime Event: type) type {
this.properties.border.content(cells, this.size);
this.properties.rectangle.content(cells, this.size);
try this.element.content(cells, this.size);
try this.element.content(model, cells, this.size);
// DEBUG render corresponding corners (except top left) of this `Container` *red*
if (comptime build_options.debug) {
@@ -945,6 +1008,7 @@ const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const build_options = @import("build_options");
const input = @import("input.zig");
const terminal = @import("terminal.zig");
const isTaggedUnion = @import("event.zig").isTaggedUnion;
const Cell = @import("cell.zig");
const Color = @import("color.zig").Color;
@@ -959,8 +1023,9 @@ test {
test "Container Fixed and Grow Size Vertical" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
.layout = .{ .direction = .vertical },
}, .{});
try container.append(try .init(std.testing.allocator, .{
@@ -978,14 +1043,15 @@ test "Container Fixed and Grow Size Vertical" {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/fixed_grow_vertical.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/fixed_grow_vertical.zon"));
}
test "Container Fixed and Grow Size Horizontal" {
const event = @import("event.zig");
const testing = @import("testing.zig");
const Model = struct {};
var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{}, .{});
var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{}, .{});
try container.append(try .init(std.testing.allocator, .{
.size = .{
.dim = .{ .x = 5 },
@@ -1001,5 +1067,5 @@ test "Container Fixed and Grow Size Horizontal" {
try testing.expectContainerScreen(.{
.y = 20,
.x = 30,
}, &container, @import("test/container/fixed_grow_horizontal.zon"));
}, @TypeOf(container), &container, Model, @import("test/container/fixed_grow_horizontal.zon"));
}

View File

@@ -50,6 +50,7 @@ pub const home = "\x1b[H";
pub const cup = "\x1b[{d};{d}H";
pub const hide_cursor = "\x1b[?25l";
pub const show_cursor = "\x1b[?25h";
pub const reset_cursor_shape = "\x1b[0 q";
pub const cursor_shape = "\x1b[{d} q";
pub const ri = "\x1bM";
pub const ind = "\n";
@@ -139,5 +140,6 @@ pub const osc10_reset = "\x1b]110\x1b\\"; // reset fg to terminal default
pub const osc11_query = "\x1b]11;?\x1b\\"; // bg
pub const osc11_set = "\x1b]11;rgb:{x:0>2}{x:0>2}/{x:0>2}{x:0>2}/{x:0>2}{x:0>2}\x1b\\"; // set default terminal bg
pub const osc11_reset = "\x1b]111\x1b\\"; // reset bg to terminal default
pub const osc12_set = "\x1b]12;{s}\x1b\\"; // set the cursor color through the name of the 8 base colors!
pub const osc12_query = "\x1b]12;?\x1b\\"; // cursor color
pub const osc12_reset = "\x1b]112\x1b\\"; // reset cursor to terminal default

File diff suppressed because it is too large Load Diff

View File

@@ -9,7 +9,13 @@ pub const SystemEvent = union(enum) {
/// Quit event to signify the end of the event loop (rendering should stop afterwards)
quit,
/// Resize event to signify that the application should re-draw to resize
///
/// Usually no `Container` nor `Element` should act on that event, as it
/// only serves for event based loops to force a re-draw with a new `Event`.
resize,
/// Ring the terminal bell to notify the user. This `Event` is handled by
/// every `Container` and will not be passed through the container tree.
bell,
/// Error event to notify other containers about a recoverable error
err: struct {
/// actual error
@@ -17,19 +23,25 @@ pub const SystemEvent = union(enum) {
/// associated error message
msg: []const u8,
},
/// Input line event received in non *raw mode* (instead of individual `key` events)
line: []const u8,
/// Input key event received from the user
key: Key,
/// Mouse input event
mouse: Mouse,
/// Focus event for mouse interaction
/// TODO this should instead be a union with a `Size` to derive which container / element the focus meant for
/// Focus event indicating that the application has gained the focus of the user
focus: bool,
};
/// Merge the two provided `union(enum)` `A` and `B` to a tagged union containing all fields of both tagged unions in `comptime`.
/// Declarations are not supported for `comptime` created types, see https://github.com/ziglang/zig/issues/6709 for details.
pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
if (!isTaggedUnion(A) or !isTaggedUnion(B)) {
@compileError("Both types for merging tagged unions need to be of type `union(enum)`.");
}
// TODO maybe it makes sense to have a nested tagged union type (i.e. system: union(enum) and event: union(enum))
// - allows re-definition of system / built-in events
// - clearly shows which events are system / built-in ones and which are user defined events
// - the memory footprint for the nesting is not really harmful
if (!isTaggedUnion(A) or !isTaggedUnion(B)) @compileError("Both types for merging tagged unions need to be of type `union(enum)`.");
const a_fields = @typeInfo(A).@"union".fields;
const a_fields_tag = @typeInfo(A).@"union".tag_type.?;
const a_enum_fields = @typeInfo(a_fields_tag).@"enum".fields;
@@ -54,11 +66,27 @@ pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
i += 1;
}
const log2_i = @bitSizeOf(@TypeOf(i)) - @clz(i);
// NOTE declarations are not supported for `comptime` types: https://github.com/ziglang/zig/issues/6709
// -> will lead to a compilation error when constructing the tagged union
// at the end of this function in case at least one of the provided tagged
// unions to merge contains declarations (which in this case can only be the
// user provided one)
const a_enum_decls = @typeInfo(A).@"union".decls;
const b_enum_decls = @typeInfo(B).@"union".decls;
var decls: [a_enum_decls.len + b_enum_decls.len]std.builtin.Type.Declaration = undefined;
var j: usize = 0;
for (a_enum_decls) |decl| {
decls[j] = decl;
j += 1;
}
for (b_enum_decls) |decl| {
decls[j] = decl;
j += 1;
}
const EventType = @Type(.{ .int = .{
.signedness = .unsigned,
.bits = log2_i,
.bits = @bitSizeOf(@TypeOf(i)) - @clz(i),
} });
const Event = @Type(.{ .@"enum" = .{
@@ -76,19 +104,25 @@ pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
} });
}
// Determine at `comptime` whether the provided type `E` is an `union(enum)`.
pub fn isTaggedUnion(comptime E: type) bool {
switch (@typeInfo(E)) {
.@"union" => |u| {
if (u.tag_type) |_| {} else {
return false;
}
/// Determine whether the provided type `T` is a tagged union: `union(enum)`.
pub fn isTaggedUnion(comptime T: type) bool {
switch (@typeInfo(T)) {
.@"union" => |u| if (u.tag_type) |_| {} else {
return false;
},
else => return false,
}
return true;
}
/// Determine whether the provided type `T` is a `struct`.
pub fn isStruct(comptime T: type) bool {
return switch (@typeInfo(T)) {
.@"struct" => true,
else => false,
};
}
const std = @import("std");
const input = @import("input.zig");
const terminal = @import("terminal.zig");

View File

@@ -65,7 +65,30 @@ pub const Key = packed struct {
return meta.eql(this, other);
}
// TODO might be useful to use the std.ascii stuff!
/// Determine if the `Key` is an unicode character that can be printed to
/// the screen. This means that the code point of the `Key` is an ascii
/// character between 32 - 255 (with the exception of 127 = Delete) and no
/// modifiers (alt and/or ctrl) are used.
///
/// # Example
///
/// Get user input's from the .key event from the application event loop:
///
/// ```zig
/// switch (event) {
/// .key => |key| if (key.isUnicode()) try this.input.append(key.cp),
/// else => {},
/// }
/// ```
pub fn isUnicode(this: @This()) bool {
return this.mod.alt == false and this.mod.ctrl == false and // no modifier keys
(this.cp >= 32 and this.cp <= 126 or // ascii printable characters (except for input.Delete)
this.cp >= 128 and this.cp <= 255) or // extended ascii codes
((this.cp >= 0x0080 and this.cp <= 0x07FF) or
(this.cp >= 0x0800 and this.cp <= 0xFFFF) or
(this.cp >= 0x100000 and this.cp <= 0x10FFFF)) and // allowed unicode character ranges (2 - 4 byte characters)
(this.cp < 57348 or this.cp > 57454); // no other predefined meanings (i.e. arrow keys, etc.)
}
/// Determine if the `Key` is an ascii character that can be printed to
/// the screen. This means that the code point of the `Key` is an ascii

View File

@@ -1,7 +1,7 @@
// taken from https://github.com/rockorager/libvaxis/blob/main/src/queue.zig (MIT-License)
// with slight modifications
/// Queue implementation. Thread safe. Fixed size. Blocking push and pop. Polling through tryPop and tryPush.
/// Queue implementation. Thread safe. Fixed size. _Blocking_ `push` and `pop`. _Polling_ through `tryPop` and `tryPush`.
pub fn Queue(comptime T: type, comptime size: usize) type {
return struct {
buf: [size]T = undefined,
@@ -215,7 +215,7 @@ fn sleepyPop(q: *Queue(u8, 2)) !void {
// still full and the push in the other thread is still blocked
// waiting for space.
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.
try testing.expectEqual(1, q.pop());
@@ -225,7 +225,7 @@ fn sleepyPop(q: *Queue(u8, 2)) !void {
try Thread.yield();
// But we want to ensure that there's a second push waiting, so
// 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...
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
// spurious and go back to sleep.
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.
try testing.expectEqual(2, q.pop());
@@ -270,14 +270,14 @@ test "Fill, block, fill, block" {
fn sleepyPush(q: *Queue(u8, 1)) !void {
// Try to ensure the other thread has already started trying to pop.
try Thread.yield();
std.time.sleep(std.time.ns_per_s / 2);
std.Thread.sleep(std.time.ns_per_s / 2);
// Spurious wake
q.not_full.signal();
q.not_empty.signal();
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.
q.push(1);
@@ -286,7 +286,7 @@ fn sleepyPush(q: *Queue(u8, 1)) !void {
try Thread.yield();
// Give the other thread time to block again.
try Thread.yield();
std.time.sleep(std.time.ns_per_s / 2);
std.Thread.sleep(std.time.ns_per_s / 2);
// Spurious wake
q.not_full.signal();
@@ -317,7 +317,7 @@ test "2 readers" {
const t1 = try Thread.spawn(cfg, readerThread, .{&queue});
const t2 = try Thread.spawn(cfg, readerThread, .{&queue});
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);
t1.join();

View File

@@ -58,10 +58,10 @@ pub const Buffered = struct {
}
/// Render provided cells at size (anchor and dimension) into the *virtual screen*.
pub fn render(this: *@This(), comptime T: type, container: *T) !void {
pub fn render(this: *@This(), comptime Container: type, container: *Container, comptime Model: type, model: *const Model) !void {
const size: Point = container.size;
const origin: Point = container.origin;
const cells: []const Cell = try container.content();
const cells: []const Cell = try container.content(model);
if (cells.len == 0) return;
@@ -80,7 +80,7 @@ pub const Buffered = struct {
// free immediately
container.allocator.free(cells);
for (container.elements.items) |*element| try this.render(T, element);
for (container.elements.items) |*element| try this.render(Container, element, Model, model);
}
/// Write *virtual screen* to alternate screen (should be called once and last during each render loop iteration in the main loop).
@@ -88,9 +88,10 @@ pub const Buffered = struct {
try terminal.hideCursor();
// TODO measure timings of rendered frames?
var cursor_position: ?Point = null;
const writer = terminal.writer();
var writer = terminal.writer();
const s = this.screen;
const vs = this.virtual_screen;
for (0..this.size.y) |row| {
for (0..this.size.x) |col| {
const idx = (row * this.size.x) + col;
@@ -104,13 +105,14 @@ pub const Buffered = struct {
.x = @truncate(col),
.y = @truncate(row),
};
try cvs.style.set_cursor_style(&writer);
}
if (cs.eql(cvs)) continue;
// render differences found in virtual screen
try terminal.setCursorPosition(.{ .y = @truncate(row), .x = @truncate(col) });
try cvs.value(writer);
try cvs.value(&writer);
// update screen to be the virtual screen for the next frame
s[idx] = vs[idx];
}
@@ -122,6 +124,85 @@ pub const Buffered = struct {
}
};
pub const Direct = struct {
gpa: Allocator,
size: Point,
resized: bool,
screen: []Cell,
pub fn init(gpa: Allocator) @This() {
return .{
.gpa = gpa,
.size = .{},
.resized = true,
.screen = undefined,
};
}
pub fn deinit(this: *@This()) void {
this.gpa.free(this.screen);
}
pub fn resize(this: *@This()) !Point {
this.size = .{};
if (!this.resized) {
this.gpa.free(this.screen);
this.screen = undefined;
}
this.resized = true;
return terminal.getTerminalSize();
}
pub fn clear(this: *@This()) !void {
_ = this;
try terminal.clearScreen();
}
/// Render provided cells at size (anchor and dimension) into the *screen*.
pub fn render(this: *@This(), comptime Container: type, container: *Container, comptime Model: type, model: *const Model) !void {
const size: Point = container.size;
const origin: Point = container.origin;
if (this.resized) {
this.size = size;
const n = @as(usize, this.size.x) * @as(usize, this.size.y);
this.screen = try this.gpa.alloc(Cell, n);
@memset(this.screen, .{});
this.resized = false;
}
const cells: []const Cell = try container.content(model);
var idx: usize = 0;
var vs = this.screen;
const anchor: usize = (@as(usize, origin.y) * @as(usize, this.size.x)) + @as(usize, origin.x);
blk: for (0..size.y) |row| {
for (0..size.x) |col| {
vs[anchor + (row * this.size.x) + col] = cells[idx];
idx += 1;
if (cells.len == idx) break :blk;
}
}
// free immediately
container.allocator.free(cells);
for (container.elements.items) |*element| try this.render(Container, element, Model, model);
}
pub fn flush(this: *@This()) !void {
var writer = terminal.writer();
for (0..this.size.y) |row| {
for (0..this.size.x) |col| {
const idx = (row * this.size.x) + col;
const cvs = this.screen[idx];
try cvs.value(&writer);
if (cvs.style.cursor) return; // that's where the cursor should be left!
}
}
}
};
const std = @import("std");
const meta = std.meta;
const assert = std.debug.assert;

View File

@@ -12,6 +12,8 @@ fg: Color = .default,
bg: Color = .default,
ul: Color = .default,
cursor: bool = false,
cursor_color: Color = .default,
cursor_shape: CursorShape = .default,
ul_style: Underline = .off,
emphasis: []const Emphasis,
@@ -36,33 +38,51 @@ pub const Emphasis = enum(u8) {
strikethrough,
};
pub const CursorShape = enum(u4) {
default = 0,
block_blinking = 1,
block_steady,
underline_blinking,
underline_steady,
bar_blinking,
bar_steady,
};
pub fn eql(this: Style, other: Style) bool {
// TODO should there be a compare for every field?
return meta.eql(this, other);
}
// TODO might be useful to use the std.ascii stuff!
pub fn set_cursor_style(this: Style, writer: *std.Io.Writer) !void {
if (!this.cursor) return;
switch (this.cursor_color) {
.default => try writer.print(ctlseqs.osc12_reset, .{}),
else => try writer.print(ctlseqs.osc12_set, .{@tagName(this.cursor_color)}),
}
try writer.print(ctlseqs.cursor_shape, .{@intFromEnum(this.cursor_shape)});
}
pub fn value(this: Style, writer: anytype, cp: u21) !void {
pub fn value(this: Style, writer: *std.Io.Writer, cp: u21) !void {
var buffer: [4]u8 = undefined;
const bytes = try unicode.utf8Encode(cp, &buffer);
assert(bytes > 0);
// build ansi sequence for 256 colors ...
// foreground
try format(writer, "\x1b[", .{});
try writer.printAscii("\x1b[", .{});
try this.fg.write(writer, .fg);
// background
try format(writer, ";", .{});
try writer.printAsciiChar(';', .{});
try this.bg.write(writer, .bg);
// underline
// FIX assert that if the underline property is set that the ul style and the attribute for underlining is available
try format(writer, ";", .{});
try writer.printAsciiChar(';', .{});
try this.ul.write(writer, .ul);
// append styles (aka attributes like bold, italic, strikethrough, etc.)
for (this.emphasis) |attribute| try format(writer, ";{d}", .{@intFromEnum(attribute)});
try format(writer, "m", .{});
for (this.emphasis) |attribute| try writer.print(";{d}", .{@intFromEnum(attribute)});
try writer.printAsciiChar('m', .{});
// content
try format(writer, "{s}", .{buffer[0..bytes]});
try format(writer, "\x1b[0m", .{});
try writer.printAscii(buffer[0..bytes], .{});
try writer.printAscii("\x1b[0m", .{});
}
// TODO implement helper functions for terminal capabilities:
@@ -73,6 +93,6 @@ const std = @import("std");
const unicode = std.unicode;
const meta = std.meta;
const assert = std.debug.assert;
const format = std.fmt.format;
const Color = @import("color.zig").Color;
const ctlseqs = @import("ctlseqs.zig");
const Style = @This();

View File

@@ -42,6 +42,11 @@ pub fn showCursor() !void {
_ = try posix.write(posix.STDIN_FILENO, ctlseqs.show_cursor);
}
pub fn resetCursor() !void {
_ = try posix.write(posix.STDIN_FILENO, ctlseqs.reset_cursor_shape);
_ = try posix.write(posix.STDIN_FILENO, ctlseqs.osc12_reset);
}
pub fn setCursorPositionHome() !void {
_ = try posix.write(posix.STDIN_FILENO, ctlseqs.home);
}
@@ -54,6 +59,10 @@ pub fn disableMouseSupport() !void {
_ = try posix.write(posix.STDIN_FILENO, ctlseqs.mouse_reset);
}
pub fn ringBell() !void {
_ = try posix.write(posix.STDIN_FILENO, &.{7});
}
pub fn read(buf: []u8) !usize {
return try posix.read(posix.STDIN_FILENO, buf);
}
@@ -62,19 +71,25 @@ pub fn write(buf: []const u8) !usize {
return try posix.write(posix.STDIN_FILENO, buf);
}
fn contextWrite(context: @This(), data: []const u8) anyerror!usize {
_ = context;
return try posix.write(posix.STDOUT_FILENO, data);
fn drainFn(w: *std.Io.Writer, data: []const []const u8, splat: usize) error{WriteFailed}!usize {
_ = w;
if (data.len == 0 or splat == 0) return 0;
var len: usize = 0;
for (data) |bytes| len += posix.write(posix.STDOUT_FILENO, bytes) catch return error.WriteFailed;
return len;
}
const Writer = std.io.Writer(
@This(),
anyerror,
contextWrite,
);
pub fn writer() Writer {
return .{ .context = .{} };
// TODO I now need to add that much, for just the one function above?
pub fn writer() std.Io.Writer {
return .{
.vtable = &.{
.drain = drainFn,
.flush = std.Io.Writer.noopFlush,
},
.buffer = &.{},
};
}
pub fn setCursorPosition(pos: Point) !void {
@@ -83,7 +98,7 @@ pub fn setCursorPosition(pos: Point) !void {
_ = try posix.write(posix.STDIN_FILENO, value);
}
pub fn getCursorPosition() !Size.Position {
pub fn getCursorPosition() !Size {
// Needs Raw mode (no wait for \n) to work properly cause
// control sequence will not be written without it.
_ = try posix.write(posix.STDIN_FILENO, "\x1b[6n");
@@ -191,7 +206,7 @@ pub fn enableRawMode(bak: *posix.termios) !void {
}
/// Reverts `enableRawMode` to restore initial functionality.
pub fn disableRawMode(bak: *posix.termios) !void {
pub fn disableRawMode(bak: *const posix.termios) !void {
try posix.tcsetattr(
posix.STDIN_FILENO,
.FLUSH,
@@ -232,7 +247,7 @@ const log = std.log.scoped(.terminal);
const std = @import("std");
const mem = std.mem;
const posix = std.posix;
const code_point = @import("code_point");
const assert = std.debug.assert;
const ctlseqs = @import("ctlseqs.zig");
const input = @import("input.zig");
const Key = input.Key;

View File

@@ -34,10 +34,10 @@ pub const Renderer = struct {
@memset(this.screen, .{});
}
pub fn render(this: *@This(), comptime T: type, container: *const T) !void {
pub fn render(this: *@This(), comptime T: type, container: *const T, comptime Model: type, model: *const Model) !void {
const size: Point = container.size;
const origin: Point = container.origin;
const cells: []const Cell = try container.content();
const cells: []const Cell = try container.content(model);
if (cells.len == 0) return;
@@ -58,7 +58,7 @@ pub const Renderer = struct {
// free immediately
container.allocator.free(cells);
for (container.elements.items) |*element| try this.render(T, element);
for (container.elements.items) |*element| try this.render(T, element, Model, model);
}
pub fn save(this: @This(), writer: anytype) !void {
@@ -74,6 +74,8 @@ pub const Renderer = struct {
/// Create a .zon file containing the expected `Cell` slice using the `zterm.testing.Renderer.save` method:
///
/// ```zig
/// const Model = struct {};
/// var model: Model = .{};
/// const file = try std.fs.cwd().createFile("test/container/border/all.zon", .{ .truncate = true });
/// defer file.close();
///
@@ -81,8 +83,8 @@ pub const Renderer = struct {
/// var renderer: testing.Renderer = .init(allocator, size);
/// defer renderer.deinit();
///
/// try container.handle(.{ .size = size });
/// try renderer.render(Container(event.SystemEvent), &container);
/// try container.handle(&model, .{ .size = size });
/// try renderer.render(@TypeOf(container), &container, Model, &.{});
/// try renderer.save(file.writer());
/// ```
///
@@ -91,7 +93,8 @@ pub const Renderer = struct {
/// Then later load that .zon file at compile time and run your test against this `Cell` slice.
///
/// ```zig
/// var container: Container(event.SystemEvent) = try .init(std.testing.allocator, .{
/// const Model = struct {};
/// var container: Container(Model, event.SystemEvent) = try .init(std.testing.allocator, .{
/// .border = .{
/// .color = .green,
/// .sides = .all,
@@ -102,20 +105,57 @@ pub const Renderer = struct {
/// try testing.expectContainerScreen(.{
/// .rows = 20,
/// .cols = 30,
/// }, &container, @import("test/container/border.all.zon"));
/// }, @TypeOf(container), &container, Model, @import("test/container/border.all.zon"));
/// ```
pub fn expectContainerScreen(size: Point, container: *Container(event.SystemEvent), expected: []const Cell) !void {
pub fn expectContainerScreen(size: Point, comptime T: type, container: *T, comptime Model: type, expected: []const Cell) !void {
const allocator = testing.allocator;
var renderer: Renderer = .init(allocator, size);
defer renderer.deinit();
container.resize(size);
container.reposition(.{});
try renderer.render(Container(event.SystemEvent), container);
const model: Model = .{};
container.resize(&model, size);
container.reposition(&model, .{});
try renderer.render(T, container, Model, &model);
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
/// provided cell arrays are identical. Usually the `Cell` slices are
/// the contents of a given screen from the `zterm.testing.Renderer`. See
@@ -127,28 +167,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));
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);
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);
defer output.deinit();
var allocating_writer = std.Io.Writer.Allocating.init(allocator);
defer allocating_writer.deinit();
var buffer = std.io.bufferedWriter(output.writer());
defer buffer.flush() catch {};
const writer = buffer.writer();
var writer = &allocating_writer.writer;
var differ = false;
const dwd = try DisplayWidth.DisplayWidthData.init(allocator);
defer dwd.deinit();
const dw: DisplayWidth = .{ .data = &dwd };
const expected_centered = try dw.center(allocator, "Expected Screen", size.x, " ");
const expected_centered = try center(allocator, "Expected Screen", size.x, " ");
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);
try writer.print("Screens are not equivalent.\n{s} ┆ {s}\n", .{ expected_centered, actual_centered });
@@ -164,8 +197,8 @@ pub fn expectEqualCells(origin: Point, size: Point, expected: []const Cell, actu
if (!expected_cell.eql(actual_cell)) differ = true;
try expected_cps.append(expected_cell);
try actual_cps.append(actual_cell);
try expected_cps.append(allocator, expected_cell);
try actual_cps.append(allocator, actual_cell);
}
// write screens both formatted to buffer
@@ -178,13 +211,16 @@ pub fn expectEqualCells(origin: Point, size: Point, expected: []const Cell, actu
if (!differ) return;
// test failed
try buffer.flush();
var buf: [1024]u8 = undefined;
debug.lockStdErr();
defer debug.unlockStdErr();
std.debug.lockStdErr();
defer std.debug.unlockStdErr();
var buffer = std.fs.File.stderr().writer(&buf);
var error_writer = &buffer.interface;
try error_writer.writeAll(writer.buffer[0..writer.end]);
try error_writer.flush();
const std_writer = std.io.getStdErr().writer();
try std_writer.writeAll(output.items);
return error.TestExpectEqualCells;
}
@@ -195,5 +231,4 @@ const Allocator = std.mem.Allocator;
const event = @import("event.zig");
const Container = @import("container.zig").Container;
const Cell = @import("cell.zig");
const DisplayWidth = @import("DisplayWidth");
const Point = @import("point.zig").Point;