oxidized_curses/window/
sub_window.rs1use crate::utils::{ScreenPoint, ScreenRect};
2use crate::window::{MoveableWindow, Window};
3
4pub struct SubWindow {
6 this: ncurses::WINDOW,
8}
9
10pub enum SubWindowError {
12 CoordinateError(ScreenRect),
13 OtherError,
14}
15
16impl SubWindow {
17 pub fn init(rect: ScreenRect) -> Result<Self, SubWindowError> {
19 let win = ncurses::newwin(
20 rect.offset.y as i32,
21 rect.offset.x as i32,
22 rect.start.y as i32,
23 rect.start.x as i32,
24 );
25
26 if win == 0 as *mut i8 {
27 Err(SubWindowError::CoordinateError(rect))
28 } else {
29 Ok(SubWindow { this: win })
30 }
31 }
32}
33
34impl Window for SubWindow {
35 fn move_print(&mut self, point: ScreenPoint, text: &str) {
37 ncurses::mvwaddstr(self.this, point.y as i32, point.x as i32, text);
38 }
39
40 fn print(&mut self, text: &str) {
42 ncurses::waddstr(self.this, text);
43 }
44
45 fn refresh(&mut self) {
46 ncurses::wrefresh(self.this);
47 }
48
49 fn get_char(&mut self) -> char {
50 ncurses::wgetch(self.this) as u8 as char
51 }
52}
53
54impl MoveableWindow for SubWindow {
55 fn move_window(&mut self, point: ScreenPoint) {
56 ncurses::mvwin(self.this, point.y as i32, point.x as i32);
57 }
58}
59
60impl Drop for SubWindow {
61 fn drop(&mut self) {
63 ncurses::delwin(self.this);
64 }
65}