use std::collections::VecDeque;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU8, AtomicU64, Ordering};
use std::time::{Duration, Instant};
const STATE_HIDDEN: u8 = 0;
const STATE_BAR: u8 = 1;
const STATE_WINDOW: u8 = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplVisibility {
Hidden,
Bar,
Window,
}
impl ReplVisibility {
fn to_u8(self) -> u8 {
match self {
Self::Hidden => STATE_HIDDEN,
Self::Bar => STATE_BAR,
Self::Window => STATE_WINDOW,
}
}
fn from_u8(v: u8) -> Self {
match v {
STATE_BAR => Self::Bar,
STATE_WINDOW => Self::Window,
_ => Self::Hidden,
}
}
}
static STATE: AtomicU8 = AtomicU8::new(STATE_HIDDEN);
static LAST_TOGGLE_NS: AtomicU64 = AtomicU64::new(0);
const TOGGLE_DEBOUNCE: Duration = Duration::from_millis(250);
fn now_nanos() -> u64 {
use std::sync::OnceLock;
static ORIGIN: OnceLock<Instant> = OnceLock::new();
let origin = *ORIGIN.get_or_init(Instant::now);
origin.elapsed().as_nanos() as u64
}
fn debounce_ok() -> bool {
let now = now_nanos();
let last = LAST_TOGGLE_NS.load(Ordering::Acquire);
if last != 0 && now.saturating_sub(last) < TOGGLE_DEBOUNCE.as_nanos() as u64 {
return false;
}
LAST_TOGGLE_NS.store(now, Ordering::Release);
true
}
pub fn current() -> ReplVisibility {
ReplVisibility::from_u8(STATE.load(Ordering::Acquire))
}
pub fn toggle_bar() -> ReplVisibility {
if !debounce_ok() {
return current();
}
let next = match current() {
ReplVisibility::Hidden => ReplVisibility::Bar,
ReplVisibility::Bar | ReplVisibility::Window => ReplVisibility::Hidden,
};
STATE.store(next.to_u8(), Ordering::Release);
next
}
pub fn toggle_window() -> ReplVisibility {
if !debounce_ok() {
return current();
}
let next = match current() {
ReplVisibility::Hidden | ReplVisibility::Bar => ReplVisibility::Window,
ReplVisibility::Window => ReplVisibility::Hidden,
};
STATE.store(next.to_u8(), Ordering::Release);
next
}
const TRANSCRIPT_CAPACITY: usize = 200;
static TRANSCRIPT: Mutex<VecDeque<String>> = Mutex::new(VecDeque::new());
pub fn push_transcript(command: &str, response: &str) {
let Ok(mut t) = TRANSCRIPT.lock() else {
return;
};
t.push_back(format!("> {command}"));
for line in response.split('\n') {
t.push_back(line.to_string());
}
while t.len() > TRANSCRIPT_CAPACITY {
t.pop_front();
}
}
pub fn push_transcript_line(line: &str) {
let Ok(mut t) = TRANSCRIPT.lock() else {
return;
};
t.push_back(line.to_string());
while t.len() > TRANSCRIPT_CAPACITY {
t.pop_front();
}
}
pub fn transcript_len() -> usize {
TRANSCRIPT.lock().map(|t| t.len()).unwrap_or(0)
}
pub fn transcript_tail(n: usize) -> Vec<String> {
let Ok(t) = TRANSCRIPT.lock() else {
return Vec::new();
};
let start = t.len().saturating_sub(n);
t.iter().skip(start).cloned().collect()
}
#[cfg(test)]
pub fn reset() {
STATE.store(STATE_HIDDEN, Ordering::Release);
LAST_TOGGLE_NS.store(0, Ordering::Release);
if let Ok(mut t) = TRANSCRIPT.lock() {
t.clear();
}
}
#[cfg(test)]
pub fn toggle_bar_undebounced() -> ReplVisibility {
LAST_TOGGLE_NS.store(0, Ordering::Release);
toggle_bar()
}
#[cfg(test)]
pub fn toggle_window_undebounced() -> ReplVisibility {
LAST_TOGGLE_NS.store(0, Ordering::Release);
toggle_window()
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Mutex;
static LOCK: Mutex<()> = Mutex::new(());
#[test]
fn tilde_cycles_hidden_to_bar_and_back() {
let _g = LOCK.lock().unwrap();
reset();
assert_eq!(current(), ReplVisibility::Hidden);
assert_eq!(toggle_bar_undebounced(), ReplVisibility::Bar);
assert_eq!(current(), ReplVisibility::Bar);
assert_eq!(toggle_bar_undebounced(), ReplVisibility::Hidden);
assert_eq!(current(), ReplVisibility::Hidden);
}
#[test]
fn ctrl_tilde_opens_and_closes_window() {
let _g = LOCK.lock().unwrap();
reset();
assert_eq!(toggle_window_undebounced(), ReplVisibility::Window);
assert_eq!(current(), ReplVisibility::Window);
assert_eq!(toggle_window_undebounced(), ReplVisibility::Hidden);
}
#[test]
fn ctrl_tilde_from_bar_promotes_to_window() {
let _g = LOCK.lock().unwrap();
reset();
toggle_bar_undebounced();
assert_eq!(current(), ReplVisibility::Bar);
assert_eq!(toggle_window_undebounced(), ReplVisibility::Window);
}
#[test]
fn tilde_from_window_collapses_to_hidden() {
let _g = LOCK.lock().unwrap();
reset();
toggle_window_undebounced();
assert_eq!(current(), ReplVisibility::Window);
assert_eq!(toggle_bar_undebounced(), ReplVisibility::Hidden);
}
#[test]
fn debounce_swallows_rapid_toggles() {
let _g = LOCK.lock().unwrap();
reset();
assert_eq!(toggle_bar(), ReplVisibility::Bar);
assert_eq!(toggle_bar(), ReplVisibility::Bar);
assert_eq!(toggle_bar(), ReplVisibility::Bar);
}
#[test]
fn transcript_push_and_tail_round_trip() {
let _g = LOCK.lock().unwrap();
reset();
push_transcript("set foo 1", "OK");
push_transcript("controls", "foo=1\nbar=0");
let tail = transcript_tail(10);
assert_eq!(
tail,
vec![
"> set foo 1".to_string(),
"OK".to_string(),
"> controls".to_string(),
"foo=1".to_string(),
"bar=0".to_string(),
]
);
}
#[test]
fn push_transcript_line_appends_raw() {
let _g = LOCK.lock().unwrap();
reset();
push_transcript("cmd", "resp");
push_transcript_line("a b c"); let tail = transcript_tail(10);
assert_eq!(
tail,
vec![
"> cmd".to_string(),
"resp".to_string(),
"a b c".to_string(),
]
);
}
#[test]
fn transcript_tail_caps_at_requested_count() {
let _g = LOCK.lock().unwrap();
reset();
push_transcript("a", "1");
push_transcript("b", "2");
let tail = transcript_tail(2);
assert_eq!(tail, vec!["> b".to_string(), "2".to_string()]);
}
#[test]
fn transcript_ring_drops_oldest_on_overflow() {
let _g = LOCK.lock().unwrap();
reset();
for i in 0..(TRANSCRIPT_CAPACITY + 5) {
push_transcript(&format!("c{i}"), "");
}
let tail = transcript_tail(usize::MAX);
assert_eq!(tail.len(), TRANSCRIPT_CAPACITY);
assert!(
!tail.iter().any(|l| l == "> c0"),
"oldest entry MUST be evicted: {tail:?}"
);
assert!(
tail.iter()
.any(|l| l == &format!("> c{}", TRANSCRIPT_CAPACITY + 4)),
"newest entry MUST be retained: {tail:?}"
);
}
}