From f151f9c5d228223a701f98dcaf2f87cbace24c50 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 28 Nov 2023 17:17:35 +0100 Subject: [PATCH] add second example with TTYApplication & Char-View --- Cargo.toml | 3 +- examples/tty-02-node/Cargo.toml | 18 +++++++ examples/tty-02-node/src/main.rs | 89 ++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 examples/tty-02-node/Cargo.toml create mode 100644 examples/tty-02-node/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 707f44e..ce6bd01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "lib-nested-core", "lib-nested-tty", - "examples/tty-01-hello" + "examples/tty-01-hello", + "examples/tty-02-node" ] diff --git a/examples/tty-02-node/Cargo.toml b/examples/tty-02-node/Cargo.toml new file mode 100644 index 0000000..589ee96 --- /dev/null +++ b/examples/tty-02-node/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "tty-02-node" +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-02-node/src/main.rs b/examples/tty-02-node/src/main.rs new file mode 100644 index 0000000..3d80c77 --- /dev/null +++ b/examples/tty-02-node/src/main.rs @@ -0,0 +1,89 @@ +extern crate cgmath; +extern crate nested; +extern crate nested_tty; +extern crate r3vi; +extern crate termion; + +use { + cgmath::Vector2, + nested::{ + editTree::NestedNode, + reprTree::{Context, ReprTree}, + }, + nested_tty::{ + terminal::TermOutWriter, DisplaySegment, Terminal, TerminalAtom, TerminalCompositor, + TerminalEvent, TerminalStyle, TerminalView, + TTYApplication + }, + r3vi::{ + buffer::singleton::*, + view::{port::UpdateTask, singleton::*, ViewPort}, + }, + std::sync::{Arc, Mutex, RwLock}, + termion::event::{Event, Key}, +}; + +#[async_std::main] +async fn main() { + let app = TTYApplication::new( |ev| { /* event handler */ } ); + let compositor = TerminalCompositor::new(app.port.inner()); + + /* setup context & create Editor-Tree + */ + let ctx = Arc::new(RwLock::new(Context::default())); + + // abstract data + let rt = ReprTree::from_char(&ctx, 'λ'); + + let mut node = Context::make_node( + &ctx, + // node type + Context::parse(&ctx, "Char"), + // depth + SingletonBuffer::new(0).get_port(), + ) + .unwrap(); + + /* add a display view to the node + */ + let char_view = rt + .read() + .unwrap() + .get_port::>() + .expect("unable to get Char-view") + .map(move |c| TerminalAtom::from(if c == '\0' { ' ' } else { c })) + .to_grid(); + + let mut display_rt = ReprTree::new(Context::parse(&ctx, "Display")); + + display_rt.insert_branch(ReprTree::new_leaf( + Context::parse(&ctx, "TerminalView"), + char_view.into(), + )); + + node = node.set_view(Arc::new(RwLock::new(display_rt))); + + 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, 0)), + ); + + compositor.write().unwrap() + .push(nested_tty::make_label("Char").offset(Vector2::new(0, 2))); + + compositor.write().unwrap() + .push(node.display_view().offset(Vector2::new(15, 2))); + + compositor.write().unwrap() + .push(nested_tty::make_label("").offset(Vector2::new(0, 3))); + + compositor.write().unwrap() + .push(nested_tty::make_label("---").offset(Vector2::new(15, 3))); + + /* write the changes in the view of `term_port` to the terminal + */ + app.show().await.expect("output error!"); +}