update example
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
parent
8d496ffcdd
commit
35fcbd05d7
2 changed files with 39 additions and 18 deletions
|
@ -5,6 +5,17 @@ const TextInput = vaxis.widgets.TextInput;
|
||||||
const border = vaxis.widgets.border;
|
const border = vaxis.widgets.border;
|
||||||
|
|
||||||
const log = std.log.scoped(.main);
|
const log = std.log.scoped(.main);
|
||||||
|
|
||||||
|
// Our EventType. This can contain internal events as well as Vaxis events.
|
||||||
|
// Internal events can be posted into the same queue as vaxis events to allow
|
||||||
|
// for a single event loop with exhaustive switching. Booya
|
||||||
|
const Event = union(enum) {
|
||||||
|
key_press: vaxis.Key,
|
||||||
|
winsize: vaxis.Winsize,
|
||||||
|
focus_in,
|
||||||
|
foo: u8,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer {
|
defer {
|
||||||
|
@ -16,14 +27,16 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
const alloc = gpa.allocator();
|
const alloc = gpa.allocator();
|
||||||
|
|
||||||
// Initialize Vaxis
|
// Initialize Vaxis with our event type
|
||||||
var vx = try vaxis.init(Event, .{});
|
var vx = try vaxis.init(Event, .{});
|
||||||
|
// deinit takes an optional allocator. If your program is exiting, you can
|
||||||
|
// choose to pass a null allocator to save some exit time.
|
||||||
defer vx.deinit(alloc);
|
defer vx.deinit(alloc);
|
||||||
|
|
||||||
// Start the read loop. This puts the terminal in raw mode and begins
|
// Start the read loop. This puts the terminal in raw mode and begins
|
||||||
// reading user input
|
// reading user input
|
||||||
try vx.start();
|
try vx.startReadThread();
|
||||||
defer vx.stop();
|
defer vx.stopReadThread();
|
||||||
|
|
||||||
// Optionally enter the alternate screen
|
// Optionally enter the alternate screen
|
||||||
try vx.enterAltScreen();
|
try vx.enterAltScreen();
|
||||||
|
@ -31,9 +44,13 @@ pub fn main() !void {
|
||||||
// We'll adjust the color index every keypress for the border
|
// We'll adjust the color index every keypress for the border
|
||||||
var color_idx: u8 = 0;
|
var color_idx: u8 = 0;
|
||||||
|
|
||||||
|
// init our text input widget. The text input widget needs an allocator to
|
||||||
|
// store the contents of the input
|
||||||
var text_input = TextInput.init(alloc);
|
var text_input = TextInput.init(alloc);
|
||||||
defer text_input.deinit();
|
defer text_input.deinit();
|
||||||
|
|
||||||
|
// Sends queries to terminal to detect certain features. This should
|
||||||
|
// _always_ be called, but is left to the application to decide when
|
||||||
try vx.queryTerminal();
|
try vx.queryTerminal();
|
||||||
|
|
||||||
// The main event loop. Vaxis provides a thread safe, blocking, buffered
|
// The main event loop. Vaxis provides a thread safe, blocking, buffered
|
||||||
|
@ -59,9 +76,22 @@ pub fn main() !void {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
.winsize => |ws| {
|
// winsize events are sent to the application to ensure that all
|
||||||
try vx.resize(alloc, ws);
|
// resizes occur in the main thread. This lets us avoid expensive
|
||||||
},
|
// locks on the screen. All applications must handle this event
|
||||||
|
// unless they aren't using a screen (IE only detecting features)
|
||||||
|
//
|
||||||
|
// This is the only call that the core of Vaxis needs an allocator
|
||||||
|
// for. The allocations are because we keep a copy of each cell to
|
||||||
|
// optimize renders. When resize is called, we allocated two slices:
|
||||||
|
// one for the screen, and one for our buffered screen. Each cell in
|
||||||
|
// the buffered screen contains an ArrayList(u8) to be able to store
|
||||||
|
// the grapheme for that cell Each cell is initialized with a size
|
||||||
|
// of 1, which is sufficient for all of ASCII. Anything requiring
|
||||||
|
// more than one byte will incur an allocation on the first render
|
||||||
|
// after it is drawn. Thereafter, it will not allocate unless the
|
||||||
|
// screen is resized
|
||||||
|
.winsize => |ws| try vx.resize(alloc, ws),
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +99,7 @@ pub fn main() !void {
|
||||||
// terminal and can spawn child windows as logical areas. Child windows
|
// terminal and can spawn child windows as logical areas. Child windows
|
||||||
// cannot draw outside of their bounds
|
// cannot draw outside of their bounds
|
||||||
const win = vx.window();
|
const win = vx.window();
|
||||||
|
|
||||||
// Clear the entire space because we are drawing in immediate mode.
|
// Clear the entire space because we are drawing in immediate mode.
|
||||||
// vaxis double buffers the screen. This new frame will be compared to
|
// vaxis double buffers the screen. This new frame will be compared to
|
||||||
// the old and only updated cells will be drawn
|
// the old and only updated cells will be drawn
|
||||||
|
@ -89,13 +120,3 @@ pub fn main() !void {
|
||||||
try vx.render();
|
try vx.render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our EventType. This can contain internal events as well as Vaxis events.
|
|
||||||
// Internal events can be posted into the same queue as vaxis events to allow
|
|
||||||
// for a single event loop with exhaustive switching. Booya
|
|
||||||
const Event = union(enum) {
|
|
||||||
key_press: vaxis.Key,
|
|
||||||
winsize: vaxis.Winsize,
|
|
||||||
focus_in,
|
|
||||||
foo: u8,
|
|
||||||
};
|
|
||||||
|
|
|
@ -109,7 +109,7 @@ pub fn Vaxis(comptime T: type) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// spawns the input thread to start listening to the tty for input
|
/// spawns the input thread to start listening to the tty for input
|
||||||
pub fn start(self: *Self) !void {
|
pub fn startReadThread(self: *Self) !void {
|
||||||
self.tty = try Tty.init();
|
self.tty = try Tty.init();
|
||||||
// run our tty read loop in it's own thread
|
// run our tty read loop in it's own thread
|
||||||
const read_thread = try std.Thread.spawn(.{}, Tty.run, .{ &self.tty.?, T, self });
|
const read_thread = try std.Thread.spawn(.{}, Tty.run, .{ &self.tty.?, T, self });
|
||||||
|
@ -117,7 +117,7 @@ pub fn Vaxis(comptime T: type) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// stops reading from the tty
|
/// stops reading from the tty
|
||||||
pub fn stop(self: *Self) void {
|
pub fn stopReadThread(self: *Self) void {
|
||||||
if (self.tty) |_| {
|
if (self.tty) |_| {
|
||||||
var tty = &self.tty.?;
|
var tty = &self.tty.?;
|
||||||
tty.stop();
|
tty.stop();
|
||||||
|
|
Loading…
Reference in a new issue