diff options
author | Klara Modin <kasm@kasm.eu> | 2018-05-17 02:33:54 +0200 |
---|---|---|
committer | Klara Modin <kasm@kasm.eu> | 2018-05-17 02:33:54 +0200 |
commit | ac36fceb0ebe6c373afa8d78940d88ed7714b754 (patch) | |
tree | 24f180f7ca59b92a60aa9803ae9b1a0435259d21 | |
parent | cd450a56eba1ecdee1280224c9838039a067762b (diff) |
add hacky history implementation
-rw-r--r-- | src/ui.rs | 99 |
1 files changed, 76 insertions, 23 deletions
@@ -11,6 +11,8 @@ pub struct UI { chan: String, nick: String, buffer: String, + history: Vec<String>, + history_place: i32, } impl UI { @@ -23,6 +25,8 @@ impl UI { chan: String::new(), nick: String::new(), buffer: String::new(), + history: Vec::new(), + history_place: -1, } } @@ -105,36 +109,85 @@ impl UI { mv(max_y-1, self.input_x); printw(chars.as_str()); } - self.getstr_unblocking() + if let Some(line) = self.getstr_unblocking() { + let mut save = false; + if line != "" { + if let Some(prev) = self.history.last() { + if prev != &line { + save = true; + } + } else { + save = true; + } + } + if save { + self.history.push(line.clone()); + self.history_place = -1; + } + Some(line) + } else { + None + } } fn getstr_unblocking(&mut self) -> Option<String> { let mut new_x = 0; let mut new_y = 0; getyx(stdscr(), &mut new_y, &mut new_x); - while let Some(WchResult::Char(wch)) = get_wch() { - let ch = wch as u8 as char; - match ch { - '\n' => { - deleteln(); - mv(self.input_y, self.input_x); - let ret = self.buffer.clone(); - self.buffer.clear(); - return Some(ret); - }, - '\x7f' => { - self.buffer.pop(); - mv(self.input_y, self.input_x); - printw(&self.buffer); - let mut new_x = 0; - let mut new_y = 0; - getyx(stdscr(), &mut new_y, &mut new_x); - printw(" "); - mv(new_y, new_x); + while let Some(wchr) = get_wch() { + //while let Some(WchResult::Char(wch)) = get_wch() { + if let WchResult::Char(wch) = wchr { + let ch = wch as u8 as char; + match ch { + '\n' => { + deleteln(); + mv(self.input_y, self.input_x); + let ret = self.buffer.clone(); + self.buffer.clear(); + return Some(ret); + }, + '\x7f' => { + self.buffer.pop(); + mv(self.input_y, self.input_x); + printw(&self.buffer); + let mut new_x = 0; + let mut new_y = 0; + getyx(stdscr(), &mut new_y, &mut new_x); + printw(" "); + mv(new_y, new_x); + } + _ => { + self.buffer.push(ch); + }, + } + } else if let WchResult::KeyCode(kc) = wchr { + match kc { + KEY_UP => { + if self.history_place < 0 { + self.history_place = self.history.len() as i32; + } + self.history_place -= 1; + if self.history_place < 0 { + self.history_place = self.history.len() as i32; + } + if let Some(history) = self.history.get(self.history_place as usize) { + self.buffer = history.clone(); + } + }, + KEY_DOWN => { + if self.history_place < 0 { + self.history_place = self.history.len() as i32 - 1; + } + self.history_place += 1; + if self.history_place >= self.history.len() as i32 { + self.history_place = self.history.len() as i32 - 1; + } + if let Some(history) = self.history.get(self.history_place as usize) { + self.buffer = history.clone(); + } + }, + _ => {}, } - _ => { - self.buffer.push(ch); - }, } } None |