diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 30bc041..03b9083 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -270,10 +270,17 @@ impl Buffer { /// If the index is off the end of the text, returns the line and column /// number of the last valid text position. pub fn index_to_line_col(&self, pos: usize) -> (usize, usize) { - let line = self.text.char_to_line(pos); - let line_pos = self.text.line_to_char(line); + if pos < self.text.len_chars() { + let line = self.text.char_to_line(pos); + let line_pos = self.text.line_to_char(line); - return (line, pos - line_pos); + return (line, pos - line_pos); + } else { + let line = self.text.len_lines() - 1; + let line_pos = self.text.line_to_char(line); + + return (line, pos - line_pos); + } } /// Converts a line number and char-column number into a char diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 18fd2a7..b4e38c8 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -99,7 +99,10 @@ impl Editor { for line in self.buffer.line_iter() { // Get the line ending let ending = if line.len_chars() > 0 { - let g = line.slice(line.len_chars() - 1, line.len_chars()).graphemes().nth(0).unwrap(); + let g = line.slice(line.len_chars() - 1, line.len_chars()) + .graphemes() + .nth(0) + .unwrap(); str_to_line_ending(g) } else { LineEnding::None diff --git a/src/string_utils.rs b/src/string_utils.rs index 2dc36bb..dfe7e4c 100644 --- a/src/string_utils.rs +++ b/src/string_utils.rs @@ -39,9 +39,8 @@ pub fn is_whitespace(text: &str) -> bool { | "\u{205F}" // MEDIUM MATHEMATICAL SPACE | "\u{3000}" // IDEOGRAPHIC SPACE | "\u{FEFF}" // ZERO WIDTH NO-BREAK SPACE - => true, - - _ => false + => true, + _ => false, } } diff --git a/src/term_ui/mod.rs b/src/term_ui/mod.rs index fd1db4f..b25c3b3 100644 --- a/src/term_ui/mod.rs +++ b/src/term_ui/mod.rs @@ -44,7 +44,7 @@ pub struct TermUI { impl TermUI { pub fn new() -> TermUI { let rb = match rustbox::RustBox::init(rustbox::InitOptions { - buffer_stderr: false, + buffer_stderr: true, ..Default::default() }) { Ok(rbox) => rbox, @@ -65,7 +65,7 @@ impl TermUI { pub fn new_from_editor(ed: Editor) -> TermUI { let rb = match rustbox::RustBox::init(rustbox::InitOptions { - buffer_stderr: false, + buffer_stderr: true, ..Default::default() }) { Ok(rbox) => rbox, @@ -406,13 +406,15 @@ impl TermUI { .line_col_to_index((line_index, line_block_index * LINE_BLOCK_LENGTH)); let temp_line = editor.buffer.get_line(line_index); let (vis_line_offset, _) = editor.formatter.index_to_v2d( - temp_line.slice( - line_block_index * LINE_BLOCK_LENGTH, - min( - temp_line.len_chars(), - (line_block_index + 1) * LINE_BLOCK_LENGTH, - ), - ).graphemes(), + temp_line + .slice( + line_block_index * LINE_BLOCK_LENGTH, + min( + temp_line.len_chars(), + (line_block_index + 1) * LINE_BLOCK_LENGTH, + ), + ) + .graphemes(), editor.view_pos.0 - grapheme_index, ); @@ -451,9 +453,10 @@ impl TermUI { let mut last_pos_y = 0; let mut lines_traversed: usize = 0; let line_len = line.len_chars(); - let mut g_iter = editor - .formatter - .iter(line.slice(line_block_index * LINE_BLOCK_LENGTH, line_len).graphemes()); + let mut g_iter = editor.formatter.iter(line.slice( + line_block_index * LINE_BLOCK_LENGTH, + line_len, + ).graphemes()); loop { if let Some((g, (pos_y, pos_x), width)) = g_iter.next() { @@ -552,9 +555,10 @@ impl TermUI { line_block_index += 1; line_g_index = 0; let line_len = line.len_chars(); - g_iter = editor - .formatter - .iter(line.slice(line_block_index * LINE_BLOCK_LENGTH, line_len).graphemes()); + g_iter = editor.formatter.iter(line.slice( + line_block_index * LINE_BLOCK_LENGTH, + line_len, + ).graphemes()); lines_traversed += 1; } }