use envision::prelude::*;
struct ChatViewApp;
#[derive(Clone)]
struct State {
chat: ChatViewState,
}
#[derive(Clone, Debug)]
enum Msg {
Chat(ChatViewMessage),
Quit,
}
impl App for ChatViewApp {
type State = State;
type Message = Msg;
fn init() -> (State, Command<Msg>) {
let mut chat = ChatViewState::new()
.with_placeholder("Type a message...")
.with_input_height(3);
chat.set_focused(true);
chat.push_system("Welcome to the chat!");
chat.push_assistant("Hello! How can I help you today?");
let state = State { chat };
(state, Command::none())
}
fn update(state: &mut State, msg: Msg) -> Command<Msg> {
match msg {
Msg::Chat(m) => {
if let Some(ChatViewOutput::Submitted(text)) = ChatView::update(&mut state.chat, m)
{
state.chat.push_user(&text);
let reply = format!("You said: \"{}\"", text);
state.chat.push_assistant(&reply);
}
}
Msg::Quit => return Command::quit(),
}
Command::none()
}
fn view(state: &State, frame: &mut Frame) {
let theme = Theme::default();
let area = frame.area();
let chunks = Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).split(area);
ChatView::view(
&state.chat,
frame,
chunks[0],
&theme,
&ViewContext::default(),
);
let status = format!(
" Messages: {} | Ctrl+Enter: send, Tab: toggle focus, Esc: quit",
state.chat.message_count()
);
frame.render_widget(
ratatui::widgets::Paragraph::new(status).style(Style::default().fg(Color::DarkGray)),
chunks[1],
);
}
fn handle_event_with_state(state: &State, event: &Event) -> Option<Msg> {
if let Some(key) = event.as_key() {
if key.code == KeyCode::Esc {
return Some(Msg::Quit);
}
}
state.chat.handle_event(event).map(Msg::Chat)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut vt = Runtime::<ChatViewApp, _>::virtual_terminal(60, 20)?;
println!("=== ChatView Example ===\n");
vt.tick()?;
println!("Initial state (system + assistant messages):");
println!("{}\n", vt.display());
vt.dispatch(Msg::Chat(ChatViewMessage::Input('H')));
vt.dispatch(Msg::Chat(ChatViewMessage::Input('e')));
vt.dispatch(Msg::Chat(ChatViewMessage::Input('l')));
vt.dispatch(Msg::Chat(ChatViewMessage::Input('l')));
vt.dispatch(Msg::Chat(ChatViewMessage::Input('o')));
vt.tick()?;
println!("After typing 'Hello':");
println!("{}\n", vt.display());
vt.dispatch(Msg::Chat(ChatViewMessage::Submit));
vt.tick()?;
println!("After submitting the message:");
println!("{}\n", vt.display());
vt.dispatch(Msg::Chat(ChatViewMessage::ToggleFocus));
vt.tick()?;
println!("After toggling to history view:");
println!("{}\n", vt.display());
Ok(())
}