1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
pub extern crate prototty_render; extern crate wasm_bindgen; use prototty_render::*; pub use prototty_render::{Coord, Size}; use wasm_bindgen::prelude::*; #[wasm_bindgen] extern "C" { pub type JsGrid; #[wasm_bindgen(method)] fn js_set_cell( this: &JsGrid, x: i32, y: i32, depth: i32, character: JsValue, bold: JsValue, underline: JsValue, foreground: JsValue, background: JsValue, ); #[wasm_bindgen(method)] fn js_clear(this: &JsGrid); #[wasm_bindgen(method)] fn js_render(this: &JsGrid); #[wasm_bindgen(method)] fn js_width(this: &JsGrid) -> u32; #[wasm_bindgen(method)] fn js_height(this: &JsGrid) -> u32; } fn rgb24_to_js_string(Rgb24 { r, g, b }: Rgb24) -> String { format!("rgb({},{},{})", r, g, b) } impl ViewGrid for JsGrid { fn set_cell_absolute(&mut self, coord: Coord, depth: i32, view_cell: ViewCell) { let x = coord.x; let y = coord.y; let character = view_cell .character() .map(|character| JsValue::from_str(&character.to_string())) .unwrap_or(JsValue::NULL); let bold = view_cell .bold() .map(JsValue::from_bool) .unwrap_or(JsValue::NULL); let underline = view_cell .underline() .map(JsValue::from_bool) .unwrap_or(JsValue::NULL); let foreground = view_cell .foreground() .map(|foreground| JsValue::from_str(&rgb24_to_js_string(foreground))) .unwrap_or(JsValue::NULL); let background = view_cell .background() .map(|background| JsValue::from_str(&rgb24_to_js_string(background))) .unwrap_or(JsValue::NULL); self.js_set_cell( x, y, depth, character, bold, underline, foreground, background, ); } fn size(&self) -> Size { Size::new(self.js_width(), self.js_height()) } } impl JsGrid { pub fn render_at<V: View<T>, T, R: ViewTransformRgb24>( &mut self, view: &mut V, data: T, context: ViewContext<R>, ) { self.js_clear(); view.view(data, context, self); self.js_render(); } pub fn render<V: View<T>, T>(&mut self, view: &mut V, data: T) { let size = self.size(); self.render_at(view, data, ViewContext::default_with_size(size)) } }