Skip to content

Commit

Permalink
Merge pull request #24 from Arnau478/module
Browse files Browse the repository at this point in the history
Expose hevi as a module
  • Loading branch information
Arnau478 authored May 12, 2024
2 parents 27a3023 + aee6fe7 commit 3a4996c
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 419 deletions.
33 changes: 28 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ fn getVersion(b: *std.Build) SemanticVersion {
}
}

fn addExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, release: bool) *std.Build.Step.Compile {
fn addExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, release: bool, build_options: *std.Build.Step.Options, hevi_mod: *std.Build.Module) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = if (release) b.fmt("hevi-{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag) }) else "hevi",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});

var build_options = b.addOptions();
build_options.addOption(SemanticVersion, "version", getVersion(b));
exe.root_module.addImport("hevi", hevi_mod);
exe.root_module.addOptions("build_options", build_options);

return exe;
Expand All @@ -88,12 +87,36 @@ pub fn build(b: *std.Build) !void {

const optimize = b.standardOptimizeOption(.{});

const exe = addExe(b, target, optimize, false);
var build_options = b.addOptions();
build_options.addOption(SemanticVersion, "version", getVersion(b));

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

const exe = addExe(b, target, optimize, false, build_options, mod);
b.installArtifact(exe);

const docs_step = b.step("docs", "Build the documentation");

const obj = b.addObject(.{
.name = "hevi",
.target = target,
.optimize = .Debug,
.root_source_file = .{ .path = "src/hevi.zig" },
});

obj.root_module.addOptions("build_options", build_options);

docs_step.dependOn(&b.addInstallDirectory(.{
.source_dir = obj.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
}).step);

const release_step = b.step("release", "Create release builds for all targets");
for (release_targets) |rt| {
const rexe = addExe(b, b.resolveTargetQuery(rt), .ReleaseSmall, true);
const rexe = addExe(b, b.resolveTargetQuery(rt), .ReleaseSmall, true, build_options, mod);
release_step.dependOn(&b.addInstallArtifact(rexe, .{ .dest_sub_path = try std.fs.path.join(b.allocator, &.{ "release", rexe.name }) }).step);
}

Expand Down
36 changes: 36 additions & 0 deletions src/DisplayOptions.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const std = @import("std");
const DisplayOptions = @This();

/// Whether to use color or not
color: bool,
/// If true, uses uppercase; otherwise lowercase
uppercase: bool,
/// Print the size of the file at the end
show_size: bool,
/// Show a column with the offset into the file
show_offset: bool,
/// Show a column with the ASCII interpretation
show_ascii: bool,
/// Skip lines if they're the same as the one before and after it
skip_lines: bool,
/// Override the binary parser that is used
parser: ?OptionString = null,

pub const OptionString = struct {
is_allocated: bool = false,
string: []const u8,

pub fn safeSet(allocator: std.mem.Allocator, options: *DisplayOptions, s: []const u8) void {
if (options.parser) |parser| {
if (parser.is_allocated) allocator.free(parser.string);
}

options.parser = .{ .string = s };
}
};

pub fn deinit(self: DisplayOptions, allocator: std.mem.Allocator) void {
if (self.parser) |parser| {
if (parser.is_allocated) allocator.free(parser.string);
}
}
22 changes: 4 additions & 18 deletions src/argparse.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const build_options = @import("build_options");
const std = @import("std");

const parsers = @import("parser.zig").parsers;
const build_options = @import("build_options");
const hevi = @import("hevi");

pub const ParseResult = struct {
filename: []const u8,
Expand Down Expand Up @@ -39,19 +38,6 @@ fn printError(comptime fmt: []const u8, args: anytype) noreturn {
}

fn printHelp() noreturn {
const parser_list: [parsers.len][]const u8 = comptime v: {
var list: [parsers.len][]const u8 = undefined;
var pos: usize = 0;
for (parsers) |parser| {
var split = std.mem.splitAny(u8, @typeName(parser), ".");
_ = split.first();

list[pos] = split.next().?;
pos += 1;
}
break :v list;
};

std.debug.print(
\\hevi - hex viewer
\\
Expand All @@ -71,12 +57,12 @@ fn printHelp() noreturn {
\\
, .{});

for (parser_list) |parser| {
for (std.enums.values(hevi.Parser)) |parser| {
// How many tabs (4 spaces) we want to print
for (0..9) |_| {
std.debug.print(" ", .{});
}
std.debug.print("- {s}\n", .{parser});
std.debug.print("- {s}\n", .{@tagName(parser)});
}

std.debug.print(
Expand Down
Loading

0 comments on commit 3a4996c

Please sign in to comment.