Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions examples/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const Cell = vaxis.Cell;
const TextInput = vaxis.widgets.TextInput;

const log = std.log.scoped(.main);

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main, .level = .err },
.{ .scope = .vaxis, .level = .err },
},
};

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
Expand All @@ -27,6 +35,7 @@ pub fn main() !void {
try loop.start();
defer loop.stop();

try vx.enterAltScreen(tty.writer());
try vx.queryTerminal(tty.writer(), 1 * std.time.ns_per_s);

var text_input = TextInput.init(alloc);
Expand Down Expand Up @@ -65,7 +74,7 @@ pub fn main() !void {
}
} else if (key.matches(vaxis.Key.enter, .{}) or key.matches('j', .{ .ctrl = true })) {
if (selected_option) |i| {
log.err("enter", .{});
log.debug("enter", .{});
try text_input.insertSliceAtCursor(options[i]);
selected_option = null;
}
Expand All @@ -88,7 +97,7 @@ pub fn main() !void {
if (selected_option) |i| {
win.hideCursor();
for (options, 0..) |opt, j| {
log.err("i = {d}, j = {d}, opt = {s}", .{ i, j, opt });
log.debug("i = {d}, j = {d}, opt = {s}", .{ i, j, opt });
var seg = [_]vaxis.Segment{.{
.text = opt,
.style = if (j == i) .{ .reverse = true } else .{},
Expand Down
6 changes: 6 additions & 0 deletions examples/counter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ const std = @import("std");
const vaxis = @import("vaxis");
const vxfw = vaxis.vxfw;

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

/// Our main application state
const Model = struct {
/// State of the counter
Expand Down
17 changes: 15 additions & 2 deletions examples/fuzzy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ const std = @import("std");
const vaxis = @import("vaxis");
const vxfw = vaxis.vxfw;

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

const Model = struct {
list: std.ArrayList(vxfw.Text),
/// Memory owned by .arena
filtered: std.ArrayList(vxfw.RichText),
list_view: vxfw.ListView,
text_field: vxfw.TextField,

Expand Down Expand Up @@ -223,7 +228,15 @@ pub fn main() !void {
defer stderr.deinit(gpa);
try fd.spawn();
try fd.collectOutput(gpa, &stdout, &stderr, 10_000_000);
_ = try fd.wait();
_ = fd.wait() catch |err| {
if (err == error.FileNotFound) {
var err_writer = std.fs.File.stderr().writer(&.{});
try err_writer.interface.writeAll("error: The 'fd' command was not found on PATH, please install it for this demo.\n");
try err_writer.interface.flush();
}

return err;
};

var iter = std.mem.splitScalar(u8, stdout.items, '\n');
while (iter.next()) |line| {
Expand Down
12 changes: 9 additions & 3 deletions examples/image.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ const vaxis = @import("vaxis");

const log = std.log.scoped(.main);

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main , .level = .err },
.{ .scope = .vaxis, .level = .err },
},
};

const Event = union(enum) {
key_press: vaxis.Key,
winsize: vaxis.Winsize,
Expand Down Expand Up @@ -40,9 +47,8 @@ pub fn main() !void {

const imgs = [_]vaxis.Image{
try vx.transmitImage(alloc, tty.writer(), &img1, .rgba),
// var img1 = try vaxis.zigimg.Image.fromFilePath(alloc, "examples/zig.png");
// try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/zig.png" }),
try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/vaxis.png" }),
try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/zig.png" }),
// try vx.loadImage(alloc, tty.writer(), .{ .path = "examples/vaxis.png" }),
};
defer vx.freeImage(tty.writer(), imgs[0].id);
defer vx.freeImage(tty.writer(), imgs[1].id);
Expand Down
6 changes: 6 additions & 0 deletions examples/list_view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ const Text = vxfw.Text;
const ListView = vxfw.ListView;
const Widget = vxfw.Widget;

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

const Model = struct {
list_view: ListView,

Expand Down
8 changes: 8 additions & 0 deletions examples/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ const vaxis = @import("vaxis");
const Cell = vaxis.Cell;

const log = std.log.scoped(.main);

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main, .level = .err },
.{ .scope = .vaxis, .level = .err },
},
};

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
Expand Down
6 changes: 6 additions & 0 deletions examples/scroll.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ const std = @import("std");
const vaxis = @import("vaxis");
const vxfw = vaxis.vxfw;

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

const ModelRow = struct {
text: []const u8,
idx: usize,
Expand Down
8 changes: 8 additions & 0 deletions examples/split_view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ const std = @import("std");
const vaxis = @import("vaxis");
const vxfw = vaxis.vxfw;


pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};


const Model = struct {
split: vxfw.SplitView,
lhs: vxfw.Text,
Expand Down
8 changes: 8 additions & 0 deletions examples/table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ const vaxis = @import("vaxis");

const log = std.log.scoped(.main);

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main, .level = .err },
.{ .scope = .vaxis, .level = .err },
},
};

const ActiveSection = enum {
top,
mid,
Expand All @@ -21,6 +28,7 @@ pub fn main() !void {

// Users set up below the main function
const users_buf = try alloc.dupe(User, users[0..]);
defer alloc.free(users_buf);

var buffer: [1024]u8 = undefined;
var tty = try vaxis.Tty.init(&buffer);
Expand Down
7 changes: 7 additions & 0 deletions examples/text_input.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const border = vaxis.widgets.border;

const log = std.log.scoped(.main);

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main, .level = .err },
.{ .scope = .vaxis, .level = .err },
},
};

// Our Event. 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
Expand Down
11 changes: 9 additions & 2 deletions examples/text_view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const Event = union(enum) {
winsize: vaxis.Winsize,
};

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};

Expand Down Expand Up @@ -47,7 +53,7 @@ pub fn main() !void {
switch (event) {
.key_press => |key| {
// Close demo
if (key.matches('c', .{})) break;
if (key.matches('c', .{}) or key.matches('c', .{ .ctrl = true })) break;
if (key.matches(vaxis.Key.enter, .{})) {
counter += 1;
const new_content = try std.fmt.bufPrint(&lineBuf, "\nLine {d}", .{counter});
Expand All @@ -57,10 +63,11 @@ pub fn main() !void {
},
.winsize => |ws| try vx.resize(alloc, tty.writer(), ws),
}

const win = vx.window();
win.clear();
text_view.draw(win, text_view_buffer);
try vx.render(tty.writer());
try tty.writer.flush();
try tty.writer().flush();
}
}
6 changes: 6 additions & 0 deletions examples/vaxis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ const Event = union(enum) {

pub const panic = vaxis.panic_handler;

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
Expand Down
7 changes: 7 additions & 0 deletions examples/view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const Event = union(enum) {
winsize: vaxis.Winsize,
};

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main, .level = .err },
.{ .scope = .vaxis, .level = .err },
},
};

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
Expand Down
17 changes: 15 additions & 2 deletions examples/vt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@ const Event = union(enum) {

pub const panic = vaxis.panic_handler;

const log = std.log.scoped(.main);

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .main, .level = .err },
.{ .scope = .terminal, .level = .err },
.{ .scope = .vaxis, .level = .err },
.{ .scope = .vaxis_terminal, .level = .err },
},
};


pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) {
std.log.err("memory leak", .{});
log.err("memory leak", .{});
}
}
const alloc = gpa.allocator();

var buffer: [1024]u8 = undefined;
var tty = try vaxis.Tty.init(&buffer);
defer tty.deinit();

const writer = tty.writer();
var vx = try vaxis.init(alloc, .{});
defer vx.deinit(alloc, writer);
Expand Down Expand Up @@ -107,7 +121,6 @@ pub fn main() !void {
.y_pixel = 0,
});
try vt.draw(alloc, child);

try vx.render(writer);
}
}
6 changes: 6 additions & 0 deletions src/Image.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const math = std.math;
const base64 = std.base64.standard.Encoder;
const zigimg = @import("zigimg");

pub const std_options: std.Options = .{
.log_scope_levels = &[_]std.log.ScopeLevel{
.{ .scope = .vaxis, .level = .err },
},
};

const Window = @import("Window.zig");

const Image = @This();
Expand Down
12 changes: 6 additions & 6 deletions src/vxfw/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ const MouseHandler = struct {
ctx.phase = .capturing;
for (hits.items) |item| {
var m_local = mouse;
m_local.col = item.local.col;
m_local.row = item.local.row;
m_local.col = @intCast(item.local.col);
m_local.row = @intCast(item.local.row);
try item.widget.captureEvent(ctx, .{ .mouse = m_local });
try app.handleCommand(&ctx.cmds);

Expand All @@ -475,8 +475,8 @@ const MouseHandler = struct {
ctx.phase = .at_target;
{
var m_local = mouse;
m_local.col = target.local.col;
m_local.row = target.local.row;
m_local.col = @intCast(target.local.col);
m_local.row = @intCast(target.local.row);
try target.widget.handleEvent(ctx, .{ .mouse = m_local });
try app.handleCommand(&ctx.cmds);

Expand All @@ -487,8 +487,8 @@ const MouseHandler = struct {
ctx.phase = .bubbling;
while (hits.pop()) |item| {
var m_local = mouse;
m_local.col = item.local.col;
m_local.row = item.local.row;
m_local.col = @intCast(item.local.col);
m_local.row = @intCast(item.local.row);
try item.widget.handleEvent(ctx, .{ .mouse = m_local });
try app.handleCommand(&ctx.cmds);

Expand Down