Alien-libhisto

 view release on metacpan or  search on metacpan

bundled/tools/src/cmd_top.c  view on Meta::CPAN

            } else {
                st->scale_mode = SCALE_LINEAR;
                tui_engine_rebuild_1d(eng, 50, 0, 0, NULL);
            }
        }
    } else if (strncasecmp(cmd, "autorange", 9) == 0 || (strncasecmp(cmd, "a", 1) == 0 && (cmd[1] == ' ' || cmd[1] == '\0'))) {
        const char *arg = strchr(cmd, ' ');
        if (arg) {
            while (*arg == ' ') arg++;
            if (strcasecmp(arg, "on") == 0 || strcasecmp(arg, "1") == 0 || strcasecmp(arg, "true") == 0) {
                tui_engine_set_autorange(eng, true, eng->auto_range_threshold);
            } else if (strcasecmp(arg, "off") == 0 || strcasecmp(arg, "0") == 0 || strcasecmp(arg, "false") == 0) {
                tui_engine_set_autorange(eng, false, eng->auto_range_threshold);
            } else {
                double thresh = atof(arg);
                if (thresh > 0.0 && thresh < 1.0) {
                    tui_engine_set_autorange(eng, true, thresh);
                }
            }
        } else {
            tui_engine_set_autorange(eng, !eng->auto_range, eng->auto_range_threshold);
        }
    } else if (strncasecmp(cmd, "zoom ", 5) == 0 || strncasecmp(cmd, "z ", 2) == 0) {
        double factor = atof(cmd + (*cmd == 'z' ? 2 : 5));
        if (factor > 0.0) {
            if (eng->is_2d) {
                tui_engine_zoom_2d(eng, 1.0 / factor, st->paused ? &st->frozen_snapshot_2d : NULL);
            } else {
                tui_engine_zoom_1d(eng, 1.0 / factor, st->paused ? &st->frozen_snapshot : NULL);
            }
            snprintf(st->status_msg, sizeof(st->status_msg), "Zoomed %.2fx", factor);
            st->status_msg_time = cli_get_time_sec();
        }
    } else if (strncasecmp(cmd, "pan ", 4) == 0) {
        double frac_x = 0.0, frac_y = 0.0;
        int n_parsed = sscanf(cmd + 4, "%lf %lf", &frac_x, &frac_y);
        if (n_parsed >= 1) {
            if (eng->is_2d) {
                tui_engine_pan_2d(eng, frac_x, (n_parsed >= 2 ? frac_y : 0.0), st->paused ? &st->frozen_snapshot_2d : NULL);
            } else {
                tui_engine_pan_1d(eng, frac_x, st->paused ? &st->frozen_snapshot : NULL);
            }
            snprintf(st->status_msg, sizeof(st->status_msg), "Panned %.0f%%", frac_x * 100.0);
            st->status_msg_time = cli_get_time_sec();
        }
    } else if (strcasecmp(cmd, "clear") == 0 || strcasecmp(cmd, "reset") == 0) {
        tui_engine_clear(eng);
    } else if (strcasecmp(cmd, "help") == 0 || strcasecmp(cmd, "h") == 0) {
        st->modal = MODAL_HELP;
    } else if (strcasecmp(cmd, "quit") == 0 || strcasecmp(cmd, "q") == 0) {
        eng->running = false;
    }
}



#include "cli_opt.h"

