2019-09-04 15:41:06 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate bitflags;
|
|
|
|
|
2019-09-06 13:24:29 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
|
2019-09-10 16:47:15 -04:00
|
|
|
pub mod editor;
|
|
|
|
pub mod terminal_helpers;
|
2019-08-22 14:25:18 -04:00
|
|
|
|
2019-08-30 16:17:06 -04:00
|
|
|
use crate::editor::Editor;
|
2019-09-06 13:24:29 -04:00
|
|
|
use crate::terminal_helpers::*;
|
2019-09-13 16:34:31 -04:00
|
|
|
use nix::libc::STDOUT_FILENO;
|
2019-09-06 13:24:29 -04:00
|
|
|
use nix::sys::termios::Termios;
|
2019-08-27 12:22:19 -04:00
|
|
|
use std::env;
|
2019-08-26 16:39:52 -04:00
|
|
|
use std::io::Error;
|
|
|
|
use std::result::Result;
|
2019-09-06 13:24:29 -04:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
// Much ugliness to get and keep a reference to the original terminal settings
|
|
|
|
lazy_static! {
|
|
|
|
// Save terminal flags on start
|
2019-09-13 16:34:31 -04:00
|
|
|
pub static ref ORIGINAL_TERMIOS: Arc<Mutex<Termios>> = Arc::new(Mutex::new(get_termios(STDOUT_FILENO)));
|
2019-09-06 13:24:29 -04:00
|
|
|
}
|
2019-08-22 14:25:18 -04:00
|
|
|
|
2019-08-26 16:39:52 -04:00
|
|
|
fn main() -> Result<(), Error> {
|
2019-08-27 12:22:19 -04:00
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
// Disable canonical/"cooked" terminal mode
|
|
|
|
enable_raw_mode();
|
2019-08-22 14:25:18 -04:00
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
// Initialize the editor
|
2019-08-22 16:44:47 -04:00
|
|
|
let mut editor = Editor::new();
|
2019-08-22 14:25:18 -04:00
|
|
|
|
2019-08-27 12:22:19 -04:00
|
|
|
// Open the file if specified, from the command line
|
|
|
|
if args.len() >= 2 {
|
|
|
|
editor.open(&args[1])?;
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:19:19 -04:00
|
|
|
editor.set_status_message("HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find");
|
2019-08-29 14:13:09 -04:00
|
|
|
|
2019-09-17 10:34:17 -04:00
|
|
|
let mut skip_refresh: bool = false;
|
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
// Main input loop. Editor::process_keypress uses an Option Enum as a sentinel.
|
|
|
|
// `None` is returned on a quit action, in other cases, `Some(())` is returned,
|
|
|
|
// continuing the loop
|
2019-08-23 16:46:04 -04:00
|
|
|
loop {
|
2019-09-17 10:34:17 -04:00
|
|
|
editor.refresh_screen(skip_refresh);
|
2019-08-23 16:46:04 -04:00
|
|
|
|
2019-09-06 13:24:29 -04:00
|
|
|
match editor.process_keypress() {
|
2019-09-12 16:49:49 -04:00
|
|
|
Some(key) => {
|
|
|
|
match key {
|
2019-09-17 10:34:17 -04:00
|
|
|
editor::KeyCode::OtherKey('\0') => {
|
|
|
|
skip_refresh = true;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
skip_refresh = false;
|
|
|
|
//println!("{:?}\r\n", key)
|
|
|
|
}
|
2019-09-12 16:49:49 -04:00
|
|
|
}
|
2019-09-06 13:24:29 -04:00
|
|
|
}
|
|
|
|
None => break,
|
2019-08-23 16:46:04 -04:00
|
|
|
}
|
2019-08-21 16:46:14 -04:00
|
|
|
}
|
2019-08-23 13:34:00 -04:00
|
|
|
|
2019-08-23 14:57:26 -04:00
|
|
|
// Restore previous terminal flags before exit
|
2019-09-06 13:24:29 -04:00
|
|
|
disable_raw_mode();
|
2019-08-26 16:39:52 -04:00
|
|
|
|
|
|
|
Ok(())
|
2019-08-21 16:46:14 -04:00
|
|
|
}
|