lib-nested/examples/tty-01-hello/src/main.rs

61 lines
1.8 KiB
Rust
Raw Permalink Normal View History

2024-03-21 18:17:04 +01:00
//! This example shows how to:
//! - initialize the TTY backend (`lib-nestetd-tty`),
//! - create a simple 'Hello World' output,
//! - create color gradients on the outputted text
//! utilizing basic projection functionality from `lib-r3vi`,
//! - perform basic layouting & compositing.
extern crate cgmath;
2023-11-26 22:09:03 +01:00
extern crate nested;
extern crate nested_tty;
extern crate r3vi;
2023-11-26 22:09:03 +01:00
extern crate termion;
use {
cgmath::Vector2,
2023-11-28 20:52:25 +01:00
nested::repr_tree::Context,
nested_tty::{Terminal, TerminalCompositor, TTYApplication, TerminalEvent, TerminalStyle, TerminalView},
r3vi::view::{port::UpdateTask, ViewPort},
std::sync::{Arc, RwLock},
2023-11-26 22:09:03 +01:00
termion::event::{Event, Key},
};
#[async_std::main]
async fn main() {
2024-03-21 18:17:04 +01:00
/* Initialize our terminal.
2023-11-26 22:09:03 +01:00
*/
2023-11-28 20:52:25 +01:00
let tty_app = TTYApplication::new(|event| { /* handle event */ });
2023-11-26 22:09:03 +01:00
2024-03-21 18:17:04 +01:00
/* Setup our "root" view of the application.
* This will be the compositor, which is able to
* mix multiple `TerminalView`-Views together.
* Its output is routed to the `app.port` Viewport,
* so it will be displayed on TTY-output.
2023-11-26 22:09:03 +01:00
*/
2023-11-28 20:52:25 +01:00
let compositor = TerminalCompositor::new(tty_app.port.inner());
2023-11-26 22:09:03 +01:00
2024-03-21 18:17:04 +01:00
/* Add the label 'test' at position (7, 2)
*/
compositor
.write()
.unwrap()
.push(nested_tty::make_label("test").offset(Vector2::new(7, 2)));
2023-11-26 22:09:03 +01:00
2024-03-21 18:17:04 +01:00
/* Add a 'Hello World' label at position (5, 3)
* and set a coloring determined by formula from
* the position of each character.
*/
2023-11-26 22:09:03 +01:00
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)),
);
2023-11-26 22:09:03 +01:00
2024-03-21 18:17:04 +01:00
/* write the changes in the root-view to the terminal
2023-11-26 22:09:03 +01:00
*/
2023-11-28 20:52:25 +01:00
tty_app.show().await.expect("output error!");
2023-11-26 22:09:03 +01:00
}
2023-11-28 20:52:25 +01:00