Compare commits
15 Commits
19d1602d3b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
6781c43fcc
|
|||
| eb36c7c410 | |||
| 4441f1b933 | |||
| 39fb8af174 | |||
|
bfbe75f8d3
|
|||
|
89517b2546
|
|||
|
a71d808250
|
|||
|
97a240c54d
|
|||
| c29c60bd89 | |||
| 4a3bec3edc | |||
|
836d7669e5
|
|||
| 1621715ad8 | |||
| 4874252e8c | |||
| e1e8907848 | |||
| 1cb7fca701 |
@@ -15,9 +15,9 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Setup zig installation
|
- name: Setup zig installation
|
||||||
uses: mlugg/setup-zig@v2
|
uses: https://codeberg.org/mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: master
|
version: latest
|
||||||
- name: Lint check
|
- name: Lint check
|
||||||
run: zig fmt --check .
|
run: zig fmt --check .
|
||||||
- name: Spell checking
|
- name: Spell checking
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ jobs:
|
|||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
- name: Setup zig installation
|
- name: Setup zig installation
|
||||||
uses: mlugg/setup-zig@v2
|
uses: https://codeberg.org/mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: master
|
version: latest
|
||||||
- name: Lint check
|
- name: Lint check
|
||||||
run: zig fmt --check --exclude src/test .
|
run: zig fmt --check --exclude src/test .
|
||||||
- name: Spell checking
|
- name: Spell checking
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
`zterm` is a terminal user interface library (*tui*) to implement terminal (fullscreen or inline) applications.
|
`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
|
## Demo
|
||||||
|
|
||||||
Clone this repository and run `zig build --help` to see the available examples. Run a given example as follows:
|
Clone this repository and run `zig build --help` to see the available examples. Run a given example as follows:
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ pub fn build(b: *std.Build) void {
|
|||||||
palette,
|
palette,
|
||||||
// error handling
|
// error handling
|
||||||
errors,
|
errors,
|
||||||
|
// non alternate screen applications
|
||||||
|
direct,
|
||||||
};
|
};
|
||||||
|
|
||||||
const example = b.option(Examples, "example", "Example to build and/or run. (default: all)") orelse .all;
|
const example = b.option(Examples, "example", "Example to build and/or run. (default: all)") orelse .all;
|
||||||
@@ -73,6 +75,8 @@ pub fn build(b: *std.Build) void {
|
|||||||
.palette => "examples/styles/palette.zig",
|
.palette => "examples/styles/palette.zig",
|
||||||
// error handling
|
// error handling
|
||||||
.errors => "examples/errors.zig",
|
.errors => "examples/errors.zig",
|
||||||
|
// non-alternate screen
|
||||||
|
.direct => "examples/direct.zig",
|
||||||
.all => unreachable, // should never happen
|
.all => unreachable, // should never happen
|
||||||
}),
|
}),
|
||||||
.target = target,
|
.target = target,
|
||||||
|
|||||||
@@ -133,10 +133,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -168,7 +165,7 @@ pub fn main() !void {
|
|||||||
}, spinner.element());
|
}, spinner.element());
|
||||||
try container.append(nested_container);
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
var framerate: u64 = 60;
|
var framerate: u64 = 60;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const QuitText = struct {
|
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 {
|
pub fn element(this: *@This()) App.Element {
|
||||||
return .{ .ptr = this, .vtable = &.{ .content = content } };
|
return .{ .ptr = this, .vtable = &.{ .content = content } };
|
||||||
@@ -31,10 +31,8 @@ pub fn main() !void {
|
|||||||
defer if (gpa.deinit() == .leak) log.err("memory leak", .{});
|
defer if (gpa.deinit() == .leak) log.err("memory leak", .{});
|
||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -135,7 +133,7 @@ pub fn main() !void {
|
|||||||
}, .{}));
|
}, .{}));
|
||||||
defer container.deinit(); // also de-initializes the children
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
@@ -151,9 +149,9 @@ pub fn main() !void {
|
|||||||
if (key.eql(.{ .cp = 'n', .mod = .{ .ctrl = true } })) {
|
if (key.eql(.{ .cp = 'n', .mod = .{ .ctrl = true } })) {
|
||||||
try app.interrupt();
|
try app.interrupt();
|
||||||
renderer.size = .{}; // reset size, such that next resize will cause a full re-draw!
|
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");
|
defer app.start(.full) catch @panic("could not start app event loop");
|
||||||
var child = std.process.Child.init(&.{"hx"}, allocator);
|
var child = std.process.Child.init(&.{"vim"}, allocator);
|
||||||
_ = child.spawnAndWait(threaded_io.io()) catch |err| app.postEvent(.{
|
_ = child.spawnAndWait() catch |err| app.postEvent(.{
|
||||||
.err = .{
|
.err = .{
|
||||||
.err = err,
|
.err = err,
|
||||||
.msg = "Spawning $EDITOR failed",
|
.msg = "Spawning $EDITOR failed",
|
||||||
|
|||||||
192
examples/direct.zig
Normal file
192
examples/direct.zig
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
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(gpa, .{}, .{});
|
||||||
|
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 draw the character with the ctrl indication and a newline to make sure the rendering stays consistent
|
||||||
|
.cancel => try renderer.writeCtrlDWithNewline(),
|
||||||
|
.line => |line| {
|
||||||
|
defer gpa.free(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) {});
|
||||||
@@ -32,10 +32,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -58,7 +55,7 @@ pub fn main() !void {
|
|||||||
var alignment: App.Alignment = .init(quit_container, .center);
|
var alignment: App.Alignment = .init(quit_container, .center);
|
||||||
try container.append(try .init(allocator, .{}, alignment.element()));
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -82,10 +82,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -105,7 +102,7 @@ pub fn main() !void {
|
|||||||
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .lightgrey } }, 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 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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -65,10 +65,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -115,7 +112,7 @@ pub fn main() !void {
|
|||||||
}, second_mouse_draw.element()));
|
}, second_mouse_draw.element()));
|
||||||
try container.append(nested_container);
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -88,7 +85,7 @@ pub fn main() !void {
|
|||||||
});
|
});
|
||||||
try container.append(try App.Container.init(allocator, .{}, progress.element()));
|
try container.append(try App.Container.init(allocator, .{}, progress.element()));
|
||||||
}
|
}
|
||||||
try app.start();
|
try app.start(.full);
|
||||||
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
var framerate: u64 = 60;
|
var framerate: u64 = 60;
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -54,7 +51,7 @@ pub fn main() !void {
|
|||||||
try container.append(try .init(allocator, .{}, radiobutton.element()));
|
try container.append(try .init(allocator, .{}, radiobutton.element()));
|
||||||
try container.append(try App.Container.init(allocator, .{ .rectangle = .{ .fill = .black } }, button.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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -65,10 +65,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -154,7 +151,7 @@ pub fn main() !void {
|
|||||||
var scrollable_bottom: App.Scrollable = .init(bottom_box, .enabled(.white, true));
|
var scrollable_bottom: App.Scrollable = .init(bottom_box, .enabled(.white, true));
|
||||||
try container.append(try App.Container.init(allocator, .{}, scrollable_bottom.element()));
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -55,7 +52,7 @@ pub fn main() !void {
|
|||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
try container.append(try .init(allocator, .{}, selection.element()));
|
try container.append(try .init(allocator, .{}, selection.element()));
|
||||||
|
|
||||||
try app.start();
|
try app.start(.full);
|
||||||
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -97,10 +97,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -119,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, .{}, info_text.element()));
|
||||||
try container.append(try App.Container.init(allocator, .{}, error_notification.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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
@@ -35,10 +35,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -72,7 +69,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
defer container.deinit(); // also de-initializes the children
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -35,10 +35,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -64,7 +61,7 @@ pub fn main() !void {
|
|||||||
}, .{}));
|
}, .{}));
|
||||||
defer container.deinit(); // also de-initializes the children
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -35,10 +35,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -80,7 +77,7 @@ pub fn main() !void {
|
|||||||
}
|
}
|
||||||
defer container.deinit(); // also de-initializes the children
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -35,10 +35,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -63,7 +60,7 @@ pub fn main() !void {
|
|||||||
}, .{}));
|
}, .{}));
|
||||||
defer container.deinit(); // also de-initializes the children
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
@@ -32,10 +32,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -61,7 +58,7 @@ pub fn main() !void {
|
|||||||
var scrollable: App.Scrollable = .init(box, .disabled);
|
var scrollable: App.Scrollable = .init(box, .disabled);
|
||||||
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|||||||
@@ -178,10 +178,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var threaded_io: std.Io.Threaded = .init(allocator, .{});
|
var app: App = .init(allocator, .{}, .{});
|
||||||
defer threaded_io.deinit();
|
|
||||||
|
|
||||||
var app: App = .init(threaded_io.ioBasic(), .{});
|
|
||||||
var renderer = zterm.Renderer.Buffered.init(allocator);
|
var renderer = zterm.Renderer.Buffered.init(allocator);
|
||||||
defer renderer.deinit();
|
defer renderer.deinit();
|
||||||
|
|
||||||
@@ -212,7 +209,7 @@ pub fn main() !void {
|
|||||||
}, text_styles.element()), .enabled(.white, true));
|
}, text_styles.element()), .enabled(.white, true));
|
||||||
try container.append(try App.Container.init(allocator, .{}, scrollable.element()));
|
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});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
// event loop
|
// event loop
|
||||||
|
|||||||
676
src/app.zig
676
src/app.zig
@@ -17,86 +17,124 @@
|
|||||||
/// );
|
/// );
|
||||||
/// // later on create an `App` instance and start the event loop
|
/// // later on create an `App` instance and start the event loop
|
||||||
/// var app: App = .init(io, .{}); // provide instance of the `std.Io` and `App` model that shall be used
|
/// var app: App = .init(io, .{}); // provide instance of the `std.Io` and `App` model that shall be used
|
||||||
/// try app.start();
|
/// try app.start(.full);
|
||||||
/// defer app.stop() catch unreachable; // does not clean-up the resources used in the model
|
/// defer app.stop() catch unreachable; // does not clean-up the resources used in the model
|
||||||
/// ```
|
/// ```
|
||||||
pub fn App(comptime M: type, comptime E: type) type {
|
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 (!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)`.");
|
if (!isTaggedUnion(E)) @compileError("Provided user event `E` for `App(comptime M: type, comptime E: type)` is not of type `union(enum)`.");
|
||||||
return struct {
|
return struct {
|
||||||
|
gpa: Allocator,
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
model: Model,
|
model: Model,
|
||||||
queue: Queue,
|
queue: Queue,
|
||||||
future: ?std.Io.Future(@typeInfo(@typeInfo(@TypeOf(run)).@"fn".return_type.?).error_union.error_set!void) = null,
|
thread: ?Thread = null,
|
||||||
|
quit_event: Thread.ResetEvent,
|
||||||
termios: ?posix.termios = null,
|
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
|
// global variable for the registered handler for WINCH
|
||||||
var handler_ctx: *anyopaque = undefined;
|
var handler_ctx: *anyopaque = undefined;
|
||||||
/// registered WINCH handler to report resize events
|
/// registered WINCH handler to report resize events
|
||||||
fn handleWinch(_: std.os.linux.SIG) 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));
|
const this: *@This() = @ptrCast(@alignCast(handler_ctx));
|
||||||
// NOTE this does not have to be done if in-band resize events are supported
|
// 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!
|
// -> the signal might not work correctly when hosting the application over ssh!
|
||||||
this.postEvent(.resize);
|
this.postEvent(.resize);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(io: std.Io, model: Model) @This() {
|
pub fn init(gpa: Allocator, io: std.Io, model: Model) @This() {
|
||||||
return .{
|
return .{
|
||||||
|
.gpa = gpa,
|
||||||
.io = io,
|
.io = io,
|
||||||
.model = model,
|
.model = model,
|
||||||
.queue = .{},
|
.queue = .{},
|
||||||
|
.quit_event = .{},
|
||||||
|
.config = undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(this: *@This()) !void {
|
pub fn start(this: *@This(), config: TerminalConfiguration) !void {
|
||||||
if (this.future) |_| return;
|
this.config = config;
|
||||||
|
if (this.thread) |_| return;
|
||||||
|
|
||||||
// post init event (as the very first element to be in the queue - event loop)
|
// post init event (as the very first element to be in the queue - event loop)
|
||||||
this.postEvent(.init);
|
this.postEvent(.init);
|
||||||
|
|
||||||
if (!this.winch_registered) {
|
if (!this.handler_registered) {
|
||||||
handler_ctx = this;
|
handler_ctx = this;
|
||||||
var act = posix.Sigaction{
|
posix.sigaction(posix.SIG.WINCH, &.{
|
||||||
.handler = .{ .handler = handleWinch },
|
.handler = .{ .handler = handleWinch },
|
||||||
.mask = posix.sigemptyset(),
|
.mask = posix.sigemptyset(),
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
};
|
}, null);
|
||||||
posix.sigaction(posix.SIG.WINCH, &act, null);
|
posix.sigaction(posix.SIG.CONT, &.{
|
||||||
this.winch_registered = true;
|
.handler = .{ .handler = handleCont },
|
||||||
|
.mask = posix.sigemptyset(),
|
||||||
|
.flags = 0,
|
||||||
|
}, null);
|
||||||
|
this.handler_registered = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.future = this.io.async(run, .{this});
|
this.quit_event.reset();
|
||||||
|
this.thread = try Thread.spawn(.{}, @This().run, .{this});
|
||||||
|
|
||||||
var termios: posix.termios = undefined;
|
var termios: posix.termios = undefined;
|
||||||
try terminal.enableRawMode(&termios);
|
if (this.config.rawMode) try terminal.enableRawMode(&termios);
|
||||||
if (this.termios) |_| {} else this.termios = termios;
|
if (this.termios) |_| {} else this.termios = termios;
|
||||||
|
|
||||||
try terminal.enterAltScreen();
|
if (this.config.altScreen) try terminal.enterAltScreen();
|
||||||
try terminal.saveScreen();
|
if (this.config.saveScreen) try terminal.saveScreen();
|
||||||
try terminal.hideCursor();
|
if (this.config.hideCursor) try terminal.hideCursor();
|
||||||
try terminal.enableMouseSupport();
|
if (this.config.altScreen and this.config.rawMode) try terminal.enableMouseSupport();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn interrupt(this: *@This()) !void {
|
pub fn interrupt(this: *@This()) !void {
|
||||||
try terminal.disableMouseSupport();
|
this.quit_event.set();
|
||||||
try terminal.restoreScreen();
|
if (this.config.altScreen and this.config.rawMode) try terminal.disableMouseSupport();
|
||||||
try terminal.exitAltScreen();
|
if (this.config.saveScreen) try terminal.restoreScreen();
|
||||||
if (this.future) |*future| {
|
if (this.config.altScreen) try terminal.exitAltScreen();
|
||||||
future.cancel(this.io) catch {};
|
if (this.thread) |*thread| {
|
||||||
this.future = null;
|
thread.join();
|
||||||
|
this.thread = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(this: *@This()) !void {
|
pub fn stop(this: *@This()) !void {
|
||||||
try this.interrupt();
|
try this.interrupt();
|
||||||
|
if (this.config.hideCursor) try terminal.showCursor();
|
||||||
|
if (this.config.saveScreen) try terminal.resetCursor();
|
||||||
if (this.termios) |termios| {
|
if (this.termios) |termios| {
|
||||||
try terminal.disableMouseSupport();
|
if (this.config.rawMode) try terminal.disableRawMode(&termios);
|
||||||
try terminal.showCursor();
|
|
||||||
try terminal.resetCursor();
|
|
||||||
try terminal.restoreScreen();
|
|
||||||
try terminal.disableRawMode(&termios);
|
|
||||||
try terminal.exitAltScreen();
|
|
||||||
this.termios = null;
|
this.termios = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,10 +142,7 @@ pub fn App(comptime M: type, comptime E: type) type {
|
|||||||
/// Quit the application loop.
|
/// Quit the application loop.
|
||||||
/// This will cancel 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 {
|
pub fn quit(this: *@This()) void {
|
||||||
if (this.future) |*future| {
|
this.quit_event.set();
|
||||||
future.cancel(this.io) catch {};
|
|
||||||
this.future = null;
|
|
||||||
}
|
|
||||||
this.postEvent(.quit);
|
this.postEvent(.quit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +160,7 @@ pub fn App(comptime M: type, comptime E: type) type {
|
|||||||
// thread to read user inputs
|
// thread to read user inputs
|
||||||
var buf: [512]u8 = undefined;
|
var buf: [512]u8 = undefined;
|
||||||
// NOTE set the `NONBLOCK` option for the stdin file, such that reading is not blocking!
|
// NOTE set the `NONBLOCK` option for the stdin file, such that reading is not blocking!
|
||||||
{
|
if (this.config.rawMode) {
|
||||||
// TODO is there a better way to do this through the `std.Io` interface?
|
// 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) {
|
var fl_flags = posix.fcntl(posix.STDIN_FILENO, posix.F.GETFL, 0) catch |err| switch (err) {
|
||||||
error.FileBusy => unreachable,
|
error.FileBusy => unreachable,
|
||||||
@@ -145,286 +180,316 @@ pub fn App(comptime M: type, comptime E: type) type {
|
|||||||
else => |e| return e,
|
else => |e| return e,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var remaining_bytes: usize = 0;
|
||||||
|
var lines: std.ArrayList(u8) = .empty;
|
||||||
|
defer lines.deinit(this.gpa);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
this.io.checkCancel() catch break;
|
this.quit_event.timedWait(20 * std.time.ns_per_ms) catch {
|
||||||
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;
|
||||||
|
remaining_bytes = 0;
|
||||||
|
|
||||||
// non-blocking read
|
if (read_bytes == 0) {
|
||||||
const read_bytes = terminal.read(buf[remaining_bytes..]) catch |err| switch (err) {
|
// received <EOF>
|
||||||
error.WouldBlock => {
|
this.postEvent(.cancel);
|
||||||
// wait a bit
|
|
||||||
this.io.sleep(.fromMilliseconds(20), .awake) catch |e| switch (e) {
|
|
||||||
error.Canceled => break,
|
|
||||||
else => return e,
|
|
||||||
};
|
|
||||||
continue;
|
continue;
|
||||||
},
|
}
|
||||||
else => return err,
|
|
||||||
} + remaining_bytes;
|
|
||||||
|
|
||||||
// escape key presses
|
// escape key presses
|
||||||
if (buf[0] == 0x1b and read_bytes > 1) {
|
if (buf[0] == 0x1b and read_bytes > 1) {
|
||||||
switch (buf[1]) {
|
switch (buf[1]) {
|
||||||
0x4F => { // ss3
|
0x4F => { // ss3
|
||||||
if (read_bytes < 3) continue;
|
if (read_bytes < 3) continue;
|
||||||
|
|
||||||
const key: Key = switch (buf[2]) {
|
const key: Key = switch (buf[2]) {
|
||||||
'A' => .{ .cp = input.Up },
|
'A' => .{ .cp = input.Up },
|
||||||
'B' => .{ .cp = input.Down },
|
'B' => .{ .cp = input.Down },
|
||||||
'C' => .{ .cp = input.Right },
|
'C' => .{ .cp = input.Right },
|
||||||
'D' => .{ .cp = input.Left },
|
'D' => .{ .cp = input.Left },
|
||||||
'E' => .{ .cp = input.KpBegin },
|
'E' => .{ .cp = input.KpBegin },
|
||||||
'F' => .{ .cp = input.End },
|
'F' => .{ .cp = input.End },
|
||||||
'H' => .{ .cp = input.Home },
|
'H' => .{ .cp = input.Home },
|
||||||
'P' => .{ .cp = input.F1 },
|
'P' => .{ .cp = input.F1 },
|
||||||
'Q' => .{ .cp = input.F2 },
|
'Q' => .{ .cp = input.F2 },
|
||||||
'R' => .{ .cp = input.F3 },
|
'R' => .{ .cp = input.F3 },
|
||||||
'S' => .{ .cp = input.F4 },
|
'S' => .{ .cp = input.F4 },
|
||||||
else => continue,
|
else => continue,
|
||||||
|
};
|
||||||
|
this.postEvent(.{ .key = key });
|
||||||
|
},
|
||||||
|
0x5B => { // csi
|
||||||
|
if (read_bytes < 3) continue;
|
||||||
|
|
||||||
|
// We start iterating at index 2 to get past the '['
|
||||||
|
const sequence: []u8 = blk: for (buf[2..], 2..) |b, i| {
|
||||||
|
switch (b) {
|
||||||
|
0x40...0xFF => break :blk buf[0 .. i + 1],
|
||||||
|
else => continue,
|
||||||
|
}
|
||||||
|
} else continue;
|
||||||
|
|
||||||
|
const final = sequence[sequence.len - 1];
|
||||||
|
switch (final) {
|
||||||
|
'A', 'B', 'C', 'D', 'E', 'F', 'H', 'P', 'Q', 'R', 'S' => {
|
||||||
|
// Legacy keys
|
||||||
|
// CSI {ABCDEFHPQS}
|
||||||
|
// CSI 1 ; modifier:event_type {ABCDEFHPQS}
|
||||||
|
var field_iter = std.mem.splitScalar(u8, sequence[2 .. sequence.len - 1], ';');
|
||||||
|
_ = field_iter.next(); // skip first field
|
||||||
|
|
||||||
|
const key: Key = .{
|
||||||
|
.cp = switch (final) {
|
||||||
|
'A' => input.Up,
|
||||||
|
'B' => input.Down,
|
||||||
|
'C' => input.Right,
|
||||||
|
'D' => input.Left,
|
||||||
|
'E' => input.KpBegin,
|
||||||
|
'F' => input.End,
|
||||||
|
'H' => input.Home,
|
||||||
|
'P' => input.F1,
|
||||||
|
'Q' => input.F2,
|
||||||
|
'R' => input.F3,
|
||||||
|
'S' => input.F4,
|
||||||
|
else => unreachable,
|
||||||
|
},
|
||||||
|
.mod = blk: {
|
||||||
|
// modifier_mask:event_type
|
||||||
|
var mod: Key.Modifier = .{};
|
||||||
|
const field_buf = field_iter.next() orelse break :blk mod;
|
||||||
|
var param_iter = std.mem.splitScalar(u8, field_buf, ':');
|
||||||
|
const modifier_buf = param_iter.next() orelse unreachable;
|
||||||
|
const modifier_mask = fmt.parseUnsigned(u8, modifier_buf, 10) catch break :blk mod;
|
||||||
|
if ((modifier_mask -| 1) & 1 != 0) mod.shift = true;
|
||||||
|
if ((modifier_mask -| 1) & 2 != 0) mod.alt = true;
|
||||||
|
if ((modifier_mask -| 1) & 4 != 0) mod.ctrl = true;
|
||||||
|
break :blk mod;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
this.postEvent(.{ .key = key });
|
||||||
|
},
|
||||||
|
'Z' => this.postEvent(.{ .key = .{ .cp = input.Tab, .mod = .{ .shift = true } } }),
|
||||||
|
'~' => {
|
||||||
|
// Legacy keys
|
||||||
|
// CSI number ~
|
||||||
|
// CSI number ; modifier ~
|
||||||
|
// CSI number ; modifier:event_type ; text_as_codepoint ~
|
||||||
|
var field_iter = mem.splitScalar(u8, sequence[2 .. sequence.len - 1], ';');
|
||||||
|
|
||||||
|
const key: Key = .{
|
||||||
|
.cp = blk: {
|
||||||
|
const number_buf = field_iter.next() orelse unreachable; // always will have one field
|
||||||
|
const number = fmt.parseUnsigned(u16, number_buf, 10) catch break;
|
||||||
|
break :blk switch (number) {
|
||||||
|
2 => input.Insert,
|
||||||
|
3 => input.Delete,
|
||||||
|
5 => input.PageUp,
|
||||||
|
6 => input.PageDown,
|
||||||
|
7 => input.Home,
|
||||||
|
8 => input.End,
|
||||||
|
11 => input.F1,
|
||||||
|
12 => input.F2,
|
||||||
|
13 => input.F3,
|
||||||
|
14 => input.F4,
|
||||||
|
15 => input.F5,
|
||||||
|
17 => input.F6,
|
||||||
|
18 => input.F7,
|
||||||
|
19 => input.F8,
|
||||||
|
20 => input.F9,
|
||||||
|
21 => input.F10,
|
||||||
|
23 => input.F11,
|
||||||
|
24 => input.F12,
|
||||||
|
25 => input.F13,
|
||||||
|
26 => input.F14,
|
||||||
|
28 => input.F15,
|
||||||
|
29 => input.F16,
|
||||||
|
31 => input.F17,
|
||||||
|
32 => input.F18,
|
||||||
|
33 => input.F19,
|
||||||
|
34 => input.F20,
|
||||||
|
// 200 => return .{ .event = .paste_start, .n = sequence.len },
|
||||||
|
// 201 => return .{ .event = .paste_end, .n = sequence.len },
|
||||||
|
57399...57454 => |code| code,
|
||||||
|
else => unreachable,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
.mod = blk: {
|
||||||
|
// modifier_mask:event_type
|
||||||
|
var mod: Key.Modifier = .{};
|
||||||
|
const field_buf = field_iter.next() orelse break :blk mod;
|
||||||
|
var param_iter = std.mem.splitScalar(u8, field_buf, ':');
|
||||||
|
const modifier_buf = param_iter.next() orelse unreachable;
|
||||||
|
const modifier_mask = fmt.parseUnsigned(u8, modifier_buf, 10) catch break :blk mod;
|
||||||
|
if ((modifier_mask -| 1) & 1 != 0) mod.shift = true;
|
||||||
|
if ((modifier_mask -| 1) & 2 != 0) mod.alt = true;
|
||||||
|
if ((modifier_mask -| 1) & 4 != 0) mod.ctrl = true;
|
||||||
|
break :blk mod;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
this.postEvent(.{ .key = key });
|
||||||
|
},
|
||||||
|
// TODO focus usage? should this even be in the default event system?
|
||||||
|
'I' => this.postEvent(.{ .focus = true }),
|
||||||
|
'O' => this.postEvent(.{ .focus = false }),
|
||||||
|
'M', 'm' => {
|
||||||
|
assert(sequence.len >= 4);
|
||||||
|
if (sequence[2] != '<') break;
|
||||||
|
|
||||||
|
const delim1 = mem.indexOfScalarPos(u8, sequence, 3, ';') orelse break;
|
||||||
|
const button_mask = fmt.parseUnsigned(u16, sequence[3..delim1], 10) catch break;
|
||||||
|
const delim2 = mem.indexOfScalarPos(u8, sequence, delim1 + 1, ';') orelse break;
|
||||||
|
const px = fmt.parseUnsigned(u16, sequence[delim1 + 1 .. delim2], 10) catch break;
|
||||||
|
const py = fmt.parseUnsigned(u16, sequence[delim2 + 1 .. sequence.len - 1], 10) catch break;
|
||||||
|
|
||||||
|
const mouse_bits = packed struct {
|
||||||
|
const motion: u8 = 0b00100000;
|
||||||
|
const buttons: u8 = 0b11000011;
|
||||||
|
const shift: u8 = 0b00000100;
|
||||||
|
const alt: u8 = 0b00001000;
|
||||||
|
const ctrl: u8 = 0b00010000;
|
||||||
|
};
|
||||||
|
|
||||||
|
const button: Mouse.Button = @enumFromInt(button_mask & mouse_bits.buttons);
|
||||||
|
const motion = button_mask & mouse_bits.motion > 0;
|
||||||
|
// const shift = button_mask & mouse_bits.shift > 0;
|
||||||
|
// const alt = button_mask & mouse_bits.alt > 0;
|
||||||
|
// const ctrl = button_mask & mouse_bits.ctrl > 0;
|
||||||
|
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
'c' => {
|
||||||
|
// Primary DA (CSI ? Pm c)
|
||||||
|
},
|
||||||
|
'n' => {
|
||||||
|
// Device Status Report
|
||||||
|
// CSI Ps n
|
||||||
|
// CSI ? Ps n
|
||||||
|
assert(sequence.len >= 3);
|
||||||
|
},
|
||||||
|
't' => {
|
||||||
|
// XTWINOPS
|
||||||
|
// Split first into fields delimited by ';'
|
||||||
|
var iter = mem.splitScalar(u8, sequence[2 .. sequence.len - 1], ';');
|
||||||
|
const ps = iter.first();
|
||||||
|
if (mem.eql(u8, "48", ps)) {
|
||||||
|
// in band window resize
|
||||||
|
// CSI 48 ; height ; width ; height_pix ; width_pix t
|
||||||
|
const width_char = iter.next() orelse break;
|
||||||
|
const height_char = iter.next() orelse break;
|
||||||
|
|
||||||
|
_ = width_char;
|
||||||
|
_ = height_char;
|
||||||
|
this.postEvent(.resize);
|
||||||
|
// this.postEvent(.{ .size = .{
|
||||||
|
// .x = fmt.parseUnsigned(u16, width_char, 10) catch break,
|
||||||
|
// .y = fmt.parseUnsigned(u16, height_char, 10) catch break,
|
||||||
|
// } });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'u' => {
|
||||||
|
// Kitty keyboard
|
||||||
|
// CSI unicode-key-code:alternate-key-codes ; modifiers:event-type ; text-as-codepoints u
|
||||||
|
// Not all fields will be present. Only unicode-key-code is
|
||||||
|
// mandatory
|
||||||
|
},
|
||||||
|
'y' => {
|
||||||
|
// DECRPM (CSI ? Ps ; Pm $ y)
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
0x50 => {
|
||||||
|
// DCS
|
||||||
|
},
|
||||||
|
0x58 => {
|
||||||
|
// SOS
|
||||||
|
},
|
||||||
|
0x5D => {
|
||||||
|
// OSC
|
||||||
|
},
|
||||||
|
// TODO parse corresponding codes
|
||||||
|
0x5F => {
|
||||||
|
// APC
|
||||||
|
// parse for kitty graphics capabilities
|
||||||
|
},
|
||||||
|
else => {
|
||||||
|
// alt + <char> keypress
|
||||||
|
this.postEvent(.{
|
||||||
|
.key = .{
|
||||||
|
.cp = buf[1],
|
||||||
|
.mod = .{ .alt = true },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.config.rawMode) {
|
||||||
|
const b = buf[0];
|
||||||
|
const key: Key = switch (b) {
|
||||||
|
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
||||||
|
0x08 => .{ .cp = input.Backspace },
|
||||||
|
0x09 => .{ .cp = input.Tab },
|
||||||
|
0x0a => .{ .cp = 'j', .mod = .{ .ctrl = true } },
|
||||||
|
0x0d => .{ .cp = input.Enter },
|
||||||
|
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
|
||||||
|
0x1b => escape: {
|
||||||
|
assert(read_bytes == 1);
|
||||||
|
break :escape .{ .cp = input.Escape };
|
||||||
|
},
|
||||||
|
0x7f => .{ .cp = input.Backspace },
|
||||||
|
else => {
|
||||||
|
var len = read_bytes;
|
||||||
|
while (!std.unicode.utf8ValidateSlice(buf[0..len])) len -= 1;
|
||||||
|
remaining_bytes = read_bytes - len;
|
||||||
|
var iter: std.unicode.Utf8Iterator = .{ .bytes = buf[0..len], .i = 0 };
|
||||||
|
while (iter.nextCodepoint()) |cp| this.postEvent(.{ .key = .{ .cp = cp } });
|
||||||
|
if (remaining_bytes > 0) {
|
||||||
|
@memmove(buf[0..remaining_bytes], buf[len .. len + remaining_bytes]);
|
||||||
|
}
|
||||||
|
continue; // this switch block does not return a `Key` we continue with loop
|
||||||
|
},
|
||||||
};
|
};
|
||||||
this.postEvent(.{ .key = key });
|
this.postEvent(.{ .key = key });
|
||||||
},
|
} else {
|
||||||
0x5B => { // csi
|
|
||||||
if (read_bytes < 3) continue;
|
|
||||||
|
|
||||||
// We start iterating at index 2 to get past the '['
|
|
||||||
const sequence: []u8 = blk: for (buf[2..], 2..) |b, i| {
|
|
||||||
switch (b) {
|
|
||||||
0x40...0xFF => break :blk buf[0 .. i + 1],
|
|
||||||
else => continue,
|
|
||||||
}
|
|
||||||
} else continue;
|
|
||||||
|
|
||||||
const final = sequence[sequence.len - 1];
|
|
||||||
switch (final) {
|
|
||||||
'A', 'B', 'C', 'D', 'E', 'F', 'H', 'P', 'Q', 'R', 'S' => {
|
|
||||||
// Legacy keys
|
|
||||||
// CSI {ABCDEFHPQS}
|
|
||||||
// CSI 1 ; modifier:event_type {ABCDEFHPQS}
|
|
||||||
var field_iter = std.mem.splitScalar(u8, sequence[2 .. sequence.len - 1], ';');
|
|
||||||
_ = field_iter.next(); // skip first field
|
|
||||||
|
|
||||||
const key: Key = .{
|
|
||||||
.cp = switch (final) {
|
|
||||||
'A' => input.Up,
|
|
||||||
'B' => input.Down,
|
|
||||||
'C' => input.Right,
|
|
||||||
'D' => input.Left,
|
|
||||||
'E' => input.KpBegin,
|
|
||||||
'F' => input.End,
|
|
||||||
'H' => input.Home,
|
|
||||||
'P' => input.F1,
|
|
||||||
'Q' => input.F2,
|
|
||||||
'R' => input.F3,
|
|
||||||
'S' => input.F4,
|
|
||||||
else => unreachable,
|
|
||||||
},
|
|
||||||
.mod = blk: {
|
|
||||||
// modifier_mask:event_type
|
|
||||||
var mod: Key.Modifier = .{};
|
|
||||||
const field_buf = field_iter.next() orelse break :blk mod;
|
|
||||||
var param_iter = std.mem.splitScalar(u8, field_buf, ':');
|
|
||||||
const modifier_buf = param_iter.next() orelse unreachable;
|
|
||||||
const modifier_mask = fmt.parseUnsigned(u8, modifier_buf, 10) catch break :blk mod;
|
|
||||||
if ((modifier_mask -| 1) & 1 != 0) mod.shift = true;
|
|
||||||
if ((modifier_mask -| 1) & 2 != 0) mod.alt = true;
|
|
||||||
if ((modifier_mask -| 1) & 4 != 0) mod.ctrl = true;
|
|
||||||
break :blk mod;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
this.postEvent(.{ .key = key });
|
|
||||||
},
|
|
||||||
'Z' => this.postEvent(.{ .key = .{ .cp = input.Tab, .mod = .{ .shift = true } } }),
|
|
||||||
'~' => {
|
|
||||||
// Legacy keys
|
|
||||||
// CSI number ~
|
|
||||||
// CSI number ; modifier ~
|
|
||||||
// CSI number ; modifier:event_type ; text_as_codepoint ~
|
|
||||||
var field_iter = mem.splitScalar(u8, sequence[2 .. sequence.len - 1], ';');
|
|
||||||
|
|
||||||
const key: Key = .{
|
|
||||||
.cp = blk: {
|
|
||||||
const number_buf = field_iter.next() orelse unreachable; // always will have one field
|
|
||||||
const number = fmt.parseUnsigned(u16, number_buf, 10) catch break;
|
|
||||||
break :blk switch (number) {
|
|
||||||
2 => input.Insert,
|
|
||||||
3 => input.Delete,
|
|
||||||
5 => input.PageUp,
|
|
||||||
6 => input.PageDown,
|
|
||||||
7 => input.Home,
|
|
||||||
8 => input.End,
|
|
||||||
11 => input.F1,
|
|
||||||
12 => input.F2,
|
|
||||||
13 => input.F3,
|
|
||||||
14 => input.F4,
|
|
||||||
15 => input.F5,
|
|
||||||
17 => input.F6,
|
|
||||||
18 => input.F7,
|
|
||||||
19 => input.F8,
|
|
||||||
20 => input.F9,
|
|
||||||
21 => input.F10,
|
|
||||||
23 => input.F11,
|
|
||||||
24 => input.F12,
|
|
||||||
25 => input.F13,
|
|
||||||
26 => input.F14,
|
|
||||||
28 => input.F15,
|
|
||||||
29 => input.F16,
|
|
||||||
31 => input.F17,
|
|
||||||
32 => input.F18,
|
|
||||||
33 => input.F19,
|
|
||||||
34 => input.F20,
|
|
||||||
// 200 => return .{ .event = .paste_start, .n = sequence.len },
|
|
||||||
// 201 => return .{ .event = .paste_end, .n = sequence.len },
|
|
||||||
57399...57454 => |code| code,
|
|
||||||
else => unreachable,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
.mod = blk: {
|
|
||||||
// modifier_mask:event_type
|
|
||||||
var mod: Key.Modifier = .{};
|
|
||||||
const field_buf = field_iter.next() orelse break :blk mod;
|
|
||||||
var param_iter = std.mem.splitScalar(u8, field_buf, ':');
|
|
||||||
const modifier_buf = param_iter.next() orelse unreachable;
|
|
||||||
const modifier_mask = fmt.parseUnsigned(u8, modifier_buf, 10) catch break :blk mod;
|
|
||||||
if ((modifier_mask -| 1) & 1 != 0) mod.shift = true;
|
|
||||||
if ((modifier_mask -| 1) & 2 != 0) mod.alt = true;
|
|
||||||
if ((modifier_mask -| 1) & 4 != 0) mod.ctrl = true;
|
|
||||||
break :blk mod;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
this.postEvent(.{ .key = key });
|
|
||||||
},
|
|
||||||
// TODO focus usage? should this even be in the default event system?
|
|
||||||
'I' => this.postEvent(.{ .focus = true }),
|
|
||||||
'O' => this.postEvent(.{ .focus = false }),
|
|
||||||
'M', 'm' => {
|
|
||||||
assert(sequence.len >= 4);
|
|
||||||
if (sequence[2] != '<') break;
|
|
||||||
|
|
||||||
const delim1 = mem.indexOfScalarPos(u8, sequence, 3, ';') orelse break;
|
|
||||||
const button_mask = fmt.parseUnsigned(u16, sequence[3..delim1], 10) catch break;
|
|
||||||
const delim2 = mem.indexOfScalarPos(u8, sequence, delim1 + 1, ';') orelse break;
|
|
||||||
const px = fmt.parseUnsigned(u16, sequence[delim1 + 1 .. delim2], 10) catch break;
|
|
||||||
const py = fmt.parseUnsigned(u16, sequence[delim2 + 1 .. sequence.len - 1], 10) catch break;
|
|
||||||
|
|
||||||
const mouse_bits = packed struct {
|
|
||||||
const motion: u8 = 0b00100000;
|
|
||||||
const buttons: u8 = 0b11000011;
|
|
||||||
const shift: u8 = 0b00000100;
|
|
||||||
const alt: u8 = 0b00001000;
|
|
||||||
const ctrl: u8 = 0b00010000;
|
|
||||||
};
|
|
||||||
|
|
||||||
const button: Mouse.Button = @enumFromInt(button_mask & mouse_bits.buttons);
|
|
||||||
const motion = button_mask & mouse_bits.motion > 0;
|
|
||||||
// const shift = button_mask & mouse_bits.shift > 0;
|
|
||||||
// const alt = button_mask & mouse_bits.alt > 0;
|
|
||||||
// const ctrl = button_mask & mouse_bits.ctrl > 0;
|
|
||||||
|
|
||||||
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;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
|
||||||
'c' => {
|
|
||||||
// Primary DA (CSI ? Pm c)
|
|
||||||
},
|
|
||||||
'n' => {
|
|
||||||
// Device Status Report
|
|
||||||
// CSI Ps n
|
|
||||||
// CSI ? Ps n
|
|
||||||
assert(sequence.len >= 3);
|
|
||||||
},
|
|
||||||
't' => {
|
|
||||||
// XTWINOPS
|
|
||||||
// Split first into fields delimited by ';'
|
|
||||||
var iter = mem.splitScalar(u8, sequence[2 .. sequence.len - 1], ';');
|
|
||||||
const ps = iter.first();
|
|
||||||
if (mem.eql(u8, "48", ps)) {
|
|
||||||
// in band window resize
|
|
||||||
// CSI 48 ; height ; width ; height_pix ; width_pix t
|
|
||||||
const width_char = iter.next() orelse break;
|
|
||||||
const height_char = iter.next() orelse break;
|
|
||||||
|
|
||||||
_ = width_char;
|
|
||||||
_ = height_char;
|
|
||||||
this.postEvent(.resize);
|
|
||||||
// this.postEvent(.{ .size = .{
|
|
||||||
// .x = fmt.parseUnsigned(u16, width_char, 10) catch break,
|
|
||||||
// .y = fmt.parseUnsigned(u16, height_char, 10) catch break,
|
|
||||||
// } });
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'u' => {
|
|
||||||
// Kitty keyboard
|
|
||||||
// CSI unicode-key-code:alternate-key-codes ; modifiers:event-type ; text-as-codepoints u
|
|
||||||
// Not all fields will be present. Only unicode-key-code is
|
|
||||||
// mandatory
|
|
||||||
},
|
|
||||||
'y' => {
|
|
||||||
// DECRPM (CSI ? Ps ; Pm $ y)
|
|
||||||
},
|
|
||||||
else => {},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
0x50 => {
|
|
||||||
// DCS
|
|
||||||
},
|
|
||||||
0x58 => {
|
|
||||||
// SOS
|
|
||||||
},
|
|
||||||
0x5D => {
|
|
||||||
// OSC
|
|
||||||
},
|
|
||||||
// TODO parse corresponding codes
|
|
||||||
0x5F => {
|
|
||||||
// APC
|
|
||||||
// parse for kitty graphics capabilities
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
// alt + <char> keypress
|
|
||||||
this.postEvent(.{
|
|
||||||
.key = .{
|
|
||||||
.cp = buf[1],
|
|
||||||
.mod = .{ .alt = true },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const b = buf[0];
|
|
||||||
const key: Key = switch (b) {
|
|
||||||
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
|
||||||
0x08 => .{ .cp = input.Backspace },
|
|
||||||
0x09 => .{ .cp = input.Tab },
|
|
||||||
0x0a => .{ .cp = 'j', .mod = .{ .ctrl = true } },
|
|
||||||
0x0d => .{ .cp = input.Enter },
|
|
||||||
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
|
|
||||||
0x1b => escape: {
|
|
||||||
assert(read_bytes == 1);
|
|
||||||
break :escape .{ .cp = input.Escape };
|
|
||||||
},
|
|
||||||
0x7f => .{ .cp = input.Backspace },
|
|
||||||
else => {
|
|
||||||
var len = read_bytes;
|
var len = read_bytes;
|
||||||
while (!std.unicode.utf8ValidateSlice(buf[0..len])) len -= 1;
|
while (!std.unicode.utf8ValidateSlice(buf[0..len])) len -= 1;
|
||||||
remaining_bytes = read_bytes - len;
|
remaining_bytes = read_bytes - len;
|
||||||
var iter: std.unicode.Utf8Iterator = .{ .bytes = buf[0..len], .i = 0 };
|
try lines.appendSlice(this.gpa, buf[0..len]);
|
||||||
while (iter.nextCodepoint()) |cp| this.postEvent(.{ .key = .{ .cp = cp } });
|
if (remaining_bytes > 0) {
|
||||||
continue;
|
@memmove(buf[0..remaining_bytes], buf[len .. len + remaining_bytes]);
|
||||||
},
|
} else {
|
||||||
};
|
if (read_bytes != 512 and buf[len - 1] == '\n') this.postEvent(.{ .line = try lines.toOwnedSlice(this.gpa) });
|
||||||
this.postEvent(.{ .key = key });
|
// NOTE line did not end with `\n` but with EOF, meaning the user canceled
|
||||||
}
|
if (read_bytes != 512 and buf[len - 1] != '\n') {
|
||||||
|
lines.clearRetainingCapacity();
|
||||||
|
this.postEvent(.cancel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,6 +536,7 @@ const mem = std.mem;
|
|||||||
const fmt = std.fmt;
|
const fmt = std.fmt;
|
||||||
const posix = std.posix;
|
const posix = std.posix;
|
||||||
const Thread = std.Thread;
|
const Thread = std.Thread;
|
||||||
|
const Allocator = mem.Allocator;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
const event = @import("event.zig");
|
const event = @import("event.zig");
|
||||||
const input = @import("input.zig");
|
const input = @import("input.zig");
|
||||||
|
|||||||
@@ -599,9 +599,11 @@ pub const Layout = packed struct {
|
|||||||
/// Sizing options which should be used by the `Container`
|
/// Sizing options which should be used by the `Container`
|
||||||
pub const Size = packed struct {
|
pub const Size = packed struct {
|
||||||
dim: Point = .{},
|
dim: Point = .{},
|
||||||
grow: enum(u2) {
|
grow: enum(u3) {
|
||||||
both,
|
both,
|
||||||
fixed,
|
fixed,
|
||||||
|
vertical_only,
|
||||||
|
horizontal_only,
|
||||||
vertical,
|
vertical,
|
||||||
horizontal,
|
horizontal,
|
||||||
} = .both,
|
} = .both,
|
||||||
@@ -732,11 +734,11 @@ pub fn Container(Model: type, Event: type) type {
|
|||||||
assert(this.properties.size.dim.y > 0 or size.y > 0);
|
assert(this.properties.size.dim.y > 0 or size.y > 0);
|
||||||
break :blk .max(size, this.properties.size.dim);
|
break :blk .max(size, this.properties.size.dim);
|
||||||
},
|
},
|
||||||
.horizontal => blk: {
|
.horizontal, .horizontal_only => blk: {
|
||||||
assert(this.properties.size.dim.y > 0 or size.y > 0);
|
assert(this.properties.size.dim.y > 0 or size.y > 0);
|
||||||
break :blk .max(size, this.properties.size.dim);
|
break :blk .max(size, this.properties.size.dim);
|
||||||
},
|
},
|
||||||
.vertical => blk: {
|
.vertical, .vertical_only => blk: {
|
||||||
assert(this.properties.size.dim.x > 0 or size.x > 0);
|
assert(this.properties.size.dim.x > 0 or size.x > 0);
|
||||||
break :blk .max(size, this.properties.size.dim);
|
break :blk .max(size, this.properties.size.dim);
|
||||||
},
|
},
|
||||||
@@ -762,23 +764,31 @@ pub fn Container(Model: type, Event: type) type {
|
|||||||
|
|
||||||
const sides = this.properties.border.sides;
|
const sides = this.properties.border.sides;
|
||||||
switch (layout.direction) {
|
switch (layout.direction) {
|
||||||
.horizontal => {
|
.vertical => {
|
||||||
if (sides.top) {
|
if (sides.top) {
|
||||||
available -|= 1;
|
|
||||||
remainder -|= 1;
|
remainder -|= 1;
|
||||||
}
|
}
|
||||||
if (sides.bottom) {
|
if (sides.bottom) {
|
||||||
available -|= 1;
|
|
||||||
remainder -|= 1;
|
remainder -|= 1;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
.vertical => {
|
|
||||||
if (sides.left) {
|
if (sides.left) {
|
||||||
available -|= 1;
|
available -|= 1;
|
||||||
remainder -|= 1;
|
|
||||||
}
|
}
|
||||||
if (sides.right) {
|
if (sides.right) {
|
||||||
available -|= 1;
|
available -|= 1;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.horizontal => {
|
||||||
|
if (sides.top) {
|
||||||
|
available -|= 1;
|
||||||
|
}
|
||||||
|
if (sides.bottom) {
|
||||||
|
available -|= 1;
|
||||||
|
}
|
||||||
|
if (sides.left) {
|
||||||
|
remainder -|= 1;
|
||||||
|
}
|
||||||
|
if (sides.right) {
|
||||||
remainder -|= 1;
|
remainder -|= 1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -799,21 +809,21 @@ pub fn Container(Model: type, Event: type) type {
|
|||||||
if (growable_children == 0) first_growable_child = child;
|
if (growable_children == 0) first_growable_child = child;
|
||||||
growable_children += 1;
|
growable_children += 1;
|
||||||
},
|
},
|
||||||
.horizontal => if (layout.direction == .horizontal) {
|
.horizontal, .horizontal_only => if (layout.direction == .horizontal) {
|
||||||
if (growable_children == 0) first_growable_child = child;
|
if (growable_children == 0) first_growable_child = child;
|
||||||
growable_children += 1;
|
growable_children += 1;
|
||||||
},
|
},
|
||||||
.vertical => if (layout.direction == .vertical) {
|
.vertical, .vertical_only => if (layout.direction == .vertical) {
|
||||||
if (growable_children == 0) first_growable_child = child;
|
if (growable_children == 0) first_growable_child = child;
|
||||||
growable_children += 1;
|
growable_children += 1;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// non layout direction side growth
|
// non layout direction side growth
|
||||||
switch (layout.direction) {
|
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;
|
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;
|
child.size.x = available;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -831,8 +841,8 @@ pub fn Container(Model: type, Event: type) type {
|
|||||||
if (child.properties.size.grow == .fixed) continue;
|
if (child.properties.size.grow == .fixed) continue;
|
||||||
|
|
||||||
switch (layout.direction) {
|
switch (layout.direction) {
|
||||||
.horizontal => if (child.properties.size.grow == .vertical) continue,
|
.horizontal => if (child.properties.size.grow == .vertical or child.properties.size.grow == .vertical_only) continue,
|
||||||
.vertical => if (child.properties.size.grow == .horizontal) continue,
|
.vertical => if (child.properties.size.grow == .horizontal or child.properties.size.grow == .horizontal_only) continue,
|
||||||
}
|
}
|
||||||
|
|
||||||
const size = switch (layout.direction) {
|
const size = switch (layout.direction) {
|
||||||
@@ -860,23 +870,23 @@ pub fn Container(Model: type, Event: type) type {
|
|||||||
};
|
};
|
||||||
if (child.properties.size.grow != .fixed and child_size == smallest_size) {
|
if (child.properties.size.grow != .fixed and child_size == smallest_size) {
|
||||||
switch (layout.direction) {
|
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;
|
child.size.x += size_to_correct;
|
||||||
remainder -|= 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;
|
child.size.y += size_to_correct;
|
||||||
remainder -|= size_to_correct;
|
remainder -|= size_to_correct;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if (overflow > 0) {
|
if (overflow > 0) {
|
||||||
switch (layout.direction) {
|
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;
|
child.size.x += 1;
|
||||||
overflow -|= 1;
|
overflow -|= 1;
|
||||||
remainder -|= 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;
|
child.size.y += 1;
|
||||||
overflow -|= 1;
|
overflow -|= 1;
|
||||||
remainder -|= 1;
|
remainder -|= 1;
|
||||||
@@ -898,6 +908,14 @@ pub fn Container(Model: type, Event: type) type {
|
|||||||
this.size = switch (this.properties.size.grow) {
|
this.size = switch (this.properties.size.grow) {
|
||||||
.both => .max(size, fit_size),
|
.both => .max(size, fit_size),
|
||||||
.fixed => fit_size,
|
.fixed => fit_size,
|
||||||
|
.horizontal_only => .{
|
||||||
|
.x = size.x,
|
||||||
|
.y = fit_size.y,
|
||||||
|
},
|
||||||
|
.vertical_only => .{
|
||||||
|
.x = fit_size.x,
|
||||||
|
.y = size.y,
|
||||||
|
},
|
||||||
.horizontal => .{
|
.horizontal => .{
|
||||||
.x = @max(size.x, fit_size.x),
|
.x = @max(size.x, fit_size.x),
|
||||||
.y = size.y,
|
.y = size.y,
|
||||||
|
|||||||
@@ -219,8 +219,8 @@ pub fn Alignment(Model: type, Event: type) type {
|
|||||||
outer: for (0..csize.y) |row| {
|
outer: for (0..csize.y) |row| {
|
||||||
inner: for (0..csize.x) |col| {
|
inner: for (0..csize.x) |col| {
|
||||||
// do not read/write out of bounce
|
// do not read/write out of bounce
|
||||||
if (this.size.x < row) break :outer;
|
if (this.size.y < row) break :outer;
|
||||||
if (this.size.y < col) break :inner;
|
if (this.size.x < col) break :inner;
|
||||||
|
|
||||||
cells[((row + origin.y) * size.x) + col + origin.x] = container_cells[(row * csize.x) + col];
|
cells[((row + origin.y) * size.x) + col + origin.x] = container_cells[(row * csize.x) + col];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,11 @@ pub const SystemEvent = union(enum) {
|
|||||||
init,
|
init,
|
||||||
/// Quit event to signify the end of the event loop (rendering should stop afterwards)
|
/// Quit event to signify the end of the event loop (rendering should stop afterwards)
|
||||||
quit,
|
quit,
|
||||||
|
/// Cancel event to signify that the user provided an EOF
|
||||||
|
///
|
||||||
|
/// Usually this event is only triggered by the system in *non raw mode*
|
||||||
|
/// renderings otherwise the corresponding `.key` event would be fired instead.
|
||||||
|
cancel,
|
||||||
/// Resize event to signify that the application should re-draw to resize
|
/// Resize event to signify that the application should re-draw to resize
|
||||||
///
|
///
|
||||||
/// Usually no `Container` nor `Element` should act on that event, as it
|
/// Usually no `Container` nor `Element` should act on that event, as it
|
||||||
@@ -23,6 +28,11 @@ pub const SystemEvent = union(enum) {
|
|||||||
/// associated error message
|
/// associated error message
|
||||||
msg: []const u8,
|
msg: []const u8,
|
||||||
},
|
},
|
||||||
|
/// Input line event received in *non raw mode* (instead of individual `key` events)
|
||||||
|
///
|
||||||
|
/// This event contains the entire line until the ending newline character
|
||||||
|
/// (which is included in the payload of this event).
|
||||||
|
line: []const u8,
|
||||||
/// Input key event received from the user
|
/// Input key event received from the user
|
||||||
key: Key,
|
key: Key,
|
||||||
/// Mouse input event
|
/// Mouse input event
|
||||||
@@ -46,44 +56,21 @@ pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
|||||||
const b_fields = @typeInfo(B).@"union".fields;
|
const b_fields = @typeInfo(B).@"union".fields;
|
||||||
const b_fields_tag = @typeInfo(B).@"union".tag_type.?;
|
const b_fields_tag = @typeInfo(B).@"union".tag_type.?;
|
||||||
const b_enum_fields = @typeInfo(b_fields_tag).@"enum".fields;
|
const b_enum_fields = @typeInfo(b_fields_tag).@"enum".fields;
|
||||||
var field_names: [a_fields.len + b_fields.len][:0]const u8 = undefined;
|
var fields: [a_fields.len + b_fields.len]std.builtin.Type.UnionField = undefined;
|
||||||
var field_types: [a_fields.len + b_fields.len]type = undefined;
|
var enum_fields: [a_fields.len + b_fields.len]std.builtin.Type.EnumField = undefined;
|
||||||
var field_attributes: [a_fields.len + b_fields.len]std.builtin.Type.UnionField.Attributes = undefined;
|
|
||||||
var enum_names: [a_fields.len + b_fields.len][:0]const u8 = undefined;
|
|
||||||
const UEnumSize = blk: {
|
|
||||||
const total_size = a_fields.len + b_fields.len;
|
|
||||||
break :blk switch (total_size) {
|
|
||||||
1...2 => u1,
|
|
||||||
3...4 => u2,
|
|
||||||
5...8 => u4,
|
|
||||||
9...16 => u4,
|
|
||||||
17...32 => u5,
|
|
||||||
33...64 => u6,
|
|
||||||
65...128 => u7,
|
|
||||||
129...256 => u8,
|
|
||||||
else => u16, // should suffice
|
|
||||||
};
|
|
||||||
};
|
|
||||||
var enum_values: [a_fields.len + b_fields.len]UEnumSize = undefined;
|
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
for (a_fields, a_enum_fields) |field, enum_field| {
|
for (a_fields, a_enum_fields) |field, enum_field| {
|
||||||
field_names[i] = field.name;
|
fields[i] = field;
|
||||||
field_types[i] = field.type;
|
|
||||||
field_attributes[i] = .{ .@"align" = field.alignment };
|
|
||||||
var enum_f = enum_field;
|
var enum_f = enum_field;
|
||||||
enum_f.value = i;
|
enum_f.value = i;
|
||||||
enum_names[i] = enum_f.name;
|
enum_fields[i] = enum_f;
|
||||||
enum_values[i] = enum_f.value;
|
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
for (b_fields, b_enum_fields) |field, enum_field| {
|
for (b_fields, b_enum_fields) |field, enum_field| {
|
||||||
field_names[i] = field.name;
|
fields[i] = field;
|
||||||
field_types[i] = field.type;
|
|
||||||
field_attributes[i] = .{ .@"align" = field.alignment };
|
|
||||||
var enum_f = enum_field;
|
var enum_f = enum_field;
|
||||||
enum_f.value = i;
|
enum_f.value = i;
|
||||||
enum_names[i] = enum_f.name;
|
enum_fields[i] = enum_f;
|
||||||
enum_values[i] = enum_f.value;
|
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,22 +81,35 @@ pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
|||||||
// user provided one)
|
// user provided one)
|
||||||
const a_enum_decls = @typeInfo(A).@"union".decls;
|
const a_enum_decls = @typeInfo(A).@"union".decls;
|
||||||
const b_enum_decls = @typeInfo(B).@"union".decls;
|
const b_enum_decls = @typeInfo(B).@"union".decls;
|
||||||
var decl_names: [a_enum_decls.len + b_enum_decls.len]std.builtin.Type.Declaration = undefined;
|
var decls: [a_enum_decls.len + b_enum_decls.len]std.builtin.Type.Declaration = undefined;
|
||||||
var j: usize = 0;
|
var j: usize = 0;
|
||||||
for (a_enum_decls) |decl| {
|
for (a_enum_decls) |decl| {
|
||||||
decl_names[j] = decl.name;
|
decls[j] = decl;
|
||||||
j += 1;
|
j += 1;
|
||||||
}
|
}
|
||||||
for (b_enum_decls) |decl| {
|
for (b_enum_decls) |decl| {
|
||||||
decl_names[j] = decl.name;
|
decls[j] = decl;
|
||||||
j += 1;
|
j += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EventType = @Int(.unsigned, @bitSizeOf(@TypeOf(i)) - @clz(i));
|
const EventType = @Type(.{ .int = .{
|
||||||
|
.signedness = .unsigned,
|
||||||
|
.bits = @bitSizeOf(@TypeOf(i)) - @clz(i),
|
||||||
|
} });
|
||||||
|
|
||||||
const Event = @Enum(EventType, .exhaustive, enum_names[0..], enum_values[0..]);
|
const Event = @Type(.{ .@"enum" = .{
|
||||||
|
.tag_type = EventType,
|
||||||
|
.fields = enum_fields[0..],
|
||||||
|
.decls = &.{},
|
||||||
|
.is_exhaustive = true,
|
||||||
|
} });
|
||||||
|
|
||||||
return @Union(.auto, Event, field_names[0..], field_types[0..], field_attributes[0..]);
|
return @Type(.{ .@"union" = .{
|
||||||
|
.layout = .auto,
|
||||||
|
.tag_type = Event,
|
||||||
|
.fields = fields[0..],
|
||||||
|
.decls = &.{},
|
||||||
|
} });
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determine whether the provided type `T` is a tagged union: `union(enum)`.
|
/// Determine whether the provided type `T` is a tagged union: `union(enum)`.
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ pub const Key = packed struct {
|
|||||||
((this.cp >= 0x0080 and this.cp <= 0x07FF) or
|
((this.cp >= 0x0080 and this.cp <= 0x07FF) or
|
||||||
(this.cp >= 0x0800 and this.cp <= 0xFFFF) 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 >= 0x100000 and this.cp <= 0x10FFFF)) and // allowed unicode character ranges (2 - 4 byte characters)
|
||||||
(this.cp < 57348 or this.cp > 57454); // no other predifined meanings (i.e. arrow keys, etc.)
|
(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
|
/// Determine if the `Key` is an ascii character that can be printed to
|
||||||
|
|||||||
@@ -91,6 +91,7 @@ pub const Buffered = struct {
|
|||||||
var writer = terminal.writer();
|
var writer = terminal.writer();
|
||||||
const s = this.screen;
|
const s = this.screen;
|
||||||
const vs = this.virtual_screen;
|
const vs = this.virtual_screen;
|
||||||
|
|
||||||
for (0..this.size.y) |row| {
|
for (0..this.size.y) |row| {
|
||||||
for (0..this.size.x) |col| {
|
for (0..this.size.x) |col| {
|
||||||
const idx = (row * this.size.x) + col;
|
const idx = (row * this.size.x) + col;
|
||||||
@@ -123,6 +124,95 @@ 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writeCtrlDWithNewline(this: *@This()) !void {
|
||||||
|
_ = this;
|
||||||
|
_ = try terminal.write("^D\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writeNewline(this: *@This()) !void {
|
||||||
|
_ = this;
|
||||||
|
_ = try terminal.write("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 std = @import("std");
|
||||||
const meta = std.meta;
|
const meta = std.meta;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ pub fn setCursorPosition(pos: Point) !void {
|
|||||||
_ = try posix.write(posix.STDIN_FILENO, value);
|
_ = 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
|
// Needs Raw mode (no wait for \n) to work properly cause
|
||||||
// control sequence will not be written without it.
|
// control sequence will not be written without it.
|
||||||
_ = try posix.write(posix.STDIN_FILENO, "\x1b[6n");
|
_ = try posix.write(posix.STDIN_FILENO, "\x1b[6n");
|
||||||
|
|||||||
@@ -211,13 +211,16 @@ pub fn expectEqualCells(origin: Point, size: Point, expected: []const Cell, actu
|
|||||||
if (!differ) return;
|
if (!differ) return;
|
||||||
|
|
||||||
// test failed
|
// test failed
|
||||||
var stdout_buffer: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
const io = debug.lockStderr(&stdout_buffer);
|
|
||||||
defer debug.unlockStderr();
|
|
||||||
|
|
||||||
const error_writer = &io.file_writer.interface;
|
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.writeAll(writer.buffer[0..writer.end]);
|
||||||
try error_writer.flush();
|
try error_writer.flush();
|
||||||
|
|
||||||
return error.TestExpectEqualCells;
|
return error.TestExpectEqualCells;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user