prototty

Version Documentation

Definition of types and traits for describing how to render a type to a grid of characters (such as a terminal). Also provides an input type to simplify making an application that works with multiple frontends.

Example

Let's make a title element which renders a single line of text, within a fixed width rectangle of height 1. The entire source code for this example is in the "examples/title" directory in this repo.

```rust extern crate prototty; use prototty::*;

// Define a type representing the element pub struct Title { pub width: u32, pub text: String, }

// Define a type representing how the element will be rendered. pub struct TitleView;

// Describe how a TitleView renders a Title by implementing View. impl View for TitleView { fn view<G: ViewGrid>(&self, title: &Title, offset: Coord, depth: i32, grid: &mut G) { for (i, ch) in title.text.chars().enumerate() { if let Some(cell) = grid.get<em>mut(offset + Coord::new(i as i32, 0), depth) { cell.set</em>character(ch); cell.set_underline(true); } } } }</p> <p>// What if we want a way to rendered titles centered within their width? pub struct CenteredTitleView; impl View<Title> for CenteredTitleView { fn view<G: ViewGrid>(&self, title: &Title, offset: Coord, depth: i32, grid: &mut G) { let space = ::std::cmp::max(title.width as i32 - title.text.len() as i32, 0) / 2; for (i, ch) in title.text.chars().enumerate() { if let Some(cell) = grid.get<em>mut(offset + Coord::new(space + i as i32, 0), depth) { cell.set</em>character(ch); cell.set_underline(true); } } } }</p> <p>// Let's demonstrate both of these in action by rendering a title // twice - once left aligned, an once centered: pub struct DemoTitleView; impl View<Title> for DemoTitleView { fn view<G: ViewGrid>(&self, title: &Title, offset: Coord, depth: i32, grid: &mut G) {</p> <pre><code> // render the title left-aligned in the top-left corner TitleView.view(title, offset, depth, grid); // render the title centered 2 lines down CenteredTitleView.view(title, offset + Coord::new(0, 2), depth, grid); } </code></pre> <p>}</p> <p>```</p> <p>We can now render a <code>Title</code> with 3 of different views: - <code>TitleView</code> renders a <code>Title</code> left-aligned - <code>CenteredTitleView</code> renders a <code>Title</code> centered within its width - <code>DemoTitleView</code> renders a <code>Title</code> twice, using the other two views</p> <p>This isn't much use by itself. To actually get something onto a screen, we'll need a frontend. Continue this example in: - <a href="https://github.com/stevebob/prototty/tree/master/unix">prototty-unix</a> - <a href="https://github.com/stevebob/prototty/tree/master/glutin">prototty-glutin</a> - <a href="https://github.com/stevebob/prototty/tree/master/wasm">prototty-wasm</a></p> <p>Or see how to decorate it with a border in <a href="https://github.com/stevebob/prototty/tree/master/common">prototty-common</a>.</p> </body></html>