use std::io::{self, BufRead, BufReader, IsTerminal, Read, Write};
use std::os::unix::io::RawFd;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use clap::Parser;
use crate::query::{self, QueryOp, QueryResult};
use crate::spec::{self, Spec};
use crate::stream::StreamState;
use crate::{cache, parser, tui};
const HELP_TEMPLATE: &str = "
{before-help}
{about}
\x1b[33m USAGE:\x1b[0m {usage}
\x1b[36m ── OPTIONS ────────────────────────────────────────────\x1b[0m
{options}
\x1b[36m ── POSITIONAL ─────────────────────────────────────────\x1b[0m
{positionals}
{after-help}";
const BANNER: &str = concat!(
"\x1b[36m █████╗ ██████╗ ██████╗\x1b[0m\n",
"\x1b[36m██╔══██╗██╔══██╗██╔══██╗\x1b[0m\n",
"\x1b[35m███████║██████╔╝██████╔╝\x1b[0m\n",
"\x1b[35m██╔══██║██╔══██╗██╔══██╗\x1b[0m\n",
"\x1b[31m██║ ██║██║ ██║██████╔╝\x1b[0m\n",
"\x1b[31m╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝\x1b[0m\n",
"\x1b[36m ┌──────────────────────────────────────────────────────┐\x1b[0m\n",
"\x1b[36m │ STATUS: ONLINE // SIGNAL: ████████░░ // v",
env!("CARGO_PKG_VERSION"),
"\x1b[36m │\x1b[0m\n",
"\x1b[36m └──────────────────────────────────────────────────────┘\x1b[0m\n",
"\x1b[35m >> A TUI FOR EVERY PIPELINE // FULL SPECTRUM <<\x1b[0m"
);
const AFTER: &str = concat!(
"\x1b[36m ── SYSTEM ─────────────────────────────────────────\x1b[0m\n",
"\x1b[35m v",
env!("CARGO_PKG_VERSION"),
" \x1b[0m// \x1b[33m(c) MenkeTechnologies\x1b[0m\n",
"\x1b[35m A TUI for every pipeline.\x1b[0m\n",
"\x1b[33m >>> PIPE IN. SHAPE THE STREAM. OWN YOUR OUTPUT. <<<\x1b[0m\n",
"\x1b[36m ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░\x1b[0m"
);
#[derive(Parser, Debug)]
#[command(
name = "arb",
version,
about = "Visualize and modify Unix pipelines.",
help_template = HELP_TEMPLATE,
before_help = BANNER,
after_help = AFTER,
)]
struct Cli {
#[arg(help = "\x1b[32m//\x1b[0m Dashboard spec file (.arb)")]
spec: Option<String>,
#[arg(
short = 'e',
long = "eval",
help = "\x1b[32m//\x1b[0m Inline spec, e.g. -e 'gauge .g -max 100; source .g { in; count }'"
)]
eval: Option<String>,
#[arg(
short = 'p',
long = "preset",
help = "\x1b[32m//\x1b[0m Run a preset / stdlib module by name (== import NAME)"
)]
preset: Option<String>,
#[arg(
short = 'l',
long = "list",
help = "\x1b[32m//\x1b[0m List available presets (stdlib + ~/.arb/lib) and exit"
)]
list: bool,
#[arg(
long = "theme",
value_name = "NAME",
help = "\x1b[32m//\x1b[0m Color theme (one of 31, e.g. neon-noir); overrides the spec"
)]
theme: Option<String>,
#[arg(
long = "list-themes",
help = "\x1b[32m//\x1b[0m List the 31 built-in color themes and exit"
)]
list_themes: bool,
#[arg(
long = "tiers",
value_name = "EXPR",
help = "\x1b[32m//\x1b[0m Evaluate EXPR, then report which fusevm tiers took it"
)]
tiers: Option<String>,
#[arg(
long = "set-theme",
value_name = "NAME",
help = "\x1b[32m//\x1b[0m Save NAME as the global default theme in ~/.arb/config.toml, then exit"
)]
set_theme: Option<String>,
#[arg(
long = "save",
value_name = "NAME",
help = "\x1b[32m//\x1b[0m Save a spec (FILE or -e SRC) as a named user preset, then exit"
)]
save: Option<String>,
#[arg(
long = "install",
value_name = "FILE",
help = "\x1b[32m//\x1b[0m Install a .arb spec into the preset library, then exit"
)]
install: Option<String>,
#[arg(
long = "as",
value_name = "NAME",
help = "\x1b[32m//\x1b[0m Name to install the spec under (default: file stem)"
)]
install_as: Option<String>,
#[arg(
long = "uninstall",
value_name = "NAME",
help = "\x1b[32m//\x1b[0m Remove a named preset from the library, then exit"
)]
uninstall: Option<String>,
#[arg(
long = "installed",
help = "\x1b[32m//\x1b[0m List installed user-library presets, then exit"
)]
installed: bool,
#[arg(
short = 'r',
long = "repl",
help = "\x1b[32m//\x1b[0m Interactive REPL — author + test specs against a sample buffer"
)]
repl: bool,
#[arg(
long = "html",
help = "\x1b[32m//\x1b[0m Emit a static HTML dashboard from the spec to stdout, then exit"
)]
html: bool,
#[arg(
long = "check",
help = "\x1b[32m//\x1b[0m Validate the spec (parse + build) and exit 0/1, no stdin"
)]
check: bool,
#[arg(
long = "test",
help = "\x1b[32m//\x1b[0m Run the spec's `test { … }` blocks (TAP output), exit 0/1"
)]
test: bool,
#[arg(
long = "serve",
help = "\x1b[32m//\x1b[0m Serve the spec as a live browser dashboard on 127.0.0.1"
)]
serve: bool,
#[arg(
long = "port",
value_name = "N",
default_value_t = 8787,
help = "\x1b[32m//\x1b[0m Port for --serve (0 = pick a free port)"
)]
port: u16,
#[arg(
long = "lsp",
help = "\x1b[32m//\x1b[0m Run the LSP over stdio (diagnostics/completion/hover/…)"
)]
lsp: bool,
#[arg(
long = "dap",
help = "\x1b[32m//\x1b[0m Run the DAP over stdio (step the stream, regex breakpoints)"
)]
dap: bool,
#[arg(
long = "dump-tokens",
help = "\x1b[32m//\x1b[0m Print the lexer token stream for the spec and exit"
)]
dump_tokens: bool,
#[arg(
long = "dump-ast",
help = "\x1b[32m//\x1b[0m Print the parsed AST for the spec and exit"
)]
dump_ast: bool,
#[arg(
long = "dump-bytecode",
help = "\x1b[32m//\x1b[0m Print the compiled pipeline op vectors for the spec and exit"
)]
dump_bytecode: bool,
#[arg(
long = "disasm",
help = "\x1b[32m//\x1b[0m Print a disassembly of the compiled pipelines and exit"
)]
disasm: bool,
#[arg(
long = "json",
help = "\x1b[32m//\x1b[0m With an out { } pipeline, emit JSON instead of plain lines"
)]
json: bool,
#[arg(
long = "fzf",
help = "\x1b[32m//\x1b[0m fzf mode: filter + select one line, printed to stdout on Enter"
)]
fzf: bool,
#[arg(
long = "run",
value_name = "PIPELINE",
help = "\x1b[32m//\x1b[0m Run 'PROD | _ | CONS': arb spawns them, stderr -> pane"
)]
run: Option<String>,
#[arg(
long = "preview",
value_name = "CMD",
overrides_with = "preview",
help = "\x1b[32m//\x1b[0m fzf preview: run CMD on the cursor line ({}), output in a pane"
)]
preview: Option<String>,
#[arg(
long = "prompt",
value_name = "STR",
overrides_with = "prompt",
help = "\x1b[32m//\x1b[0m fzf prompt string (default '> ')"
)]
prompt: Option<String>,
#[arg(
long = "header",
value_name = "STR",
overrides_with = "header",
help = "\x1b[32m//\x1b[0m fzf header line shown above the list"
)]
header: Option<String>,
#[arg(
long = "exact",
overrides_with = "exact",
help = "\x1b[32m//\x1b[0m fzf: exact substring match (not fuzzy)"
)]
exact: bool,
#[arg(
long = "no-sort",
overrides_with = "no_sort",
help = "\x1b[32m//\x1b[0m fzf: keep input order (no score sort)"
)]
no_sort: bool,
#[arg(
long = "query",
value_name = "STR",
overrides_with = "query",
help = "\x1b[32m//\x1b[0m fzf: initial query"
)]
query: Option<String>,
#[arg(
short = 'm',
long = "multi",
overrides_with = "multi",
help = "\x1b[32m//\x1b[0m fzf: multi-select (Tab marks)"
)]
multi: bool,
#[arg(
short = 'f',
long = "filter",
value_name = "STR",
overrides_with = "filter",
help = "\x1b[32m//\x1b[0m fzf: print ranked matches for STR and exit (no UI)"
)]
filter: Option<String>,
#[arg(
long = "height",
value_name = "N|N%",
overrides_with = "height",
help = "\x1b[32m//\x1b[0m fzf height: inline in N rows or N% (not full-screen)"
)]
height: Option<String>,
#[arg(
last = true,
help = "\x1b[32m//\x1b[0m Preview `-- CMD …`: re-run over the filtered output, shown in a pane"
)]
down: Vec<String>,
}
type Argv = std::iter::Peekable<std::vec::IntoIter<String>>;
fn eats_next(next: Option<&String>, numeric: bool) -> bool {
match next {
Some(v) if numeric => v.parse::<i64>().is_ok(),
Some(v) => !v.starts_with('-'),
None => false,
}
}
fn cancel(out: &mut Vec<String>, names: &[&str], takes_value: bool) {
let Some(i) = out.iter().rposition(|a| {
names
.iter()
.any(|n| a == n || a.starts_with(&format!("{n}=")))
}) else {
return;
};
let attached = out[i].contains('=');
out.remove(i);
if takes_value && !attached && i < out.len() {
out.remove(i);
}
}
fn fzf_compat_args(args: impl Iterator<Item = String>) -> Vec<String> {
const DROP_BOOL: &[&str] = &[
"--ambidouble",
"--ansi",
"--async",
"--bash",
"--bench",
"--black",
"--bold",
"--clear",
"--cycle",
"--disabled",
"--enabled",
"--exit-0",
"--extended",
"--filepath-word",
"--fish",
"--force-tty-in",
"--header-first",
"--highlight-line",
"--hscroll",
"--ignore-case",
"--inline-info",
"--keep-right",
"--literal",
"--man",
"--multi-line",
"--no-256",
"--no-ambidouble",
"--no-ansi",
"--no-black",
"--no-bold",
"--no-border",
"--no-border-label",
"--no-clear",
"--no-color",
"--no-cycle",
"--no-exit-0",
"--no-expect",
"--no-extended",
"--no-filepath-word",
"--no-footer",
"--no-footer-border",
"--no-footer-label",
"--no-force-tty-in",
"--no-gap",
"--no-gap-line",
"--no-header-border",
"--no-header-first",
"--no-header-label",
"--no-header-lines",
"--no-header-lines-border",
"--no-highlight-line",
"--no-history",
"--no-hscroll",
"--no-id-nth",
"--no-ignore-case",
"--no-info",
"--no-info-command",
"--no-inline-info",
"--no-input",
"--no-input-border",
"--no-input-label",
"--no-keep-right",
"--no-list-border",
"--no-list-label",
"--no-listen",
"--no-listen-unsafe",
"--no-literal",
"--no-margin",
"--no-mouse",
"--no-multi-line",
"--no-padding",
"--no-phony",
"--no-popup",
"--no-preview-border",
"--no-preview-label",
"--no-print-query",
"--no-print0",
"--no-raw",
"--no-read0",
"--no-reverse",
"--no-scrollbar",
"--no-select-1",
"--no-separator",
"--no-sync",
"--no-tac",
"--no-tail",
"--no-tmux",
"--no-track",
"--no-tty-default",
"--no-unicode",
"--no-winpty",
"--no-wrap",
"--no-wrap-word",
"--nushell",
"--phony",
"--print-query",
"--print0",
"--raw",
"--read0",
"--reverse",
"--select-1",
"--smart-case",
"--sync",
"--tac",
"--track",
"--unicode",
"--wrap-word",
"--zsh",
"-0",
"-1",
];
const DROP_VALUE: &[&str] = &[
"--accept-nth",
"--algo",
"--bind",
"--border-label",
"--border-label-pos",
"--delimiter",
"--ellipsis",
"--expect",
"--footer",
"--footer-label",
"--footer-label-pos",
"--freeze-left",
"--freeze-right",
"--ghost",
"--gutter",
"--gutter-raw",
"--header-label",
"--header-label-pos",
"--header-lines",
"--history",
"--history-size",
"--hscroll-off",
"--id-nth",
"--info",
"--info-command",
"--input-label",
"--input-label-pos",
"--jump-labels",
"--layout",
"--list-label",
"--list-label-pos",
"--margin",
"--marker",
"--marker-multi-line",
"--min-height",
"--nth",
"--padding",
"--pointer",
"--preview-label",
"--preview-label-pos",
"--preview-window",
"--preview-wrap-sign",
"--profile-block",
"--profile-cpu",
"--profile-mem",
"--profile-mutex",
"--proxy-script",
"--scheme",
"--scroll-off",
"--separator",
"--style",
"--tabstop",
"--tail",
"--threads",
"--tiebreak",
"--toggle-sort",
"--tty-default",
"--walker",
"--walker-root",
"--walker-skip",
"--with-nth",
"--with-shell",
"--wrap-sign",
];
const DROP_OPT_STR: &[&str] = &[
"--border",
"--color",
"--footer-border",
"--gap-line",
"--header-border",
"--header-lines-border",
"--input-border",
"--list-border",
"--listen",
"--listen-unsafe",
"--popup",
"--preview-border",
"--scrollbar",
"--tmux",
"--wrap",
];
const DROP_OPT_NUM: &[&str] = &["--gap"];
const MULTI: &[&str] = &["--multi", "-m"];
let argv: Vec<String> = args.collect();
let fzf = argv.iter().any(|a| a == "--fzf");
let mut out: Vec<String> = Vec::new();
let mut it: Argv = argv.into_iter().peekable();
while let Some(a) = it.next() {
if fzf {
match a.as_str() {
"-e" | "--extended-exact" => {
cancel(&mut out, &["--exact"], false);
out.push("--exact".to_string());
continue;
}
"+e" => {
cancel(&mut out, &["--exact"], false);
continue;
}
"-i" | "+i" | "-x" | "+x" | "+c" => continue,
"+m" => {
cancel(&mut out, MULTI, false);
continue;
}
"+s" => {
cancel(&mut out, &["--no-sort"], false);
out.push("--no-sort".to_string());
continue;
}
_ => {}
}
if let Some(rest) = a.strip_prefix("-q") {
let v = match rest.is_empty() {
true => it.next().unwrap_or_default(),
false => rest.to_string(),
};
out.push("--query".to_string());
out.push(v);
continue;
}
if let Some(rest) = a.strip_prefix("-n").or_else(|| a.strip_prefix("-d")) {
if rest.is_empty() {
it.next();
}
continue;
}
if let Some(rest) = a.strip_prefix("-m") {
if rest.is_empty() && eats_next(it.peek(), true) {
it.next();
}
cancel(&mut out, MULTI, false);
out.push("--multi".to_string());
continue;
}
if let Some(rest) = a.strip_prefix("-s") {
if rest.is_empty() && eats_next(it.peek(), true) {
it.next();
}
cancel(&mut out, &["--no-sort"], false);
continue;
}
}
let key = a.split('=').next().unwrap_or(&a);
match key {
"--no-preview" => {
cancel(&mut out, &["--preview"], true);
continue;
}
"--no-height" => {
cancel(&mut out, &["--height"], true);
continue;
}
"--no-header" => {
cancel(&mut out, &["--header"], true);
continue;
}
"--no-multi" => {
cancel(&mut out, MULTI, false);
continue;
}
"--no-exact" => {
cancel(&mut out, &["--exact"], false);
continue;
}
"--no-sort" => {
cancel(&mut out, &["--no-sort"], false);
out.push("--no-sort".to_string());
continue;
}
"--sort" => {
cancel(&mut out, &["--no-sort"], false);
if !a.contains('=') && eats_next(it.peek(), true) {
it.next();
}
continue;
}
"--multi" => {
cancel(&mut out, MULTI, false);
out.push("--multi".to_string());
if !a.contains('=') && eats_next(it.peek(), true) {
it.next();
}
continue;
}
_ => {}
}
if DROP_BOOL.contains(&key) {
continue;
}
if DROP_VALUE.contains(&key) {
if !a.contains('=') {
it.next();
}
continue;
}
if DROP_OPT_STR.contains(&key) {
if !a.contains('=') && eats_next(it.peek(), false) {
it.next();
}
continue;
}
if DROP_OPT_NUM.contains(&key) {
if !a.contains('=') && eats_next(it.peek(), true) {
it.next();
}
continue;
}
out.push(a);
}
out
}
fn run_filter(pat: &str, exact: bool, no_sort: bool, look: &crate::fzf::Look) -> io::Result<()> {
use std::io::Read;
use std::io::{BufWriter, Write};
let stdin = io::stdin();
let mut reader = io::BufReader::new(stdin.lock());
let mut blob = Vec::new();
reader.read_to_end(&mut blob)?;
let sep = match look.read0 {
true => b'\0',
false => b'\n',
};
let sep_char = sep as char;
let mut lines: Vec<&str> = match std::str::from_utf8(&blob) {
Ok(text) => text.split(sep_char).collect(),
Err(_) => blob
.split(|b| *b == sep)
.map(|l| std::str::from_utf8(l).unwrap_or(""))
.collect(),
};
if lines.last() == Some(&"") {
lines.pop();
}
let mut dropped = 0usize;
if look.header_lines > 0 {
dropped += look.header_lines.min(lines.len());
lines.drain(..look.header_lines.min(lines.len()));
}
if let Some(n) = look.tail {
if lines.len() > n {
dropped += lines.len() - n;
lines.drain(..lines.len() - n);
}
}
let order = crate::tui::rank(&lines, pat, exact, no_sort, look.tac, look);
let out = io::stdout();
let mut out = BufWriter::new(out.lock());
let end = match look.print0 {
true => b"\0" as &[u8],
false => b"\n",
};
if look.print_query {
out.write_all(pat.as_bytes())?;
out.write_all(end)?;
}
for i in order {
let text = crate::tui::item_text(lines[i], look);
match &look.accept_nth {
Some(spec) => {
let rendered = spec.render(&text, look.delimiter.as_deref(), dropped + i);
out.write_all(rendered.as_bytes())?;
}
None => out.write_all(text.as_bytes())?,
}
out.write_all(end)?;
}
out.flush()
}
pub fn run_argv(argv: &[String]) -> i32 {
crate::hosted::run(|| match run(argv) {
Ok(()) => 0,
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => 0,
Err(e) => {
eprintln!("arb: {e}");
1
}
})
}
fn run(full_argv: &[String]) -> io::Result<()> {
let raw: Vec<String> = full_argv.iter().skip(1).cloned().collect();
if let Some(code) = crate::pkg::dispatch(&raw) {
crate::hosted::exit(code);
}
let mut argv: Vec<String> = full_argv.to_vec();
if argv.iter().any(|a| a == "--fzf") {
let env = crate::fzf::env_args();
argv.splice(1..1, env);
}
let fzf_look = crate::fzf::Look::parse(&argv);
let cli = Cli::parse_from(fzf_compat_args(argv.into_iter()));
if cli.repl {
crate::repl::run();
return Ok(());
}
if let Some(pat) = cli.filter.as_deref() {
return run_filter(pat, cli.exact, cli.no_sort, &fzf_look);
}
if cli.lsp {
crate::lsp::run();
return Ok(());
}
if cli.dap {
crate::dap::run();
return Ok(());
}
if cli.dump_tokens {
return finish_dump(dump_tokens(&cli));
}
if cli.dump_ast {
return finish_dump(dump_ast(&cli));
}
if cli.dump_bytecode {
return finish_dump(dump_bytecode(&cli));
}
if cli.disasm {
return finish_dump(disasm(&cli));
}
if cli.list {
let mut out = io::stdout().lock();
for (name, desc, example) in spec::list_presets() {
writeln!(out, "{name:<10} {desc}")?;
if !example.is_empty() {
writeln!(out, "{:<10} {example}", "")?;
}
}
return Ok(());
}
if let Some(expr) = &cli.tiers {
match crate::tiers::report(expr) {
Ok(r) => println!("{r}"),
Err(e) => {
eprintln!("arb: --tiers: {e}");
crate::hosted::exit(1);
}
}
return Ok(());
}
if cli.list_themes {
let mut out = io::stdout().lock();
for (name, pal) in crate::theme::THEMES {
let swatch: String = pal
.iter()
.map(|&c| format!("\x1b[48;5;{c}m \x1b[0m"))
.collect();
writeln!(out, "{swatch} {name}")?;
}
return Ok(());
}
if let Some(name) = &cli.set_theme {
match crate::theme::set_config_default(name) {
Ok(()) => {
println!("arb: default theme set to `{name}` (~/.arb/config.toml)");
return Ok(());
}
Err(e) => {
eprintln!("arb: --set-theme: {e}");
crate::hosted::exit(1);
}
}
}
if let Some(name) = cli.save.clone() {
return save_preset(&name, &cli);
}
if cli.installed {
let Some(dir) = spec::lib_dir() else {
eprintln!("arb: no preset library (set HOME or ARB_LIB)");
crate::hosted::exit(1);
};
let mut out = io::stdout().lock();
for (name, desc, example) in spec::list_user_presets(&dir) {
writeln!(out, "{name:<10} {desc}")?;
if !example.is_empty() {
writeln!(out, "{:<10} {example}", "")?;
}
}
return Ok(());
}
if let Some(file) = cli.install.clone() {
return install_cmd(&file, cli.install_as.as_deref());
}
if let Some(name) = cli.uninstall.clone() {
let Some(dir) = spec::lib_dir() else {
eprintln!("arb: no preset library (set HOME or ARB_LIB)");
crate::hosted::exit(1);
};
match spec::uninstall_preset(&dir, &name) {
Ok(true) => eprintln!("arb: uninstalled `{name}`"),
Ok(false) => {
eprintln!("arb: `{name}` is not installed");
crate::hosted::exit(1);
}
Err(e) => {
eprintln!("arb: uninstall: {e}");
crate::hosted::exit(1);
}
}
return Ok(());
}
let run_pipeline: Option<String> = cli
.run
.clone()
.or_else(|| cli.spec.as_ref().filter(|s| is_arb_pipeline(s)).cloned());
let positional_pipeline = cli.run.is_none() && run_pipeline.is_some();
let no_spec_args = cli.spec.is_none() && cli.eval.is_none() && cli.preset.is_none();
if no_spec_args && run_pipeline.is_none() && io::stdin().is_terminal() {
crate::repl::run();
return Ok(());
}
let sniff_ok = no_spec_args && run_pipeline.is_none() && !cli.fzf && !io::stdin().is_terminal();
let mut prelude: Vec<String> = Vec::new();
let sniffed: Option<&'static str> = if sniff_ok {
prelude = peek_lines(SNIFF_LINES, SNIFF_DEADLINE_MS);
let refs: Vec<&str> = prelude.iter().map(String::as_str).collect();
crate::sniff::sniff(&refs)
} else {
None
};
let mut spec = if positional_pipeline {
spec::build(&parser::parse(default_spec_src(cli.fzf)).unwrap()).unwrap()
} else if let Some(name) = sniffed {
parser::parse(&format!("import {name}"))
.and_then(|c| spec::build(&c))
.unwrap_or_else(|_| {
spec::build(&parser::parse(default_spec_src(false)).unwrap()).unwrap()
})
} else {
match load_spec(&cli) {
Ok(s) => s,
Err(e) => {
eprintln!("arb: {e}");
crate::hosted::exit(1);
}
}
};
let theme_requested = cli.theme.is_some() || spec.theme.is_some();
match cli.theme.as_deref() {
Some("off") | Some("none") => spec.theme = None,
Some(name) => match crate::theme::by_name(name) {
Some(p) => spec.theme = Some(p),
None => {
eprintln!("arb: --theme: unknown theme `{name}` (see `arb --list-themes`)");
crate::hosted::exit(1);
}
},
None if !spec.theme_off && spec.theme.is_none() => {
spec.theme =
Some(crate::theme::config_default().unwrap_or_else(crate::theme::default_palette));
}
None => {}
}
let fzf_mode = cli.fzf || has_select(&spec);
if fzf_mode && !theme_requested {
spec.theme = None;
}
let (sel_prompt, sel_header) = spec
.widgets
.iter()
.find(|w| w.kind == spec::WidgetKind::Select)
.map(|w| (w.opts.get("prompt").cloned(), w.opts.get("header").cloned()))
.unwrap_or((None, None));
if cli.html {
print!("{}", crate::web::render_html(&spec));
return Ok(());
}
if cli.test {
let report = crate::testrun::run(&spec.tests);
print!("{}", report.text);
crate::hosted::exit(if report.failed == 0 { 0 } else { 1 });
}
if cli.check {
println!(
"arb: ok \u{2014} {} widget(s){}",
spec.widgets.len(),
if spec.out.is_some() {
", out pipeline"
} else {
""
}
);
return Ok(());
}
let (run_producer, consumer) = match &run_pipeline {
Some(p) => {
let (pr, co) = parse_pipeline(p);
(Some(pr), co)
}
None => (None, None),
};
let producer = run_producer.or_else(|| spec_producer(&spec));
if let Some(f) = spec.source_file.as_deref() {
crate::yaml::set_input_name(f);
}
let poll = if producer.is_some() {
None
} else {
spec.poll.clone()
};
let needs_stdin = needs_stdin(&spec, fzf_mode);
if needs_stdin && producer.is_none() && poll.is_none() && io::stdin().is_terminal() {
match spec_example(&cli, sniffed) {
ex if ex.is_empty() => {
eprintln!("arb: spec reads stdin but nothing is piped — e.g. `find / | arb`");
}
ex => eprintln!("arb: spec reads stdin but nothing is piped — try:\narb: {ex}"),
}
crate::hosted::exit(2);
}
let state = Arc::new(Mutex::new(stream_state(&spec, fzf_mode)));
if cli.serve {
let _feed = if let Some(prod) = &producer {
if let Err(e) = spawn_producer(prod, state.clone()) {
eprintln!("arb: run: {e}");
crate::hosted::exit(1);
}
None
} else if let Some((cmd, dur)) = &poll {
poll_producer(cmd, *dur, state.clone());
None
} else if needs_stdin {
let controls = Arc::new(Mutex::new(tui::Controls::default()));
Some(spawn_reader(
state.clone(),
false,
controls,
None,
prelude.clone(),
))
} else {
None
};
return crate::serve::serve(spec, state, cli.port);
}
let interactive_out = spec
.widgets
.iter()
.any(|w| w.kind == spec::WidgetKind::Input)
&& spec
.out
.as_ref()
.is_some_and(|ops| ops.iter().any(|op| matches!(op, QueryOp::Apply(_))));
let downstream_reshape = spec.out.is_some() && !io::stdout().is_terminal() && !interactive_out;
if tui::events_available() && !downstream_reshape {
let controls = Arc::new(Mutex::new(tui::Controls::default()));
let mut feed: Option<StdinFeed> = None;
let err_pane = if spec.spawn_pty && run_pipeline.is_none() && producer.is_some() {
let prod = producer.as_deref().unwrap();
match crate::pty::spawn_pty_producer(prod, state.clone()) {
Ok(writer) => {
controls.lock().unwrap().pty_writer = Some(writer);
None
}
Err(e) => {
eprintln!("arb: spawn -pty: {e}");
crate::hosted::exit(1);
}
}
} else if let Some(prod) = &producer {
match spawn_producer(prod, state.clone()) {
Ok(es) => Some((es, "stderr".to_string())),
Err(e) => {
eprintln!("arb: run: {e}");
crate::hosted::exit(1);
}
}
} else if let Some((cmd, dur)) = &poll {
Some((
poll_producer(cmd, *dur, state.clone()),
"stderr".to_string(),
))
} else {
if needs_stdin {
let tee = !fzf_mode && !io::stdout().is_terminal();
feed = Some(spawn_reader(
state.clone(),
tee,
controls.clone(),
spec.out.clone(),
prelude.clone(),
));
}
None
};
let down_cmd: Vec<String> = match &consumer {
Some(c) => vec!["sh".to_string(), "-c".to_string(), c.clone()],
None => cli.down.clone(),
};
let down_pane = if fzf_mode {
cli.preview.as_ref().map(|pv| {
let pstate = Arc::new(Mutex::new(StreamState::new()));
spawn_item_preview(
pv.clone(),
fzf_look.shell(),
fzf_look.delimiter.clone(),
fzf_look.preview_window.scroll.clone(),
controls.clone(),
pstate.clone(),
);
(pstate, "preview".to_string())
})
} else if down_cmd.is_empty() {
None
} else {
let dstate = Arc::new(Mutex::new(StreamState::new()));
spawn_preview(
down_cmd.clone(),
state.clone(),
controls.clone(),
dstate.clone(),
);
let label = consumer.clone().unwrap_or_else(|| cli.down.join(" "));
Some((dstate, label))
};
{
let mut c = controls.lock().unwrap();
c.prompt = cli
.prompt
.clone()
.or(sel_prompt.clone())
.unwrap_or_default();
c.header = cli
.header
.clone()
.or(sel_header.clone())
.unwrap_or_default();
for w in spec.widgets.iter().filter(|w| w.kind.is_control()) {
let name = if w.kind == spec::WidgetKind::Sel {
format!("{}.sel", w.path.trim_start_matches('.'))
} else {
w.path.trim_start_matches('.').to_string()
};
let opt_f =
|k: &str, d: f64| w.opts.get(k).map(|s| spec::parse_scalar(s)).unwrap_or(d);
let kind = tui::control_kind(w.kind);
let (min, max, step) = (opt_f("min", 0.0), opt_f("max", 100.0), opt_f("step", 1.0));
let init = match kind {
tui::ControlKind::Slider => format!("{min}"),
tui::ControlKind::Check => "0".to_string(),
_ => String::new(),
};
let opts: Vec<String> = w
.opts
.get("opts")
.map(|s| {
s.split(',')
.filter(|x| !x.is_empty())
.map(String::from)
.collect()
})
.unwrap_or_default();
c.inputs.push((name, init));
c.control_meta.push(tui::ControlMeta {
kind,
min,
max,
step,
opts,
cursor: 0,
});
}
if !spec.actor_refs.is_empty() {
match crate::actor::Session::build(&spec.actors, &spec.actor_refs) {
Ok(session) => c.session = session,
Err(e) => {
eprintln!("arb: {e}");
crate::hosted::exit(1);
}
}
}
c.theme = spec.theme;
c.theme_idx = spec.theme.map(crate::theme::index_of).unwrap_or(0);
c.binds = spec.binds.clone();
c.mouse_binds = spec.mouse_binds.clone();
c.exact = cli.exact;
c.no_sort = cli.no_sort;
c.look = if cli.fzf {
fzf_look.clone()
} else {
crate::fzf::Look {
layout: crate::fzf::Layout::Reverse,
..crate::fzf::Look::default()
}
};
if let Some(q) = &cli.query {
c.filter = q.clone();
}
c.multi = cli.multi || !cli.fzf;
}
let outcome = tui::run(
&spec,
state,
controls.clone(),
down_pane,
err_pane,
fzf_mode,
cli.height.clone(),
);
drop(feed);
if fzf_mode {
let submit = controls.lock().unwrap().submit;
if submit {
let c = controls.lock().unwrap();
let out = match &consumer {
Some(cons) => run_capture(&["sh".into(), "-c".into(), cons.clone()], &c.result),
None => c.result.clone(),
};
if fzf_look.print_query {
println!("{}", c.filter);
}
if !fzf_look.expect.is_empty() {
println!("{}", c.expect_key.clone().unwrap_or_default());
}
for line in out {
println!("{line}");
}
} else {
crate::hosted::exit(130);
}
}
outcome
} else {
let feed = Feed {
producer: producer.as_deref(),
poll: poll.as_ref().map(|(cmd, _)| cmd.as_str()),
needs_stdin,
prelude: &prelude,
};
headless(
&spec,
&state,
&feed,
cli.json,
io::stdin().lock(),
io::stdout().lock(),
&mut io::stderr(),
)
}
}
fn has_select(spec: &Spec) -> bool {
spec.widgets
.iter()
.any(|w| w.kind == spec::WidgetKind::Select)
}
fn spec_producer(spec: &Spec) -> Option<String> {
spec.spawn.clone().or_else(|| {
spec.source_file
.as_deref()
.map(|f| format!("cat -- {}", shell_quote(f)))
})
}
fn needs_stdin(spec: &Spec, fzf_mode: bool) -> bool {
fzf_mode || spec.out.is_some() || spec.widgets.iter().any(|w| w.source.is_some())
}
fn stream_state(spec: &Spec, fzf_mode: bool) -> StreamState {
const MAX_RETAIN: usize = 2_000_000;
if fzf_mode || spec.out.is_some() {
StreamState::with_cap(MAX_RETAIN)
} else {
StreamState::new()
}
}
struct Feed<'a> {
producer: Option<&'a str>,
poll: Option<&'a str>,
needs_stdin: bool,
prelude: &'a [String],
}
fn headless<R: Read, W: Write>(
spec: &Spec,
state: &Arc<Mutex<StreamState>>,
feed: &Feed,
json: bool,
input: R,
out: W,
err: &mut dyn Write,
) -> io::Result<()> {
if let Some(out_ops) = &spec.out {
if let Some(prod) = feed.producer {
read_producer_sync(prod, state)?;
emit_out(out_ops, state, json, out, err)
} else if let Some(cmd) = feed.poll {
read_producer_sync(cmd, state)?;
emit_out(out_ops, state, json, out, err)
} else if feed.needs_stdin && !json && query::is_line_streamable(out_ops) {
stream_out(out_ops, input, out, err)
} else {
if feed.needs_stdin {
read_stdin_sync(state, feed.prelude, input);
}
emit_out(out_ops, state, json, out, err)
}
} else if let Some(prod) = feed.producer {
stream_producer(prod, out)
} else if let Some(cmd) = feed.poll {
stream_producer(cmd, out)
} else if feed.needs_stdin {
passthrough(feed.prelude, input, out)
} else {
dump(spec, state, out)
}
}
pub(crate) fn run_headless<R: Read, W: Write>(
src: &str,
input: R,
out: W,
err: &mut dyn Write,
) -> io::Result<()> {
let spec = match build_spec(src) {
Ok(s) => s,
Err(e) => {
let _ = writeln!(err, "arb: {e}");
crate::hosted::exit(1);
}
};
if let Some(f) = spec.source_file.as_deref() {
crate::yaml::set_input_name(f);
}
let fzf_mode = has_select(&spec);
let producer = spec_producer(&spec);
let feed = Feed {
producer: producer.as_deref(),
poll: match &producer {
Some(_) => None,
None => spec.poll.as_ref().map(|(cmd, _)| cmd.as_str()),
},
needs_stdin: needs_stdin(&spec, fzf_mode),
prelude: &[],
};
let state = Arc::new(Mutex::new(stream_state(&spec, fzf_mode)));
headless(&spec, &state, &feed, false, input, out, err)
}
fn shell_quote(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
fn read_producer_sync(producer: &str, state: &Arc<Mutex<StreamState>>) -> io::Result<()> {
let mut child = Command::new("sh")
.arg("-c")
.arg(producer)
.stdout(Stdio::piped())
.spawn()?;
if let Some(out) = child.stdout.take() {
for line in BufReader::new(out).lines() {
match line {
Ok(l) => state.lock().unwrap().push(l),
Err(_) => break,
}
}
}
let _ = child.wait();
Ok(())
}
struct LineOut<W: Write>(io::BufWriter<W>);
impl<W: Write> LineOut<W> {
fn new(w: W) -> Self {
LineOut(io::BufWriter::with_capacity(256 * 1024, w))
}
fn line(&mut self, l: &str) -> io::Result<()> {
writeln!(self.0, "{l}")
}
fn sync(&mut self, more: bool) -> io::Result<()> {
if more {
Ok(())
} else {
self.0.flush()
}
}
fn finish(&mut self) -> io::Result<()> {
self.0.flush()
}
}
fn read_line_into<R: BufRead>(r: &mut R, buf: &mut Vec<u8>, line: &mut String) -> bool {
buf.clear();
match r.read_until(b'\n', buf) {
Ok(0) | Err(_) => return false,
Ok(_) => {}
}
if buf.last() == Some(&b'\n') {
buf.pop();
if buf.last() == Some(&b'\r') {
buf.pop();
}
}
let Ok(text) = std::str::from_utf8(buf) else {
return false;
};
line.clear();
line.push_str(text);
true
}
fn stream_producer<W: Write>(producer: &str, out: W) -> io::Result<()> {
let mut child = Command::new("sh")
.arg("-c")
.arg(producer)
.stdout(Stdio::piped())
.spawn()?;
if let Some(pipe) = child.stdout.take() {
let mut w = LineOut::new(out);
let mut r = BufReader::with_capacity(256 * 1024, pipe);
let (mut buf, mut line) = (Vec::new(), String::new());
while read_line_into(&mut r, &mut buf, &mut line) {
let more = !r.buffer().is_empty();
if w.line(&line).and_then(|()| w.sync(more)).is_err() {
break;
}
}
let _ = w.finish();
}
let _ = child.wait();
Ok(())
}
fn passthrough<R: Read, W: Write>(prelude: &[String], input: R, out: W) -> io::Result<()> {
let mut out = LineOut::new(out);
let mut r = BufReader::with_capacity(256 * 1024, input);
for (i, l) in prelude.iter().enumerate() {
let more = i + 1 < prelude.len();
if let Err(e) = out.line(l).and_then(|()| out.sync(more)) {
return ok_on_broken_pipe(Err(e));
}
}
let (mut buf, mut line) = (Vec::new(), String::new());
while read_line_into(&mut r, &mut buf, &mut line) {
let more = !r.buffer().is_empty();
if let Err(e) = out.line(&line).and_then(|()| out.sync(more)) {
return ok_on_broken_pipe(Err(e));
}
}
ok_on_broken_pipe(out.finish())
}
fn ok_on_broken_pipe(r: io::Result<()>) -> io::Result<()> {
match r {
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(()),
other => other,
}
}
fn stream_out<R: Read, W: Write>(
ops: &[QueryOp],
input: R,
out: W,
err: &mut dyn Write,
) -> io::Result<()> {
let mut out = LineOut::new(out);
let mut r = BufReader::with_capacity(256 * 1024, input);
let (mut buf, mut line) = (Vec::new(), String::new());
let (mut docs, ops) = match ops.split_first() {
Some((QueryOp::JsonDocs, rest)) => (Some(crate::jsondocs::JsonDocs::default()), rest),
_ => (None, ops),
};
let mut items = Vec::new();
let mut failed = false;
let mut lineno = 0usize;
loop {
let more = read_line_into(&mut r, &mut buf, &mut line);
if more {
lineno += 1;
}
let run = match (&mut docs, more) {
(Some(d), true) if d.passes_through(&line) => stream_item(
ops,
std::slice::from_ref(&line),
lineno,
&mut out,
&mut failed,
err,
),
(Some(d), true) => {
d.push(&line, &mut items);
Ok(())
}
(Some(d), false) => {
d.finish(&mut items);
Ok(())
}
(None, true) => stream_item(
ops,
std::slice::from_ref(&line),
lineno,
&mut out,
&mut failed,
err,
),
(None, false) => Ok(()),
};
let run = run.and_then(|()| {
items.drain(..).try_for_each(|item| {
stream_item(
ops,
std::slice::from_ref(&item),
lineno,
&mut out,
&mut failed,
err,
)
})
});
if let Err(e) = run {
return ok_on_broken_pipe(Err(e));
}
if !more {
break;
}
if let Err(e) = out.sync(!r.buffer().is_empty()) {
return ok_on_broken_pipe(Err(e));
}
}
let done = ok_on_broken_pipe(out.finish());
if failed {
crate::hosted::exit(5);
}
done
}
fn stream_item<W: Write>(
ops: &[QueryOp],
item: &[String],
lineno: usize,
out: &mut LineOut<W>,
failed: &mut bool,
err: &mut dyn Write,
) -> io::Result<()> {
let mut rep = query::JqReport::default();
match query::eval_reporting(ops, item, 0.0, lineno, &mut rep) {
QueryResult::Lines(ls) => {
for l in ls {
out.line(&l)?;
}
*failed = rep.last_failed;
}
QueryResult::Error(msg) => {
rep.errors.push(msg);
*failed = true;
}
_ => *failed = false,
}
if !rep.errors.is_empty() || rep.halt.is_some() {
let _ = out.finish();
report_jq(&rep, err);
}
if let Some((code, _)) = rep.halt {
crate::hosted::exit(code);
}
Ok(())
}
fn report_jq(rep: &query::JqReport, err: &mut dyn Write) {
for e in &rep.errors {
let _ = writeln!(err, "arb: {e}");
}
if let Some((_, msg)) = &rep.halt {
let _ = write!(err, "{msg}");
}
}
fn emit_out<W: Write>(
ops: &[QueryOp],
state: &Arc<Mutex<StreamState>>,
json: bool,
mut out: W,
err: &mut dyn Write,
) -> io::Result<()> {
let st = state.lock().unwrap();
let raw: Vec<String> = st.lines.iter().map(|l| l.to_string()).collect();
let elapsed = st.start.elapsed().as_secs_f64();
if json {
let v = match query::eval(ops, &raw, elapsed) {
QueryResult::Lines(ls) => {
serde_json::Value::Array(ls.into_iter().map(serde_json::Value::String).collect())
}
QueryResult::Scalar(s) => serde_json::Number::from_f64(s)
.map(serde_json::Value::Number)
.unwrap_or(serde_json::Value::Null),
QueryResult::Pairs(ps) => {
let mut m = serde_json::Map::new();
for (k, c) in ps {
m.insert(k, serde_json::Value::Number(c.into()));
}
serde_json::Value::Object(m)
}
QueryResult::Error(msg) => {
let _ = writeln!(err, "arb: {msg}");
crate::hosted::exit(5);
}
};
writeln!(out, "{}", serde_json::to_string(&v).unwrap_or_default())?;
return Ok(());
}
let mut rep = query::JqReport::default();
match query::eval_reporting(ops, &raw, elapsed, 1, &mut rep) {
QueryResult::Lines(ls) => {
for l in ls {
writeln!(out, "{l}")?;
}
}
QueryResult::Scalar(v) => writeln!(out, "{}", query::fmt_scalar(v))?,
QueryResult::Pairs(ps) => {
for (k, c) in ps {
writeln!(out, "{k}\t{c}")?;
}
}
QueryResult::Error(msg) => {
let _ = out.flush();
let _ = writeln!(err, "arb: {msg}");
crate::hosted::exit(5);
}
}
if !rep.errors.is_empty() || rep.halt.is_some() {
out.flush()?;
report_jq(&rep, err);
}
match rep.halt {
Some((code, _)) => crate::hosted::exit(code),
None if rep.last_failed => crate::hosted::exit(5),
None => Ok(()),
}
}
fn save_preset(name: &str, cli: &Cli) -> io::Result<()> {
let src = if let Some(e) = &cli.eval {
e.clone()
} else if let Some(f) = &cli.spec {
std::fs::read_to_string(f)?
} else {
eprintln!("arb: --save needs a spec — a FILE argument or -e SRC");
crate::hosted::exit(2);
};
if let Err(e) = parser::parse(&src).and_then(|c| spec::build(&c)) {
eprintln!("arb: --save: invalid spec: {e}");
crate::hosted::exit(1);
}
let dir = spec::lib_dir().ok_or_else(|| {
io::Error::new(
io::ErrorKind::NotFound,
"no preset library (set HOME or ARB_LIB)",
)
})?;
match spec::install_preset(&dir, name, &src) {
Ok(path) => {
eprintln!("arb: saved preset `{name}` -> {}", path.display());
Ok(())
}
Err(e) => {
eprintln!("arb: --save: {e}");
crate::hosted::exit(1);
}
}
}
fn install_cmd(file: &str, as_name: Option<&str>) -> io::Result<()> {
let src = std::fs::read_to_string(file)
.map_err(|e| io::Error::new(e.kind(), format!("{file}: {e}")))?;
let name = as_name
.map(str::to_string)
.or_else(|| {
std::path::Path::new(file)
.file_stem()
.and_then(|s| s.to_str())
.map(str::to_string)
})
.unwrap_or_default();
let Some(dir) = spec::lib_dir() else {
eprintln!("arb: no preset library (set HOME or ARB_LIB)");
crate::hosted::exit(1);
};
match spec::install_preset(&dir, &name, &src) {
Ok(path) => {
eprintln!("arb: installed `{name}` -> {}", path.display());
Ok(())
}
Err(e) => {
eprintln!("arb: {e}");
crate::hosted::exit(1);
}
}
}
fn spec_example(cli: &Cli, sniffed: Option<&str>) -> String {
if let Some(name) = cli.preset.as_deref().or(sniffed) {
return spec::preset_example(name);
}
match cli
.spec
.as_deref()
.and_then(|p| std::fs::read_to_string(p).ok())
{
Some(src) => spec::example_comment(&src),
None => String::new(),
}
}
fn dump_src(cli: &Cli) -> Result<String, String> {
if let Some(p) = &cli.preset {
Ok(format!("import {p}"))
} else if let Some(e) = &cli.eval {
Ok(e.clone())
} else if let Some(path) = &cli.spec {
std::fs::read_to_string(path).map_err(|e| format!("{path}: {e}"))
} else {
Err("dump needs a spec — a FILE argument, -e SRC, or -p NAME".into())
}
}
fn finish_dump(r: Result<(), String>) -> io::Result<()> {
if let Err(e) = r {
eprintln!("arb: {e}");
crate::hosted::exit(1);
}
Ok(())
}
fn dump_tokens(cli: &Cli) -> Result<(), String> {
let src = dump_src(cli)?;
for (tok, off) in crate::lexer::lex(&src, false).map_err(|e| e.to_string())? {
println!("{off}\t{tok:?}");
}
Ok(())
}
fn dump_ast(cli: &Cli) -> Result<(), String> {
let src = dump_src(cli)?;
let cmds = crate::parser::parse(&src).map_err(|e| e.to_string())?;
println!("{cmds:#?}");
Ok(())
}
fn dump_bytecode(cli: &Cli) -> Result<(), String> {
let spec = build_dump_spec(cli)?;
if let Some(ops) = &spec.out {
println!("== out ==\n{ops:#?}");
}
for w in &spec.widgets {
match &w.source {
Some(s) => println!("== {} ({}) ==\n{:#?}", w.path, w.kind.label(), s.pipeline),
None => println!(
"== {} ({}) ==\n(no source pipeline)",
w.path,
w.kind.label()
),
}
}
Ok(())
}
fn disasm(cli: &Cli) -> Result<(), String> {
let spec = build_dump_spec(cli)?;
if let Some(ops) = &spec.out {
println!("; arb pipeline — out");
disasm_ops(ops);
}
for w in &spec.widgets {
println!("; arb pipeline — {} ({})", w.path, w.kind.label());
match &w.source {
Some(s) => disasm_ops(&s.pipeline),
None => println!(" (no source pipeline)"),
}
}
Ok(())
}
fn build_dump_spec(cli: &Cli) -> Result<Spec, String> {
let src = dump_src(cli)?;
let cmds = parser::parse(&src).map_err(|e| e.to_string())?;
spec::build(&cmds).map_err(|e| e.to_string())
}
fn disasm_ops(ops: &[QueryOp]) {
for (i, op) in ops.iter().enumerate() {
println!("{i:04} {op:?}");
}
}
fn load_spec(cli: &Cli) -> Result<Spec, String> {
let src = if let Some(p) = &cli.preset {
format!("import {p}")
} else if let Some(e) = &cli.eval {
e.clone()
} else if let Some(path) = &cli.spec {
std::fs::read_to_string(path).map_err(|e| format!("{path}: {e}"))?
} else {
default_spec_src(cli.fzf).to_string()
};
build_spec(&src)
}
fn build_spec(src: &str) -> Result<Spec, String> {
spec::build(&cache::parse_cached(src).map_err(String::from)?).map_err(String::from)
}
const SNIFF_LINES: usize = 8;
const SNIFF_DEADLINE_MS: i32 = 150;
fn stdin_ready(timeout_ms: i32) -> bool {
let mut pfd = libc::pollfd {
fd: 0,
events: libc::POLLIN,
revents: 0,
};
unsafe { libc::poll(&mut pfd, 1, timeout_ms) > 0 && (pfd.revents & libc::POLLIN) != 0 }
}
fn peek_lines(max: usize, deadline_ms: i32) -> Vec<String> {
use std::io::BufRead;
let mut out = Vec::new();
let stdin = io::stdin();
let mut lock = stdin.lock();
for _ in 0..max {
if !stdin_ready(deadline_ms) {
break; }
let mut line = String::new();
match lock.read_line(&mut line) {
Ok(0) => break, Ok(_) => out.push(line.trim_end_matches(['\n', '\r']).to_string()),
Err(_) => break,
}
}
out
}
fn default_spec_src(fzf: bool) -> &'static str {
if fzf {
"select .sel\nsource .sel { in }"
} else {
"tail .stream\nsource .stream { in }"
}
}
fn is_arb_pipeline(s: &str) -> bool {
s.split('|').any(|seg| seg.trim() == "_")
}
fn parse_pipeline(s: &str) -> (String, Option<String>) {
let segs: Vec<&str> = s.split('|').map(str::trim).collect();
match segs.iter().position(|seg| *seg == "_") {
Some(p) => {
let producer = segs[..p].join(" | ");
let consumer = segs[p + 1..].join(" | ");
(
producer,
if consumer.is_empty() {
None
} else {
Some(consumer)
},
)
}
None => (s.trim().to_string(), None),
}
}
fn spawn_source_reader<R: io::Read + Send + 'static>(reader: R, state: Arc<Mutex<StreamState>>) {
thread::spawn(move || {
for line in BufReader::new(reader).lines() {
match line {
Ok(l) => state.lock().unwrap().push(l),
Err(_) => break,
}
}
});
}
fn spawn_producer(
producer: &str,
state: Arc<Mutex<StreamState>>,
) -> io::Result<Arc<Mutex<StreamState>>> {
let mut child = Command::new("sh")
.arg("-c")
.arg(producer)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let out = child
.stdout
.take()
.ok_or_else(|| io::Error::other("no stdout"))?;
let err = child
.stderr
.take()
.ok_or_else(|| io::Error::other("no stderr"))?;
thread::spawn(move || {
let _ = child.wait();
});
spawn_source_reader(out, state);
let err_state = Arc::new(Mutex::new(StreamState::new()));
spawn_source_reader(err, err_state.clone());
Ok(err_state)
}
fn poll_producer(
cmd: &str,
interval: Duration,
state: Arc<Mutex<StreamState>>,
) -> Arc<Mutex<StreamState>> {
let err_state = Arc::new(Mutex::new(StreamState::new()));
let (cmd, es) = (cmd.to_string(), err_state.clone());
thread::spawn(move || loop {
if let Ok(mut child) = Command::new("sh")
.arg("-c")
.arg(&cmd)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
{
let (st, et) = (state.clone(), es.clone());
let ho = child.stdout.take().map(|o| {
thread::spawn(move || {
for l in BufReader::new(o).lines().map_while(Result::ok) {
st.lock().unwrap().push(l);
}
})
});
let he = child.stderr.take().map(|e| {
thread::spawn(move || {
for l in BufReader::new(e).lines().map_while(Result::ok) {
et.lock().unwrap().push(l);
}
})
});
if let Some(h) = ho {
let _ = h.join();
}
if let Some(h) = he {
let _ = h.join();
}
let _ = child.wait();
}
thread::sleep(interval);
});
err_state
}
struct Interruptible<R> {
inner: R,
fd: RawFd,
stop: Arc<AtomicBool>,
}
impl<R: io::Read> io::Read for Interruptible<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
loop {
if self.stop.load(Ordering::Relaxed) {
return Ok(0);
}
match self.inner.read(buf) {
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
if tui::select_readable(self.fd, tui::STOP_POLL_MS).is_none() {
thread::sleep(Duration::from_millis(tui::STOP_POLL_MS as u64));
}
}
Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
r => return r,
}
}
}
}
fn set_nonblocking(fd: RawFd) -> Option<libc::c_int> {
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
return None;
}
Some(flags)
}
struct StdinFeed {
stop: Arc<AtomicBool>,
thread: Option<thread::JoinHandle<()>>,
saved_flags: Option<libc::c_int>,
}
impl Drop for StdinFeed {
fn drop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
let Some(flags) = self.saved_flags else {
return;
};
if let Some(t) = self.thread.take() {
let _ = t.join();
}
unsafe { libc::fcntl(0, libc::F_SETFL, flags) };
}
}
fn spawn_reader(
state: Arc<Mutex<StreamState>>,
tee: bool,
controls: Arc<Mutex<tui::Controls>>,
out_ops: Option<Vec<QueryOp>>,
prelude: Vec<String>,
) -> StdinFeed {
let stop = Arc::new(AtomicBool::new(false));
let saved_flags = set_nonblocking(0);
let thread_stop = stop.clone();
let thread = thread::spawn(move || {
let stop = thread_stop;
let mut out = if tee { Some(io::stdout().lock()) } else { None };
let mut downstream_open = tee;
let map = out_ops.is_some();
let mut last_inputs: Option<Vec<(String, String)>> = None;
let mut resolved: Vec<QueryOp> = Vec::new();
let mut resolved_ok = false;
let mut stdin = io::BufReader::with_capacity(
256 * 1024,
Interruptible {
inner: io::stdin().lock(),
fd: 0,
stop: stop.clone(),
},
);
let mut raw = Vec::new();
let mut prelude = prelude.into_iter();
let feed = std::iter::from_fn(move || -> Option<std::sync::Arc<str>> {
if let Some(p) = prelude.next() {
return Some(std::sync::Arc::from(p.as_str()));
}
raw.clear();
match stdin.read_until(b'\n', &mut raw) {
Ok(0) | Err(_) => None,
Ok(_) => {
if raw.last() == Some(&b'\n') {
raw.pop();
}
Some(std::sync::Arc::from(String::from_utf8_lossy(&raw).as_ref()))
}
}
});
const BATCH: usize = 512;
const FLUSH_AFTER: std::time::Duration = std::time::Duration::from_millis(15);
let mut batch: Vec<std::sync::Arc<str>> = Vec::with_capacity(BATCH);
let mut last_flush = std::time::Instant::now();
for l in feed {
if stop.load(Ordering::Relaxed) {
break;
}
if let Some(o) = out.as_mut() {
let (filter, inputs) = {
let c = controls.lock().unwrap();
(c.filter.clone(), c.inputs.clone())
};
if tui::filter_matches(&l, &filter) && downstream_open {
if map {
if last_inputs.as_ref() != Some(&inputs) {
let imap: std::collections::HashMap<String, String> =
inputs.iter().cloned().collect();
resolved =
spec::resolve_pipeline(out_ops.as_deref().unwrap_or(&[]), &imap);
resolved_ok = query::is_line_streamable(&resolved);
last_inputs = Some(inputs);
}
let outs = if resolved_ok {
tui::project_line(&resolved, &l)
} else {
vec![l.to_string()]
};
for ol in outs {
if writeln!(o, "{ol}").and_then(|()| o.flush()).is_err() {
downstream_open = false;
break;
}
}
} else if writeln!(o, "{l}").and_then(|()| o.flush()).is_err() {
downstream_open = false;
}
}
}
batch.push(l);
if batch.len() >= BATCH || last_flush.elapsed() >= FLUSH_AFTER {
state.lock().unwrap().extend(batch.drain(..));
last_flush = std::time::Instant::now();
}
}
if !batch.is_empty() {
state.lock().unwrap().extend(batch.drain(..));
}
});
StdinFeed {
stop,
thread: Some(thread),
saved_flags,
}
}
fn spawn_preview(
cmd: Vec<String>,
state: Arc<Mutex<StreamState>>,
controls: Arc<Mutex<tui::Controls>>,
dstate: Arc<Mutex<StreamState>>,
) {
thread::spawn(move || {
let mut last: Option<(String, u64)> = None;
loop {
thread::sleep(Duration::from_millis(250));
let (filter, quit) = {
let c = controls.lock().unwrap();
(c.filter.clone(), c.quit)
};
if quit {
break;
}
let key = (filter.clone(), state.lock().unwrap().total);
if last.as_ref() == Some(&key) {
continue;
}
last = Some(key);
let input: Vec<String> = {
let s = state.lock().unwrap();
s.lines
.iter()
.filter(|l| tui::filter_matches(l, &filter))
.map(|l| l.to_string())
.collect()
};
let output = run_capture(&cmd, &input);
let mut d = dstate.lock().unwrap();
*d = StreamState::new();
for l in output {
d.push(l);
}
}
});
}
fn shell_escape(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
fn spawn_item_preview(
template: String,
shell: Vec<String>,
delimiter: Option<String>,
scroll: Option<crate::fzf::PreviewScroll>,
controls: Arc<Mutex<tui::Controls>>,
pstate: Arc<Mutex<StreamState>>,
) {
thread::spawn(move || {
let mut last = String::from("\u{0}");
loop {
thread::sleep(Duration::from_millis(120));
let (cur, quit) = {
let c = controls.lock().unwrap();
(c.current.clone(), c.quit)
};
if quit {
break;
}
if cur == last {
continue;
}
last = cur.clone();
if cur.is_empty() {
continue;
}
let cmd =
crate::fzf::expand_preview(&template, &cur, delimiter.as_deref(), shell_escape);
let mut argv = shell.clone();
argv.push(cmd);
let output = run_capture(&argv, &[]);
let mut p = pstate.lock().unwrap();
*p = StreamState::new();
for l in output {
p.push(l);
}
drop(p);
let target = scroll
.as_ref()
.and_then(|s| Some((s.target(&cur, delimiter.as_deref())?, s.denom)));
let mut c = controls.lock().unwrap();
if c.current == cur {
c.preview_start = target;
}
}
});
}
fn run_capture(cmd: &[String], input: &[String]) -> Vec<String> {
let child = Command::new(&cmd[0])
.args(&cmd[1..])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn();
let mut child = match child {
Ok(c) => c,
Err(e) => return vec![format!("arb: {}: {e}", cmd[0])],
};
if let Some(mut si) = child.stdin.take() {
for l in input {
if writeln!(si, "{l}").is_err() {
break; }
}
}
match child.wait_with_output() {
Ok(o) => {
let mut v: Vec<String> = String::from_utf8_lossy(&o.stdout)
.lines()
.map(String::from)
.collect();
v.extend(String::from_utf8_lossy(&o.stderr).lines().map(String::from));
v
}
Err(e) => vec![format!("arb: {}: {e}", cmd[0])],
}
}
fn read_stdin_sync<R: Read>(state: &Arc<Mutex<StreamState>>, prelude: &[String], input: R) {
for l in prelude {
state.lock().unwrap().push(l.clone());
}
for line in BufReader::new(input).lines() {
match line {
Ok(l) => state.lock().unwrap().push(l),
Err(_) => break,
}
}
}
fn dump<W: Write>(spec: &Spec, state: &Arc<Mutex<StreamState>>, mut out: W) -> io::Result<()> {
let st = state.lock().unwrap();
let raw: Vec<String> = st.lines.iter().map(|l| l.to_string()).collect();
let elapsed = st.start.elapsed().as_secs_f64();
writeln!(
out,
"arb: spec — {} widget(s) (no terminal; render skipped)",
spec.widgets.len()
)?;
for w in &spec.widgets {
let (src, res) = match &w.source {
Some(s) => {
let r = match query::eval(&s.pipeline, &raw, elapsed) {
QueryResult::Scalar(v) => format!("= {v}"),
QueryResult::Lines(ls) => format!("-> {} line(s)", ls.len()),
QueryResult::Pairs(p) => format!("-> {} group(s)", p.len()),
QueryResult::Error(e) => format!("! {e}"),
};
(format!("stdin[{} op]", s.pipeline.len()), r)
}
None => ("-".to_string(), String::new()),
};
writeln!(
out,
" {:<10} {:<6} source={:<12} {:<16} opts={:?}",
w.path,
w.kind.label(),
src,
res,
w.opts
)?;
}
writeln!(out, "stream: {} lines", st.total)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::fzf_compat_args;
fn run(args: &[&str]) -> Vec<String> {
fzf_compat_args(args.iter().map(|s| s.to_string()))
}
#[test]
fn presentation_flags_are_parsed_then_kept_from_clap() {
let argv = [
"arb",
"--fzf",
"--layout=reverse",
"--info",
"inline",
"--pointer",
">",
"--marker",
"+",
"--color=dark,hl:2",
"--bind",
"tab:toggle+down",
"--scroll-off",
"0",
"--ellipsis",
"~",
"--tac",
"--no-scrollbar",
"--prompt",
"P> ",
];
let look = crate::fzf::Look::parse(&argv.iter().map(|s| s.to_string()).collect::<Vec<_>>());
assert_eq!(look.layout, crate::fzf::Layout::Reverse);
assert_eq!(look.pointer, ">");
assert_eq!(look.scroll_off, 0);
assert_eq!(look.ellipsis, "~");
assert!(look.tac && !look.scrollbar);
let out = run(&argv);
for a in [
"--layout=reverse",
"--info",
"inline",
"--pointer",
"--marker",
"--color=dark,hl:2",
"--bind",
"tab:toggle+down",
"--scroll-off",
"--ellipsis",
"--tac",
"--no-scrollbar",
] {
assert!(!out.iter().any(|o| o == a), "{a} reached clap");
}
assert!(out.iter().any(|o| o == "--prompt"));
assert!(out.iter().any(|o| o == "P> "));
}
#[test]
fn strips_cosmetic_fzf_flags_keeps_honored() {
let out = run(&[
"arb",
"--fzf",
"--ansi",
"--border",
"--reverse",
"--preview-window",
"down:3",
"--min-height",
"15",
"--no-sort",
"--query",
"foo",
]);
assert!(!out
.iter()
.any(|a| a == "--ansi" || a == "--border" || a == "--reverse"));
assert!(!out.iter().any(|a| a == "--preview-window" || a == "down:3"));
assert!(!out.iter().any(|a| a == "--min-height" || a == "15"));
assert!(out.iter().any(|a| a == "--no-sort"));
assert_eq!(
out.iter().position(|a| a == "--query").map(|i| &out[i + 1]),
Some(&"foo".to_string())
);
assert!(out.iter().any(|a| a == "--fzf"));
}
#[test]
fn translates_fzf_plus_and_exact_shorthands() {
let out = run(&["arb", "--fzf", "+m", "+s", "-e"]);
assert!(!out.iter().any(|a| a == "+m" || a == "+s"));
assert!(out.iter().any(|a| a == "--no-sort"));
assert!(out.iter().any(|a| a == "--exact"));
let out2 = run(&["arb", "-e", "gauge .g"]);
assert!(out2.iter().any(|a| a == "-e"));
assert!(!out2.iter().any(|a| a == "--exact"));
}
#[test]
fn drops_fzf_short_value_flags() {
let out = run(&["arb", "--fzf", "-n2..,..", "-d", ":", "--query", "q"]);
assert!(!out
.iter()
.any(|a| a.starts_with("-n") || a.starts_with("-d")));
assert!(!out.iter().any(|a| a == "2.." || a == ":"));
assert_eq!(
out.iter().position(|a| a == "--query").map(|i| &out[i + 1]),
Some(&"q".to_string())
);
}
#[test]
fn preview_window_equals_form_drops_cleanly() {
let out = run(&["arb", "--fzf", "--preview-window=right:50%", "--query", "q"]);
assert!(!out.iter().any(|a| a.starts_with("--preview-window")));
assert_eq!(
out.iter().position(|a| a == "--query").map(|i| &out[i + 1]),
Some(&"q".to_string())
);
}
#[test]
fn a_no_flag_cancels_the_arb_option_it_negates() {
let out = run(&[
"arb",
"--fzf",
"--preview",
"cat {}",
"--no-preview",
"--height",
"40%",
"--no-height",
"--header=H",
"--no-header",
"-m",
"--no-multi",
"--exact",
"--no-exact",
"--query",
"q",
]);
for a in ["--preview", "cat {}", "--height", "40%", "--exact", "-m"] {
assert!(!out.iter().any(|o| o == a), "{a} survived its --no- form");
}
assert!(!out.iter().any(|o| o.starts_with("--header")));
assert!(!out.iter().any(|o| o.starts_with("--no-")));
assert_eq!(
out.iter().position(|a| a == "--query").map(|i| &out[i + 1]),
Some(&"q".to_string())
);
}
#[test]
fn sort_and_multi_take_their_optional_count_back() {
let out = run(&["arb", "--fzf", "--no-sort", "--sort", "20", "--multi=5"]);
assert!(!out.iter().any(|a| a == "--no-sort" || a == "20"));
assert_eq!(out.iter().filter(|a| *a == "--multi").count(), 1);
let out = run(&["arb", "--fzf", "--sort", "--query", "q"]);
assert_eq!(
out.iter().position(|a| a == "--query").map(|i| &out[i + 1]),
Some(&"q".to_string())
);
let out = run(&["arb", "--fzf", "-m5", "+m", "-s", "+s"]);
assert!(!out.iter().any(|a| a == "--multi" || a == "5"));
assert_eq!(out, vec!["arb", "--fzf", "--no-sort"]);
}
#[test]
fn an_optional_value_flag_only_eats_a_real_value() {
let out = run(&[
"arb",
"--fzf",
"--color",
"--preview",
"cat {}",
"--border",
"rounded",
"--gap",
"--query",
"q",
]);
assert_eq!(
out.iter()
.position(|a| a == "--preview")
.map(|i| &out[i + 1]),
Some(&"cat {}".to_string())
);
assert!(!out.iter().any(|a| a == "rounded" || a == "--border"));
assert_eq!(
out.iter().position(|a| a == "--query").map(|i| &out[i + 1]),
Some(&"q".to_string())
);
let out = run(&["arb", "--fzf", "--gap", "2", "--query", "q"]);
assert!(!out.iter().any(|a| a == "2"));
}
#[test]
fn fzf_short_flags_translate_or_drop() {
assert_eq!(
run(&["arb", "--fzf", "-qfoo"]),
vec!["arb", "--fzf", "--query", "foo"]
);
assert_eq!(
run(&["arb", "--fzf", "-q", "foo"]),
vec!["arb", "--fzf", "--query", "foo"]
);
assert_eq!(
run(&["arb", "--fzf", "-i", "+i", "-x", "+x", "+c"]),
vec!["arb", "--fzf"]
);
assert!(run(&["arb", "-e", "gauge .g"]).iter().any(|a| a == "-e"));
}
#[test]
fn case_flags_drive_the_matcher() {
use crate::fzf::{Case, Look};
let look = |args: &[&str]| {
Look::parse(&args.iter().map(|s| s.to_string()).collect::<Vec<_>>()).case
};
assert_eq!(look(&["arb", "--fzf"]), Case::Smart);
assert_eq!(look(&["arb", "--fzf", "-i"]), Case::Ignore);
assert_eq!(look(&["arb", "--fzf", "--ignore-case"]), Case::Ignore);
assert_eq!(look(&["arb", "--fzf", "+i"]), Case::Respect);
assert_eq!(look(&["arb", "--fzf", "-i", "--smart-case"]), Case::Smart);
use crate::tui::fuzzy_score_case;
assert!(fuzzy_score_case("README", "read", Case::Smart).is_some());
assert!(fuzzy_score_case("README", "read", Case::Respect).is_none());
assert!(fuzzy_score_case("readme", "READ", Case::Smart).is_none());
assert!(fuzzy_score_case("readme", "READ", Case::Ignore).is_some());
}
#[test]
fn an_interruptible_reader_leaves_an_open_idle_pipe_when_stopped() {
use super::{set_nonblocking, Interruptible};
use std::io::{BufRead, BufReader, Write};
use std::os::unix::io::FromRawFd;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use std::time::Duration;
let mut fds = [0; 2];
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
let (rfd, wfd) = (fds[0], fds[1]);
set_nonblocking(rfd).expect("a pipe goes non-blocking");
let (read_end, mut writer) = unsafe {
(
std::fs::File::from_raw_fd(rfd),
std::fs::File::from_raw_fd(wfd),
)
};
let stop = Arc::new(AtomicBool::new(false));
let reader = Interruptible {
inner: read_end,
fd: rfd,
stop: stop.clone(),
};
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
for line in BufReader::new(reader).lines() {
tx.send(Some(line.unwrap())).unwrap();
}
tx.send(None).unwrap();
});
let next = || rx.recv_timeout(Duration::from_secs(5)).unwrap();
writeln!(writer, "a\nb").unwrap();
assert_eq!(next().as_deref(), Some("a"));
assert_eq!(next().as_deref(), Some("b"));
assert!(rx.recv_timeout(Duration::from_millis(200)).is_err());
stop.store(true, Ordering::Relaxed);
assert_eq!(next(), None);
}
}