Add line command

This commit is contained in:
mars 2023-04-12 17:39:46 -04:00
parent 1f33402a42
commit afaf3ab0fe
2 changed files with 21 additions and 5 deletions

View File

@ -33,7 +33,7 @@ use crate::{Cursor, Direction};
#[derive(Clone, Debug)]
pub struct Buffer {
styles: Arc<Mutex<StyleStore>>,
pub text: Rope,
text: Rope,
syntax_set: Arc<SyntaxSet>,
parser: ParseState,
style_dirty: bool,
@ -41,6 +41,12 @@ pub struct Buffer {
styled: Vec<Vec<(Style, Range<usize>)>>,
}
impl AsRef<Rope> for Buffer {
fn as_ref(&self) -> &Rope {
&self.text
}
}
impl Buffer {
pub fn from_str(styles: Arc<Mutex<StyleStore>>, text: &str) -> Self {
let syntax_set = SyntaxSet::load_defaults_nonewlines();

View File

@ -325,7 +325,7 @@ impl State {
}
fn write_buffer(&mut self, file: OsString) -> Result<()> {
let out = self.buffer.text.bytes().collect::<Vec<u8>>();
let out = self.buffer.as_ref().bytes().collect::<Vec<u8>>();
let mut handle = OpenOptions::new().write(true).open(file)?;
handle.write_all(out.as_slice())?;
@ -364,9 +364,16 @@ impl State {
self.write_command(command, args)?;
self.quit = true;
}
command => {
return Err(format!("{}: Unrecognized command.", command));
}
command => match command.parse::<usize>() {
Err(_) => return Err(format!("{}: Unrecognized command.", command)),
Ok(line) if line >= self.buffer.as_ref().len_lines() => {
return Err(format!("Line {} is out-of-bounds", line))
}
Ok(line) => {
self.cursor.line = line;
self.scroll_to_cursor();
}
},
}
Ok(())
@ -374,7 +381,10 @@ impl State {
fn move_cursor(&mut self, direction: Direction) {
self.buffer.move_cursor(&mut self.cursor, direction, true);
self.scroll_to_cursor();
}
fn scroll_to_cursor(&mut self) {
if self.cursor.column < self.scroll.column + 3 {
self.scroll.column = self.cursor.column.saturating_sub(3);
} else if self.cursor.column + 6 >= self.scroll.column + self.size.0 {