Implement confirm quit

(+ 2 minor crash fixes due to out-of-bounds cursor_idx)
This commit is contained in:
Yorhel 2021-05-11 12:55:15 +02:00
parent b0e81ea4e9
commit 4cc422d628
3 changed files with 60 additions and 10 deletions

View file

@ -60,6 +60,8 @@ Potentially to be implemented:
- Faster --exclude-pattern matching
- Multithreaded scanning
- Exporting a JSON dump after scanning into RAM
- Transparent dump (de)compression by piping through gzip/bzip2/etc
### Regressions compared to the C version
@ -67,6 +69,9 @@ Aside from this implementation being unfinished:
- Assumes a UTF-8 locale and terminal.
- No doubt somewhat less portable.
- Listing all paths for a particular hard link requires a full search through
the in-memory directory tree.
- Not nearly as well tested.
## Requirements

View file

@ -41,6 +41,7 @@ const View = struct {
fn load(self: *@This()) void {
if (opened_dir_views.get(@ptrToInt(dir_parents.top()))) |v| self.* = v
else self.* = @This(){};
cursor_idx = 0;
for (dir_items.items) |e, i| {
if (self.cursor_hash == hashEntry(e)) {
cursor_idx = i;
@ -194,6 +195,22 @@ const Row = struct {
}
};
var need_confirm_quit = false;
fn drawQuit() void {
const box = ui.Box.create(4, 22, "Confirm quit");
box.move(2, 2);
ui.addstr("Really quit? (");
ui.style(.key);
ui.addch('y');
ui.style(.default);
ui.addch('/');
ui.style(.key);
ui.addch('N');
ui.style(.default);
ui.addch(')');
}
pub fn draw() !void {
ui.style(.hd);
ui.move(0,0);
@ -243,6 +260,8 @@ pub fn draw() !void {
ui.addsize(.hd, dir_parents.top().entry.size);
ui.addstr(" Items: ");
ui.addnum(.hd, dir_parents.top().total_items);
if (need_confirm_quit) drawQuit();
}
fn sortToggle(col: main.SortCol, default_order: main.SortOrder) void {
@ -254,10 +273,18 @@ fn sortToggle(col: main.SortCol, default_order: main.SortOrder) void {
}
pub fn key(ch: i32) !void {
if (need_confirm_quit) {
switch (ch) {
'y', 'Y' => if (need_confirm_quit) ui.quit(),
else => need_confirm_quit = false,
}
return;
}
defer current_view.save();
switch (ch) {
'q' => ui.quit(), // TODO: Confirm quit
'q' => if (main.config.confirm_quit) { need_confirm_quit = true; } else ui.quit(),
// Selection
'j', ui.c.KEY_DOWN => {
@ -298,7 +325,8 @@ pub fn key(ch: i32) !void {
// Navigation
10, 'l', ui.c.KEY_RIGHT => {
if (dir_items.items[cursor_idx]) |e| {
if (dir_items.items.len == 0) {
} else if (dir_items.items[cursor_idx]) |e| {
if (e.dir()) |d| {
try dir_parents.push(d);
try loadDir();

View file

@ -273,6 +273,7 @@ pub fn scanRoot(path: []const u8) !void {
}
var animation_pos: u32 = 0;
var need_confirm_quit = false;
fn drawBox() !void {
ui.init();
@ -304,12 +305,21 @@ fn drawBox() !void {
ui.addstr("some directory sizes may not be correct.");
}
box.move(8, saturateSub(width, 18));
ui.addstr("Press ");
ui.style(.key);
ui.addch('q');
ui.style(.default);
ui.addstr(" to abort");
if (need_confirm_quit) {
box.move(8, saturateSub(width, 20));
ui.addstr("Press ");
ui.style(.key);
ui.addch('y');
ui.style(.default);
ui.addstr(" to confirm");
} else {
box.move(8, saturateSub(width, 18));
ui.addstr("Press ");
ui.style(.key);
ui.addch('q');
ui.style(.default);
ui.addstr(" to abort");
}
if (main.config.update_delay < std.time.ns_per_s and width > 40) {
const txt = "Scanning...";
@ -352,8 +362,15 @@ pub fn draw() !void {
}
pub fn key(ch: i32) !void {
if (need_confirm_quit) {
switch (ch) {
'y', 'Y' => if (need_confirm_quit) ui.quit(),
else => need_confirm_quit = false,
}
return;
}
switch (ch) {
'q' => ui.quit(), // TODO: Confirm quit
else => {},
'q' => if (main.config.confirm_quit) { need_confirm_quit = true; } else ui.quit(),
else => need_confirm_quit = false,
}
}