on master help sign in
created view struct
commite93b15cef3d9274a95ac4342e777d240856e5803 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateFri 19 Jun 2020, 02:14 (6 years ago) parent8958f06
2 files changed+41−5
helix-core/src/state.rs+1−2 helix-term/src/editor.rs+40−3
helix-core/src/state.rs+1−2
@@ -120,8 +120,7 @@ impl State {
120120
121121 pub fn file(&self) -> &Rope {
122122 // used to access file contents for rendering to screen
123− let copy = &self.doc.contents;
124− copy
123+ &self.doc.contents
125124 }
126125}
127126
helix-term/src/editor.rs+40−3
@@ -16,13 +16,35 @@ use anyhow::Error;
1616use crate::{keymap, Args};
1717use helix_core::{Buffer, State};
1818
19+pub struct View<'a> {
20+ x: u16,
21+ y: u16,
22+ contents: &'a str,
23+}
24+
25+impl View<'_> {
26+ pub fn render(&self) {
27+ execute!(
28+ stdout(),
29+ cursor::MoveTo(self.x, self.y),
30+ Print(self.contents)
31+ );
32+ }
33+}
34+
1935pub struct Editor {
2036 state: Option<State>,
37+ current_line: u16,
38+ size: (u16, u16),
2139}
2240
2341impl Editor {
2442 pub fn new(mut args: Args) -> Result<Self, Error> {
25− let mut editor = Editor { state: None };
43+ let mut editor = Editor {
44+ state: None,
45+ current_line: 0,
46+ size: terminal::size().unwrap(),
47+ };
2648
2749 if let Some(file) = args.files.pop() {
2850 editor.open(file)?;
@@ -38,10 +60,25 @@ impl Editor {
3860 Ok(())
3961 }
4062
41− fn render(&self) {
63+ fn render(&mut self) {
4264 // TODO:
65+
4366 match &self.state {
44− Some(s) => execute!(stdout(), cursor::MoveTo(0, 0), Print(s.file())).unwrap(),
67+ Some(s) => {
68+ for line in s
69+ .file()
70+ .lines_at(self.current_line as usize)
71+ .take(self.size.1 as usize)
72+ {
73+ let view = View {
74+ x: 0,
75+ y: self.current_line,
76+ contents: line.as_str().unwrap(),
77+ };
78+ view.render();
79+ self.current_line += 1;
80+ }
81+ }
4582 None => (),
4683 }
4784 }