2019-08-22 14:25:18 -04:00
|
|
|
//! Editor functionality
|
2019-08-22 16:44:47 -04:00
|
|
|
use crate::helpers::*;
|
2019-08-22 14:25:18 -04:00
|
|
|
|
2019-08-22 16:44:47 -04:00
|
|
|
use nix::sys::termios::Termios;
|
|
|
|
use nix::unistd::read;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
2019-08-23 13:34:00 -04:00
|
|
|
use std::io::{BufReader, Error, Stdin};
|
|
|
|
use std::str::Chars;
|
2019-08-22 16:44:47 -04:00
|
|
|
|
|
|
|
/// Main structure for the editor
|
2019-08-23 14:57:26 -04:00
|
|
|
///
|
|
|
|
/// impl blocks are split similarly to the original C implementation
|
|
|
|
pub struct Editor {}
|
2019-08-22 14:25:18 -04:00
|
|
|
|
2019-08-23 13:34:00 -04:00
|
|
|
// init
|
2019-08-22 14:25:18 -04:00
|
|
|
impl Editor {
|
|
|
|
pub fn new() -> Self {
|
2019-08-23 14:57:26 -04:00
|
|
|
Editor {}
|
2019-08-22 16:44:47 -04:00
|
|
|
}
|
2019-08-23 13:34:00 -04:00
|
|
|
}
|
2019-08-22 16:44:47 -04:00
|
|
|
|
2019-08-23 13:34:00 -04:00
|
|
|
// Terminal
|
|
|
|
impl Editor {
|
|
|
|
fn read_key(&mut self) -> Option<Vec<char>> {
|
2019-08-22 16:44:47 -04:00
|
|
|
let stdin = io::stdin();
|
2019-08-23 14:57:26 -04:00
|
|
|
let stdin = stdin.lock();
|
2019-08-22 16:44:47 -04:00
|
|
|
let mut in_str = String::new();
|
2019-08-23 14:57:26 -04:00
|
|
|
let mut input = BufReader::with_capacity(3, stdin);
|
2019-08-22 16:44:47 -04:00
|
|
|
input.read_to_string(&mut in_str).unwrap();
|
|
|
|
|
2019-08-23 13:34:00 -04:00
|
|
|
let mut output: Vec<char> = vec![];
|
|
|
|
|
|
|
|
for char in in_str.chars() {
|
|
|
|
output.push(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
if output.len() == 0 {
|
2019-08-23 14:57:26 -04:00
|
|
|
return None;
|
2019-08-22 16:44:47 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
return Some(output);
|
2019-08-23 13:34:00 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 16:44:47 -04:00
|
|
|
|
2019-08-23 13:34:00 -04:00
|
|
|
// Input
|
|
|
|
impl Editor {
|
|
|
|
pub fn process_keypress(&mut self) -> Option<()> {
|
2019-08-23 14:57:26 -04:00
|
|
|
match self.read_key() {
|
|
|
|
// Just continue the input loop on an "empty" keypress
|
|
|
|
None => Some(()),
|
|
|
|
Some(chars) => {
|
|
|
|
let first = chars[0];
|
2019-08-22 16:44:47 -04:00
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
if first == ctrl_key('q') {
|
|
|
|
// Break out of the input loop
|
|
|
|
return None;
|
|
|
|
}
|
2019-08-23 13:34:00 -04:00
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
print!("{:?}\r\n", chars);
|
2019-08-23 13:34:00 -04:00
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
// Continue the main input loop
|
|
|
|
Some(())
|
2019-08-23 13:34:00 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 14:25:18 -04:00
|
|
|
}
|
2019-08-23 13:34:00 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
// Output
|
|
|
|
impl Editor {
|
|
|
|
pub fn refresh_screen() {
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let mut handle = stdout.lock();
|
|
|
|
|
|
|
|
let mut buffer = String::from("\x1b[2J").into_bytes();
|
2019-08-23 13:34:00 -04:00
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
handle.write_all(&mut buffer).unwrap();
|
|
|
|
}
|
2019-08-23 13:34:00 -04:00
|
|
|
}
|