Skip to content

Commit

Permalink
feat: add ZF_VI_MODE environment variable
Browse files Browse the repository at this point in the history
Adds an environment variable that changes the behavior of the ctrl+k
binding. When enabled (the variable is present and is not empty) ctrl+k
moves up a linke. When disabled, ctrl+k acts like readline and deletes
from the cursor to the end of the line.

See #7
  • Loading branch information
natecraddock committed Jul 2, 2022
1 parent 541052a commit ca05d38
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,22 @@ pub fn main() anyerror!void {
}
} else {
const prompt_str = std.process.getEnvVarOwned(allocator, "ZF_PROMPT") catch "> ";
const vi_mode = if (std.process.getEnvVarOwned(allocator, "ZF_VI_MODE")) |value| blk: {
break :blk value.len > 0;
} else |_| false;
const no_color = if (std.process.getEnvVarOwned(allocator, "NO_COLOR")) |value| blk: {
break :blk value.len > 0;
}
else |_| false;
} else |_| false;

var terminal = try ui.Terminal.init(@minimum(candidates.len, config.lines), no_color);
var selected = try ui.run(allocator, &terminal, candidates, config.keep_order, prompt_str);
var selected = try ui.run(
allocator,
&terminal,
candidates,
config.keep_order,
prompt_str,
vi_mode,
);
try ui.cleanUp(&terminal);
terminal.deinit();

Expand Down
18 changes: 13 additions & 5 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ const Action = union(enum) {
delete,
delete_word,
delete_line,
delete_line_forward,
select,
close,
pass,
Expand All @@ -343,7 +344,7 @@ fn ctrl(comptime key: u8) u8 {

// TODO: for some reason this needs to be extracted to a separate function,
// perhaps related to ziglang/zig#137
fn ctrlToAction(key: u8) Action {
fn ctrlToAction(key: u8, vi_mode: bool) Action {
return switch (key) {
ctrl('c') => .close,
ctrl('w') => .delete_word,
Expand All @@ -354,16 +355,17 @@ fn ctrlToAction(key: u8) Action {
ctrl('d') => .delete,
ctrl('f') => .cursor_right,
ctrl('b') => .cursor_left,
ctrl('p'), ctrl('k') => .line_up,
ctrl('p') => .line_up,
ctrl('n'), ctrl('j') => .line_down,
ctrl('k') => if (vi_mode) Action{ .line_up = {} } else Action{ .delete_line_forward = {} },
else => .pass,
};
}

fn keyToAction(key: Key) Action {
fn keyToAction(key: Key, vi_mode: bool) Action {
return switch (key) {
.character => |c| .{ .byte = c },
.control => |c| ctrlToAction(c),
.control => |c| ctrlToAction(c, vi_mode),
.backspace => .backspace,
.delete => .delete,
.up => .line_up,
Expand Down Expand Up @@ -411,6 +413,7 @@ pub fn run(
candidates: []Candidate,
keep_order: bool,
prompt_str: []const u8,
vi_mode: bool,
) !?[]const u8 {
var query = ArrayList(u8).init(allocator);
defer query.deinit();
Expand Down Expand Up @@ -454,7 +457,7 @@ pub fn run(

const visible_rows = @intCast(i64, std.math.min(terminal.height, filtered.len));

const action = keyToAction(readKey(terminal));
const action = keyToAction(readKey(terminal), vi_mode);
switch (action) {
.byte => |b| {
try query.insert(state.cursor, b);
Expand All @@ -467,6 +470,11 @@ pub fn run(
state.cursor -= 1;
}
},
.delete_line_forward => {
while (query.items.len > state.cursor) {
_ = query.pop();
}
},
.backspace => {
if (query.items.len > 0 and state.cursor == query.items.len) {
_ = query.pop();
Expand Down

0 comments on commit ca05d38

Please sign in to comment.