Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnau478 committed Dec 6, 2023
1 parent d480356 commit e6dc827
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 38 deletions.
72 changes: 72 additions & 0 deletions src/argparse.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const std = @import("std");

const ParseResult = struct{
filename: []const u8,
color: ?bool,
uppercase: ?bool,
size: ?bool,
};

const Flag = union(enum){
help,
toggle,
}

fn printError(comptime fmt: []const u8, args: anytype) noreturn {
std.debug.print("Error: " ++ fmt ++ "\nTip: use `--help` for help\n", args);
std.process.exit(1);
}

fn printHelp() noreturn {
std.debug.print(
\\hevi - hex viewer
\\
\\Usage:
\\ hevi <file> [flags]
\\
\\Flags:
\\ -h, --help Print this help message
\\ --color, --no-color Enable or disable output coloring
\\ --lowercase, --uppercase Switch between lowercase and uppercase hex
\\ --size, --no-size Enable or disable showing the size at the end
\\
\\Made by Arnau478
);
std.process.exit(0);
}

pub fn parse(args) ParseResult {
var filename: ?[]const u8 = null;

for(args) |arg| {
if(arg[0] == '-') {
if(arg.len <= 1) {
printError("expected flag", .{});
}
switch(arg[1]) {
'h' => {
printHelp();
},
'-' => {
const name = arg[2..];
if(name.len == 0) printError("expected flag name", .{});

const flags: []Flag = &.{

};
},
else => |f| printError("invalid flag `{s}`", .{arg}),
}
}
else {
if(filename) {
printError("multiple files specified", .{});
}
else filename = arg;
}
}

return .{
.filename = filename orelse printError("no file specified", .{}),
};
}
46 changes: 8 additions & 38 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const clap = @import("clap");
const argparse = @import("argparse.zig");
const NormalizedSize = @import("NormalizedSize.zig");

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
Expand Down Expand Up @@ -102,49 +102,19 @@ fn display(reader: anytype, writer: anytype, options: DisplayOptions) !void {
}

pub fn main() !void {
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\--color Enable output coloring
\\--no-color Disable output coloring
\\--uppercase Print uppercase hexadecimal
\\--no-size Do not show the file size at the end
\\<file> The file to open
);

const clap_parsers = comptime .{
.file = clap.parsers.string,
};
var clap_diag = clap.Diagnostic{};
var clap_res = clap.parse(clap.Help, &params, clap_parsers, .{ .diagnostic = &clap_diag }) catch |e| {
clap_diag.report(std.io.getStdErr().writer(), e) catch {};
std.process.exit(1);
};
defer clap_res.deinit();

if (clap_res.args.help != 0) return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});

if (clap_res.positionals.len != 1) {
try std.io.getStdErr().writer().print("Expected one positional, found {}\n", .{clap_res.positionals.len});
std.process.exit(1);
}
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

const filename = clap_res.positionals[0];
const parsed_args = argparse.parse(args);

const file = try std.fs.cwd().openFile(filename, .{});
const file = try std.fs.cwd().openFile(parsed_args.filename, .{});
defer file.close();

const stdout = std.io.getStdOut();

const color_mode_default = stdout.supportsAnsiEscapeCodes();

const color_mode = if (clap_res.args.color != 0 and clap_res.args.@"no-color" == 0) true else if (clap_res.args.@"no-color" != 0 and clap_res.args.color == 0) false else if (clap_res.args.color == 0 and clap_res.args.@"no-color" == 0) color_mode_default else {
try std.io.getStdErr().writer().print("--color and --no-color cannot be specified at the same time\n", .{});
std.process.exit(1);
};

try display(file.reader(), stdout.writer(), .{
.color = color_mode,
.uppercase = clap_res.args.uppercase != 0,
.show_size = clap_res.args.@"no-size" == 0,
.color = parsed_args.color orelse stdout.supportsAnsiEscapeCodes(),
.uppercase = parsed_args.uppercase orelse false,
.show_size = parsed_args.show_size orelse true,
});
}

0 comments on commit e6dc827

Please sign in to comment.