refactor: make this repo into a library

This commit is contained in:
JasterV 2025-08-19 19:03:52 +02:00
parent 1b792e6ad0
commit f27020e7c8
13 changed files with 33 additions and 113 deletions

22
Cargo.lock generated
View file

@ -123,12 +123,12 @@ dependencies = [
[[package]]
name = "counter"
version = "0.1.0"
version = "0.0.0"
dependencies = [
"color-eyre",
"crossterm 0.29.0",
"framework",
"ratatui",
"ratatui-elm",
]
[[package]]
@ -283,15 +283,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
[[package]]
name = "framework"
version = "0.1.0"
dependencies = [
"color-eyre",
"crossterm 0.29.0",
"ratatui",
]
[[package]]
name = "gimli"
version = "0.31.1"
@ -538,6 +529,15 @@ dependencies = [
"unicode-width 0.2.0",
]
[[package]]
name = "ratatui-elm"
version = "0.1.0"
dependencies = [
"color-eyre",
"crossterm 0.29.0",
"ratatui",
]
[[package]]
name = "redox_syscall"
version = "0.5.17"

View file

@ -1,11 +1,8 @@
[workspace]
resolver = "3"
members = ["src/*"]
members = ["ratatui-elm", "examples/*"]
[workspace.package]
name = "ratatui-playground"
version = "0.1.0"
description = "A playground workspace for ratatui"
authors = ["JasterV <49537445+JasterV@users.noreply.github.com>"]
license = "MIT"
edition = "2024"
@ -13,4 +10,4 @@ edition = "2024"
[workspace.dependencies]
color-eyre = "0.6.3"
crossterm = "0.29.0"
ratatui = "0.29.0"
ratatui = { version = "0.29.0", features = ["unstable-widget-ref"] }

View file

@ -1,8 +1,8 @@
# Ratatui playground
# Ratatui Elm
This is a playground repo to play around with [Ratatui](https://ratatui.rs/).
This is an experimental project that aims to build an Elm-like framework for TUI development in Rust.
One of my main goals is to understand if I can structure my Ratatui application in an Elm-like fashion.
My main motivation to build this was to understand if I could structure a Ratatui application in an Elm-like fashion.
The provided templates from the Ratatui official sources write code in a very procedural way, abusing of mutability a bit too much for my taste. (That is, using `&mut` all over the place in function arguments).

View file

@ -1,7 +1,7 @@
[package]
name = "counter"
publish = false
version.workspace = true
version = "0.0.0"
edition.workspace = true
authors.workspace = true
@ -9,4 +9,4 @@ authors.workspace = true
color-eyre.workspace = true
crossterm.workspace = true
ratatui.workspace = true
framework = { path = "../framework" }
ratatui-elm = { path = "../../ratatui-elm" }

View file

@ -1,15 +1,15 @@
use color_eyre::Result;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use framework::{Update, View};
use ratatui::{
style::Stylize,
text::Line,
widgets::{Block, Paragraph},
};
use ratatui_elm::{Update, View};
fn main() -> Result<()> {
color_eyre::install()?;
let result = framework::start(Model::default(), update, view, run_effects);
let result = ratatui_elm::start(Model::default(), update, view, run_effects);
result
}
@ -92,7 +92,7 @@ impl From<crossterm::event::Event> for Message {
/// Elm-like update function.
///
/// Given the current state (model) and an incoming message from the outside world,
/// return a new state and a side effect.
/// return the next updated state
pub fn update(model: Model, msg: Message) -> Result<Update<Model, Effect>> {
match msg {
Message::Exit => Ok(Update::Exit),

12
ratatui-elm/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "ratatui-elm"
version = "0.1.0"
description = "An elm-like abstraction over Ratatui"
license.workspace = true
authors.workspace = true
edition.workspace = true
[dependencies]
color-eyre.workspace = true
crossterm.workspace = true
ratatui.workspace = true

View file

@ -1,78 +0,0 @@
//! Elm-like model module.
//!
//! Defines the state of the application, the messages that can be received
//! from the outside world and the effects that can be produced.
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
#[derive(Debug, Clone, Default)]
pub struct Model {
pub counter: u64,
}
impl Model {
pub fn increment_counter(model: Model) -> Model {
Model {
counter: model.counter + 1,
}
}
pub fn decrement_counter(model: Model) -> Model {
let counter = if model.counter == 0 {
0
} else {
model.counter - 1
};
Model { counter }
}
}
pub enum Effect {}
pub enum Message {
IncCounter,
DecCounter,
Exit,
NoOp,
}
impl From<crossterm::event::Event> for Message {
fn from(value: Event) -> Self {
match value {
Event::Key(KeyEvent {
code: KeyCode::Esc | KeyCode::Char('q'),
kind: KeyEventKind::Press,
state: _,
modifiers: _,
}) => Self::Exit,
Event::Key(KeyEvent {
code: KeyCode::Right,
kind: KeyEventKind::Press,
state: _,
modifiers: _,
}) => Self::IncCounter,
Event::Key(KeyEvent {
code: KeyCode::Left,
kind: KeyEventKind::Press,
state: _,
modifiers: _,
}) => Self::DecCounter,
Event::Key(KeyEvent {
code: KeyCode::Char('c') | KeyCode::Char('C'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
state: _,
}) => Self::Exit,
Event::FocusGained
| Event::FocusLost
| Event::Key(_)
| Event::Mouse(_)
| Event::Paste(_)
| Event::Resize(_, _) => Self::NoOp,
}
}
}

View file

@ -1,11 +0,0 @@
[package]
name = "framework"
publish = false
version.workspace = true
edition.workspace = true
authors.workspace = true
[dependencies]
color-eyre.workspace = true
crossterm.workspace = true
ratatui = { workspace = true, features = ["unstable-widget-ref"] }