diff --git a/src/caret/motion.rs b/src/caret/motion.rs index 2f1a70ab2761374a416f52113939aca06217789e..e3646bfe92d7ab1b7e508c05d034c44f3f53da41 100644 --- a/src/caret/motion.rs +++ b/src/caret/motion.rs @@ -26,7 +26,6 @@ impl Editor { Char('L') => Some((self.buffers.current_buffer()[y].len() - 1, y)), Char('H') => Some((0, y)), Char('t') => { - let ch = self.get_char(); if let Some(o) = self.next_ocur(ch, n.d()) { @@ -36,7 +35,6 @@ impl Editor { } } Char('f') => { - let ch = self.get_char(); if let Some(o) = self.previous_ocur(ch, n.d()) { @@ -70,11 +68,13 @@ impl Editor { Char('j') => Some(self.down_unbounded(n.d())), Char('k') => Some(self.up_unbounded(n.d())), Char('g') => Some((0, n.or(1) as isize - 1)), - Char('G') => Some((self.buffers.current_buffer()[y].len() as isize, self.buffers.current_buffer().len() as isize - 1)), + Char('G') => Some(( + self.buffers.current_buffer()[y].len() as isize, + self.buffers.current_buffer().len() as isize - 1, + )), Char('L') => Some(to_signed_pos((self.buffers.current_buffer()[y].len(), y))), Char('H') => Some((0, y as isize)), Char('t') => { - let ch = self.get_char(); if let Some(o) = self.next_ocur(ch, n.d()) { @@ -84,7 +84,6 @@ impl Editor { } } Char('f') => { - let ch = self.get_char(); if let Some(o) = self.previous_ocur(ch, n.d()) { diff --git a/src/caret/movement.rs b/src/caret/movement.rs index fd0a92f01e574759d892d6926043a176bc414903..12bf6df556d0160bd153db81372ad7098e92b8f2 100644 --- a/src/caret/movement.rs +++ b/src/caret/movement.rs @@ -24,10 +24,8 @@ impl Editor { /// Get position after a given position, i.e. a generalisation of .next() #[inline] pub fn after(&self, n: usize, (x, y): (usize, usize)) -> Option<(usize, usize)> { - // TODO: Make this more idiomatic { if x + n < self.buffers.current_buffer()[y].len() { - Some((x + n, y)) } else { if y + 1 >= self.buffers.current_buffer().len() { @@ -48,7 +46,6 @@ impl Editor { } } } - } } // } @@ -76,7 +73,6 @@ impl Editor { ry -= 1; } else if ry == 0 { return None; - } } } @@ -134,15 +130,18 @@ impl Editor { #[inline] pub fn down_unbounded(&self, n: usize) -> (isize, isize) { (self.cursor().x as isize, self.y() as isize + n as isize) - } /// Get n'th next ocurrence of a given charecter (relatively to the cursor) pub fn next_ocur(&self, c: char, n: usize) -> Option<usize> { let mut dn = 0; - let mut x = self.x(); + let mut x = self.x(); - for (i, ch) in self.buffers.current_buffer()[self.y()].chars().skip(x).enumerate() { + for (i, ch) in self.buffers.current_buffer()[self.y()] + .chars() + .skip(x) + .enumerate() + { if ch == c { if i > 0 { dn += 1; @@ -160,10 +159,15 @@ impl Editor { /// Get n'th previous ocurrence of a given charecter (relatively to the cursor) pub fn previous_ocur(&self, c: char, n: usize) -> Option<usize> { let mut dn = 0; - let mut x = self.x(); - let y = self.y(); - - for (i, ch) in self.buffers.current_buffer()[y].chars().rev().skip(self.buffers.current_buffer()[y].len() - x).enumerate() { + let mut x = self.x(); + let y = self.y(); + + for (i, ch) in self.buffers.current_buffer()[y] + .chars() + .rev() + .skip(self.buffers.current_buffer()[y].len() - x) + .enumerate() + { if ch == c { dn += 1; if dn == n { @@ -181,9 +185,13 @@ impl Editor { /// whitespace. An empty line is also considered to be a WORD." pub fn _next_word_forward(&self, n: usize) -> Option<usize> { let mut dn = 0; - let mut x = self.x(); + let mut x = self.x(); - for (i, ch) in self.buffers.current_buffer()[self.y()].chars().skip(x).enumerate() { + for (i, ch) in self.buffers.current_buffer()[self.y()] + .chars() + .skip(x) + .enumerate() + { if ch.is_whitespace() { dn += 1; if dn == n { diff --git a/src/caret/position.rs b/src/caret/position.rs index bc5c7780fc843fa4b6e7fb8dc4742e9a9c40df85..4a8b4226b2300269077dc10f402bd13fc2134696 100644 --- a/src/caret/position.rs +++ b/src/caret/position.rs @@ -29,15 +29,13 @@ impl Editor { /// Convert a position value to a bounded position value #[inline] pub fn bound(&self, (x, mut y): (usize, usize), tight: bool) -> (usize, usize) { - - y = if y >= self.buffers.current_buffer().len() { self.buffers.current_buffer().len() - 1 } else { y }; - let ln = self.buffers.current_buffer()[y].len() + if tight {0} else {1}; + let ln = self.buffers.current_buffer()[y].len() + if tight { 0 } else { 1 }; if x >= ln { if ln == 0 { (0, y) @@ -59,7 +57,6 @@ impl Editor { /// axis is bounded. #[inline] pub fn bound_ver(&self, (x, mut y): (usize, usize)) -> (usize, usize) { - // Is this premature optimization? Yes, yes it is! y = if y > self.buffers.current_buffer().len() - 1 { self.buffers.current_buffer().len() - 1 diff --git a/src/core/exec.rs b/src/core/exec.rs index 6d8910e42458e03643cb2d03466b4521ca7ded90..18198366319c35d048e8894f5cecbfd7d2361920 100644 --- a/src/core/exec.rs +++ b/src/core/exec.rs @@ -1,7 +1,7 @@ use state::editor::Editor; use io::parse::{Inst, Parameter}; -use state::mode::{Mode, CommandMode, PrimitiveMode}; -use edit::insert::{InsertOptions, InsertMode}; +use state::mode::{CommandMode, Mode, PrimitiveMode}; +use edit::insert::{InsertMode, InsertOptions}; use io::redraw::RedrawTask; use edit::buffer::TextBuffer; use core::prompt::PromptCommand; @@ -28,7 +28,7 @@ impl Editor { let left = self.left(1); self.goto(left); self.cursor_mut().mode = Mode::Command(CommandMode::Normal); - }, + } (Primitive(Insert(_)), Char(' ')) if self.key_state.shift => { let left = self.left(1); self.goto(left); @@ -38,60 +38,50 @@ impl Editor { self.cursor_mut().mode = Mode::Command(CommandMode::Normal) } (_, Char(' ')) if self.key_state.alt => self.next_cursor(), - _ if self.key_state.alt => { - if let Some(m) = self.to_motion(Inst(para, cmd)) { - self.goto(m); - } - } + _ if self.key_state.alt => if let Some(m) = self.to_motion(Inst(para, cmd)) { + self.goto(m); + }, (Command(Normal), Char('i')) => { - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); - + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } (Command(Normal), Char('I')) => { self.cursor_mut().x = 0; - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); - + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } (Command(Normal), Char('a')) => { let pos = self.right(1, false); self.goto(pos); - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } (Command(Normal), Char('A')) => { let pos = (self.buffers.current_buffer()[self.y()].len(), self.y()); //let pos = self.right(1, false); self.goto(pos); - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } (Command(Normal), Char('o')) => { let y = self.y(); let ind = if self.options.autoindent { - self.buffers - .current_buffer() - .get_indent(y) - .to_owned() + self.buffers.current_buffer().get_indent(y).to_owned() } else { String::new() }; let last = ind.len(); - self.buffers.current_buffer_mut().insert_line(y + 1, ind.into()); + self.buffers + .current_buffer_mut() + .insert_line(y + 1, ind.into()); self.goto((last, y + 1)); - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } (Command(Normal), Char('h')) => { let left = self.left(n); @@ -154,10 +144,9 @@ impl Editor { current_buffer.raw_buffer[y].insert(x, c); } (Command(Normal), Char('R')) => { - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Replace, - })); + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Replace, + })); } (Command(Normal), Char('d')) => { let ins = self.get_inst(); @@ -169,10 +158,9 @@ impl Editor { let ins = self.get_inst(); if let Some(m) = self.to_motion_unbounded(ins) { self.remove_rb(m); - self.cursor_mut().mode = - Mode::Primitive(PrimitiveMode::Insert(InsertOptions { - mode: InsertMode::Insert, - })); + self.cursor_mut().mode = Mode::Primitive(PrimitiveMode::Insert(InsertOptions { + mode: InsertMode::Insert, + })); } } (Command(Normal), Char('G')) => { @@ -191,17 +179,13 @@ impl Editor { mov = true; } } - } (Command(Normal), Char('b')) => { // Branch cursor - if self.buffers - .current_buffer_info() - .cursors - .len() < 255 { + if self.buffers.current_buffer_info().cursors.len() < 255 { let cursor = self.cursor().clone(); - let current_cursor_index = self.buffers.current_buffer_info().current_cursor as - usize; + let current_cursor_index = + self.buffers.current_buffer_info().current_cursor as usize; self.buffers .current_buffer_info_mut() .cursors @@ -213,10 +197,7 @@ impl Editor { } (Command(Normal), Char('B')) => { // Delete cursor - if self.buffers - .current_buffer_info() - .cursors - .len() > 1 { + if self.buffers.current_buffer_info().cursors.len() > 1 { let current_cursor_index = self.buffers.current_buffer_info().current_cursor; self.buffers .current_buffer_info_mut() @@ -262,12 +243,10 @@ impl Editor { (Command(Normal), Char('z')) => { let Inst(param, cmd) = self.get_inst(); match param { - Parameter::Null => { - if let Some(m) = self.to_motion(Inst(param, cmd)) { - self.buffers.current_buffer_info_mut().scroll_y = m.1; - self.goto(m); - } - } + Parameter::Null => if let Some(m) = self.to_motion(Inst(param, cmd)) { + self.buffers.current_buffer_info_mut().scroll_y = m.1; + self.goto(m); + }, Parameter::Int(n) => { self.buffers.current_buffer_info_mut().scroll_y = n; } @@ -281,14 +260,12 @@ impl Editor { (Command(Normal), Char('~')) => { self.invert_chars(n); } - (Command(Normal), Char('.')) => { - if let Some(inst) = self.previous_instruction { - self.exec(inst); - } else { - self.status_bar.msg = "No previous command".into(); - self.redraw_task = RedrawTask::StatusBar; - } - } + (Command(Normal), Char('.')) => if let Some(inst) = self.previous_instruction { + self.exec(inst); + } else { + self.status_bar.msg = "No previous command".into(); + self.redraw_task = RedrawTask::StatusBar; + }, (Command(Normal), Char(c)) => { self.status_bar.msg = format!("Unknown command: {}", c); self.redraw_task = RedrawTask::StatusBar; @@ -296,7 +273,7 @@ impl Editor { (Primitive(Insert(opt)), k) => self.insert(k, opt), (Primitive(Prompt), Char('\n')) => { self.cursor_mut().mode = Command(Normal); - if let Some(cmd) = PromptCommand::parse(&self.prompt.clone()) {; + if let Some(cmd) = PromptCommand::parse(&self.prompt.clone()) { self.invoke(cmd); self.prompt = String::new(); self.redraw_task = RedrawTask::StatusBar; diff --git a/src/core/prompt.rs b/src/core/prompt.rs index 51a4b72d00ce99de8e70702f8bedbaea5a36c9c1..dc585cbfc30b328643d2e086034611eea7ec439b 100644 --- a/src/core/prompt.rs +++ b/src/core/prompt.rs @@ -1,7 +1,7 @@ use io::file::FileStatus; use io::redraw::RedrawTask; use state::editor::{Buffer, BufferManager, Editor}; -use edit::buffer::{TextBuffer, SplitBuffer}; +use edit::buffer::{SplitBuffer, TextBuffer}; use std::process::exit; @@ -10,32 +10,32 @@ pub enum PromptCommand<'a> { /// Set an option. Set { /// The option to set. - option: &'a str + option: &'a str, }, /// Unset an option. Unset { /// The option to unset. - option: &'a str + option: &'a str, }, /// Toggle an option. Toggle { /// The option to toggle. - option: &'a str + option: &'a str, }, /// Get whether an option is set or not. Get { /// The option to get. - option: &'a str + option: &'a str, }, /// Open the specified file in a new buffer. Open { /// The path to open. - path: &'a str + path: &'a str, }, /// Write the current buffer to the specified path. Write { /// The path to write to. - path: &'a str + path: &'a str, }, /// List the available buffers. ListBuffers, @@ -44,7 +44,7 @@ pub enum PromptCommand<'a> { /// Switch to the nth buffer. SwitchToBuffer { /// The index of the buffer to switch to. - buffer_index: usize + buffer_index: usize, }, /// Display help in a new buffer. Help, @@ -77,12 +77,14 @@ impl<'a> PromptCommand<'a> { let rest: String = bn.chars().skip(1).collect(); if let Ok(number) = rest.parse::<usize>() { - SwitchToBuffer { buffer_index: number } + SwitchToBuffer { + buffer_index: number, + } } else { return None; } - }, - _ => return None + } + _ => return None, }) } } @@ -98,46 +100,45 @@ impl Editor { Ok(()) => format!("Option set: {}", option), Err(()) => format!("Option does not exist: {}", option), } - }, + } Unset { option } => { self.status_bar.msg = match self.options.unset(option) { Ok(()) => format!("Option unset: {}", option), Err(()) => format!("Option does not exist: {}", option), } - }, + } Toggle { option } => { self.status_bar.msg = match self.options.toggle(option) { Ok(()) => format!("Option toggled: {}", option), Err(()) => format!("Option does not exist: {}", option), } - }, + } Get { option } => { self.status_bar.msg = match self.options.get(option) { Some(true) => format!("Option set: {}", option), Some(false) => format!("Option unset: {}", option), None => format!("Option does not exist: {}", option), } - }, + } Open { path } => { self.status_bar.msg = match self.open(path) { FileStatus::NotFound => format!("File {} could not be opened", path), FileStatus::Ok => format!("File {} opened", path), _ => unreachable!(), } - }, + } Write { path } => { if self.options.get("readonly") == Some(true) { // TODO: add override (w!) self.status_bar.msg = format!("File {} is opened in readonly mode", path) - } - else { + } else { self.status_bar.msg = match self.write(path) { FileStatus::NotFound => format!("File {} could not be opened", path), FileStatus::Ok => format!("File {} written", path), FileStatus::Other => format!("Couldn't write {}", path), } } - }, + } ListBuffers => { let description = get_buffers_description(&self.buffers); let mut new_buffer: Buffer = SplitBuffer::from_str(&description).into(); @@ -147,27 +148,25 @@ impl Editor { let new_buffer_index = self.buffers.new_buffer(new_buffer); self.buffers.switch_to(new_buffer_index); self.redraw_task = RedrawTask::Full; - }, - SwitchToBuffer { buffer_index: ix } => { - if !self.buffers.is_buffer_index_valid(ix) { - self.status_bar.msg = format!("Invalid buffer #{}", ix); - } else { - self.buffers.switch_to(ix); - self.redraw_task = RedrawTask::Full; - self.status_bar.msg = format!("Switched to buffer #{}", ix); - } + } + SwitchToBuffer { buffer_index: ix } => if !self.buffers.is_buffer_index_valid(ix) { + self.status_bar.msg = format!("Invalid buffer #{}", ix); + } else { + self.buffers.switch_to(ix); + self.redraw_task = RedrawTask::Full; + self.status_bar.msg = format!("Switched to buffer #{}", ix); }, DeleteBuffer => { let ix = self.buffers.current_buffer_index(); self.buffers.delete_buffer(ix); self.redraw_task = RedrawTask::Full; - }, + } Help => { self.open("/apps/sodium/help.txt"); - }, + } Quit => { exit(0); - }, + } } self.hint(); @@ -181,8 +180,7 @@ fn get_buffers_description(buffers: &BufferManager) -> String { format!("b{}\t\t\t{}", i, title) } - let descriptions = - buffers + let descriptions = buffers .iter() // don't include transient buffers like the one // this is going to be shown in @@ -192,5 +190,8 @@ fn get_buffers_description(buffers: &BufferManager) -> String { .collect::<Vec<_>>() .join("\n"); - format!("Buffers\n=====================================\n\n{}", descriptions) + format!( + "Buffers\n=====================================\n\n{}", + descriptions + ) } diff --git a/src/edit/buffer.rs b/src/edit/buffer.rs index b40f59c8d89a2fe832f8006bfe74e5fc8e168033..a553d839988c16d0e90e2f42f0fd86b495a83d6b 100644 --- a/src/edit/buffer.rs +++ b/src/edit/buffer.rs @@ -75,17 +75,18 @@ pub trait TextBuffer<'a> { pub struct SplitBuffer { before: Vec<String>, after: Vec<String>, - #[cfg(debug)] - _hinted_since_edit: bool, + #[cfg(debug)] _hinted_since_edit: bool, } impl SplitBuffer { fn up(&mut self) { - self.after.push(self.before.pop().expect("Popped last element")); + self.after + .push(self.before.pop().expect("Popped last element")); } fn down(&mut self) { - self.before.push(self.after.pop().expect("Popped last element")); + self.before + .push(self.after.pop().expect("Popped last element")); } fn y(&self) -> usize { @@ -207,7 +208,7 @@ impl<'a> TextBuffer<'a> for SplitBuffer { for c in ln.chars() { match c { '\t' | ' ' => len += 1, - _ => break, + _ => break, } } &ln[..len] @@ -226,7 +227,6 @@ impl Index<usize> for SplitBuffer { } } impl IndexMut<usize> for SplitBuffer { - fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut String { #[cfg(debug)] fn debug_check(b: &mut SplitBuffer) { diff --git a/src/edit/delete.rs b/src/edit/delete.rs index b294fcac729d1e6fe77f2ba7c94996ffea23df2f..80f29cbadade7330b3c98dc0ef78fa3d99cd48a9 100644 --- a/src/edit/delete.rs +++ b/src/edit/delete.rs @@ -7,7 +7,7 @@ impl Editor { /// Delete a character. #[inline] pub fn delete(&mut self) { - let &Cursor{ x, y, .. } = self.cursor(); + let &Cursor { x, y, .. } = self.cursor(); if x == self.buffers.current_buffer()[y].len() { if y + 1 < self.buffers.current_buffer().len() { let s = self.buffers.current_buffer_mut().remove_line(y + 1); diff --git a/src/edit/insert.rs b/src/edit/insert.rs index 5fe4b9a62619206ccedbf2dbcdfadfca07b23836..d793ceb99c56683fcb1bdc60eb0d93eb3c43cd0a 100644 --- a/src/edit/insert.rs +++ b/src/edit/insert.rs @@ -26,7 +26,7 @@ impl Editor { let (mut x, mut y) = self.pos(); match (mode, k) { (InsertMode::Insert, Key::Char('\n')) => { - let first_part = self.buffers.current_buffer()[y][..x].to_owned(); + let first_part = self.buffers.current_buffer()[y][..x].to_owned(); let second_part = self.buffers.current_buffer()[y][x..].to_owned(); self.buffers.current_buffer_mut()[y] = first_part; @@ -38,11 +38,13 @@ impl Editor { }; let begin = nl.len(); - self.buffers.current_buffer_mut().insert_line(y + 1, nl + &second_part); + self.buffers + .current_buffer_mut() + .insert_line(y + 1, nl + &second_part); self.redraw_task = RedrawTask::LinesAfter(y); self.goto((begin, y + 1)); - }, + } (InsertMode::Insert, Key::Backspace) => self.backspace(), (InsertMode::Insert, Key::Char(c)) => { self.buffers.current_buffer_mut()[y].insert(x, c); @@ -50,7 +52,7 @@ impl Editor { self.redraw_task = RedrawTask::Lines(y..y + 1); let right = self.right(1, false); self.goto(right); - }, + } (InsertMode::Replace, Key::Char(c)) => { if x == self.buffers.current_buffer()[y].len() { let next = self.next(1); @@ -77,8 +79,8 @@ impl Editor { self.goto(p); } self.redraw_task = RedrawTask::Lines(y..y + 1); - }, - _ => {}, + } + _ => {} } self.hint(); @@ -90,5 +92,4 @@ impl Editor { self.insert(Key::Char(c), opt); } } - } diff --git a/src/edit/selection.rs b/src/edit/selection.rs index 64143eacae66b508c55ab654326baa267e0f1fa0..96d2f49de99fb6cf901b881c68ec1f237e89d2c0 100644 --- a/src/edit/selection.rs +++ b/src/edit/selection.rs @@ -7,7 +7,7 @@ impl Editor { /// defines a position on the same line, only the characters from the current position to the /// motion's position are removed. pub fn remove_rb<'a>(&mut self, (x, y): (isize, isize)) { - if y == (self.y() as isize ) { + if y == (self.y() as isize) { let (x, y) = self.bound((x as usize, y as usize), false); // Single line mode let (a, b) = if self.x() > x { diff --git a/src/io/file.rs b/src/io/file.rs index c958694e25ce76ab2491e198aa091d2c6856ac74..655f491f7f8939a929f817563885232483f887fb 100644 --- a/src/io/file.rs +++ b/src/io/file.rs @@ -1,4 +1,4 @@ -use edit::buffer::{TextBuffer, SplitBuffer}; +use edit::buffer::{SplitBuffer, TextBuffer}; use state::editor::{Buffer, Editor}; use std::fs::File; use std::io::{Read, Write}; @@ -36,7 +36,9 @@ impl Editor { pub fn write(&mut self, path: &str) -> FileStatus { self.buffers.current_buffer_info_mut().title = Some(path.into()); if let Some(mut file) = File::create(path).ok() { - if file.write(self.buffers.current_buffer().to_string().as_bytes()).is_ok() { + if file.write(self.buffers.current_buffer().to_string().as_bytes()) + .is_ok() + { FileStatus::Ok } else { FileStatus::Other diff --git a/src/io/graphics.rs b/src/io/graphics.rs index 39ad71cf7856b63e1d672a4a2af99a7920beb6c4..e73010dac04c54ab2a31f6caa0060946d7674907 100644 --- a/src/io/graphics.rs +++ b/src/io/graphics.rs @@ -19,11 +19,13 @@ impl Editor { let w = self.window.width(); let h = self.window.height(); - if self.buffers.current_buffer_info().scroll_y > 0 && pos_y <= self.buffers.current_buffer_info().scroll_y { + if self.buffers.current_buffer_info().scroll_y > 0 + && pos_y <= self.buffers.current_buffer_info().scroll_y + { self.buffers.current_buffer_info_mut().scroll_y = pos_y - 1; } - let window_lines = (h as usize/16) - 2; + let window_lines = (h as usize / 16) - 2; if pos_y > self.buffers.current_buffer_info().scroll_y + window_lines { self.buffers.current_buffer_info_mut().scroll_y = pos_y - window_lines; @@ -34,31 +36,32 @@ impl Editor { (current_buffer.scroll_x, current_buffer.scroll_y) }; - + // Redraw window self.window.set(Color::rgb(25, 25, 25)); if self.options.line_marker { - self.window.rect(0, - (pos_y - scroll_y) as i32 * 16, - w, - 16, - Color::rgb(45, 45, 45)); + self.window.rect( + 0, + (pos_y - scroll_y) as i32 * 16, + w, + 16, + Color::rgb(45, 45, 45), + ); } - self.window.rect(8 * (pos_x - scroll_x) as i32, - 16 * (pos_y - scroll_y) as i32, - 8, - 16, - Color::rgb(255, 255, 255)); + self.window.rect( + 8 * (pos_x - scroll_x) as i32, + 16 * (pos_y - scroll_y) as i32, + 8, + 16, + Color::rgb(255, 255, 255), + ); let mut string = false; - for (y, row) in self.buffers - .current_buffer() - .lines() - .enumerate() { + for (y, row) in self.buffers.current_buffer().lines().enumerate() { for (x, c) in row.chars().enumerate() { // TODO: Move outta here let color = if self.options.highlight { @@ -68,8 +71,22 @@ impl Editor { (226, 225, 167) //(167, 222, 156) } _ if string => (226, 225, 167), //(167, 222, 156) - '!' | '@' | '#' | '$' | '%' | '^' | '&' | '|' | '*' | '+' | '-' | '/' | - ':' | '=' | '<' | '>' => (198, 83, 83), //(228, 190, 175), //(194, 106, 71), + '!' | + '@' | + '#' | + '$' | + '%' | + '^' | + '&' | + '|' | + '*' | + '+' | + '-' | + '/' | + ':' | + '=' | + '<' | + '>' => (198, 83, 83), //(228, 190, 175), //(194, 106, 71), '.' | ',' => (241, 213, 226), '(' | ')' | '[' | ']' | '{' | '}' => (164, 212, 125), //(195, 139, 75), '0'...'9' => (209, 209, 177), @@ -78,19 +95,23 @@ impl Editor { } else { (255, 255, 255) }; - + let c = if c == '\t' { ' ' } else { c }; if pos_x == x && pos_y == y { - self.window.char(8 * (x as isize - scroll_x as isize) as i32, - 16 * (y as isize - scroll_y as isize) as i32, - c, - Color::rgb(color.0 / 3, color.1 / 3, color.2 / 3)); + self.window.char( + 8 * (x as isize - scroll_x as isize) as i32, + 16 * (y as isize - scroll_y as isize) as i32, + c, + Color::rgb(color.0 / 3, color.1 / 3, color.2 / 3), + ); } else { - self.window.char(8 * (x as isize - scroll_x as isize) as i32, - 16 * (y as isize - scroll_y as isize) as i32, - c, - Color::rgb(color.0, color.1, color.2)); + self.window.char( + 8 * (x as isize - scroll_x as isize) as i32, + 16 * (y as isize - scroll_y as isize) as i32, + c, + Color::rgb(color.0, color.1, color.2), + ); } } } @@ -107,26 +128,29 @@ impl Editor { let h = self.window.height(); let w = self.window.width(); let mode = self.cursor().mode; - self.window.rect(0, - h as i32 - 18 - - { - if mode == Mode::Primitive(PrimitiveMode::Prompt) { - 18 - } else { - 0 - } - }, - w, - 18, - Color::rgba(74, 74, 74, 255)); + self.window.rect( + 0, + h as i32 - 18 - { + if mode == Mode::Primitive(PrimitiveMode::Prompt) { + 18 + } else { + 0 + } + }, + w, + 18, + Color::rgba(74, 74, 74, 255), + ); self.draw_status_bar(); for (n, c) in self.prompt.chars().enumerate() { - self.window.char(n as i32 * 8, - h as i32 - 16 - 1, - c, - Color::rgb(255, 255, 255)); + self.window.char( + n as i32 * 8, + h as i32 - 16 - 1, + c, + Color::rgb(255, 255, 255), + ); } self.window.sync(); @@ -146,34 +170,36 @@ impl Editor { .map(|s| s.as_str()) .unwrap_or(""); - let items = [(self.status_bar.mode, 0, 4), - (current_title, 1, 4), - (&self.status_bar.cmd, 2, 4), - (&self.status_bar.msg, 3, 4)]; + let items = [ + (self.status_bar.mode, 0, 4), + (current_title, 1, 4), + (&self.status_bar.cmd, 2, 4), + (&self.status_bar.msg, 3, 4), + ]; for &(text, a, b) in items.iter() { for (n, c) in (if text.len() as u32 > w / (8 * b) { - text.chars() - .take((w / (8 * b) - 5) as usize) - .chain(vec!['.'; 3]) - .collect::<Vec<_>>() - } else { - text.chars().collect() - }) - .into_iter() - .enumerate() { - - self.window.char(((w * a) / b) as i32 + (n as i32 * 8), - h as i32 - 16 - 1 - - { - if mode == Mode::Primitive(PrimitiveMode::Prompt) { - 16 + 1 + 1 - } else { - 0 - } - }, - c, - Color::rgb(255, 255, 255)); + text.chars() + .take((w / (8 * b) - 5) as usize) + .chain(vec!['.'; 3]) + .collect::<Vec<_>>() + } else { + text.chars().collect() + }).into_iter() + .enumerate() + { + self.window.char( + ((w * a) / b) as i32 + (n as i32 * 8), + h as i32 - 16 - 1 - { + if mode == Mode::Primitive(PrimitiveMode::Prompt) { + 16 + 1 + 1 + } else { + 0 + } + }, + c, + Color::rgb(255, 255, 255), + ); } } } diff --git a/src/io/key.rs b/src/io/key.rs index 6216d264aa6b40f32ebb22cf8bd41d5f89f0b728..38fa25e327dcad147b3c58a44fc2cd7ac90aed2f 100644 --- a/src/io/key.rs +++ b/src/io/key.rs @@ -1,14 +1,5 @@ #[cfg(feature = "orbital")] -use orbclient::{ - KeyEvent, - K_BKSP, - K_LEFT, - K_RIGHT, - K_UP, - K_DOWN, - K_TAB, - K_ESC, -}; +use orbclient::{KeyEvent, K_BKSP, K_DOWN, K_ESC, K_LEFT, K_RIGHT, K_TAB, K_UP}; #[derive(Copy, Clone, PartialEq)] /// A key @@ -54,7 +45,7 @@ impl Key { s => match k.character { '\0' => Key::Unknown(s), c => Key::Char(c), - } + }, } } else { Key::Null diff --git a/src/io/parse.rs b/src/io/parse.rs index 76478dae3f25af1cc0455de792f8122f093b38d5..f544d2bc0a643e09053b445b421fd9d4b5c1b864 100644 --- a/src/io/parse.rs +++ b/src/io/parse.rs @@ -46,13 +46,11 @@ impl Editor { loop { for event in self.window.events() { match event.to_option() { - EventOption::Key(k) => { - if let Some(Key::Char(c)) = self.key_state.feed(k) { - self.status_bar.cmd.push(c); - self.redraw_task = RedrawTask::StatusBar; - return c; - } - } + EventOption::Key(k) => if let Some(Key::Char(c)) = self.key_state.feed(k) { + self.status_bar.cmd.push(c); + self.redraw_task = RedrawTask::StatusBar; + return c; + }, _ => {} } } @@ -93,24 +91,24 @@ impl Editor { n * 10 + ((c as u8) - b'0') as usize } _ => { - key = k; n } }; } - } } match key { Key::Null => {} _ => { - return Inst(if unset { - Parameter::Null - } else { - Parameter::Int(n) - }, - Cmd { key: key }); + return Inst( + if unset { + Parameter::Null + } else { + Parameter::Int(n) + }, + Cmd { key: key }, + ); } } } @@ -124,6 +122,5 @@ impl Editor { } #[cfg(not(feature = "orbital"))] Inst(Parameter::Null, Cmd { key: Key::Null }) - } } diff --git a/src/state/cursor.rs b/src/state/cursor.rs index ab227740f6f853ae084b4f8cfe61a8bab501a57c..d61ff04bc0d4f200080fef6b890f69fdaff6424b 100644 --- a/src/state/cursor.rs +++ b/src/state/cursor.rs @@ -1,5 +1,5 @@ use state::editor::Editor; -use state::mode::{Mode, CommandMode}; +use state::mode::{CommandMode, Mode}; #[derive(Clone)] /// A cursor, i.e. a state defining a mode, and a position. The cursor does not define the content @@ -46,15 +46,18 @@ impl Editor { #[inline] pub fn cursor_mut(&mut self) -> &mut Cursor { let buffer = self.buffers.current_buffer_info_mut(); - buffer.cursors.get_mut(buffer.current_cursor as usize).unwrap() + buffer + .cursors + .get_mut(buffer.current_cursor as usize) + .unwrap() } /// Go to next cursor #[inline] pub fn next_cursor(&mut self) { let buffer = self.buffers.current_buffer_info_mut(); - buffer.current_cursor = (buffer.current_cursor.wrapping_add(1)) % - (buffer.cursors.len() as u8); + buffer.current_cursor = + (buffer.current_cursor.wrapping_add(1)) % (buffer.cursors.len() as u8); } /// Go to previous cursor diff --git a/src/state/editor.rs b/src/state/editor.rs index 5fc98ca5eb6ce605eb08a7ee6b1eb10417f63cfd..5950e441bd0642c3e0ff2f9eb9b8a7877758cc2b 100644 --- a/src/state/editor.rs +++ b/src/state/editor.rs @@ -1,8 +1,8 @@ use std::slice::Iter; -use edit::buffer::{TextBuffer, SplitBuffer}; +use edit::buffer::{SplitBuffer, TextBuffer}; use state::cursor::Cursor; use io::graphics::StatusBar; -use io::key::{Key, Cmd}; +use io::key::{Cmd, Key}; use io::key_state::KeyState; use state::options::Options; use io::parse::Inst; @@ -148,7 +148,7 @@ impl BufferManager { self.buffers.push(Buffer::new()); self.current_buffer_index = 0; } else if self.current_buffer_index <= n { - self.current_buffer_index -= 1; + self.current_buffer_index -= 1; } } @@ -182,9 +182,9 @@ pub struct Editor { impl Editor { /// Create new default state editor pub fn init() { - #[cfg(feature = "orbital")] - let window = Window::new_flags(-1, -1, 700, 500, &"Sodium", &[WindowFlag::Resizable]).unwrap(); + let window = + Window::new_flags(-1, -1, 700, 500, &"Sodium", &[WindowFlag::Resizable]).unwrap(); #[cfg(feature = "orbital")] let mut editor = Editor { @@ -210,23 +210,26 @@ impl Editor { }; let mut files: Vec<String> = Vec::new(); - + let mut args_iter = args().skip(1).peekable(); loop { let arg = match args_iter.next() { Some(x) => x, - None => break + None => break, }; match arg.as_str() { "--version" => { - println!("Sodium {}", option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")); + println!( + "Sodium {}", + option_env!("CARGO_PKG_VERSION").unwrap_or("unknown") + ); return; - }, + } "--help" => { println!("{}", HELP); return; - }, + } "-u" => { unimplemented!(); /* @@ -255,16 +258,14 @@ impl Editor { if arg_chars.next() == Some('-') { for ch in arg_chars { match ch { - 'R' => { - match editor.options.set("readonly") { - Ok(_) => debugln!(editor, "Set readonly mode"), - Err(_) => println!("Could not set readonly mode") - } + 'R' => match editor.options.set("readonly") { + Ok(_) => debugln!(editor, "Set readonly mode"), + Err(_) => println!("Could not set readonly mode"), }, 'h' => { println!("{}", HELP); return; - }, + } _ => { unimplemented!(); } @@ -283,7 +284,7 @@ impl Editor { if files.len() > 0 { // TODO: open multiple files into separate buffers editor.open(&files[0]); - } + } debugln!(editor, "Starting Sodium");