Cargo fmt and use STDOUT_FILENO for getting Termios

This commit is contained in:
Timothy Warren 2019-09-13 16:34:31 -04:00
parent 0dd537dc14
commit b9dee69522
3 changed files with 27 additions and 33 deletions

View File

@ -421,7 +421,6 @@ impl Editor {
// If you can't write to stdout, you might as well just panic // If you can't write to stdout, you might as well just panic
handle.write_all(query.as_bytes()).unwrap(); handle.write_all(query.as_bytes()).unwrap();
let stdin = io::stdin(); let stdin = io::stdin();
let stdin = stdin.lock(); let stdin = stdin.lock();
let mut handle = stdin.take(32); let mut handle = stdin.take(32);
@ -430,7 +429,10 @@ impl Editor {
clean_unwrap(read_res); clean_unwrap(read_res);
if input.len() < 6 { 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(); let mut row_str = String::new();
@ -455,10 +457,7 @@ impl Editor {
let rows = clean_unwrap(row_str.parse()); let rows = clean_unwrap(row_str.parse());
let cols = clean_unwrap(row_str.parse()); let cols = clean_unwrap(row_str.parse());
return TermSize { return TermSize { cols, rows };
cols,
rows,
}
} }
fn get_window_size(&mut self) -> TermSize { fn get_window_size(&mut self) -> TermSize {
@ -544,11 +543,7 @@ impl Editor {
// End of a comment // End of a comment
if &row.render[mce_range.clone()] == mce { if &row.render[mce_range.clone()] == mce {
highlight_range( highlight_range(&mut row.highlight, mce_range, Highlight::MultiLineComment);
&mut row.highlight,
mce_range,
Highlight::MultiLineComment,
);
i += mce.len(); i += mce.len();
in_comment = false; in_comment = false;
@ -560,11 +555,7 @@ impl Editor {
} }
} else if &row.render[mcs_range.clone()] == mcs { } else if &row.render[mcs_range.clone()] == mcs {
// Start of a multi-line comment // Start of a multi-line comment
highlight_range( highlight_range(&mut row.highlight, mcs_range, Highlight::MultiLineComment);
&mut row.highlight,
mcs_range,
Highlight::MultiLineComment,
);
i += mcs.len(); i += mcs.len();
in_comment = true; in_comment = true;
@ -1157,7 +1148,7 @@ impl Editor {
fn row_cx_to_rx(&mut self, index: usize, cx: usize) -> usize { fn row_cx_to_rx(&mut self, index: usize, cx: usize) -> usize {
let mut rx: usize = 0; 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 { if i == cx {
return rx; return rx;
} }
@ -1612,7 +1603,9 @@ fn get_syntax_db() -> Vec<Syntax> {
"in", "in",
"as", "as",
], ],
vec!["=>", "Number", "String", "Object", "Math", "JSON", "Boolean"], vec![
"=>", "Number", "String", "Object", "Math", "JSON", "Boolean",
],
"//", "//",
"/*", "/*",
"*/", "*/",

View File

@ -9,7 +9,7 @@ pub mod terminal_helpers;
use crate::editor::Editor; use crate::editor::Editor;
use crate::terminal_helpers::*; use crate::terminal_helpers::*;
use nix::libc::STDIN_FILENO; use nix::libc::STDOUT_FILENO;
use nix::sys::termios::Termios; use nix::sys::termios::Termios;
use std::env; use std::env;
use std::io::Error; 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 // Much ugliness to get and keep a reference to the original terminal settings
lazy_static! { lazy_static! {
// Save terminal flags on start // Save terminal flags on start
pub static ref ORIGINAL_TERMIOS: Arc<Mutex<Termios>> = Arc::new(Mutex::new(get_termios(STDIN_FILENO))); pub static ref ORIGINAL_TERMIOS: Arc<Mutex<Termios>> = Arc::new(Mutex::new(get_termios(STDOUT_FILENO)));
} }
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
@ -50,7 +50,7 @@ fn main() -> Result<(), Error> {
editor::KeyCode::OtherKey('\0') => (), editor::KeyCode::OtherKey('\0') => (),
// Just for debugging // Just for debugging
_ => () //println!("{:?}\r\n", key) _ => (), //println!("{:?}\r\n", key)
} }
} }
None => break, None => break,

View File

@ -18,17 +18,6 @@ pub struct TermSize {
pub cols: u16, 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 /// Get a `Termios` struct, for getting/setting terminal flags
pub fn get_termios(fd: RawFd) -> Termios { pub fn get_termios(fd: RawFd) -> Termios {
termios::tcgetattr(fd).unwrap() termios::tcgetattr(fd).unwrap()
@ -41,7 +30,7 @@ pub fn enable_raw_mode() {
let mutex = Arc::clone(&super::ORIGINAL_TERMIOS); let mutex = Arc::clone(&super::ORIGINAL_TERMIOS);
mutex.lock().unwrap(); mutex.lock().unwrap();
let mut raw = get_termios(STDIN_FILENO); let mut raw = get_termios(STDOUT_FILENO);
raw.input_flags.remove( raw.input_flags.remove(
InputFlags::BRKINT 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 /// Attempt to get the size of the terminal (in rows and columns) from an `ioctl` call
#[inline]
pub fn get_term_size() -> Option<TermSize> { pub fn get_term_size() -> Option<TermSize> {
#[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 { let raw = UnixTermSize {
rows: 0, rows: 0,
cols: 0, cols: 0,