From b9dee695220bda40d6a39e5e68a50048393929e1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 13 Sep 2019 16:34:31 -0400 Subject: [PATCH] Cargo fmt and use STDOUT_FILENO for getting Termios --- src/editor.rs | 29 +++++++++++------------------ src/main.rs | 6 +++--- src/terminal_helpers.rs | 25 +++++++++++++------------ 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 0390ae6..5a75ef8 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -421,7 +421,6 @@ impl Editor { // If you can't write to stdout, you might as well just panic handle.write_all(query.as_bytes()).unwrap(); - let stdin = io::stdin(); let stdin = stdin.lock(); let mut handle = stdin.take(32); @@ -430,7 +429,10 @@ impl Editor { clean_unwrap(read_res); if input.len() < 6 { - panic!("Invalid or missing response to cursor location query: {:?}", input); + panic!( + "Invalid or missing response to cursor location query: {:?}", + input + ); } let mut row_str = String::new(); @@ -455,10 +457,7 @@ impl Editor { let rows = clean_unwrap(row_str.parse()); let cols = clean_unwrap(row_str.parse()); - return TermSize { - cols, - rows, - } + return TermSize { cols, rows }; } fn get_window_size(&mut self) -> TermSize { @@ -544,11 +543,7 @@ impl Editor { // End of a comment if &row.render[mce_range.clone()] == mce { - highlight_range( - &mut row.highlight, - mce_range, - Highlight::MultiLineComment, - ); + highlight_range(&mut row.highlight, mce_range, Highlight::MultiLineComment); i += mce.len(); in_comment = false; @@ -560,11 +555,7 @@ impl Editor { } } else if &row.render[mcs_range.clone()] == mcs { // Start of a multi-line comment - highlight_range( - &mut row.highlight, - mcs_range, - Highlight::MultiLineComment, - ); + highlight_range(&mut row.highlight, mcs_range, Highlight::MultiLineComment); i += mcs.len(); in_comment = true; @@ -1157,7 +1148,7 @@ impl Editor { fn row_cx_to_rx(&mut self, index: usize, cx: usize) -> usize { let mut rx: usize = 0; - for (i, ch) in self.rows[index].chars.char_indices() { + for (i, ch) in self.rows[index].chars.char_indices() { if i == cx { return rx; } @@ -1612,7 +1603,9 @@ fn get_syntax_db() -> Vec { "in", "as", ], - vec!["=>", "Number", "String", "Object", "Math", "JSON", "Boolean"], + vec![ + "=>", "Number", "String", "Object", "Math", "JSON", "Boolean", + ], "//", "/*", "*/", diff --git a/src/main.rs b/src/main.rs index 0c6ad06..fa13403 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ pub mod terminal_helpers; use crate::editor::Editor; use crate::terminal_helpers::*; -use nix::libc::STDIN_FILENO; +use nix::libc::STDOUT_FILENO; use nix::sys::termios::Termios; use std::env; use std::io::Error; @@ -19,7 +19,7 @@ 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 - pub static ref ORIGINAL_TERMIOS: Arc> = Arc::new(Mutex::new(get_termios(STDIN_FILENO))); + pub static ref ORIGINAL_TERMIOS: Arc> = Arc::new(Mutex::new(get_termios(STDOUT_FILENO))); } fn main() -> Result<(), Error> { @@ -50,7 +50,7 @@ fn main() -> Result<(), Error> { editor::KeyCode::OtherKey('\0') => (), // Just for debugging - _ => () //println!("{:?}\r\n", key) + _ => (), //println!("{:?}\r\n", key) } } None => break, diff --git a/src/terminal_helpers.rs b/src/terminal_helpers.rs index a6eb794..e2c291f 100644 --- a/src/terminal_helpers.rs +++ b/src/terminal_helpers.rs @@ -18,17 +18,6 @@ pub struct TermSize { pub cols: u16, } -#[repr(C)] -#[derive(Debug)] -struct UnixTermSize { - /// number of rows - pub rows: c_ushort, - /// number of columns - pub cols: c_ushort, - x: c_ushort, - y: c_ushort, -} - /// Get a `Termios` struct, for getting/setting terminal flags pub fn get_termios(fd: RawFd) -> Termios { termios::tcgetattr(fd).unwrap() @@ -41,7 +30,7 @@ pub fn enable_raw_mode() { let mutex = Arc::clone(&super::ORIGINAL_TERMIOS); mutex.lock().unwrap(); - let mut raw = get_termios(STDIN_FILENO); + let mut raw = get_termios(STDOUT_FILENO); raw.input_flags.remove( InputFlags::BRKINT @@ -79,7 +68,19 @@ pub fn disable_raw_mode() { } /// Attempt to get the size of the terminal (in rows and columns) from an `ioctl` call +#[inline] pub fn get_term_size() -> Option { + #[repr(C)] + #[derive(Debug)] + struct UnixTermSize { + /// number of rows + pub rows: c_ushort, + /// number of columns + pub cols: c_ushort, + x: c_ushort, + y: c_ushort, + } + let raw = UnixTermSize { rows: 0, cols: 0,