forked from mars/breed
Add line command
This commit is contained in:
parent
1f33402a42
commit
afaf3ab0fe
|
@ -33,7 +33,7 @@ use crate::{Cursor, Direction};
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Buffer {
|
pub struct Buffer {
|
||||||
styles: Arc<Mutex<StyleStore>>,
|
styles: Arc<Mutex<StyleStore>>,
|
||||||
pub text: Rope,
|
text: Rope,
|
||||||
syntax_set: Arc<SyntaxSet>,
|
syntax_set: Arc<SyntaxSet>,
|
||||||
parser: ParseState,
|
parser: ParseState,
|
||||||
style_dirty: bool,
|
style_dirty: bool,
|
||||||
|
@ -41,6 +41,12 @@ pub struct Buffer {
|
||||||
styled: Vec<Vec<(Style, Range<usize>)>>,
|
styled: Vec<Vec<(Style, Range<usize>)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsRef<Rope> for Buffer {
|
||||||
|
fn as_ref(&self) -> &Rope {
|
||||||
|
&self.text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Buffer {
|
impl Buffer {
|
||||||
pub fn from_str(styles: Arc<Mutex<StyleStore>>, text: &str) -> Self {
|
pub fn from_str(styles: Arc<Mutex<StyleStore>>, text: &str) -> Self {
|
||||||
let syntax_set = SyntaxSet::load_defaults_nonewlines();
|
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<()> {
|
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)?;
|
let mut handle = OpenOptions::new().write(true).open(file)?;
|
||||||
|
|
||||||
handle.write_all(out.as_slice())?;
|
handle.write_all(out.as_slice())?;
|
||||||
|
@ -364,9 +364,16 @@ impl State {
|
||||||
self.write_command(command, args)?;
|
self.write_command(command, args)?;
|
||||||
self.quit = true;
|
self.quit = true;
|
||||||
}
|
}
|
||||||
command => {
|
command => match command.parse::<usize>() {
|
||||||
return Err(format!("{}: Unrecognized command.", command));
|
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(())
|
Ok(())
|
||||||
|
@ -374,7 +381,10 @@ impl State {
|
||||||
|
|
||||||
fn move_cursor(&mut self, direction: Direction) {
|
fn move_cursor(&mut self, direction: Direction) {
|
||||||
self.buffer.move_cursor(&mut self.cursor, direction, true);
|
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 {
|
if self.cursor.column < self.scroll.column + 3 {
|
||||||
self.scroll.column = self.cursor.column.saturating_sub(3);
|
self.scroll.column = self.cursor.column.saturating_sub(3);
|
||||||
} else if self.cursor.column + 6 >= self.scroll.column + self.size.0 {
|
} else if self.cursor.column + 6 >= self.scroll.column + self.size.0 {
|
||||||
|
|
Reference in New Issue