Skip to content
Snippets Groups Projects
scan.zig 40 KiB
Newer Older
fn drawCounting() void {
    const box = ui.Box.create(4, 25, "Finalizing");
    box.move(2, 2);
    ui.addstr("Counting hardlinks...");
}

Yorhel's avatar
Yorhel committed
fn drawBox() void {
    ui.init();
Yorhel's avatar
Yorhel committed
    const ctx = active_context;
    if (ctx.fatal_error) |err| return drawError(err);
    if (counting_hardlinks) return drawCounting();
Yorhel's avatar
Yorhel committed
    const width = ui.cols -| 5;
    const box = ui.Box.create(10, width, "Scanning...");
    box.move(2, 2);
    ui.addstr("Total items: ");
    ui.addnum(.default, ctx.items_seen);

Yorhel's avatar
Yorhel committed
    if (width > 48 and ctx.parents != null) {
        box.move(2, 30);
        ui.addstr("size: ");
        // TODO: Should display the size of the dir-to-be-refreshed on refreshing, not the root.
Yorhel's avatar
Yorhel committed
        ui.addsize(.default, util.blocksToSize(model.root.entry.blocks +| model.inodes.total_blocks));
    }

    box.move(3, 2);
    ui.addstr("Current item: ");
Yorhel's avatar
Yorhel committed
    ui.addstr(ui.shorten(ui.toUtf8(ctx.pathZ()), width -| 18));

    if (ctx.last_error) |path| {
        box.move(5, 2);
        ui.style(.bold);
        ui.addstr("Warning: ");
        ui.style(.default);
        ui.addstr("error scanning ");
Yorhel's avatar
Yorhel committed
        ui.addstr(ui.shorten(ui.toUtf8(path), width -| 28));
        box.move(6, 3);
        ui.addstr("some directory sizes may not be correct.");
    }

Yorhel's avatar
Yorhel committed
    if (need_confirm_quit) {
Yorhel's avatar
Yorhel committed
        box.move(8, width -| 20);
Yorhel's avatar
Yorhel committed
        ui.addstr("Press ");
        ui.style(.key);
        ui.addch('y');
        ui.style(.default);
        ui.addstr(" to confirm");
    } else {
Yorhel's avatar
Yorhel committed
        box.move(8, width -| 18);
Yorhel's avatar
Yorhel committed
        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...";
        animation_pos += 1;
        if (animation_pos >= txt.len*2) animation_pos = 0;
        if (animation_pos < txt.len) {
            var i: u32 = 0;
            box.move(8, 2);
            while (i <= animation_pos) : (i += 1) ui.addch(txt[i]);
        } else {
            var i: u32 = txt.len-1;
            while (i > animation_pos-txt.len) : (i -= 1) {
                box.move(8, 2+i);
                ui.addch(txt[i]);
            }
        }
    }
}

Yorhel's avatar
Yorhel committed
pub fn draw() void {
Yorhel's avatar
Yorhel committed
    if (active_context.fatal_error != null and main.config.scan_ui.? != .full)
        ui.die("Error reading {s}: {s}\n", .{ active_context.last_error.?, ui.errorString(active_context.fatal_error.?) });
Yorhel's avatar
Yorhel committed
    switch (main.config.scan_ui.?) {
        .none => {},
        .line => {
            var buf: [256]u8 = undefined;
            var line: []const u8 = undefined;
            if (counting_hardlinks) {
                line = "\x1b7\x1b[JCounting hardlinks...\x1b8";
            } else if (active_context.parents == null) {
                line = std.fmt.bufPrint(&buf, "\x1b7\x1b[J{s: <63} {d:>9} files\x1b8",
Yorhel's avatar
Yorhel committed
                    .{ ui.shorten(active_context.pathZ(), 63), active_context.items_seen }
                ) catch return;
            } else {
Yorhel's avatar
Yorhel committed
                const r = ui.FmtSize.fmt(util.blocksToSize(model.root.entry.blocks));
                line = std.fmt.bufPrint(&buf, "\x1b7\x1b[J{s: <51} {d:>9} files / {s}{s}\x1b8",
Yorhel's avatar
Yorhel committed
                    .{ ui.shorten(active_context.pathZ(), 51), active_context.items_seen, r.num(), r.unit }
                ) catch return;
            }
            _ = std.io.getStdErr().write(line) catch {};
        },
Yorhel's avatar
Yorhel committed
        .full => drawBox(),
Yorhel's avatar
Yorhel committed
pub fn keyInput(ch: i32) void {
Yorhel's avatar
Yorhel committed
    if (active_context.fatal_error != null) {
        if (main.state == .scan) ui.quit()
        else main.state = .browse;
        return;
    }
Yorhel's avatar
Yorhel committed
    if (need_confirm_quit) {
        switch (ch) {
            'y', 'Y' => if (need_confirm_quit) ui.quit(),
            else => need_confirm_quit = false,
        }
        return;
    }
    switch (ch) {
Yorhel's avatar
Yorhel committed
        'q' => if (main.config.confirm_quit) { need_confirm_quit = true; } else ui.quit(),
        else => need_confirm_quit = false,