Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor typo fixing and code refactoring #2

Merged
merged 4 commits into from
Feb 11, 2024
Merged
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.zig text eol=lf
*.zon text eol=lf
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const border = vaxis.widgets.border;

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

// Our EventType. This can contain internal events as well as Vaxis events.
// 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) {
Expand Down Expand Up @@ -102,20 +102,20 @@ pub fn main() !void {

// The main event loop. Vaxis provides a thread safe, blocking, buffered
// queue which can serve as the primary event queue for an application
outer: while (true) {
while (true) {
// nextEvent blocks until an event is in the queue
const event = vx.nextEvent();
log.debug("event: {}\r\n", .{event});
// exhaustive switching ftw. Vaxis will send events if your EventType
// enum has the fields for those events (ie "key_press", "winsize")
log.debug("event: {}", .{event});
// exhaustive switching ftw. Vaxis will send events if your Event enum
// has the fields for those events (ie "key_press", "winsize")
switch (event) {
.key_press => |key| {
color_idx = switch (color_idx) {
255 => 0,
else => color_idx + 1,
};
if (key.matches('c', .{ .ctrl = true })) {
break :outer;
break;
} else if (key.matches('l', .{ .ctrl = true })) {
vx.queueRefresh();
} else {
Expand Down
67 changes: 38 additions & 29 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,61 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_source_file = std.Build.LazyPath.relative("src/main.zig");

const vaxis = b.addModule("vaxis", .{ .root_source_file = .{ .path = "src/main.zig" } });

const ziglyph = b.dependency("ziglyph", .{
// Dependencies
const ziglyph_dep = b.dependency("ziglyph", .{
.optimize = optimize,
.target = target,
});
vaxis.addImport("ziglyph", ziglyph.module("ziglyph"));

const zigimg = b.dependency("zigimg", .{
const zigimg_dep = b.dependency("zigimg", .{
.optimize = optimize,
.target = target,
});
vaxis.addImport("zigimg", zigimg.module("zigimg"));

const exe = b.addExecutable(.{
.name = "vaxis",
.root_source_file = .{ .path = "examples/pathological.zig" },
// Module
const vaxis_mod = b.addModule("vaxis", .{ .root_source_file = root_source_file });
vaxis_mod.addImport("ziglyph", ziglyph_dep.module("ziglyph"));
vaxis_mod.addImport("zigimg", zigimg_dep.module("zigimg"));

// Examples
const example_step = b.step("example", "Run examples");

const example = b.addExecutable(.{
.name = "vaxis_pathological_example",
.root_source_file = std.Build.LazyPath.relative("examples/pathological.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("vaxis", vaxis);

const run_cmd = b.addRunArtifact(exe);
example.root_module.addImport("vaxis", vaxis_mod);

run_cmd.step.dependOn(b.getInstallStep());
const example_run = b.addRunArtifact(example);
example_step.dependOn(&example_run.step);
b.default_step.dependOn(example_step);

if (b.args) |args| {
run_cmd.addArgs(args);
}
// Tests
const tests_step = b.step("test", "Run tests");

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
const tests = b.addTest(.{
.root_source_file = root_source_file,
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addImport("ziglyph", ziglyph.module("ziglyph"));
lib_unit_tests.root_module.addImport("zigimg", zigimg.module("zigimg"));
tests.root_module.addImport("ziglyph", ziglyph_dep.module("ziglyph"));
tests.root_module.addImport("zigimg", zigimg_dep.module("zigimg"));

const tests_run = b.addRunArtifact(tests);
tests_step.dependOn(&tests_run.step);
b.default_step.dependOn(tests_step);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
// Lints
const lints_step = b.step("lint", "Run lints");

const lints = b.addFmt(.{
.paths = &.{ "src", "build.zig" },
.check = true,
});

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
lints_step.dependOn(&lints.step);
b.default_step.dependOn(lints_step);
}
35 changes: 7 additions & 28 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
.{
.name = "vaxis",
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
.version = "0.1.0",

// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",

.paths = .{""},
.dependencies = .{
.ziglyph = .{
.url = "https://codeberg.org/dude_the_builder/ziglyph/archive/ac50ab06c91d2dd632ff4573c035dafe3b374aba.tar.gz",
.hash = "1220e097fbfb3a15a6f3484cf507f1f10ab571d1bcf519c3b5447ca727782b7a5264",
.url = "https://codeberg.org/dude_the_builder/ziglyph/archive/ac50ab06c9.tar.gz",
.hash = "1220e097fbfb3a15a6f3484cf507f1f10ab571d1bcf519c3b5447ca727782b7a5264",
},
.zigimg = .{
.url = "https://github.com/zigimg/zigimg/archive/2224f91.tar.gz",
.hash = "12207067e4892c48369415268648380859baa89c324748ae5bfda414a12868c9fc8b",
},
.zigimg = .{
.url = "https://github.com/zigimg/zigimg/archive/f6998808f283f8d3c2ef34e8b4af423bc1786f32.tar.gz",
.hash = "12202ee5d22ade0c300e9e7eae4c1951bda3d5f236fe1a139eb3613b43e2f12a88db",
}
},

.paths = .{
// This makes *all* files, recursively, included in this package. It is generally
// better to explicitly list the files and directories instead, to insure that
// fetching from tarballs, file system paths, and version control all result
// in the same contents hash.
"",
// For example...
//"build.zig",
//"build.zig.zon",
//"src",
//"LICENSE",
//"README.md",
},
}
2 changes: 1 addition & 1 deletion examples/image.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn main() !void {

const img = imgs[n];
const dims = try img.cellSize(win);
const center = vaxis.alignment.center(win, dims.cols, dims.rows);
const center = vaxis.widgets.alignment.center(win, dims.cols, dims.rows);
const scale = false;
const z_index = 0;
img.draw(center, scale, z_index);
Expand Down
10 changes: 5 additions & 5 deletions examples/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ pub fn main() !void {

// The main event loop. Vaxis provides a thread safe, blocking, buffered
// queue which can serve as the primary event queue for an application
outer: while (true) {
while (true) {
// nextEvent blocks until an event is in the queue
const event = vx.nextEvent();
log.debug("event: {}\r\n", .{event});
// exhaustive switching ftw. Vaxis will send events if your EventType
log.debug("event: {}", .{event});
// exhaustive switching ftw. Vaxis will send events if your Event
// enum has the fields for those events (ie "key_press", "winsize")
switch (event) {
.key_press => |key| {
Expand All @@ -45,7 +45,7 @@ pub fn main() !void {
else => color_idx + 1,
};
if (key.codepoint == 'c' and key.mods.ctrl) {
break :outer;
break;
}
},
.winsize => |ws| {
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn main() !void {
}
}

// Our EventType. This can contain internal events as well as Vaxis events.
// 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
const Event = union(enum) {
Expand Down
4 changes: 2 additions & 2 deletions examples/pathological.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ pub fn main() !void {
try vx.enterAltScreen();
try vx.queryTerminal();

outer: while (true) {
while (true) {
const event = vx.nextEvent();
switch (event) {
.winsize => |ws| {
try vx.resize(alloc, ws);
break :outer;
break;
},
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/text_input.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const border = vaxis.widgets.border;

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

// Our EventType. This can contain internal events as well as Vaxis events.
// 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
const Event = union(enum) {
Expand Down Expand Up @@ -59,11 +59,11 @@ pub fn main() !void {

// The main event loop. Vaxis provides a thread safe, blocking, buffered
// queue which can serve as the primary event queue for an application
outer: while (true) {
while (true) {
// nextEvent blocks until an event is in the queue
const event = vx.nextEvent();
log.debug("event: {}\r\n", .{event});
// exhaustive switching ftw. Vaxis will send events if your EventType
log.debug("event: {}", .{event});
// exhaustive switching ftw. Vaxis will send events if your Event
// enum has the fields for those events (ie "key_press", "winsize")
switch (event) {
.key_press => |key| {
Expand All @@ -72,7 +72,7 @@ pub fn main() !void {
else => color_idx + 1,
};
if (key.matches('c', .{ .ctrl = true })) {
break :outer;
break;
} else if (key.matches('l', .{ .ctrl = true })) {
vx.queueRefresh();
} else if (key.matches('n', .{ .ctrl = true })) {
Expand Down
12 changes: 5 additions & 7 deletions src/cell.zig → src/Cell.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const Image = @import("Image.zig");

pub const Cell = struct {
char: Character = .{},
style: Style = .{},
link: Hyperlink = .{},
image: ?Image.Placement = null,
};
char: Character = .{},
style: Style = .{},
link: Hyperlink = .{},
image: ?Image.Placement = null,

/// Segment is a contiguous run of text that has a constant style
pub const Segment = struct {
Expand All @@ -17,7 +15,7 @@ pub const Segment = struct {
pub const Character = struct {
grapheme: []const u8 = " ",
/// width should only be provided when the application is sure the terminal
/// will meeasure the same width. This can be ensure by using the gwidth method
/// will measure the same width. This can be ensure by using the gwidth method
/// included in libvaxis. If width is 0, libvaxis will measure the glyph at
/// render time
width: usize = 1,
Expand Down
4 changes: 2 additions & 2 deletions src/InternalScreen.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;
const Style = @import("cell.zig").Style;
const Cell = @import("cell.zig").Cell;
const Style = @import("Cell.zig").Style;
const Cell = @import("Cell.zig");
const Shape = @import("Mouse.zig").Shape;

const log = std.log.scoped(.internal_screen);
Expand Down
2 changes: 1 addition & 1 deletion src/Screen.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;

const Cell = @import("cell.zig").Cell;
const Cell = @import("Cell.zig");
const Shape = @import("Mouse.zig").Shape;
const Image = @import("Image.zig");
const Winsize = @import("Tty.zig").Winsize;
Expand Down
Loading