Alien-libhisto

 view release on metacpan or  search on metacpan

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

    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},
        {0, "ymax", NULL, CLI_OPT_TYPE_DOUBLE, &ymax, NULL, 0, "Y",
         "Y axis upper bound in 2D mode", NULL},
        {'w', "weights", NULL, CLI_OPT_TYPE_BOOL, &has_weights, NULL, 0, NULL,
         "Input stream contains sample weights", NULL},
        {'d', "delimiter", NULL, CLI_OPT_TYPE_CHAR, &delim, NULL, 0, "CHAR",
         "Field delimiter character", "space"},
        {0, "value-col", "val-col", CLI_OPT_TYPE_INT, &val_col, NULL, 0, "COL",
         "1-based column for sample value (1D mode)", "1"},
        {0, "xcol", NULL, CLI_OPT_TYPE_INT, &x_col, NULL, 0, "COL",
         "1-based column for X coordinate in 2D mode", "1"},
        {0, "ycol", NULL, CLI_OPT_TYPE_INT, &y_col, NULL, 0, "COL",
         "1-based column for Y coordinate in 2D mode", "2"},
        {0, "weights-col", NULL, CLI_OPT_TYPE_INT, &w_col, NULL, 0, "COL",
         "1-based column for sample weight", "2"},
        {'p', "palette", "colormap", CLI_OPT_TYPE_STRING, &palette_name, NULL, 0, "NAME",
         "Color palette for terminal rendering", "viridis"},
        {'H', "compact", NULL, CLI_OPT_TYPE_BOOL, &compact_header, NULL, CLI_OPT_FLAG_SET_TRUE, NULL,
         "Start in compact single-line header mode", NULL},
        {'M', "mono", NULL, CLI_OPT_TYPE_BOOL, &monochrome, NULL, 0, NULL,
         "Monochrome mode without ANSI color escapes", NULL}
    };

    cli_opt_parser_t parser;
    cli_opt_init(&parser, specs, sizeof(specs) / sizeof(specs[0]),
                 "histo-top", "[OPTIONS] [FILE]",
                 "Interactive terminal monitor for live 1D/2D data streams.");

    int rc = cli_opt_parse(&parser, argc, argv, 1);
    if (rc < 0) {
        cli_opt_print_help(&parser, out);
        cli_opt_free(&parser);
        return 0;
    }
    if (rc > 0) {
        fprintf(err, "%s\n", cli_opt_error(&parser));
        cli_opt_free(&parser);
        return 1;
    }

    if (no_autorange) auto_range = false;

    if (xbins == 0) xbins = nbins;
    if (ybins == 0) ybins = nbins;
    if (isnan(xmin)) xmin = rmin;
    if (isnan(xmax)) xmax = rmax;
    if (isnan(ymin)) ymin = rmin;
    if (isnan(ymax)) ymax = rmax;

    if (palette_name) {
        palette = histo_palette_from_name(palette_name);
    }

    if (parser.num_positionals > 0) {
        file_arg = parser.positionals[0];
    }
    cli_opt_free(&parser);

    FILE *in_fp = stdin;
    if (file_arg && strcmp(file_arg, "-") != 0) {
        in_fp = fopen(file_arg, "r");
        if (!in_fp) {
            fprintf(err, "Error: Cannot open input file '%s'\n", file_arg);
            return 1;
        }
    }

    tui_engine_t eng;
    if (shm_path) {
        if (is_2d) {
            if (!tui_engine_init_shm(&eng, shm_path, true, xbins, xmin, xmax, flags)) {
                fprintf(err, "Error: Failed to attach to shared memory ring buffer '%s'\n", shm_path);
                if (in_fp != stdin) fclose(in_fp);
                return 1;
            }
        } else {
            if (!tui_engine_init_shm(&eng, shm_path, false, nbins, rmin, rmax, flags)) {
                fprintf(err, "Error: Failed to attach to shared memory ring buffer '%s'\n", shm_path);
                if (in_fp != stdin) fclose(in_fp);
                return 1;
            }



( run in 1.639 second using v1.01-cache-2.11-cpan-54e63673c56 )