From ea38b0f9b062fc7474642569297732b321e199a4 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Sun, 26 Nov 2023 22:09:03 +0100 Subject: [PATCH] add hello world example --- Cargo.toml | 3 +- examples/tty-01-hello/Cargo.toml | 18 ++++++ examples/tty-01-hello/src/main.rs | 101 ++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 examples/tty-01-hello/Cargo.toml create mode 100644 examples/tty-01-hello/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 26d3208..707f44e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "lib-nested-core", - "lib-nested-tty" + "lib-nested-tty", + "examples/tty-01-hello" ] diff --git a/examples/tty-01-hello/Cargo.toml b/examples/tty-01-hello/Cargo.toml new file mode 100644 index 0000000..4582fd7 --- /dev/null +++ b/examples/tty-01-hello/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "tty-01-hello" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +r3vi = { path = "../../../lib-r3vi" } +nested = { path = "../../lib-nested-core" } +nested-tty = { path = "../../lib-nested-tty" } +termion = "*" +cgmath = "*" + +[dependencies.async-std] +version = "1.9.0" +features = ["unstable", "attributes"] + diff --git a/examples/tty-01-hello/src/main.rs b/examples/tty-01-hello/src/main.rs new file mode 100644 index 0000000..3dedd2b --- /dev/null +++ b/examples/tty-01-hello/src/main.rs @@ -0,0 +1,101 @@ +extern crate r3vi; +extern crate nested; +extern crate nested_tty; +extern crate termion; +extern crate cgmath; + +use { + r3vi::view::{ + ViewPort, + port::UpdateTask + }, + nested::{ + tree::{NestedNode}, + type_system::{Context, ReprTree} + }, + nested_tty::{ + Terminal, TerminalView, TerminalEvent, + TerminalStyle, + TerminalCompositor + }, + cgmath::Vector2, + termion::event::{Event, Key}, + std::sync::{Arc, RwLock} +}; + + +/* this task handles all terminal events (e.g. key press, resize) + */ +pub async fn event_loop( + mut term: Terminal, + term_port: ViewPort, + portmutex: Arc> +) { + loop { + let ev = term.next_event().await; + let _l = portmutex.write().unwrap(); + + if ev == TerminalEvent::Input(Event::Key(Key::Ctrl('d'))) { + break; + } + term_port.update(); + } +} + +/* this task will continuously pull forward + * all notifications which are influencing + * the view in `term_port` + */ +pub async fn update_loop( + term_port: ViewPort, + portmutex: Arc> +) { + loop { + { + let _l = portmutex.write().unwrap(); + term_port.update(); + } + async_std::task::sleep(std::time::Duration::from_millis(500)).await; + } +} + +#[async_std::main] +async fn main() { + /* initialize our terminal + */ + let term_port = ViewPort::new(); + + let mut term = Terminal::new(term_port.outer()); + let term_writer = term.get_writer(); + + let portmutex = Arc::new(RwLock::new(())); + + /* spawn event-handling & updating tasks + */ + async_std::task::spawn( + update_loop(term_port.clone(), portmutex.clone())); + + async_std::task::spawn( + event_loop(term, term_port.clone(), portmutex.clone())); + + /* populate the view in `term_port` + */ + let compositor = TerminalCompositor::new(term_port.inner()); + + compositor.write().unwrap().push( + nested_tty::make_label("test") + .offset(Vector2::new(7,2))); + + compositor.write().unwrap().push( + nested_tty::make_label("Hello World") + .map_item(|p,a| + a.add_style_back( + TerminalStyle::fg_color(( (25*p.x%255) as u8, 200, 0 )) )) + .offset(Vector2::new(5, 3))); + + /* write the changes in the view of `term_port` to the terminal + */ + term_writer.show().await.expect("output error!"); +} + +