int histo_cli_top(int argc, char **argv, FILE *out, FILE *err) {
    if (!out) out = stdout;
    if (!err) err = stderr;
    (void)out;
    bool is_2d = false;
    uint32_t nbins = 50;
    uint32_t xbins = 0, ybins = 0;
    double rmin = 0.0, rmax = 100.0;
    double xmin = NAN, xmax = NAN;
    double ymin = NAN, ymax = NAN;
    bool auto_range = true;
    bool no_autorange = false;
    double auto_range_threshold = 0.05;
    uint64_t window_size = 0;
    double decay_lambda = 0.0;
    bool compact_header = false;
    bool monochrome = false;
    bool has_weights = false;
    bool binary_input = false;
    char delim = ' ';
    int val_col = 1, x_col = 1, y_col = 2, w_col = 2;
    const char *palette_name = NULL;
    histo_palette_t palette = HISTO_PALETTE_VIRIDIS;
    uint32_t flags = HISTO_FLAG_TRACK_SUMW2 | HISTO_FLAG_EXACT_MOMENTS;
    double scale_input = 1.0;
    const char *shm_path = NULL;
    const char *file_arg = NULL;

    const cli_opt_spec_t specs[] = {
        {'n', "bins", NULL, CLI_OPT_TYPE_UINT32, &nbins, NULL, 0, "N",
         "Number of uniform bins (default: 50)", "50"},
        {0, "binary-f64", "binary", CLI_OPT_TYPE_BOOL, &binary_input, NULL, CLI_OPT_FLAG_SET_TRUE, NULL,
         "Read raw Little-Endian double binary stream (stdin/file)", NULL},
        {'S', "scale-input", NULL, CLI_OPT_TYPE_DOUBLE, &scale_input, NULL, 0, "FACTOR",
         "Multiply incoming measurements by scale factor (e.g. 1e-3 for ns->us)", "1.0"},
        {0, "shm", NULL, CLI_OPT_TYPE_STRING, &shm_path, NULL, 0, "PATH",
         "Attach to memory-mapped shared memory ring buffer (e.g. /histo_shm)", NULL},
        {0, "window", "reservoir", CLI_OPT_TYPE_UINT64, &window_size, NULL, 0, "N",
         "Rolling window size (last N samples in reservoir cache, default: all)", NULL},
        {0, "decay", NULL, CLI_OPT_TYPE_DOUBLE, &decay_lambda, NULL, 0, "LAMBDA",
         "Exponential decay rate per second (default: 0.0, no decay)", "0.0"},
        {0, "min", NULL, CLI_OPT_TYPE_DOUBLE, &rmin, NULL, 0, "X",
         "Lower boundary", "0.0"},
        {0, "max", NULL, CLI_OPT_TYPE_DOUBLE, &rmax, NULL, 0, "X",
         "Upper boundary", "100.0"},
        {'a', "autorange", "auto-range", CLI_OPT_TYPE_BOOL, &auto_range, NULL, CLI_OPT_FLAG_SET_TRUE, NULL,
         "Automatically adjust bin ranges to live data bounds", NULL},
        {0, "no-autorange", "no-auto-range", CLI_OPT_TYPE_BOOL, &no_autorange, NULL, CLI_OPT_FLAG_SET_TRUE, NULL,
         "Disable automatic dynamic range scaling", NULL},
        {0, "autorange-threshold", "threshold", CLI_OPT_TYPE_DOUBLE, &auto_range_threshold, NULL, 0, "FRAC",
         "Quantile margin threshold for dynamic auto-ranging (default: 0.05)", "0.05"},
        {0, "2d", NULL, CLI_OPT_TYPE_BOOL, &is_2d, NULL, 0, NULL,
         "Enable 2D bivariate live monitor mode", NULL},
        {0, "xbins", NULL, CLI_OPT_TYPE_UINT32, &xbins, NULL, 0, "N",
         "Number of bins along X axis in 2D mode", NULL},
        {0, "ybins", NULL, CLI_OPT_TYPE_UINT32, &ybins, NULL, 0, "N",
         "Number of bins along Y axis in 2D mode", NULL},
        {0, "xmin", NULL, CLI_OPT_TYPE_DOUBLE, &xmin, NULL, 0, "X",
         "X axis lower bound in 2D mode", NULL},
        {0, "xmax", NULL, CLI_OPT_TYPE_DOUBLE, &xmax, NULL, 0, "X",
         "X axis upper bound in 2D mode", NULL},
        {0, "ymin", NULL, CLI_OPT_TYPE_DOUBLE, &ymin, NULL, 0, "Y",
         "Y axis lower bound in 2D mode", NULL},



( run in 0.830 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )