#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Snapshot {
pub focus: String,
pub line: Option<(usize, String)>,
pub diagnostic: Option<String>,
pub item: Option<String>,
pub status: String,
pub event: Option<(u64, String)>,
}
pub const MAX_LINE: usize = 160;
fn clip(s: &str) -> String {
let s = s.trim();
if s.chars().count() <= MAX_LINE {
return s.to_string();
}
let mut out: String = s.chars().take(MAX_LINE).collect();
out.push('…');
out
}
pub fn announce(prev: &Snapshot, cur: &Snapshot) -> Option<String> {
let mut parts: Vec<String> = Vec::new();
let focus_changed = prev.focus != cur.focus;
if focus_changed && !cur.focus.is_empty() {
parts.push(cur.focus.clone());
}
if cur.event != prev.event
&& let Some((_, event)) = &cur.event
{
parts.push(clip(event));
}
let line_moved = cur.line.as_ref().map(|(n, _)| n) != prev.line.as_ref().map(|(n, _)| n);
if let Some(item) = cur
.item
.as_ref()
.filter(|_| cur.item != prev.item || focus_changed)
{
parts.push(clip(item));
} else if let Some((n, text)) = &cur.line
&& (focus_changed || line_moved || (prev.item.is_some() && cur.item.is_none()))
{
let text = clip(text);
let line = crate::i18n::tr("Line");
parts.push(if text.is_empty() {
format!("{line} {n}, {}", crate::i18n::tr("blank"))
} else {
format!("{line} {n}: {text}")
});
}
if cur.diagnostic != prev.diagnostic
&& let Some(d) = &cur.diagnostic
{
parts.push(clip(d));
}
if cur.status != prev.status && !cur.status.is_empty() {
parts.push(clip(&cur.status));
}
(!parts.is_empty()).then(|| parts.join(". "))
}
#[derive(Default)]
pub struct Announcer {
last: Snapshot,
pub line: String,
child: Option<std::process::Child>,
}
impl Announcer {
pub fn update(&mut self, cur: Snapshot, speak: Option<&str>) -> bool {
let said = announce(&self.last, &cur);
self.last = cur;
let Some(line) = said else {
return false;
};
if let Some(cmd) = speak {
self.speak(cmd, &line);
}
self.line = line;
true
}
fn speak(&mut self, cmd: &str, line: &str) {
if let Some(mut old) = self.child.take() {
let _ = old.kill();
let _ = old.wait();
}
let mut words = cmd.split_whitespace();
let Some(program) = words.next() else {
return;
};
let line = if line.starts_with('-') {
format!(" {line}")
} else {
line.to_string()
};
self.child = std::process::Command::new(program)
.args(words)
.arg(line)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.ok();
}
pub fn reset(&mut self) {
self.last = Snapshot::default();
self.line.clear();
}
}
impl Drop for Announcer {
fn drop(&mut self) {
if let Some(mut c) = self.child.take() {
let _ = c.kill();
let _ = c.wait();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn editor(line: usize, text: &str) -> Snapshot {
Snapshot {
focus: "Editor main.rs".into(),
line: Some((line, text.into())),
..Default::default()
}
}
#[test]
fn moving_to_another_line_reads_it_and_staying_says_nothing() {
let a = editor(3, "fn main() {");
assert_eq!(
announce(&Snapshot::default(), &a).as_deref(),
Some("Editor main.rs. Line 3: fn main() {")
);
let b = editor(4, " ");
assert_eq!(announce(&a, &b).as_deref(), Some("Line 4, blank"));
let mut typed = b.clone();
typed.line = Some((4, " let".into()));
assert_eq!(
announce(&b, &typed),
None,
"typing on a line is not re-read"
);
}
#[test]
fn a_diagnostic_and_a_status_are_added_when_they_appear() {
let a = editor(3, "x");
let mut b = a.clone();
b.diagnostic = Some("error: cannot find value `x`".into());
b.status = "Saved main.rs".into();
assert_eq!(
announce(&a, &b).as_deref(),
Some("error: cannot find value `x`. Saved main.rs")
);
}
#[test]
fn a_highlighted_item_is_read_instead_of_the_line() {
let a = editor(3, "pri");
let mut b = a.clone();
b.item = Some("println!".into());
assert_eq!(announce(&a, &b).as_deref(), Some("println!"));
let mut back = b.clone();
back.item = None;
assert_eq!(
announce(&b, &back).as_deref(),
Some("Line 3: pri"),
"closing a popup re-reads the line"
);
let mut c = b.clone();
c.focus = "Command Palette".into();
c.item = Some("File: Save".into());
assert_eq!(
announce(&b, &c).as_deref(),
Some("Command Palette. File: Save")
);
}
#[test]
fn long_lines_are_clipped() {
let long = "x".repeat(500);
let said = announce(&Snapshot::default(), &editor(1, &long)).unwrap();
assert!(said.chars().count() < 200);
assert!(said.ends_with('…'));
}
#[test]
fn the_announcer_keeps_the_last_line_until_something_changes() {
let mut a = Announcer::default();
assert!(a.update(editor(1, "a"), None));
assert_eq!(a.line, "Editor main.rs. Line 1: a");
assert!(!a.update(editor(1, "ab"), None));
assert_eq!(a.line, "Editor main.rs. Line 1: a");
}
#[test]
fn an_event_is_read_without_a_change_of_focus() {
let prev = editor(3, "total += n");
let cur = Snapshot {
event: Some((0, "Paused on breakpoint, app.py line 3: total += n".into())),
..prev.clone()
};
assert_eq!(
announce(&prev, &cur).as_deref(),
Some("Paused on breakpoint, app.py line 3: total += n")
);
}
#[test]
fn a_repeated_event_is_read_but_a_seen_one_is_not() {
let first = Snapshot {
event: Some((0, "Paused on step, app.py line 3".into())),
..editor(3, "total += n")
};
let again = Snapshot {
event: Some((1, "Paused on step, app.py line 3".into())),
..first.clone()
};
assert_eq!(
announce(&first, &again).as_deref(),
Some("Paused on step, app.py line 3")
);
assert_eq!(announce(&again, &again), None);
}
}