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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
extern crate pancurses;

use pancurses::{initscr, endwin, Input, noecho};
use std::cmp::{max, min};
use std::ops::Deref;
use std::fmt::Display;

pub type Message = String;
const INPUT_HEIGHT: usize = 2; // the number of lines we need for the input area


pub struct Window {
    win: pancurses::Window,
    backlog: Vec<String>,
    input: Vec<char>,
    position: i32,
    max_y: i32,
    max_x: i32,
}

impl Window {
    pub fn initscr() -> Window {
        let win = initscr();
        win.timeout(100);
        win.keypad(true);
        noecho();

        let (max_y, max_x) = win.get_max_yx();

        Window {
            win: win,
            backlog: Vec::new(),
            input: Vec::new(),
            position: 0,
            max_y,
            max_x,
        }
    }

    pub fn writeln<D: Display>(&mut self, txt: D) {
        let line = format!("{}\n", txt);
        self.backlog.push(line);

        if self.backlog.len() + INPUT_HEIGHT > self.max_y as usize {
            self.backlog.remove(0);
        }

        self.redraw();
    }

    pub fn redraw(&self) {
        self.win.erase();

        for line in &self.backlog {
            self.win.printw(&line);
        }

        self.draw_input();
        self.win.refresh();
    }

    pub fn draw_input(&self) {
        self.win.mv(self.max_y -2, 0);
        self.win.hline('-', self.max_x);
        self.win.mv(self.max_y -1, 0);
        for x in &self.input {
            self.win.addch(*x);
        }
        self.win.mv(self.max_y -1, self.position);
    }

    pub fn resize(&mut self) {
        let (max_y, max_x) = self.win.get_max_yx();
        self.max_y = max_y;
        self.max_x = max_x;
        while self.backlog.len() + INPUT_HEIGHT > self.max_y as usize {
            self.backlog.remove(0);
        }
        self.redraw();
    }

    pub fn get(&mut self) -> Option<String> {
        match self.win.getch() {
            Some(Input::Character('\n')) => {
                if self.input.len() == 0 {
                    return None;
                }

                let line = self.input.drain(..).collect();
                self.position = 0;
                return Some(line);
            },
            Some(Input::Character('\x7f')) => {
                if self.position > 0 {
                    self.win.mv(self.max_y -1, self.position-1);
                    self.win.delch();
                    self.position -= 1;
                    self.input.remove(self.position as usize);
                }
            },
            Some(Input::KeyDC) => {
                if self.position < self.input.len() as i32 {
                    self.win.delch();
                    self.input.remove(self.position as usize);
                }
            },
            Some(Input::Character(c)) => {
                self.win.addch(c);
                self.position += 1;
                self.input.push(c);
            },
            Some(Input::KeyLeft) => {
                self.position = max(0, self.position -1);
                self.win.mv(self.max_y -1, self.position);
            },
            Some(Input::KeyRight) => {
                self.position = min(self.input.len() as i32, self.position +1);
                self.win.mv(self.max_y -1, self.position);
            },
            Some(Input::KeyResize) => self.resize(),
            Some(_input) => {
                // TODO: KeyPPage
                // TODO: KeyNPage
                // TODO: KeyUp
                // TODO: KeyDown
                // self.win.addstr(&format!("<{:?}>", input));
            },
            None => (),
        }

        self.win.refresh();

        None
    }
}

impl Deref for Window {
    type Target = pancurses::Window;

    fn deref(&self) -> &Self::Target {
        &self.win
    }
}

impl Drop for Window {
    fn drop(&mut self) {
        endwin();
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}