use std::io::{self, BufRead, BufReader, IsTerminal, Write};
use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use clap::Parser;
use arb::query::{self, QueryOp, QueryResult};
use arb::spec::{self, Spec};
use arb::stream::StreamState;
use arb::{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",
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",
help = "\x1b[32m//\x1b[0m fzf prompt string (default '> ')"
)]
prompt: Option<String>,
#[arg(
long = "header",
value_name = "STR",
help = "\x1b[32m//\x1b[0m fzf header line shown above the list"
)]
header: Option<String>,
#[arg(
long = "exact",
help = "\x1b[32m//\x1b[0m fzf: exact substring match (not fuzzy)"
)]
exact: bool,
#[arg(
long = "no-sort",
help = "\x1b[32m//\x1b[0m fzf: keep input order (no score sort)"
)]
no_sort: bool,
#[arg(
long = "query",
value_name = "STR",
help = "\x1b[32m//\x1b[0m fzf: initial query"
)]
query: Option<String>,
#[arg(
short = 'm',
long = "multi",
help = "\x1b[32m//\x1b[0m fzf: multi-select (Tab marks)"
)]
multi: bool,
#[arg(
long = "height",
value_name = "N|N%",
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>,
}
fn fzf_compat_args(args: impl Iterator<Item = String>) -> Vec<String> {
const DROP_BOOL: &[&str] = &[
"--ansi",
"--border",
"--reverse",
"--print-query",
"--cycle",
"--select-1",
"-1",
"--exit-0",
"-0",
"--sort",
"--extended",
"--no-mouse",
"--filepath-word",
"--keep-right",
];
const DROP_VALUE: &[&str] = &[
"--min-height",
"--tiebreak",
"--layout",
"--info",
"--preview-window",
"--header-lines",
"--with-nth",
"--nth",
"--bind",
"--color",
"--pointer",
"--marker",
"--border-label",
"--tabstop",
];
let argv: Vec<String> = args.collect();
let fzf = argv.iter().any(|a| a == "--fzf");
let mut out = Vec::new();
let mut it = argv.into_iter().peekable();
while let Some(a) = it.next() {
if fzf && a == "-e" {
out.push("--exact".to_string());
continue;
}
if fzf && (a.starts_with("-n") || a.starts_with("-d")) {
if a.len() == 2 {
it.next(); }
continue;
}
match a.as_str() {
"+m" => {
continue; }
"+s" => {
out.push("--no-sort".to_string());
continue;
}
_ => {}
}
let key = a.split('=').next().unwrap_or(&a);
if DROP_BOOL.contains(&key) {
continue;
}
if DROP_VALUE.contains(&key) {
if !a.contains('=') {
it.next();
}
continue;
}
out.push(a);
}
out
}
fn main() -> io::Result<()> {
let raw: Vec<String> = std::env::args().skip(1).collect();
if let Some(code) = arb::pkg::dispatch(&raw) {
std::process::exit(code);
}
let cli = Cli::parse_from(fzf_compat_args(std::env::args()));
if cli.repl {
arb::repl::run();
return Ok(());
}
if cli.lsp {
arb::lsp::run();
return Ok(());
}
if cli.dap {
arb::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) in spec::list_presets() {
writeln!(out, "{name:<10} {desc}")?;
}
return Ok(());
}
if let Some(expr) = &cli.tiers {
match arb::tiers::report(expr) {
Ok(r) => println!("{r}"),
Err(e) => {
eprintln!("arb: --tiers: {e}");
std::process::exit(1);
}
}
return Ok(());
}
if cli.list_themes {
let mut out = io::stdout().lock();
for (name, pal) in arb::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 arb::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}");
std::process::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)");
std::process::exit(1);
};
let mut out = io::stdout().lock();
for (name, desc) in spec::list_user_presets(&dir) {
writeln!(out, "{name:<10} {desc}")?;
}
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)");
std::process::exit(1);
};
match spec::uninstall_preset(&dir, &name) {
Ok(true) => eprintln!("arb: uninstalled `{name}`"),
Ok(false) => {
eprintln!("arb: `{name}` is not installed");
std::process::exit(1);
}
Err(e) => {
eprintln!("arb: uninstall: {e}");
std::process::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() {
arb::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();
arb::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}");
std::process::exit(1);
}
}
};
match cli.theme.as_deref() {
Some("off") | Some("none") => spec.theme = None,
Some(name) => match arb::theme::by_name(name) {
Some(p) => spec.theme = Some(p),
None => {
eprintln!("arb: --theme: unknown theme `{name}` (see `arb --list-themes`)");
std::process::exit(1);
}
},
None if !spec.theme_off && spec.theme.is_none() => {
spec.theme =
Some(arb::theme::config_default().unwrap_or_else(arb::theme::default_palette));
}
None => {}
}
let fzf_mode = cli.fzf
|| spec
.widgets
.iter()
.any(|w| w.kind == spec::WidgetKind::Select);
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!("{}", arb::web::render_html(&spec));
return Ok(());
}
if cli.test {
let report = arb::testrun::run(&spec.tests);
print!("{}", report.text);
std::process::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.spawn.clone()).or_else(|| {
spec.source_file
.as_deref()
.map(|f| format!("cat -- {}", shell_quote(f)))
});
let poll = if producer.is_some() {
None
} else {
spec.poll.clone()
};
let needs_stdin =
fzf_mode || spec.out.is_some() || spec.widgets.iter().any(|w| w.source.is_some());
if needs_stdin && producer.is_none() && poll.is_none() && io::stdin().is_terminal() {
eprintln!("arb: spec reads stdin but nothing is piped — e.g. `find / | arb`");
std::process::exit(2);
}
const MAX_RETAIN: usize = 2_000_000;
let state = Arc::new(Mutex::new(if fzf_mode || spec.out.is_some() {
StreamState::with_cap(MAX_RETAIN)
} else {
StreamState::new()
}));
if cli.serve {
if let Some(prod) = &producer {
if let Err(e) = spawn_producer(prod, state.clone()) {
eprintln!("arb: run: {e}");
std::process::exit(1);
}
} else if let Some((cmd, dur)) = &poll {
poll_producer(cmd, *dur, state.clone());
} else if needs_stdin {
let controls = Arc::new(Mutex::new(tui::Controls::default()));
spawn_reader(state.clone(), false, controls, None, prelude.clone());
}
return arb::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 err_pane = if spec.spawn_pty && run_pipeline.is_none() && producer.is_some() {
let prod = producer.as_deref().unwrap();
match arb::pty::spawn_pty_producer(prod, state.clone()) {
Ok(writer) => {
controls.lock().unwrap().pty_writer = Some(writer);
None
}
Err(e) => {
eprintln!("arb: spawn -pty: {e}");
std::process::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}");
std::process::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();
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(), 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 arb::actor::Session::build(&spec.actors, &spec.actor_refs) {
Ok(session) => c.session = session,
Err(e) => {
eprintln!("arb: {e}");
std::process::exit(1);
}
}
}
c.theme = spec.theme;
c.theme_idx = spec.theme.map(arb::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;
if let Some(q) = &cli.query {
c.filter = q.clone();
}
let _ = cli.multi;
}
let outcome = tui::run(
&spec,
state,
controls.clone(),
down_pane,
err_pane,
fzf_mode,
cli.height.clone(),
);
if fzf_mode {
let c = controls.lock().unwrap();
if c.submit {
let out = match &consumer {
Some(cons) => run_capture(&["sh".into(), "-c".into(), cons.clone()], &c.result),
None => c.result.clone(),
};
for line in out {
println!("{line}");
}
} else {
std::process::exit(130);
}
}
outcome
} else if let Some(out_ops) = &spec.out {
if let Some(prod) = &producer {
read_producer_sync(prod, &state)?;
emit_out(out_ops, &state, cli.json)
} else if let Some((cmd, _)) = &poll {
read_producer_sync(cmd, &state)?;
emit_out(out_ops, &state, cli.json)
} else if needs_stdin && !cli.json && query::is_line_streamable(out_ops) {
stream_out(out_ops)
} else {
if needs_stdin {
read_stdin_sync(&state, &prelude);
}
emit_out(out_ops, &state, cli.json)
}
} else if let Some(prod) = &producer {
stream_producer(prod)
} else if let Some((cmd, _)) = &poll {
stream_producer(cmd)
} else if needs_stdin {
passthrough(&prelude)
} else {
dump(&spec, &state)
}
}
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(())
}
fn stream_producer(producer: &str) -> io::Result<()> {
let mut child = Command::new("sh")
.arg("-c")
.arg(producer)
.stdout(Stdio::piped())
.spawn()?;
if let Some(out) = child.stdout.take() {
let mut w = io::stdout().lock();
for line in BufReader::new(out).lines() {
match line {
Ok(l) => {
if writeln!(w, "{l}").and_then(|()| w.flush()).is_err() {
break;
}
}
Err(_) => break,
}
}
}
let _ = child.wait();
Ok(())
}
fn passthrough(prelude: &[String]) -> io::Result<()> {
let stdin = io::stdin();
let mut out = io::stdout().lock();
let feed = prelude
.iter()
.cloned()
.map(Ok::<_, io::Error>)
.chain(stdin.lock().lines());
for line in feed {
let l = match line {
Ok(l) => l,
Err(_) => break,
};
if let Err(e) = writeln!(out, "{l}").and_then(|()| out.flush()) {
return ok_on_broken_pipe(Err(e));
}
}
Ok(())
}
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(ops: &[QueryOp]) -> io::Result<()> {
let stdin = io::stdin();
let mut out = io::stdout().lock();
for line in stdin.lock().lines() {
let line = match line {
Ok(l) => l,
Err(_) => break,
};
if let QueryResult::Lines(ls) = query::eval(ops, std::slice::from_ref(&line), 0.0) {
for l in ls {
if let Err(e) = writeln!(out, "{l}") {
return ok_on_broken_pipe(Err(e));
}
}
}
if let Err(e) = out.flush() {
return ok_on_broken_pipe(Err(e));
}
}
Ok(())
}
fn emit_out(ops: &[QueryOp], state: &Arc<Mutex<StreamState>>, json: bool) -> io::Result<()> {
let st = state.lock().unwrap();
let raw: Vec<String> = st.lines.iter().cloned().collect();
let elapsed = st.start.elapsed().as_secs_f64();
let result = query::eval(ops, &raw, elapsed);
let mut out = io::stdout().lock();
if json {
let v = match result {
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)
}
};
writeln!(out, "{}", serde_json::to_string(&v).unwrap_or_default())?;
return Ok(());
}
match result {
QueryResult::Lines(ls) => {
for l in ls {
writeln!(out, "{l}")?;
}
}
QueryResult::Scalar(v) => writeln!(out, "{v}")?,
QueryResult::Pairs(ps) => {
for (k, c) in ps {
writeln!(out, "{k}\t{c}")?;
}
}
}
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");
std::process::exit(2);
};
if let Err(e) = parser::parse(&src).and_then(|c| spec::build(&c)) {
eprintln!("arb: --save: invalid spec: {e}");
std::process::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}");
std::process::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)");
std::process::exit(1);
};
match spec::install_preset(&dir, &name, &src) {
Ok(path) => {
eprintln!("arb: installed `{name}` -> {}", path.display());
Ok(())
}
Err(e) => {
eprintln!("arb: {e}");
std::process::exit(1);
}
}
}
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}");
std::process::exit(1);
}
Ok(())
}
fn dump_tokens(cli: &Cli) -> Result<(), String> {
let src = dump_src(cli)?;
for (tok, off) in arb::lexer::lex(&src).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 = arb::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()
};
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
}
fn spawn_reader(
state: Arc<Mutex<StreamState>>,
tee: bool,
controls: Arc<Mutex<tui::Controls>>,
out_ops: Option<Vec<QueryOp>>,
prelude: Vec<String>,
) {
thread::spawn(move || {
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 feed = prelude
.into_iter()
.map(Ok::<_, io::Error>)
.chain(io::stdin().lock().lines());
for line in feed {
let l = match line {
Ok(l) => l,
Err(_) => 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.clone()]
};
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;
}
}
}
state.lock().unwrap().push(l);
}
});
}
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))
.cloned()
.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,
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 = template.replace("{}", &shell_escape(&cur));
let output = run_capture(&["sh".to_string(), "-c".to_string(), cmd], &[]);
let mut p = pstate.lock().unwrap();
*p = StreamState::new();
for l in output {
p.push(l);
}
}
});
}
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(state: &Arc<Mutex<StreamState>>, prelude: &[String]) {
for l in prelude {
state.lock().unwrap().push(l.clone());
}
for line in io::stdin().lock().lines() {
match line {
Ok(l) => state.lock().unwrap().push(l),
Err(_) => break,
}
}
}
fn dump(spec: &Spec, state: &Arc<Mutex<StreamState>>) -> io::Result<()> {
let st = state.lock().unwrap();
let raw: Vec<String> = st.lines.iter().cloned().collect();
let elapsed = st.start.elapsed().as_secs_f64();
let mut out = io::stdout().lock();
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()),
};
(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 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())
);
}
}