Skip to content

Commit

Permalink
feat: add ZF_PROMPT environment variable
Browse files Browse the repository at this point in the history
This allows customization of the zf prompt string with an environment
variable. Two notes:
1. This does _not_ yet support unicode! That will be added later
2. If you want a space after the prompt you must include it in the
   string, for example

   export ZF_PROMPT="> "

See #7
  • Loading branch information
natecraddock committed Jul 2, 2022
1 parent bf437f0 commit 7c6b0a6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ pub fn main() anyerror!void {
try stdout.print("{s}\n", .{candidate.str});
}
} else {
const prompt_str = std.process.getEnvVarOwned(allocator, "ZF_PROMPT") catch "> ";

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

Expand Down
12 changes: 8 additions & 4 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub const Terminal = struct {
}

pub fn cursorRight(self: *Terminal, num: usize) void {
if (num == 0) return;
self.write(.{ num, 'C' });
}

Expand Down Expand Up @@ -197,6 +198,7 @@ fn readKey(terminal: *Terminal) Key {
const State = struct {
cursor: usize,
selected: usize,
prompt: []const u8,
};

const HighlightSlice = struct {
Expand Down Expand Up @@ -296,11 +298,11 @@ fn draw(terminal: *Terminal, state: *State, query: ArrayList(u8), candidates: []
terminal.cursorUp(terminal.height);

// draw the prompt
const prompt_width = state.prompt.len;
terminal.clearLine();
terminal.print("> {s}", .{query.items[0..std.math.min(width - 2, query.items.len)]});
terminal.print("{s}{s}", .{ state.prompt, query.items[0..std.math.min(width - prompt_width, query.items.len)] });

// draw info if there is room
const prompt_width = 2;
const separator_width = 1;
const spacing = @intCast(i32, width) - @intCast(i32, prompt_width + query.items.len + numDigits(candidates.len) + numDigits(total_candidates) + separator_width);
if (spacing >= 1) {
Expand All @@ -309,8 +311,8 @@ fn draw(terminal: *Terminal, state: *State, query: ArrayList(u8), candidates: []
}

// position the cursor at the edit location
terminal.cursorCol(1);
terminal.cursorRight(std.math.min(width - 1, state.cursor + 2));
terminal.cursorCol(0);
terminal.cursorRight(std.math.min(width - 1, state.cursor + prompt_width));

try terminal.writer.flush();
}
Expand Down Expand Up @@ -405,13 +407,15 @@ pub fn run(
terminal: *Terminal,
candidates: []Candidate,
keep_order: bool,
prompt_str: []const u8,
) !?[]const u8 {
var query = ArrayList(u8).init(allocator);
defer query.deinit();

var state = State{
.cursor = 0,
.selected = 0,
.prompt = prompt_str,
};

// ensure enough room to draw all lines of output by drawing blank lines,
Expand Down

0 comments on commit 7c6b0a6

Please sign in to comment.