Skip to content

Commit

Permalink
refactor: update arg parsing
Browse files Browse the repository at this point in the history
Moves argument parsing to a separate file and adds new features to make
parsing a bit more like other tools

Closes natecraddock#17
  • Loading branch information
natecraddock committed Jul 26, 2023
1 parent 1d0ff9e commit 0bbee9d
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 188 deletions.
144 changes: 0 additions & 144 deletions src/args.zig

This file was deleted.

49 changes: 7 additions & 42 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const args = @import("args.zig");
const filter = @import("filter.zig");
const heap = std.heap;
const io = std.io;
const opts = @import("opts.zig");
const std = @import("std");
const term = @import("term.zig");
const testing = std.testing;
Expand All @@ -10,30 +10,11 @@ const ziglyph = @import("ziglyph");

const ArrayList = std.ArrayList;
const Normalizer = ziglyph.Normalizer;
const Parser = @import("args.zig").Parser;
const SGRAttribute = term.SGRAttribute;
const Terminal = term.Terminal;

const eql = std.mem.eql;

const version = "0.9.0-dev";
const version_str = std.fmt.comptimePrint("zf {s} Nathan Craddock", .{version});

const help =
\\Usage: zf [options]
\\
\\-d, --delimiter DELIMITER Set the delimiter used to split candidates (default \n)
\\-f, --filter Skip interactive use and filter using the given query
\\ --height HEIGHT The height of the interface in rows (default 10)
\\-k, --keep-order Don't sort by rank and preserve order of lines read on stdin
\\-l, --lines LINES Alias of --height (deprecated)
\\-p, --plain Treat input as plaintext and disable filepath matching features
\\ --preview COMMAND Execute COMMAND for the selected line and display the output in a seprate column
\\ --preview-width WIDTH Set the preview column width (default 60%)
\\-v, --version Show version information and exit
\\-h, --help Display this help and exit
;

pub fn main() anyerror!void {
// create an arena allocator to reduce time spent allocating
// and freeing memory during runtime.
Expand All @@ -44,25 +25,8 @@ pub fn main() anyerror!void {
const stderr = std.io.getStdErr().writer();
const allocator = arena.allocator();

const arguments = try std.process.argsAlloc(allocator);
const config = args.parse(allocator, arguments) catch |e| switch (e) {
error.InvalidCharacter, error.Overflow => {
try stderr.print("Height must be an integer greater than 1\n", .{});
std.process.exit(2);
},
else => return e,
};

if (config.err) {
try stderr.print("{s}\n", .{config.err_str});
std.process.exit(2);
} else if (config.help) {
try stdout.print("{s}\n", .{help});
std.process.exit(0);
} else if (config.version) {
try stdout.print("{s}\n", .{version_str});
std.process.exit(0);
}
const args = try std.process.argsAlloc(allocator);
const config = opts.parse(allocator, args, stderr);

var normalizer = try Normalizer.init(allocator);
defer normalizer.deinit();
Expand All @@ -88,12 +52,12 @@ pub fn main() anyerror!void {
var candidates = try filter.collectCandidates(allocator, buf, delimiter);
if (candidates.len == 0) std.process.exit(1);

if (config.skip_ui) {
if (config.filter) |query| {
// Use the heap here rather than an array on the stack. Testing showed that this is actually
// faster, likely due to locality with other heap-alloced data used in the algorithm.
var tokens_buf = try allocator.alloc([]const u8, 16);
const tokens = ui.splitQuery(tokens_buf, config.query);
const case_sensitive = ui.hasUpper(config.query);
const tokens = ui.splitQuery(tokens_buf, query);
const case_sensitive = ui.hasUpper(query);
var filtered_buf = try allocator.alloc(filter.Candidate, candidates.len);
const filtered = filter.rankCandidates(filtered_buf, candidates, tokens, config.keep_order, config.plain, case_sensitive);
if (filtered.len == 0) std.process.exit(1);
Expand Down Expand Up @@ -127,6 +91,7 @@ pub fn main() anyerror!void {
config.plain,
config.height,
config.preview,
config.preview_width,
prompt_str,
vi_mode,
) catch |err| switch (err) {
Expand Down
Loading

0 comments on commit 0bbee9d

Please sign in to comment.