Skip to content

Commit

Permalink
feat(#23): allow scrolling the list of candidates
Browse files Browse the repository at this point in the history
When the list of candidates is larger than the terminal height or
configured display lines (default 10), moving the selected line past the
bottom will now scroll the list so any item can be selected with the
arrow keys alone.

Closes #23
  • Loading branch information
natecraddock committed Feb 24, 2023
1 parent 09996da commit 4ea08e7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const term = @import("term.zig");

const State = struct {
selected: usize,
offset: usize,
prompt: []const u8,
prompt_width: usize,
};
Expand Down Expand Up @@ -158,7 +159,7 @@ fn draw(
while (line < terminal.height) : (line += 1) {
terminal.cursorDown(1);
terminal.clearLine();
if (line < candidates.len) drawCandidate(terminal, candidates[line], tokens, width, line == state.selected, case_sensitive, plain);
if (line < candidates.len) drawCandidate(terminal, candidates[line + state.offset], tokens, width, line == state.selected, case_sensitive, plain);
}
terminal.sgr(.reset);
terminal.cursorUp(terminal.height);
Expand Down Expand Up @@ -327,6 +328,7 @@ pub fn run(

var state = State{
.selected = 0,
.offset = 0,
.prompt = prompt_str,
.prompt_width = try dw.strWidth(try escapeANSI(allocator, prompt_str), .half),
};
Expand Down Expand Up @@ -364,6 +366,7 @@ pub fn run(
filtered = filter.rankCandidates(filtered_buf, candidates, tokens, keep_order, plain, case_sensitive);
redraw = true;
state.selected = 0;
state.offset = 0;
}

// do we need to redraw?
Expand All @@ -372,14 +375,15 @@ pub fn run(
try draw(terminal, &state, &query, tokens, filtered, candidates.len, case_sensitive, plain);
}

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

var buf: [2048]u8 = undefined;
const input = try terminal.read(&buf);
const action = inputToAction(input, vi_mode);

const last_cursor = query.cursor;
const last_selected = state.selected;
const last_offset = state.offset;

switch (action) {
.str => |str| try query.insert(str),
Expand All @@ -390,23 +394,27 @@ pub fn run(
.delete => query.delete(1, .right),
.line_up => if (state.selected > 0) {
state.selected -= 1;
} else if (state.offset > 0) {
state.offset -= 1;
},
.line_down => if (state.selected < visible_rows - 1) {
state.selected += 1;
} else if (state.offset < filtered.len - visible_rows) {
state.offset += 1;
},
.cursor_left => query.moveCursor(1, .left),
.cursor_leftmost => query.setCursor(0),
.cursor_rightmost => query.setCursor(query.len),
.cursor_right => query.moveCursor(1, .right),
.select => {
if (filtered.len == 0) break;
return filtered[state.selected].str;
return filtered[state.selected + state.offset].str;
},
.close => break,
.pass => {},
}

redraw = last_cursor != query.cursor or last_selected != state.selected;
redraw = last_cursor != query.cursor or last_selected != state.selected or last_offset != state.offset;
}

return null;
Expand Down

0 comments on commit 4ea08e7

Please sign in to comment.