//! # terminal-garden //! //! The first face of the bus: a renderer, not a collector. //! Git is the cleanest signal in the suite — discrete, unambiguous, no privacy //! questions — so the garden proves the bus before anything reads the shell. //! //! Like every renderer, the garden is thin: connect to the socket → read the //! git aggregates → fold them into per-repo plots → render. It never touches a //! sensor. The mapping (commits → growth, staleness → wilt) lives in the //! `terminal-garden` library so it can be unit-tested; this binary is just the //! subscriber loop. //! //! Usage: //! ```text //! terminal-garden [--socket ] //! terminal-garden --help | --version //! ``` //! Socket defaults to `$XDG_RUNTIME_DIR/signald.sock`, falling back to //! `~/.local/state/signald/sock`. use std::collections::BTreeMap; use std::path::{Path, PathBuf}; use signal_schema::{Signal, SignalName}; use terminal_garden::{plots_from_signals, render}; /// The `name`s the garden cares about: the git aggregates. const SUBSCRIBE: &[SignalName] = &[ SignalName::CommitsWindow, SignalName::CommitsToday, SignalName::BranchCount, SignalName::DaysSinceLastCommit, ]; fn main() { let socket = parse_socket(); if let Err(e) = run(&socket) { eprintln!("terminal-garden: could not read signald at {}: {e}", socket.display()); eprintln!("terminal-garden: is signald running? (signald --socket {})", socket.display()); std::process::exit(1); } } /// Connect and render live: the daemon replays the last-value cache on connect, /// then streams updates. We keep the latest value of each metric (keyed by name /// and repo tag) and re-render the garden on every frame. `Ok(())` is a clean EOF /// (the daemon closed the stream). Frames this build cannot decode are skipped, /// so an older renderer keeps working against a newer daemon. fn run(socket: &Path) -> std::io::Result<()> { let frames = signal_client::Frames::connect(socket)?; // Keyed by (metric name, source, repo tag) so per-repo signals coexist, // two collectors reporting the same name stay distinct, and updates replace // prior values rather than accumulating. let mut latest: BTreeMap<(u8, u8, Option), Signal> = BTreeMap::new(); // Frames this build cannot decode are skipped by the iterator, so a metric // appended after this renderer was built does not end the stream. for sig in frames { let sig = sig?; if !SUBSCRIBE.contains(&sig.name) { continue; } let key = ( sig.name.to_u8(), sig.source.to_u8(), sig.tag.as_ref().map(|t| t.as_str().to_string()), ); latest.insert(key, sig); let signals: Vec = latest.values().cloned().collect(); let plots = plots_from_signals(&signals); // Clear + home so the garden redraws in place as signals change. print!("\x1b[2J\x1b[H{}", render(&plots)); } Ok(()) } const USAGE: &str = "\ Usage: terminal-garden [--socket ] --socket signald's socket (default $XDG_RUNTIME_DIR/signald.sock, else ~/.local/state/signald/sock) -h, --help print this and exit -V, --version print the version and exit "; fn parse_socket() -> PathBuf { let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "--help" | "-h" => { print!("{USAGE}"); std::process::exit(0); } "--version" | "-V" => { println!("terminal-garden {}", env!("CARGO_PKG_VERSION")); std::process::exit(0); } _ => {} } if arg == "--socket" { if let Some(path) = args.next() { return PathBuf::from(path); } } } signal_client::default_socket_path() }