Skip to content

Commit

Permalink
fix: ensure terminal is in a good state after exiting
Browse files Browse the repository at this point in the history
After exiting early (errors, help strings, etc.) ensure the terminal is
in a good state by exiting raw mode and printing new lines as needed.

Closes natecraddock#47
  • Loading branch information
natecraddock committed Jul 27, 2023
1 parent 4c737f5 commit b1fdf91
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,14 @@ pub fn main() anyerror!void {
vi_mode,
) catch |err| switch (err) {
error.UnknownANSIEscape => {
try stderr.print("error: unknown ANSI escape sequence in ZF_PROMPT", .{});
try terminal.deinit(0);
try stderr.print("zf: unknown ANSI escape sequence in ZF_PROMPT\n", .{});
std.process.exit(2);
},
else => return err,
else => {
try terminal.deinit(0);
return err;
},
};

try terminal.deinit(config.height);
Expand Down
20 changes: 10 additions & 10 deletions src/opts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ pub fn parse(allocator: Allocator, args: []const []const u8, stderr: File.Writer
while (iter.next()) |opt| {
// help
if (mem.eql(u8, opt, "h") or mem.eql(u8, opt, "help")) {
stderr.writeAll(help) catch unreachable;
stderr.print("{s}\n", .{help}) catch unreachable;
process.exit(0);
}

// version
else if (mem.eql(u8, opt, "v") or mem.eql(u8, opt, "version")) {
stderr.writeAll(version_str) catch unreachable;
stderr.print("{s}\n", .{version_str}) catch unreachable;
process.exit(0);
}

Expand Down Expand Up @@ -162,15 +162,15 @@ pub fn parse(allocator: Allocator, args: []const []const u8, stderr: File.Writer
if (mem.endsWith(u8, arg, "%")) break :blk arg[0 .. arg.len - 1];
break :blk arg;
};
const preview_width = fmt.parseUnsigned(usize, width_str, 10) catch argError(stderr, "width must be an integer");
if (preview_width < 20 or preview_width > 80) argError(stderr, "width must be between 20% and 80%");
const preview_width = fmt.parseUnsigned(usize, width_str, 10) catch argError(stderr, "preview-width must be an integer");
if (preview_width < 20 or preview_width > 80) argError(stderr, "preview-width must be between 20% and 80%");

config.preview_width = @as(f64, @floatFromInt(preview_width)) / 100.0;
}

// invalid option
else {
stderr.print("zf: unrecognized option '{s}{s}'\n{s}", .{ if (iter.short_index != null) "-" else "--", opt, help }) catch unreachable;
stderr.print("zf: unrecognized option '{s}{s}'\n{s}\n", .{ if (iter.short_index != null) "-" else "--", opt, help }) catch unreachable;
process.exit(2);
}
}
Expand All @@ -180,14 +180,14 @@ pub fn parse(allocator: Allocator, args: []const []const u8, stderr: File.Writer

fn missingArg(stderr: File.Writer, iter: OptionIter, opt: []const u8) noreturn {
stderr.print(
"zf: option '{s}{s}' requires an argument\n{s}",
"zf: option '{s}{s}' requires an argument\n{s}\n",
.{ if (iter.short_index != null) "-" else "--", opt, help },
) catch unreachable;
process.exit(2);
}

fn argError(stderr: File.Writer, err: []const u8) noreturn {
stderr.print("zf: {s}\n{s}", .{ err, help }) catch unreachable;
stderr.print("zf: {s}\n{s}\n", .{ err, help }) catch unreachable;
process.exit(2);
}

Expand Down Expand Up @@ -220,7 +220,7 @@ test "OptionIter" {

// chained short options with argument
{
var iter: OptionIter = .{ .args = &.{"-af", "argument"} };
var iter: OptionIter = .{ .args = &.{ "-af", "argument" } };
try expectEqualStrings("a", iter.next().?);
try expectEqualStrings("f", iter.next().?);
try expectEqualStrings("argument", iter.getArg().?);
Expand All @@ -234,14 +234,14 @@ test "OptionIter" {

// long option with argument
{
var iter: OptionIter = .{ .args = &.{"--filter", "argument"} };
var iter: OptionIter = .{ .args = &.{ "--filter", "argument" } };
try expectEqualStrings("filter", iter.next().?);
try expectEqualStrings("argument", iter.getArg().?);
}

// mixed
{
var iter: OptionIter = .{ .args = &.{"-a", "arg", "--long", "-sbarg", "--long", "-abcopt", "-a", "opt", "--long", "--flag", "opt" } };
var iter: OptionIter = .{ .args = &.{ "-a", "arg", "--long", "-sbarg", "--long", "-abcopt", "-a", "opt", "--long", "--flag", "opt" } };
try expectEqualStrings("a", iter.next().?);
try expectEqualStrings("arg", iter.getArg().?);
try expectEqualStrings("long", iter.next().?);
Expand Down

0 comments on commit b1fdf91

Please sign in to comment.