pub mod client;
pub mod keys;
pub mod state;
pub mod view;
use std::io::IsTerminal;
use std::sync::Arc;
use std::sync::mpsc::{self, Receiver, Sender};
use std::time::Duration;
use ratatui::Terminal;
use ratatui::backend::Backend;
use ratatui::crossterm::event::{self, Event as Input, KeyEventKind};
use time::OffsetDateTime;
use crate::mcp::stdio::ClientConfig;
use client::{Api, HttpApi};
use state::{Effect, Event, State};
use view::Theme;
const TICK: Duration = Duration::from_millis(100);
#[must_use]
pub fn translate(input: &Input) -> Option<Event> {
match input {
Input::Key(key) if key.kind != KeyEventKind::Release => {
keys::action(key.code, key.modifiers).map(Event::Action)
}
Input::Resize(columns, lines) => Some(Event::Resize(*columns, *lines)),
_ => None,
}
}
pub fn run(config: &ClientConfig) -> Result<(), String> {
require_terminal(
std::io::stdin().is_terminal(),
std::io::stdout().is_terminal(),
)?;
let api: Arc<dyn Api> = Arc::new(HttpApi::new(config)?);
let theme = view::theme(
std::env::var("NO_COLOR").ok().as_deref(),
std::env::var("TERM").ok().as_deref(),
);
with_terminal(|terminal| {
let (answers, inbox) = mpsc::channel();
let mut dispatch = |effects: Vec<Effect>| send(&api, effects, &answers);
drive(
terminal,
&client::base(&config.url),
theme,
&mut Keyboard,
&inbox,
&mut dispatch,
&OffsetDateTime::now_utc,
)
.map(|_| ())
})?
}
pub fn with_terminal<T>(
body: impl FnOnce(&mut ratatui::DefaultTerminal) -> T,
) -> Result<T, String> {
let mut terminal = ratatui::try_init().map_err(|e| format!("the terminal: {e}"))?;
let value = body(&mut terminal);
ratatui::restore();
Ok(value)
}
pub fn require_terminal(input: bool, output: bool) -> Result<(), String> {
let what = match (input, output) {
(true, true) => return Ok(()),
(false, true) => "standard input is not one",
(true, false) => "standard output is not one",
(false, false) => "standard input and standard output are not one",
};
Err(format!(
"a terminal is required: {what}; run it in a terminal, not through a pipe, a redirect or a job without one"
))
}
pub trait Inputs {
fn next(&mut self, wait: Duration) -> std::io::Result<Option<Input>>;
}
struct Keyboard;
impl Inputs for Keyboard {
fn next(&mut self, wait: Duration) -> std::io::Result<Option<Input>> {
if event::poll(wait)? {
event::read().map(Some)
} else {
Ok(None)
}
}
}
pub fn drive<B: Backend>(
terminal: &mut Terminal<B>,
server: &str,
theme: Theme,
inputs: &mut dyn Inputs,
inbox: &Receiver<Event>,
dispatch: &mut dyn FnMut(Vec<Effect>),
now: &dyn Fn() -> OffsetDateTime,
) -> Result<State, String> {
let size = terminal.size().map_err(|e| format!("the terminal: {e}"))?;
let (mut state, first) = State::new(server, (size.width, size.height));
dispatch(first);
while !state.quit {
while let Ok(event) = inbox.try_recv() {
dispatch(state.step(event, now()));
}
terminal
.draw(|frame| view::draw(frame, &state, theme))
.map_err(|e| format!("the terminal: {e}"))?;
let input = inputs
.next(TICK)
.map_err(|e| format!("the terminal: {e}"))?;
if let Some(event) = input.as_ref().and_then(translate) {
dispatch(state.step(event, now()));
}
}
Ok(state)
}
fn send(api: &Arc<dyn Api>, effects: Vec<Effect>, answers: &Sender<Event>) {
for effect in effects {
let api = Arc::clone(api);
let answers = answers.clone();
std::thread::spawn(move || {
drop(answers.send(client::perform(api.as_ref(), &effect)));
});
}
}
#[cfg(test)]
mod tests {
use ratatui::crossterm::event::{
Event as Input, KeyCode, KeyEvent, KeyEventKind, KeyModifiers,
};
use std::collections::VecDeque;
use std::sync::mpsc;
use std::time::Duration;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use time::OffsetDateTime;
use time::macros::datetime;
use super::client::{self, Api};
use super::state::{Action, ApiError, Effect, Event, KeyInfo, Page, Row, State, View};
use super::view::Theme;
use super::{Inputs, drive, translate};
const NOW: OffsetDateTime = datetime!(2026-11-10 01:20:00 UTC);
fn row(n: u8) -> Row {
Row {
uuid: format!("01a07da8-c2bb-83e5-b9a2-279e0d16bc{n:02}"),
stored_at: "2026-10-25T12:00:00Z".to_owned(),
updated_at: "2026-10-25T12:30:00Z".to_owned(),
version: format!("4f0c4b0a1d2e3f405162738495a6b7{n:02}"),
}
}
struct TwoVcons;
impl Api for TwoVcons {
fn key(&self) -> Result<KeyInfo, ApiError> {
Ok(KeyInfo {
id: "0123456789abcdef".to_owned(),
tenant: Some("t_example".to_owned()),
scopes: vec!["read".to_owned()],
})
}
fn page(&self, _cursor: Option<&str>) -> Result<Page, ApiError> {
Ok(Page {
vcons: vec![row(0), row(1)],
next: None,
})
}
}
struct Script(VecDeque<KeyCode>);
impl Inputs for Script {
fn next(&mut self, _wait: Duration) -> std::io::Result<Option<Input>> {
let code = self
.0
.pop_front()
.ok_or_else(|| std::io::Error::other("the script ended"))?;
Ok(Some(key(code, KeyEventKind::Press)))
}
}
fn driven(keys: &[KeyCode]) -> (Result<State, String>, String) {
let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("a terminal");
let (answers, inbox) = mpsc::channel();
let mut dispatch = |effects: Vec<Effect>| {
for effect in effects {
answers
.send(client::perform(&TwoVcons, &effect))
.expect("the loop holds the receiver");
}
};
let result = drive(
&mut terminal,
"https://vcon.example.com:8443",
Theme { color: false },
&mut Script(keys.iter().copied().collect()),
&inbox,
&mut dispatch,
&|| NOW,
);
let buffer = terminal.backend().buffer();
let frame = buffer
.content()
.iter()
.map(ratatui::buffer::Cell::symbol)
.collect();
(result, frame)
}
#[test]
fn the_loop_applies_answers_and_keys_in_order_and_ends_on_quit() {
let (result, frame) = driven(&[KeyCode::Down, KeyCode::Enter, KeyCode::Char('q')]);
let state = result.expect("the loop ends on q");
assert_eq!(state.view, View::Detail, "Enter opened the detail view");
assert_eq!(
state.detail,
Some(row(1).uuid),
"both answers were applied before Down, so Down moved to the second vCon"
);
assert!(
frame.contains("Content is not shown"),
"the last frame drawn is the detail view: {frame}"
);
}
fn key(code: KeyCode, kind: KeyEventKind) -> Input {
Input::Key(KeyEvent::new_with_kind(code, KeyModifiers::NONE, kind))
}
#[test]
fn the_loop_stops_with_the_terminals_error_when_input_fails() {
let (result, _) = driven(&[KeyCode::Down]);
assert_eq!(
result,
Err("the terminal: the script ended".to_owned()),
"the error is returned, with what it is about"
);
}
#[test]
fn the_loop_stamps_answers_with_the_clock_it_is_given() {
let (result, _) = driven(&[KeyCode::Char('q')]);
assert_eq!(
result.expect("the loop ends on q").as_of,
Some(NOW),
"the list was read at the time the clock said"
);
}
#[test]
fn a_bound_key_going_down_is_its_action() {
assert_eq!(
translate(&key(KeyCode::Char('q'), KeyEventKind::Press)),
Some(Event::Action(Action::Quit)),
"q quits"
);
}
#[test]
fn a_held_key_repeats_its_action() {
assert_eq!(
translate(&key(KeyCode::Down, KeyEventKind::Repeat)),
Some(Event::Action(Action::Down)),
"holding Down keeps moving"
);
}
#[test]
fn a_key_coming_up_is_nothing() {
assert_eq!(
translate(&key(KeyCode::Char('q'), KeyEventKind::Release)),
None,
"a terminal that reports releases must not act twice"
);
}
#[test]
fn an_unbound_key_is_nothing() {
assert_eq!(
translate(&key(KeyCode::Char('x'), KeyEventKind::Press)),
None,
"x is not bound"
);
}
#[test]
fn a_resize_carries_the_new_size() {
assert_eq!(
translate(&Input::Resize(132, 43)),
Some(Event::Resize(132, 43)),
"columns, then lines"
);
}
#[test]
fn focus_and_paste_are_nothing() {
assert_eq!(
translate(&Input::FocusGained),
None,
"focus changes nothing"
);
assert_eq!(
translate(&Input::Paste("q".to_owned())),
None,
"pasted text is not typed keys"
);
}
}