Skip to content

Commit

Permalink
Use anytype instead of AnyReader/AnyWriter
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
Arnau478 committed Jul 2, 2024
1 parent 45b477f commit b4b60e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/hevi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub const TextColor = struct {
};
};

pub fn ansiCode(self: TextColor, writer: std.io.AnyWriter) !void {
pub fn ansiCode(self: TextColor, writer: anytype) !void {
if (self.foreground) |foreground| {
switch (foreground) {
.standard => |standard| _ = try writer.write(switch (standard) {
Expand Down Expand Up @@ -185,7 +185,7 @@ pub const Parser = enum {
}
};

fn getColors(allocator: std.mem.Allocator, reader: std.io.AnyReader, options: DisplayOptions) ![]const PaletteColor {
fn getColors(allocator: std.mem.Allocator, reader: anytype, options: DisplayOptions) ![]const PaletteColor {
const data = try reader.readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(data);

Expand Down Expand Up @@ -224,7 +224,7 @@ const DisplayLineOptions = struct {
raw: bool,
};

fn displayLine(line: []const u8, colors: []const TextColor, writer: std.io.AnyWriter, options: DisplayLineOptions) !void {
fn displayLine(line: []const u8, colors: []const TextColor, writer: anytype, options: DisplayLineOptions) !void {
if (!options.raw) {
if (options.color) {
try writer.print("\x1b[2m|\x1b[0m ", .{});
Expand Down Expand Up @@ -286,7 +286,7 @@ fn displayLine(line: []const u8, colors: []const TextColor, writer: std.io.AnyWr
try writer.print("\n", .{});
}

fn printBuffer(line: []const u8, colors: []const TextColor, count: usize, writer: std.io.AnyWriter, options: DisplayOptions) !void {
fn printBuffer(line: []const u8, colors: []const TextColor, count: usize, writer: anytype, options: DisplayOptions) !void {
if (options.show_offset) {
if (options.uppercase) {
try writer.print("{X:0>8} ", .{count});
Expand All @@ -301,7 +301,7 @@ fn printBuffer(line: []const u8, colors: []const TextColor, count: usize, writer
});
}

fn display(reader: std.io.AnyReader, colors: []const TextColor, raw_writer: std.io.AnyWriter, options: DisplayOptions) !void {
fn display(reader: anytype, colors: []const TextColor, raw_writer: anytype, options: DisplayOptions) !void {
var count: usize = 0;

var buf: [16]u8 = undefined;
Expand All @@ -312,7 +312,7 @@ fn display(reader: std.io.AnyReader, colors: []const TextColor, raw_writer: std.
var lines_skipped: usize = 0;

var buf_writer = std.io.bufferedWriter(raw_writer);
const writer = buf_writer.writer().any();
const writer = buf_writer.writer();

while (true) {
const line_len = try reader.readAll(&buf);
Expand Down Expand Up @@ -374,10 +374,10 @@ fn display(reader: std.io.AnyReader, colors: []const TextColor, raw_writer: std.
}

/// Dump `data` to `writer`
pub fn dump(allocator: std.mem.Allocator, data: []const u8, writer: std.io.AnyWriter, options: DisplayOptions) !void {
pub fn dump(allocator: std.mem.Allocator, data: []const u8, writer: anytype, options: DisplayOptions) !void {
var fbs = std.io.fixedBufferStream(data);

const colors = try getColors(allocator, fbs.reader().any(), options);
const colors = try getColors(allocator, fbs.reader(), options);
defer allocator.free(colors);

fbs.reset();
Expand All @@ -401,7 +401,7 @@ pub fn dump(allocator: std.mem.Allocator, data: []const u8, writer: std.io.AnyWr
}

try display(
fbs.reader().any(),
fbs.reader(),
text_colors,
writer,
new_options,
Expand All @@ -417,7 +417,7 @@ fn testDump(expected: []const u8, input: []const u8, options: DisplayOptions) !v
defer out.deinit();
try out.ensureTotalCapacity(expected.len);

try dump(std.testing.allocator, input, out.writer().any(), options);
try dump(std.testing.allocator, input, out.writer(), options);

try std.testing.expectEqualSlices(u8, expected, out.items);
}
Expand Down
10 changes: 6 additions & 4 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn fail(comptime fmt: []const u8, args: anytype) noreturn {
std.process.exit(1);
}

fn printPalette(opts: hevi.DisplayOptions, writer: std.io.AnyWriter) !void {
fn printPalette(opts: hevi.DisplayOptions, writer: anytype) !void {
try writer.print(" (alt) (accent)\n", .{});
try writer.print("(main) ", .{});
try opts.palette.normal.ansiCode(writer);
Expand Down Expand Up @@ -82,7 +82,7 @@ pub fn main() void {
};

if (parsed_args.show_palette != null and parsed_args.show_palette.?) {
printPalette(opts, stdout.writer().any()) catch |err| switch (err) {
printPalette(opts, stdout.writer()) catch |err| switch (err) {
else => fail("{s}", .{@errorName(err)}),
};
} else {
Expand All @@ -102,9 +102,11 @@ pub fn main() void {

defer allocator.free(data);

hevi.dump(allocator, data, stdout.writer().any(), opts) catch |err| switch (err) {
hevi.dump(allocator, data, stdout.writer(), opts) catch |err| switch (err) {
error.NonMatchingParser => fail("{s} does not match parser {s}", .{ filename, @tagName(opts.parser.?) }),
else => fail("{s}", .{@errorName(err)}),
error.OutOfMemory => fail("Out of memory", .{}),
error.BrokenPipe => fail("Broken pipe", .{}),
else => fail("Error writing to stdout: {s}", .{@errorName(err)}),
};
} else {
var no_args = true;
Expand Down

0 comments on commit b4b60e8

Please sign in to comment.