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

Fix 'no matches found' parity #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 25 additions & 20 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ pub fn main() !void {

try debug.log("Done searching\n", .{});

const hasMatches = try printResults(allocator, &results);
const hasMatches = try printResults(&results);
if (!hasMatches)
std.process.exit(1);
}

/// Returns whether there were any matches.
fn printResults(allocator: std.mem.Allocator, results: *std.ArrayList(SearchResult)) !bool {
fn printResults(results: *std.ArrayList(SearchResult)) !bool {
const SearchResultBucketSort = struct {
fn lessThan(context: void, lhs: SearchResult, rhs: SearchResult) bool {
_ = context;
Expand All @@ -116,40 +116,45 @@ fn printResults(allocator: std.mem.Allocator, results: *std.ArrayList(SearchResu

var hasMatches = false;

// reserve some conservative amount of memory to avoid initial allocs
var buffer = try std.ArrayList(u8).initCapacity(allocator, 8 * 1024);
defer buffer.deinit();
// get a buffered writer for stdout
const stdout = std.io.getStdOut();
var bw = std.io.BufferedWriter(8 * 1024, std.fs.File.Writer){ .unbuffered_writer = stdout.writer() };
defer stdout.close();

const colorConfig = std.io.tty.detectConfig(stdout);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be created only once needed right (I think it makes a syscall on windows)?


for (results.items) |*result| {
if (result.result.matches.items.len == 0) {
continue;
}
hasMatches = true;

try buffer.append('\'');
try buffer.appendSlice(result.bucketName);
try buffer.appendSlice("' bucket:\n");
_ = try bw.write("'");
_ = try bw.write(result.bucketName);
_ = try bw.write("' bucket:\n");

for (result.result.matches.items) |match| {
try buffer.appendSlice(" ");
try buffer.appendSlice(match.name);
try buffer.appendSlice(" (");
try buffer.appendSlice(match.version);
try buffer.append(')');
_ = try bw.write(" ");
_ = try bw.write(match.name);
_ = try bw.write(" (");
_ = try bw.write(match.version);
_ = try bw.write(")");
if (match.bins.items.len != 0) {
try buffer.appendSlice(" --> includes '");
try buffer.appendSlice(match.bins.items[0]);
try buffer.append('\'');
_ = try bw.write(" --> includes '");
_ = try bw.write(match.bins.items[0]);
_ = try bw.write("'");
}
try buffer.append('\n');
_ = try bw.write("\n");
}
try buffer.append('\n');
_ = try bw.write("\n");
}

if (!hasMatches) {
try buffer.appendSlice("No matches found.\n");
try colorConfig.setColor(bw.writer(), std.io.tty.Color.yellow);
_ = try bw.write("WARN No matches found.\n");
try colorConfig.setColor(bw.writer(), std.io.tty.Color.reset);
}

try std.io.getStdOut().writeAll(buffer.items);
try bw.flush();
return hasMatches;
}