| giolekva | 0990ccf | 2022-05-13 16:12:38 +0400 | [diff] [blame] | 1 | use std::error; |
| 2 | use tui::{ |
| 3 | layout::{Alignment}, |
| 4 | widgets::{Block, BorderType, Borders}, |
| 5 | Terminal, |
| 6 | }; |
| 7 | |
| 8 | |
| 9 | use core::result::Result; |
| 10 | use std::io::Error; |
| 11 | use tui::backend::Backend; |
| 12 | use tui::buffer::Cell; |
| 13 | use tui::layout::Rect; |
| 14 | |
| 15 | extern "C" { |
| 16 | fn cursorHide(); |
| 17 | fn cursorShow(); |
| 18 | fn cursorSet(x: u16, y: u16); |
| 19 | fn clearScreen(); |
| 20 | fn getSize() -> (i32, i32, i32, i32); |
| 21 | fn flush(); |
| 22 | fn draw(x: u16, y: u16); |
| 23 | } |
| 24 | |
| 25 | struct HostBackend {} |
| 26 | |
| 27 | impl Backend for HostBackend { |
| 28 | fn draw<'a, I>(&mut self, content: I) -> Result<(), Error> |
| 29 | where |
| 30 | I: Iterator<Item = (u16, u16, &'a Cell)>, |
| 31 | { |
| 32 | for i in content { |
| 33 | let x: u16 = i.0; |
| 34 | let y: u16 = i.1; |
| 35 | unsafe { draw(x, y) } |
| 36 | } |
| 37 | Ok(()) |
| 38 | } |
| 39 | |
| 40 | fn hide_cursor(&mut self) -> Result<(), Error> { |
| 41 | unsafe { cursorHide() } |
| 42 | Ok(()) |
| 43 | } |
| 44 | |
| 45 | fn show_cursor(&mut self) -> Result<(), Error> { |
| 46 | unsafe { cursorShow() } |
| 47 | Ok(()) |
| 48 | } |
| 49 | |
| 50 | fn get_cursor(&mut self) -> Result<(u16, u16), Error> { |
| 51 | Ok((100, 100)) |
| 52 | } |
| 53 | |
| 54 | fn set_cursor(&mut self, x: u16, y: u16) -> Result<(), Error> { |
| 55 | unsafe { cursorSet(x, y) } |
| 56 | Ok(()) |
| 57 | } |
| 58 | |
| 59 | fn clear(&mut self) -> Result<(), Error> { |
| 60 | unsafe { clearScreen() } |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | fn size(&self) -> Result<Rect, Error> { |
| 65 | let ws: (i32, i32, i32, i32); |
| 66 | unsafe { ws = getSize(); } |
| 67 | Ok(Rect { |
| 68 | x: ws.0 as u16, |
| 69 | y: ws.1 as u16, |
| 70 | width: ws.2 as u16, |
| 71 | height: ws.3 as u16, |
| 72 | }) |
| 73 | // Ok(Rect { |
| 74 | // x: 0, |
| 75 | // y: 0, |
| 76 | // width: 50, |
| 77 | // height: 50, |
| 78 | // }) |
| 79 | } |
| 80 | |
| 81 | fn flush(&mut self) -> Result<(), Error> { |
| 82 | unsafe { flush() } |
| 83 | Ok(()) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn main() -> Result<(), Box<dyn error::Error>> { |
| 88 | let backend = HostBackend{}; |
| 89 | let mut terminal = Terminal::new(backend)?; |
| 90 | let mut i: i32 = 0; |
| 91 | loop { |
| 92 | terminal.draw(|f| { |
| 93 | let size = f.size(); |
| 94 | let block = Block::default() |
| 95 | .borders(Borders::ALL) |
| 96 | .title("Main block with round corners") |
| 97 | .title_alignment(Alignment::Center) |
| 98 | .border_type(BorderType::Rounded); |
| 99 | f.render_widget(block, size); |
| 100 | })?; |
| 101 | i = i + 1; |
| 102 | if i > 1000 { |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | terminal.show_cursor()?; |
| 107 | |
| 108 | Ok(()) |
| 109 | } |
| 110 | |
| 111 | struct Unused<T>(T); |
| 112 | |
| 113 | #[no_mangle] |
| 114 | pub extern "C" fn run() { |
| 115 | // unsafe { cursorSet(10, 10) } |
| 116 | Unused(main()); |
| 117 | } |