Files
util_boxgui/apps/c/bigfind/bigfind.cpp
T
cnmcdee 05b33a9c33 Pilot batch: banner, bigfind, emptybig LightGUI apps + repo index and MIT license
First 3 of the 5 pilot apps, all built and screenshot-tested on hot_dallas:
- banner (Python): text + font combo + scrolling output, 12/12 GUI-vs-CLI
- bigfind (C++): top-N largest files table, exact CLI match
- emptybig (C++): sparse-file finder, exact CLI match on fixture
Top-level README indexes all 50 drawn apps; LICENSE is MIT (util_box
itself is unlicensed; MIT matches lzply and lightgui).
datecalc and certcheck are blocked pending execution-risk approval.
2026-12-05 04:38:49 +00:00

341 lines
12 KiB
C++

// bigfind - util_boxgui: top-N largest files with age, type and parent-dir context.
// LightGUI (C++) front-end for the `bigfind` CLI utility from util_box
// (https://github.com/cnmcdee/util_box, src/bigfind.c).
//
// The scan logic below is a direct port of the CLI: one recursive walk,
// only the N largest files are kept (bounded memory), sorted largest-first,
// with the CLI's size/age/type formatting reproduced exactly -- including
// its quirks (ext_of runs strrchr over the whole path, so extensionless
// files under dotted directories report odd "types").
//
// Build: make (see Makefile)
// Run: ./bigfind (under Xvfb or a real X server)
// Window title: "bigfind - util_boxgui"
#include "GtkEntry/GtkEntry.h"
#include "GtkSpinButton/GtkSpinButton.h"
#include "GtkButton/GtkButton.h"
#include "GtkLabel/GtkLabel.h"
#include "TListView/TListView.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <string>
#include <vector>
#include <ctype.h>
#include <dirent.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
// ---------------------------------------------------------------------------
// Scan logic (ported from util_box src/bigfind.c)
// ---------------------------------------------------------------------------
struct Entry {
std::string path;
off_t size;
time_t mtime;
};
static std::vector<Entry> g_top;
static size_t g_cap = 10;
static long long g_total_bytes = 0;
static long g_total_files = 0;
static void consider(const std::string &p, off_t s, time_t m) {
g_total_bytes += s;
g_total_files++;
if (g_top.size() < g_cap) {
g_top.push_back({p, s, m});
return;
}
size_t mn = 0;
for (size_t i = 1; i < g_top.size(); i++)
if (g_top[i].size < g_top[mn].size) mn = i;
if (s > g_top[mn].size) g_top[mn] = {p, s, m};
}
static void walk(const std::string &dir) {
DIR *d = opendir(dir.c_str());
if (!d) return;
struct dirent *e;
while ((e = readdir(d))) {
if (!strcmp(e->d_name, ".") || !strcmp(e->d_name, "..")) continue;
std::string full = dir + "/" + e->d_name;
struct stat st;
if (lstat(full.c_str(), &st)) continue;
if (S_ISDIR(st.st_mode))
walk(full);
else if (S_ISREG(st.st_mode))
consider(full, st.st_size, st.st_mtime);
}
closedir(d);
}
static std::string human(off_t b) {
static const char *u[] = {"B", "KiB", "MiB", "GiB", "TiB"};
double v = (double)b;
int i = 0;
while (v >= 1024 && i < 4) {
v /= 1024;
i++;
}
char buf[32];
snprintf(buf, sizeof buf, "%.1f %s", v, u[i]);
return buf;
}
static std::string age_str(time_t m) {
time_t now = time(NULL);
long s = (long)(now - m);
if (s < 0) s = 0;
char buf[32];
if (s < 60)
snprintf(buf, sizeof buf, "%lds", s);
else if (s < 3600)
snprintf(buf, sizeof buf, "%ldm", s / 60);
else if (s < 86400)
snprintf(buf, sizeof buf, "%ldh", s / 3600);
else if (s < 86400 * 30)
snprintf(buf, sizeof buf, "%ldd", s / 86400);
else if (s < 86400 * 365)
snprintf(buf, sizeof buf, "%ldmo", s / (86400 * 30));
else
snprintf(buf, sizeof buf, "%ldy", s / (86400 * 365));
return buf;
}
// Same as the CLI: last '.' in the whole path, lowercased, capped at 31 chars.
static std::string ext_of(const std::string &p) {
const char *d = strrchr(p.c_str(), '.');
if (!d || d == p.c_str()) return "(none)";
std::string b;
for (const char *q = d + 1; *q && b.size() < 31; q++)
b += (char)tolower((unsigned char)*q);
return b;
}
static std::string parent_of(const std::string &p) {
size_t s = p.rfind('/');
if (s == std::string::npos) return ".";
if (s == 0) return "/";
return p.substr(0, s);
}
static std::string base_of(const std::string &p) {
size_t s = p.rfind('/');
if (s == std::string::npos) return p;
return p.substr(s + 1);
}
// ---------------------------------------------------------------------------
// GUI
// ---------------------------------------------------------------------------
static Display *g_display = nullptr;
static Window g_window;
static GC g_gc;
static SStyle_Manager *g_styles = nullptr;
static GtkEntry *g_path_entry = nullptr;
static GtkSpinButton *g_n_spin = nullptr;
static GtkButton *g_run_button = nullptr;
static GtkLabel *g_status_label = nullptr;
static GtkLabel *g_dir_label = nullptr;
static GtkLabel *g_n_label = nullptr;
static TListView *g_table = nullptr;
static bool g_ready = false; // callbacks live only after the initial scan
static void draw_all() {
XClearWindow(g_display, g_window);
g_dir_label->draw();
g_n_label->draw();
g_path_entry->draw();
g_n_spin->draw();
g_run_button->draw();
g_table->draw();
g_status_label->draw();
XFlush(g_display);
}
static void run_scan() {
std::string dir = g_path_entry->get_text();
if (dir.empty()) dir = ".";
int n = g_n_spin->get_value_as_int();
if (n < 1) n = 1;
g_cap = (size_t)n;
g_top.clear();
g_total_bytes = 0;
g_total_files = 0;
walk(dir);
std::sort(g_top.begin(), g_top.end(),
[](const Entry &a, const Entry &b) { return a.size > b.size; });
g_table->clear_items();
for (const auto &e : g_top) {
g_table->add_item({human(e.size), age_str(e.mtime), ext_of(e.path),
base_of(e.path), parent_of(e.path)});
}
char status[256];
snprintf(status, sizeof status, "top %zu of %ld files; tree total %s",
g_top.size(), g_total_files, human(g_total_bytes).c_str());
g_status_label->set_text(status);
// Echo the scan to stdout in CLI form so the GUI table can be diffed
// against `bigfind <dir> -n <N>`.
printf("SCAN %s N=%d\n", dir.c_str(), n);
for (const auto &e : g_top)
printf("%-10s %-6s %-10s %s\n", human(e.size).c_str(),
age_str(e.mtime).c_str(), ext_of(e.path).c_str(),
e.path.c_str());
printf("%s\n", status);
fflush(stdout);
}
int main() {
g_display = XOpenDisplay(nullptr);
if (!g_display) {
fprintf(stderr, "bigfind: cannot open display\n");
return 1;
}
int screen = DefaultScreen(g_display);
g_window = XCreateSimpleWindow(g_display, RootWindow(g_display, screen),
20, 20, 960, 680, 1,
BlackPixel(g_display, screen),
WhitePixel(g_display, screen));
XStoreName(g_display, g_window, "bigfind - util_boxgui");
XSelectInput(g_display, g_window,
ExposureMask | KeyPressMask | StructureNotifyMask |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
Atom deleteWindowMessage =
XInternAtom(g_display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(g_display, g_window, &deleteWindowMessage, 1);
XMapWindow(g_display, g_window);
g_gc = DefaultGC(g_display, screen);
SStyle_Manager styles(g_display, g_window, screen, g_gc);
g_styles = &styles;
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.set_active_style_byName("default");
g_dir_label = new GtkLabel(g_display, screen, g_window, g_gc, g_styles,
20, 14, 85, 26);
g_dir_label->set_text("Directory:");
g_path_entry = new GtkEntry(g_display, screen, g_window, g_gc, g_styles,
110, 10, 540, 32);
g_path_entry->set_text("/root/fileutils");
g_n_label = new GtkLabel(g_display, screen, g_window, g_gc, g_styles,
665, 14, 55, 26);
g_n_label->set_text("Top N:");
g_n_spin = new GtkSpinButton(g_display, screen, g_window, g_gc, g_styles,
725, 10, 85, 32);
g_n_spin->set_range(1, 100);
g_n_spin->set_digits(0);
g_n_spin->set_value(10);
g_run_button = new GtkButton(g_display, screen, g_window, g_gc, g_styles,
825, 10, 95, 32);
g_run_button->set_text("Run");
g_table = new TListView(g_display, screen, g_window, g_gc, g_styles,
20, 56, 920, 540);
g_table->set_columns({});
g_table->add_column("Size", 95);
g_table->add_column("Age", 60);
g_table->add_column("Type", 90);
g_table->add_column("File", 300);
g_table->add_column("Parent dir", 375);
g_status_label = new GtkLabel(g_display, screen, g_window, g_gc, g_styles,
20, 612, 920, 28);
g_status_label->set_text("");
// Initial scan so the window opens with a populated table (the default
// state: /root/fileutils, N=10).
run_scan();
g_ready = true;
g_run_button->on_click = [] {
run_scan();
draw_all();
};
g_path_entry->on_activate = [] {
run_scan();
draw_all();
};
g_n_spin->on_value_changed = [](double) {
if (g_ready) {
run_scan();
draw_all();
}
};
draw_all();
bool running = true;
while (running) {
XEvent event;
XNextEvent(g_display, &event);
bool handled = false;
switch (event.type) {
case Expose:
handled = true;
break;
case ButtonPress:
handled =
g_path_entry->on_button_press(event.xbutton.x,
event.xbutton.y,
event.xbutton.button) ||
g_n_spin->on_button_press(event.xbutton.x, event.xbutton.y,
event.xbutton.button) ||
g_run_button->on_button_press(event.xbutton.x,
event.xbutton.y,
event.xbutton.button) ||
g_table->on_button_press(event.xbutton.x, event.xbutton.y,
event.xbutton.button);
break;
case ButtonRelease:
handled =
g_run_button->on_button_release(event.xbutton.x,
event.xbutton.y,
event.xbutton.button) ||
g_n_spin->on_button_release(event.xbutton.x, event.xbutton.y,
event.xbutton.button) ||
g_path_entry->on_button_release(event.xbutton.x,
event.xbutton.y,
event.xbutton.button) ||
g_table->on_button_release(event.xbutton.x, event.xbutton.y,
event.xbutton.button);
break;
case MotionNotify:
handled =
g_path_entry->on_motion(event.xmotion.x, event.xmotion.y) ||
g_n_spin->on_motion(event.xmotion.x, event.xmotion.y) ||
g_run_button->on_motion(event.xmotion.x, event.xmotion.y) ||
g_table->on_motion(event.xmotion.x, event.xmotion.y);
break;
case KeyPress: {
char buffer[32];
KeySym key = 0;
int n = XLookupString(&event.xkey, buffer,
(int)sizeof(buffer) - 1, &key, nullptr);
buffer[n] = 0;
handled = g_path_entry->on_key(key, buffer) ||
g_n_spin->on_key(key, buffer) ||
g_table->on_key(key, buffer);
break;
}
case ClientMessage:
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
running = false;
break;
}
if (handled) draw_all();
}
XCloseDisplay(g_display);
return 0;
}