Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // run.rs
- use std::io::Write;
- use termion::event::Key;
- use termion::input::TermRead;
- use termion::raw::IntoRawMode;
- use crate::DisplayableFile;
- use crate::Mode;
- impl DisplayableFile {
- pub fn run_off_mode(&mut self) {
- self.display();
- match self.mode {
- Mode::Command => self.command_run(),
- Mode::Insert => self.insert_run(),
- Mode::Normal => self.normal_run()
- }
- self.display();
- }
- pub fn normal_run(&mut self) {
- self.display();
- let _stdout = std::io::stdout().into_raw_mode().unwrap();
- let stdin = std::io::stdin();
- for c in stdin.keys() {
- match c.unwrap() {
- Key::Char('c') => { self.mode = Mode::Command; self.display(); },
- Key::Char('i') => { self.mode = Mode::Insert; self.display(); },
- Key::Ctrl(ch) => {
- if ch == 'c' {
- std::process::exit(0);
- }
- if ch == 's' {
- self.saved = true;
- }
- self.display();
- }
- _ => {
- self.display();
- }
- }
- }
- }
- pub fn command_run(&mut self) {
- self.display();
- let _stdout = std::io::stdout().into_raw_mode().unwrap();
- let stdin = std::io::stdin();
- for c in stdin.keys() {
- match c.unwrap() {
- Key::Esc => { self.mode = Mode::Normal; self.display(); },
- Key::Char(ch) => { self.command.push(ch); self.display(); },
- Key::Backspace => { drop(self.command.pop()); self.display(); },
- Key::Ctrl(ch) => {
- if ch == 'c' {
- std::process::exit(0);
- }
- if ch == 's' {
- self.saved = true;
- }
- self.display();
- }
- _ => {
- self.display();
- }
- }
- }
- }
- pub fn insert_run(&mut self) {
- self.display();
- let mut stdout = std::io::stdout().into_raw_mode().unwrap();
- let stdin = std::io::stdin();
- for c in stdin.keys() {
- match c.unwrap() {
- Key::Esc => { self.mode = Mode::Normal; self.display(); },
- Key::Ctrl(ch) => {
- if ch == 'c' {
- std::process::exit(0);
- }
- if ch == 's' {
- self.saved = !self.saved;
- }
- self.display();
- }
- Key::Char(ch) => {
- self.saved = false;
- // Some guards to prevent blatant errors
- if self.contents.get(self.cursor_pos.y) == Option::None
- || self.cursor_pos.x > self.contents.get(self.cursor_pos.y).unwrap_or(&String::from(" ")).len() {
- } else if ch != '\n' {
- // Regular key press
- let mut new_str = String::from(self.contents.get(self.cursor_pos.y).unwrap());
- new_str.insert(self.cursor_pos.x, ch);
- self.contents[self.cursor_pos.y]
- .insert(self.cursor_pos.x, ch);
- self.cursor_pos.x += 1;
- self.display();
- } else {
- // Enter Key
- self.contents.insert(self.cursor_pos.y+1, String::new());
- self.cursor_pos.y += 1;
- self.cursor_pos.x = 0;
- self.display();
- }
- }
- Key::Backspace => {
- self.saved = false;
- if self.contents.get(self.cursor_pos.y) == Option::None
- || self.cursor_pos.x > self.contents.get(self.cursor_pos.y).unwrap_or(&String::from(" ")).len()
- || self.cursor_pos.x < 1 {
- } else {
- self.contents[self.cursor_pos.y]
- .replace_range(self.cursor_pos.x-1..self.cursor_pos.x, "");
- self.cursor_pos.x -= 1;
- self.display();
- }
- }
- Key::Up => {
- if self.cursor_pos.y > 0 {
- self.cursor_pos.y -= 1;
- self.cursor_pos.x = self.contents[self.cursor_pos.y].len();
- }
- self.display();
- }
- Key::Down => {
- if self.cursor_pos.y < self.contents.len()-1 {
- self.cursor_pos.y += 1;
- self.cursor_pos.x = self.contents[self.cursor_pos.y].len();
- self.display();
- }
- }
- Key::Left => {
- if self.cursor_pos.x > 0 {
- self.cursor_pos.x -= 1;
- }
- self.display();
- }
- Key::Right => {
- self.cursor_pos.x += 1;
- self.display();
- }
- _ => {
- self.display();
- }
- }
- }
- self.display();
- stdout.flush().unwrap();
- }
- }
Add Comment
Please, Sign In to add comment