Skip to content

Commit

Permalink
keep up with latest Zig
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Dec 4, 2023
1 parent 9e52dbc commit e002882
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
zig 0.11.0
zig master
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

build:
zig build -Doptimize=ReleaseFast \
-Dis_ci \
-Dbuild_date=$(shell date +"%Y-%m-%dT%H:%M:%S%z") \
-Dgit_commit=$(shell git rev-parse --short HEAD) \
--summary all
Expand Down
18 changes: 7 additions & 11 deletions src/bin/loc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ pub fn main() !void {
opt.positional_args.items[0];

var loc_map = LocMap{};
var iter_dir =
fs.cwd().openIterableDir(file_or_dir, .{}) catch |err| switch (err) {
const dir = fs.cwd().openDir(file_or_dir, .{ .iterate = true }) catch |err| switch (err) {
error.NotDir => {
try populateLoc(allocator, &loc_map, fs.cwd(), file_or_dir);
return printLocMap(
Expand All @@ -226,9 +225,7 @@ pub fn main() !void {
},
else => return err,
};
defer iter_dir.close();

try walk(allocator, &loc_map, iter_dir);
try walk(allocator, &loc_map, dir);
try printLocMap(
allocator,
&loc_map,
Expand Down Expand Up @@ -276,12 +273,12 @@ fn printLocMap(
try std.io.getStdOut().writer().print("{}\n", .{table});
}

fn walk(allocator: std.mem.Allocator, loc_map: *LocMap, dir: fs.IterableDir) anyerror!void {
fn walk(allocator: std.mem.Allocator, loc_map: *LocMap, dir: fs.Dir) anyerror!void {
var it = dir.iterate();
while (try it.next()) |e| {
switch (e.kind) {
.file => {
try populateLoc(allocator, loc_map, dir.dir, e.name);
try populateLoc(allocator, loc_map, dir, e.name);
},
.directory => {
var should_ignore = false;
Expand All @@ -292,9 +289,8 @@ fn walk(allocator: std.mem.Allocator, loc_map: *LocMap, dir: fs.IterableDir) any
}
}
if (!should_ignore) {
var child_dir = try dir.dir.openIterableDir(e.name, .{});
defer child_dir.close();
try walk(allocator, loc_map, child_dir);
const sub_dir = try dir.openDir(e.name, .{ .iterate = true });
try walk(allocator, loc_map, sub_dir);
}
},
else => {},
Expand Down Expand Up @@ -468,7 +464,7 @@ test "trimWhitespace" {
test "LOC Zig/Python/Ruby" {
const allocator = std.testing.allocator;
var loc_map = LocMap{};
var dir = fs.cwd();
const dir = fs.cwd();

const testcases = .{
.{
Expand Down
2 changes: 1 addition & 1 deletion src/bin/pidof.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn findPids(allocator: std.mem.Allocator, opt: Options, program: []const u8)
return error.sysctl;
}

var procList = try allocator.alloc(c.struct_kinfo_proc, procSize / @sizeOf(c.struct_kinfo_proc));
const procList = try allocator.alloc(c.struct_kinfo_proc, procSize / @sizeOf(c.struct_kinfo_proc));
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctl.3.html
rc = c.sysctl(&mib, mib.len, @ptrCast(procList), &procSize, null, 0);
if (rc != 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/repeat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn main() !void {
keep_running = false;
}
}
var term = try run(allocator, argv);
const term = try run(allocator, argv);
switch (term) {
.Exited => |rc| {
if (rc == 0) {
Expand Down
27 changes: 12 additions & 15 deletions src/bin/tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,9 @@ pub fn main() anyerror!void {
_ = try writer.write(root_dir);
_ = try writer.write("\n");

var iter_dir =
try fs.cwd().openIterableDir(root_dir, .{});
defer iter_dir.close();

const ret = try walk(allocator, opt.args, &iter_dir, &writer, "", 1);
const dir = try fs.cwd().openDir(root_dir, .{ .iterate = true });
var iter = dir.iterate();
const ret = try walk(allocator, opt.args, &iter, &writer, "", 1);

_ = try writer.write(try std.fmt.allocPrint(allocator, "\n{d} directories, {d} files\n", .{
ret.directories,
Expand Down Expand Up @@ -147,7 +145,7 @@ const WalkResult = struct {
fn walk(
allocator: mem.Allocator,
walk_ctx: anytype,
iter_dir: *fs.IterableDir,
iter: *fs.Dir.Iterator,
writer: anytype,
prefix: []const u8,
level: usize,
Expand All @@ -159,16 +157,15 @@ fn walk(
}
}

var it = iter_dir.iterate();
var files = std.ArrayList(fs.IterableDir.Entry).init(allocator);
var files = std.ArrayList(fs.Dir.Entry).init(allocator);
defer {
for (files.items) |entry| {
allocator.free(entry.name);
}
files.deinit();
}

while (try it.next()) |entry| {
while (try iter.next()) |entry| {
const dupe_name = try allocator.dupe(u8, entry.name);
errdefer allocator.free(dupe_name);

Expand All @@ -187,8 +184,8 @@ fn walk(
try files.append(.{ .name = dupe_name, .kind = entry.kind });
}

std.sort.heap(fs.IterableDir.Entry, files.items, {}, struct {
fn lessThan(ctx: void, a: fs.IterableDir.Entry, b: fs.IterableDir.Entry) bool {
std.sort.heap(fs.Dir.Entry, files.items, {}, struct {
fn lessThan(ctx: void, a: fs.Dir.Entry, b: fs.Dir.Entry) bool {
_ = ctx;

// file < directory
Expand Down Expand Up @@ -217,7 +214,7 @@ fn walk(
_ = try writer.write(entry.name);

if (walk_ctx.size) {
const stat = try iter_dir.dir.statFile(entry.name);
const stat = try iter.dir.statFile(entry.name);
_ = try writer.write(" [");
_ = try writer.write(try StringUtil.humanSize(allocator, stat.size));
_ = try writer.write("]");
Expand All @@ -226,8 +223,8 @@ fn walk(
.directory => {
_ = try writer.write("\n");
ret.directories += 1;
var sub_iter_dir = try iter_dir.dir.openIterableDir(entry.name, .{});
defer sub_iter_dir.close();
var sub_dir = try iter.dir.openDir(entry.name, .{ .iterate = true });
var sub_iter_dir = sub_dir.iterate();

const new_prefix =
if (i < files.items.len - 1)
Expand All @@ -239,7 +236,7 @@ fn walk(
},
.sym_link => {
ret.files += 1;
const linked_name = try iter_dir.dir.readLink(entry.name, &buf);
const linked_name = try iter.dir.readLink(entry.name, &buf);
_ = try writer.write(" -> ");
_ = try writer.write(linked_name);
_ = try writer.write("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/mod/pretty-table.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn Table(comptime len: usize) type {

try writer.writeAll(column);

var left: usize = col_len - column.len;
const left: usize = col_len - column.len;
for (0..left) |_| {
try writer.writeAll(" ");
}
Expand Down

0 comments on commit e002882

Please sign in to comment.