use std::path::Path;
use std::process::{Command, Stdio};
use std::time::Duration;
use anyhow::{Context, Result};
use serde_json::Value;
use super::command::Hc;
use crate::config::{Placement, SplitDirection};
fn show_args(placement: Placement, direction: SplitDirection, address: &str) -> Vec<String> {
let mut args: Vec<String> = ["do"].into_iter().map(str::to_owned).collect();
match placement {
Placement::Overlay | Placement::Popup => {
args.extend(["terminal.peek", "--terminal", address].map(str::to_owned));
}
Placement::Split => {
args.extend(["terminal.keep", "--terminal", address, "--place"].map(str::to_owned));
args.push(direction.as_str().to_owned());
}
}
args
}
const SHOW_TRIES: u32 = 30;
const SHOW_PAUSE: Duration = Duration::from_millis(100);
const NOT_YET: &str = "no running terminal";
#[derive(Debug, Clone, PartialEq, Eq)]
enum Try {
Shown,
Again(String),
GiveUp(String),
}
fn show(bin: &Path, args: &[String]) -> Try {
let output = Command::new(bin).args(args).stdin(Stdio::null()).output();
match output {
Ok(output) if output.status.success() => Try::Shown,
Ok(output) => {
refusal(&String::from_utf8_lossy(&output.stdout), &String::from_utf8_lossy(&output.stderr))
}
Err(err) => Try::GiveUp(format!("cannot run {}: {err}", bin.display())),
}
}
fn refusal(stdout: &str, stderr: &str) -> Try {
let reply: Option<Value> = serde_json::from_str(stdout.trim()).ok();
let error = reply.as_ref().and_then(|r| r.get("error")).and_then(Value::as_str);
let stderr = stderr.trim();
let reason = match error {
Some(error) => error.to_owned(),
None if stderr.is_empty() => "herdr-app failed with no output".to_owned(),
None => stderr.trim_start_matches("herdr-app: ").to_owned(),
};
if error.is_some_and(|e| e.starts_with(NOT_YET)) { Try::Again(reason) } else { Try::GiveUp(reason) }
}
fn retried(tries: u32, pause: Duration, mut attempt: impl FnMut() -> Try) -> Result<(), String> {
let mut last = String::new();
for n in 0..tries {
match attempt() {
Try::Shown => return Ok(()),
Try::GiveUp(reason) => return Err(reason),
Try::Again(reason) => last = reason,
}
if n + 1 < tries {
std::thread::sleep(pause);
}
}
Err(last)
}
const COLOURS_SETTLE: Duration = Duration::from_millis(250);
fn release(hc: &Hc, address: &str, shown: bool) -> Result<()> {
if shown {
std::thread::sleep(COLOURS_SETTLE);
}
hc.run(&["keys", address, "enter"]).map(drop).with_context(|| {
format!("releasing the review {address}; attach with `hc attach {address}` and press Enter")
})
}
pub(crate) fn show_and_release(
hc: &Hc,
address: &str,
placement: Placement,
direction: SplitDirection,
) -> Result<Option<String>> {
let args = show_args(placement, direction, address);
let shown = retried(SHOW_TRIES, SHOW_PAUSE, || show(Path::new("herdr-app"), &args));
release(hc, address, shown.is_ok())?;
Ok(shown.err())
}
#[cfg(test)]
#[allow(clippy::expect_used, reason = "tests assert by panicking")]
mod tests {
use super::*;
fn strs(list: &[&str]) -> Vec<String> {
list.iter().map(|a| (*a).to_owned()).collect()
}
fn showing(tries: u32, answers: &[Try]) -> (Result<(), String>, usize) {
let mut calls = 0;
let shown = retried(tries, Duration::ZERO, || {
calls += 1;
answers
.get(calls - 1)
.cloned()
.unwrap_or_else(|| Try::Again("no running terminal \"t9\"".to_owned()))
});
(shown, calls)
}
#[test]
fn showing_waits_for_herdr_app_to_know_the_terminal_but_not_past_a_refusal_that_stays() {
let not_yet = Try::Again("no running terminal \"t9\"".to_owned());
assert_eq!(
showing(5, &[not_yet.clone(), not_yet, Try::Shown]),
(Ok(()), 3),
"stops at the first success"
);
assert_eq!(
showing(5, &[]),
(Err("no running terminal \"t9\"".to_owned()), 5),
"a refusal that never passes ends after the last try, and says why"
);
let gone = Try::GiveUp("no Herdr window at /tmp/herdr-app.sock".to_owned());
assert_eq!(showing(5, &[gone]), (Err("no Herdr window at /tmp/herdr-app.sock".to_owned()), 1));
}
#[test]
fn only_an_unknown_terminal_is_asked_about_again_and_every_refusal_keeps_its_reason() {
let not_yet = refusal("{\n \"error\": \"no running terminal \\\"t9\\\"\"\n}\n", "");
assert_eq!(not_yet, Try::Again("no running terminal \"t9\"".to_owned()));
let unknown = refusal(r#"{"error":"unknown command \"terminal.peek\""}"#, "");
assert_eq!(unknown, Try::GiveUp("unknown command \"terminal.peek\"".to_owned()));
let no_window = refusal("", "herdr-app: no Herdr window at /tmp/h.sock: Connection refused\n");
assert_eq!(no_window, Try::GiveUp("no Herdr window at /tmp/h.sock: Connection refused".to_owned()));
assert_eq!(refusal("", ""), Try::GiveUp("herdr-app failed with no output".to_owned()));
}
#[cfg(unix)]
fn fake_app(tag: &str, not_yet: u32, fail: bool) -> std::path::PathBuf {
let dir =
std::env::temp_dir().join(format!("plannotator-tui-herdr-app-{tag}-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("temp dir");
let script = format!(
r#"#!/bin/sh
echo "$*" >> {dir}/log
if [ {fail} = 1 ]; then echo "herdr-app: no Herdr window at {dir}/sock: No such file" >&2; exit 2; fi
if [ "$(wc -l < {dir}/log)" -le {not_yet} ]; then printf '{{\n "error": "no running terminal \\"%s\\""\n}}\n' "$4"; exit 1; fi
echo '{{}}'
"#,
dir = dir.display(),
fail = u8::from(fail),
);
let bin = dir.join("herdr-app");
crate::test_support::write_script(&bin, &script);
bin
}
#[cfg(unix)]
#[test]
fn a_real_herdr_app_answer_is_retried_while_the_window_catches_up_and_not_without_a_window() {
let args = show_args(Placement::Overlay, SplitDirection::Right, "t9");
let catching_up = fake_app("late", 2, false);
assert_eq!(retried(5, Duration::ZERO, || show(&catching_up, &args)), Ok(()));
let calls =
|bin: &Path| std::fs::read_to_string(bin.with_file_name("log")).map(|l| l.lines().count());
assert_eq!(calls(&catching_up).ok(), Some(3));
let late = fake_app("never", 99, false);
assert_eq!(
retried(3, Duration::ZERO, || show(&late, &args)),
Err("no running terminal \"t9\"".to_owned())
);
let windowless = fake_app("windowless", 0, true);
let err = retried(5, Duration::ZERO, || show(&windowless, &args)).expect_err("no window");
assert!(err.starts_with("no Herdr window at "), "{err}");
assert_eq!(calls(&windowless).ok(), Some(1), "a missing window is not asked again");
for bin in [catching_up, late, windowless] {
bin.parent().map(std::fs::remove_dir_all);
}
}
#[test]
fn an_overlay_peeks_and_a_split_takes_a_pane() {
let peek = strs(&["do", "terminal.peek", "--terminal", "t9"]);
assert_eq!(show_args(Placement::Overlay, SplitDirection::Right, "t9"), peek);
assert_eq!(show_args(Placement::Popup, SplitDirection::Down, "t9"), peek);
assert_eq!(
show_args(Placement::Split, SplitDirection::Down, "t9"),
strs(&["do", "terminal.keep", "--terminal", "t9", "--place", "down"])
);
}
}