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.
This commit is contained in:
55
src/app.zig
55
src/app.zig
@@ -31,6 +31,28 @@ pub fn App(comptime M: type, comptime E: type) type {
|
||||
quit_event: Thread.ResetEvent,
|
||||
termios: ?posix.termios = null,
|
||||
winch_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;
|
||||
@@ -48,10 +70,12 @@ pub fn App(comptime M: type, comptime E: type) type {
|
||||
.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)
|
||||
@@ -71,21 +95,21 @@ pub fn App(comptime M: type, comptime E: type) type {
|
||||
this.quit_event.reset();
|
||||
this.thread = try Thread.spawn(.{}, @This().run, .{this});
|
||||
|
||||
//var termios: posix.termios = undefined;
|
||||
//try terminal.enableRawMode(&termios);
|
||||
//if (this.termios) |_| {} else this.termios = termios;
|
||||
var termios: posix.termios = undefined;
|
||||
if (this.config.rawMode) try terminal.enableRawMode(&termios);
|
||||
if (this.termios) |_| {} else this.termios = termios;
|
||||
|
||||
//try terminal.enterAltScreen();
|
||||
//try terminal.saveScreen();
|
||||
//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.restoreScreen();
|
||||
//try terminal.exitAltScreen();
|
||||
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;
|
||||
@@ -94,13 +118,10 @@ pub fn App(comptime M: type, comptime E: type) type {
|
||||
|
||||
pub fn stop(this: *@This()) !void {
|
||||
try this.interrupt();
|
||||
if (this.config.hideCursor) try terminal.showCursor();
|
||||
if (this.config.saveScreen) try terminal.resetCursor();
|
||||
if (this.termios) |termios| {
|
||||
try terminal.disableMouseSupport();
|
||||
try terminal.showCursor();
|
||||
try terminal.resetCursor();
|
||||
try terminal.restoreScreen();
|
||||
try terminal.disableRawMode(&termios);
|
||||
try terminal.exitAltScreen();
|
||||
if (this.config.rawMode) try terminal.disableRawMode(&termios);
|
||||
this.termios = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,8 +144,6 @@ pub const Direct = struct {
|
||||
}
|
||||
|
||||
pub fn resize(this: *@This()) !Point {
|
||||
// TODO the size in the y-axis is *not fixed* and *not limited*
|
||||
// how can I achive this?
|
||||
this.size = .{};
|
||||
if (!this.resized) {
|
||||
this.gpa.free(this.screen);
|
||||
@@ -160,17 +158,17 @@ pub const Direct = struct {
|
||||
try terminal.clearScreen();
|
||||
}
|
||||
|
||||
/// Render provided cells at size (anchor and dimension) into the *virtual screen*.
|
||||
/// 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;
|
||||
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);
|
||||
@@ -179,7 +177,6 @@ pub const Direct = struct {
|
||||
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];
|
||||
@@ -200,6 +197,7 @@ pub const Direct = struct {
|
||||
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!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,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");
|
||||
|
||||
Reference in New Issue
Block a user