Add line command
This commit is contained in:
parent
1f33402a42
commit
afaf3ab0fe
|
@ -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();
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue