add second example with TTYApplication & Char-View

This commit is contained in:
Michael Sippel 2023-11-28 17:17:35 +01:00
parent 57cb1ee3ff
commit f151f9c5d2
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 109 additions and 1 deletions

View file

@ -2,6 +2,7 @@
members = [ members = [
"lib-nested-core", "lib-nested-core",
"lib-nested-tty", "lib-nested-tty",
"examples/tty-01-hello" "examples/tty-01-hello",
"examples/tty-02-node"
] ]

View file

@ -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"]

View file

@ -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::<dyn SingletonView<Item = char>>()
.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("<List Char>").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!");
}