created view struct

imgbot
Jan Hrastnik 4 years ago
parent 8958f06f08
commit e93b15cef3

@ -120,8 +120,7 @@ impl State {
pub fn file(&self) -> &Rope { pub fn file(&self) -> &Rope {
// used to access file contents for rendering to screen // used to access file contents for rendering to screen
let copy = &self.doc.contents; &self.doc.contents
copy
} }
} }

@ -16,13 +16,35 @@ use anyhow::Error;
use crate::{keymap, Args}; use crate::{keymap, Args};
use helix_core::{Buffer, State}; use helix_core::{Buffer, State};
pub struct View<'a> {
x: u16,
y: u16,
contents: &'a str,
}
impl View<'_> {
pub fn render(&self) {
execute!(
stdout(),
cursor::MoveTo(self.x, self.y),
Print(self.contents)
);
}
}
pub struct Editor { pub struct Editor {
state: Option<State>, state: Option<State>,
current_line: u16,
size: (u16, u16),
} }
impl Editor { impl Editor {
pub fn new(mut args: Args) -> Result<Self, Error> { pub fn new(mut args: Args) -> Result<Self, Error> {
let mut editor = Editor { state: None }; let mut editor = Editor {
state: None,
current_line: 0,
size: terminal::size().unwrap(),
};
if let Some(file) = args.files.pop() { if let Some(file) = args.files.pop() {
editor.open(file)?; editor.open(file)?;
@ -38,10 +60,25 @@ impl Editor {
Ok(()) Ok(())
} }
fn render(&self) { fn render(&mut self) {
// TODO: // TODO:
match &self.state { match &self.state {
Some(s) => execute!(stdout(), cursor::MoveTo(0, 0), Print(s.file())).unwrap(), Some(s) => {
for line in s
.file()
.lines_at(self.current_line as usize)
.take(self.size.1 as usize)
{
let view = View {
x: 0,
y: self.current_line,
contents: line.as_str().unwrap(),
};
view.render();
self.current_line += 1;
}
}
None => (), None => (),
} }
} }

Loading…
Cancel
Save