pub fn note_command(text: &str) -> Option<String> {
let trimmed = text.trim();
let (first, rest) = trimmed.split_once(char::is_whitespace)?;
let word = first.trim_end_matches(':');
if !(word.eq_ignore_ascii_case("note") || word.eq_ignore_ascii_case("notes")) {
return None;
}
let body = rest.trim();
(!body.is_empty()).then(|| body.to_string())
}
pub fn is_queues_command(text: &str) -> bool {
text.trim().eq_ignore_ascii_case("queues")
}
pub async fn capture(body: &str) -> String {
let exe = crate::exe::self_exe();
let out = tokio::process::Command::new(exe)
.args(["kg", "note", body])
.stdin(std::process::Stdio::null())
.output()
.await;
match out {
Ok(out) if out.status.success() => String::from_utf8_lossy(&out.stdout)
.lines()
.next()
.unwrap_or("noted")
.to_string(),
Ok(out) => {
let err = String::from_utf8_lossy(&out.stderr);
format!(
"the note did not land: {}",
err.trim()
.lines()
.next_back()
.unwrap_or("mecha kg note failed")
)
}
Err(e) => format!("the note could not run: {e}"),
}
}
pub async fn queues_report() -> String {
let exe = crate::exe::self_exe();
let out = tokio::process::Command::new(exe)
.args(["review", "queues"])
.stdin(std::process::Stdio::null())
.output()
.await;
match out {
Ok(out) if out.status.success() => {
format!("```{}```", String::from_utf8_lossy(&out.stdout).trim_end())
}
Ok(out) => {
let err = String::from_utf8_lossy(&out.stderr);
format!(
"the queues could not be read: {}",
err.trim()
.lines()
.next_back()
.unwrap_or("mecha review failed")
)
}
Err(e) => format!("the queues could not be read: {e}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_note_word_captures_and_everything_else_falls_through() {
assert_eq!(
note_command("note buy milk").as_deref(),
Some("buy milk"),
"the plain capture"
);
assert_eq!(
note_command(" Note: met Sarah about the fMRI slot ").as_deref(),
Some("met Sarah about the fMRI slot"),
"cased, colon, padded"
);
assert_eq!(
note_command("notes from the lab meeting were great").as_deref(),
Some("from the lab meeting were great"),
"the notes spelling captures too"
);
assert_eq!(note_command("note"), None, "the bare word is a prompt");
assert_eq!(note_command("notes?"), None, "a question is a prompt");
assert_eq!(
note_command("noted everything down"),
None,
"a word that merely starts with it is a prompt"
);
assert_eq!(
note_command("can you note this down"),
None,
"the word mid-sentence is a prompt"
);
}
#[test]
fn the_queues_word_is_exact() {
assert!(is_queues_command("queues"));
assert!(is_queues_command(" Queues "));
assert!(!is_queues_command("queues?"));
assert!(!is_queues_command("show me the queues"));
}
}