Skip to content

Commit 724b1cb

Browse files
committed
adds mvp for new ui layer
1 parent b49df73 commit 724b1cb

File tree

13 files changed

+452
-15
lines changed

13 files changed

+452
-15
lines changed

Cargo.lock

Lines changed: 82 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/chat-cli-ui/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ serde_json.workspace = true
2020
chrono.workspace = true
2121
crossterm.workspace = true
2222
tokio.workspace = true
23+
eyre.workspace = true
24+
tokio-util.workspace = true
25+
futures.workspace = true
26+
ratatui = "0.29.0"
2327

2428
[target.'cfg(unix)'.dependencies]
2529
nix.workspace = true

crates/chat-cli-ui/src/conduit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
use std::{io::Write as _, marker::PhantomData};
1+
use std::io::Write as _;
2+
use std::marker::PhantomData;
23

3-
use crate::protocol::{Event, LegacyPassThroughOutput};
4+
use crate::protocol::{
5+
Event,
6+
LegacyPassThroughOutput,
7+
};
48

59
#[derive(thiserror::Error, Debug)]
610
pub enum ConduitError {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

crates/chat-cli-ui/src/protocol.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! This is largely based on https://docs.ag-ui.com/concepts/events
22
//! They do not have a rust SDK so for now we are handrolling these types
33
4-
use serde::{Deserialize, Serialize};
4+
use serde::{
5+
Deserialize,
6+
Serialize,
7+
};
58
use serde_json::Value;
69

710
/// Role of a message sender
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![allow(dead_code)]
2+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3+
pub enum Action {
4+
Quit,
5+
Tick,
6+
Noop,
7+
}

crates/chat-cli-ui/src/ui/component.rs

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use eyre::Result;
2+
use tokio::sync::mpsc::unbounded_channel;
3+
use tracing::error;
4+
5+
use crate::ui::action::Action;
6+
use crate::ui::tui::Tui;
7+
8+
pub struct App {
9+
pub should_quit: bool,
10+
}
11+
12+
impl App {
13+
pub async fn run(&mut self) -> Result<()> {
14+
let (_render_tx, mut render_rx) = unbounded_channel::<()>();
15+
let (action_tx, mut _action_rx) = unbounded_channel::<Action>();
16+
17+
let mut tui = Tui::new(4.0, 60.0)?;
18+
// TODO: make a defer routine that restores the terminal on exit
19+
tui.enter()?;
20+
21+
let mut event_receiver = tui.event_rx.take().expect("Missing event receiver");
22+
23+
// Render Task
24+
tokio::spawn(async move {
25+
while render_rx.recv().await.is_some() {
26+
// TODO: render here
27+
tui.terminal.draw(|_f| {})?;
28+
}
29+
30+
Ok::<(), Box<dyn std::error::Error + Send + Sync + 'static>>(())
31+
});
32+
33+
// Event monitoring task
34+
tokio::spawn(async move {
35+
while let Some(_event) = event_receiver.recv().await {
36+
// TODO: derive action from the main component
37+
let action = Action::Tick;
38+
if let Err(e) = action_tx.send(action) {
39+
error!("Error sending action: {:?}", e);
40+
}
41+
}
42+
});
43+
44+
Ok(())
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![allow(dead_code)]
2+
use crossterm::event::{
3+
Event,
4+
KeyEvent,
5+
MouseEvent,
6+
};
7+
use eyre::Result;
8+
use ratatui::Frame;
9+
use ratatui::layout::Rect;
10+
use tokio::sync::mpsc::UnboundedSender;
11+
12+
use super::action::Action;
13+
14+
mod app;
15+
16+
pub trait Component {
17+
#[allow(unused_variables)]
18+
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
19+
Ok(())
20+
}
21+
fn init(&mut self) -> Result<()> {
22+
Ok(())
23+
}
24+
fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Action>> {
25+
let r = match event {
26+
Some(Event::Key(key_event)) => self.handle_key_events(key_event)?,
27+
Some(Event::Mouse(mouse_event)) => self.handle_mouse_events(mouse_event)?,
28+
_ => None,
29+
};
30+
Ok(r)
31+
}
32+
#[allow(unused_variables)]
33+
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>> {
34+
Ok(None)
35+
}
36+
#[allow(unused_variables)]
37+
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result<Option<Action>> {
38+
Ok(None)
39+
}
40+
#[allow(unused_variables)]
41+
fn update(&mut self, action: Action) -> Result<Option<Action>> {
42+
Ok(None)
43+
}
44+
fn draw(&mut self, f: &mut Frame<'_>, rect: Rect) -> Result<()>;
45+
}

crates/chat-cli-ui/src/ui/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod action;
2+
mod components;
3+
mod tui;

0 commit comments

Comments
 (0)