mod cargo;
mod docker;
mod git;
mod go;
pub mod hook;
mod js;
mod pytest;
mod search;
pub(crate) mod shell;
mod text;
use std::ffi::OsString;
use std::io::{IsTerminal, Read, Write};
use std::process::{Command, Stdio};
use std::sync::{mpsc, Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};
pub(crate) struct Run<'a> {
pub code: i32,
pub stdout: &'a str,
pub stderr: &'a str,
}
pub(crate) struct Compact {
pub stdout: String,
pub stderr: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Family {
GitStatus,
GitDiff,
GitLog,
GitCommit,
GitPush,
GitPull,
Cargo,
CargoTest,
Go,
GoTest,
Pytest,
JsTest,
Search,
DockerPs,
}
impl Family {
fn apply(self, run: &Run<'_>) -> Option<Compact> {
match self {
Family::GitStatus => git::status(run),
Family::GitDiff => git::diff(run),
Family::GitLog => git::log(run),
Family::GitCommit => git::commit(run),
Family::GitPush => git::push(run),
Family::GitPull => git::pull(run),
Family::Cargo => cargo::build(run),
Family::CargoTest => cargo::test(run),
Family::Go => go::build(run),
Family::GoTest => go::test(run),
Family::Pytest => pytest::filter(run),
Family::JsTest => js::test(run),
Family::Search => search::filter(run),
Family::DockerPs => docker::ps(run),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Plan {
Passthrough,
Filter(Family),
Generic,
}
const VERBATIM_PROGRAMS: &[&str] = &[
"cat", "head", "tail", "less", "more", "bat", "sed", "awk", "jq", "yq", "echo", "printf",
"xxd", "od", "hexdump", "strings", "base64", "tee", "dd", "sort", "cut", "tr", "curl", "wget",
"ssh", "scp", "vi", "vim", "nvim", "nano", "emacs", "man", "top", "htop", "watch", "sudo",
"doas", "env", "xargs", "exec", "time", "nohup", "script", "tmux", "screen",
];
const VERBATIM_FLAGS: &[&str] = &[
"--json",
"--format",
"--message-format",
"--output-format",
"--porcelain",
"--template",
"--null",
"--null-data",
"--help",
"--version",
"--watch",
"--watchAll",
"--follow",
];
fn program_name(arg: &str) -> &str {
let base = arg.rsplit(['/', '\\']).next().unwrap_or(arg);
base.strip_suffix(".exe").unwrap_or(base)
}
pub(crate) fn before_dashes(args: &[String]) -> &[String] {
let end = args.iter().position(|a| a == "--").unwrap_or(args.len());
&args[..end]
}
pub(crate) fn has_flag(args: &[String], flags: &[&str]) -> bool {
args.iter().any(|a| {
flags
.iter()
.any(|f| a == f || a.strip_prefix(f).is_some_and(|r| r.starts_with('=')))
})
}
pub(crate) fn has_short(args: &[String], letters: &str) -> bool {
args.iter().any(|a| {
a.starts_with('-') && !a.starts_with("--") && a.chars().skip(1).any(|c| letters.contains(c))
})
}
pub(crate) fn skip_options<'a>(args: &'a [String], value_options: &[&str]) -> &'a [String] {
let mut i = 0;
while let Some(arg) = args.get(i) {
if value_options.contains(&arg.as_str()) {
i += 2;
} else if arg.starts_with(['-', '+']) {
i += 1;
} else {
return &args[i..];
}
}
&[]
}
pub(crate) fn asks_for_exact_output(args: &[String]) -> bool {
has_flag(args, VERBATIM_FLAGS) || has_short(args, "z") || args.iter().any(|a| a == "-V")
}
pub(crate) fn plan(args: &[String]) -> Plan {
let Some(first) = args.first() else {
return Plan::Passthrough;
};
let program = program_name(first);
let rest = &args[1..];
if VERBATIM_PROGRAMS.contains(&program) {
return Plan::Passthrough;
}
let plan = match program {
"git" => git::plan(rest),
"cargo" => cargo::plan(rest),
"go" => go::plan(rest),
"pytest" | "py.test" => pytest::plan(rest),
"python" | "python3" => match rest {
[m, module, tail @ ..] if m == "-m" && module == "pytest" => pytest::plan(tail),
_ => Plan::Generic,
},
"npm" | "pnpm" | "yarn" => js::plan(rest),
"grep" | "egrep" | "fgrep" | "rg" => search::plan(rest),
"docker" => docker::plan(rest),
_ => Plan::Generic,
};
if asks_for_exact_output(rest) {
return Plan::Passthrough;
}
plan
}
pub(crate) fn is_filtered(args: &[String]) -> bool {
matches!(plan(args), Plan::Filter(_))
}
pub(crate) fn render(plan: Plan, code: i32, stdout: &str, stderr: &str) -> Compact {
let stdout = text::clean(stdout);
let stderr = text::clean(stderr);
let run = Run {
code,
stdout: &stdout,
stderr: &stderr,
};
let filtered = match plan {
Plan::Filter(family) => family.apply(&run),
Plan::Generic | Plan::Passthrough => None,
};
let mut out = match filtered {
Some(c) => Compact {
stdout: text::cap(&c.stdout),
stderr: text::cap(&c.stderr),
},
None => Compact {
stdout: text::squeeze(&stdout),
stderr: text::squeeze(&stderr),
},
};
if out.stdout.trim().is_empty() && !stdout.trim().is_empty() {
out.stdout = text::squeeze(&stdout);
}
if out.stderr.trim().is_empty() && !stderr.trim().is_empty() {
out.stderr = text::squeeze(&stderr);
}
out
}
fn is_off(value: Option<&str>) -> bool {
value.is_some_and(|v| v.trim().eq_ignore_ascii_case("off"))
}
fn effective_plan(words: &[String], off: bool, interactive: bool) -> Plan {
if off || interactive {
Plan::Passthrough
} else {
plan(words)
}
}
pub fn run(argv: &[OsString]) -> i32 {
let words: Vec<String> = argv
.iter()
.map(|a| a.to_string_lossy().into_owned())
.collect();
let plan = effective_plan(
&words,
is_off(std::env::var("LLMMAN_TK").ok().as_deref()),
std::io::stdin().is_terminal(),
);
run_with(
argv,
plan,
&mut std::io::stdout().lock(),
&mut std::io::stderr().lock(),
)
}
fn run_with(argv: &[OsString], plan: Plan, stdout: &mut dyn Write, stderr: &mut dyn Write) -> i32 {
let Some(program) = argv.first() else {
return 2;
};
let mut cmd = Command::new(program);
cmd.args(&argv[1..]);
if plan == Plan::Passthrough {
return passthrough(cmd, program);
}
cmd.stdin(Stdio::inherit());
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
let mut child = match cmd.spawn() {
Ok(child) => child,
Err(e) => return spawn_failed(program, &e),
};
let out = child.stdout.take().map(Reader::spawn);
let err = child.stderr.take().map(Reader::spawn);
let status = child.wait();
let deadline = Instant::now() + PIPE_GRACE;
let collect = |r: Option<Reader>| r.map_or((String::new(), true), |r| r.finish(deadline));
let ((raw_out, out_ended), (raw_err, err_ended)) = (collect(out), collect(err));
let code = match status {
Ok(status) => exit_code(status),
Err(e) => {
eprintln!("llmman tk: waiting for {}: {e}", program.to_string_lossy());
1
}
};
let shown = render(plan, code, &raw_out, &raw_err);
write_all(stdout, &shown.stdout);
write_all(stderr, &shown.stderr);
if !(out_ended && err_ended) {
write_all(
stderr,
"llmman tk: a process the command left running still holds its output open; what it writes later is not shown\n",
);
}
code
}
fn write_all(to: &mut dyn Write, text: &str) {
let _ = to.write_all(text.as_bytes());
let _ = to.flush();
}
fn passthrough(mut cmd: Command, program: &OsString) -> i32 {
#[cfg(all(unix, not(test)))]
{
use std::os::unix::process::CommandExt;
let e = cmd.exec();
spawn_failed(program, &e)
}
#[cfg(not(all(unix, not(test))))]
{
match cmd.status() {
Ok(status) => exit_code(status),
Err(e) => spawn_failed(program, &e),
}
}
}
fn spawn_failed(program: &OsString, e: &std::io::Error) -> i32 {
let name = program.to_string_lossy();
if e.kind() == std::io::ErrorKind::NotFound {
eprintln!("llmman tk: {name}: command not found");
127
} else {
eprintln!("llmman tk: {name}: {e}");
126
}
}
fn exit_code(status: std::process::ExitStatus) -> i32 {
if let Some(code) = status.code() {
return code;
}
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
if let Some(signal) = status.signal() {
return 128 + signal;
}
}
1
}
const KEEP_HEAD: usize = 4 << 20;
const KEEP_TAIL: usize = 4 << 20;
#[derive(Default)]
struct Bounded {
head: Vec<u8>,
tail: std::collections::VecDeque<u8>,
dropped: u64,
}
impl Bounded {
fn push(&mut self, mut chunk: &[u8]) {
if self.head.len() < KEEP_HEAD {
let n = (KEEP_HEAD - self.head.len()).min(chunk.len());
self.head.extend_from_slice(&chunk[..n]);
chunk = &chunk[n..];
}
self.tail.extend(chunk);
while self.tail.len() > KEEP_TAIL {
self.tail.pop_front();
self.dropped += 1;
}
}
fn into_text(self) -> String {
let mut bytes = self.head;
if self.dropped > 0 {
bytes.extend_from_slice(
format!("\n... {} bytes omitted ...\n", self.dropped).as_bytes(),
);
}
bytes.extend(self.tail);
String::from_utf8_lossy(&bytes).into_owned()
}
}
const PIPE_GRACE: Duration = Duration::from_secs(1);
struct Reader {
kept: Arc<Mutex<Bounded>>,
ended: mpsc::Receiver<()>,
}
impl Reader {
fn spawn(source: impl Read + Send + 'static) -> Self {
let kept = Arc::new(Mutex::new(Bounded::default()));
let (done, ended) = mpsc::channel();
let into = Arc::clone(&kept);
std::thread::spawn(move || {
capture(source, &into);
let _ = done.send(());
});
Reader { kept, ended }
}
fn finish(self, deadline: Instant) -> (String, bool) {
let ended = self
.ended
.recv_timeout(deadline.saturating_duration_since(Instant::now()))
.is_ok();
let mut kept = self.kept.lock().unwrap_or_else(PoisonError::into_inner);
(std::mem::take(&mut *kept).into_text(), ended)
}
}
fn capture(mut source: impl Read, into: &Mutex<Bounded>) {
let mut buf = [0u8; 64 * 1024];
loop {
match source.read(&mut buf) {
Ok(0) => return,
Ok(n) => into
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push(&buf[..n]),
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(_) => return,
}
}
}
#[cfg(test)]
mod tests;