on master help sign in
created view struct
e93b15cJan Hrastnik
helix-core/src/state.rs+1−2
@@ -120,8 +120,7 @@ impl State {
120
120
121 pub fn file(&self) -> &Rope {
121 pub fn file(&self) -> &Rope {
122 // used to access file contents for rendering to screen
122 // used to access file contents for rendering to screen
123− let copy = &self.doc.contents;
123+ &self.doc.contents
124− copy
125 }
124 }
126}
125}
127
126
helix-term/src/editor.rs+40−3
@@ -16,13 +16,35 @@ use anyhow::Error;
16use crate::{keymap, Args};
16use crate::{keymap, Args};
17use helix_core::{Buffer, State};
17use helix_core::{Buffer, State};
18
18
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+
19pub struct Editor {
35pub struct Editor {
20 state: Option<State>,
36 state: Option<State>,
37+ current_line: u16,
38+ size: (u16, u16),
21}
39}
22
40
23impl Editor {
41impl Editor {
24 pub fn new(mut args: Args) -> Result<Self, Error> {
42 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+ };
26
48
27 if let Some(file) = args.files.pop() {
49 if let Some(file) = args.files.pop() {
28 editor.open(file)?;
50 editor.open(file)?;
@@ -38,10 +60,25 @@ impl Editor {
38 Ok(())
60 Ok(())
39 }
61 }
40
62
41− fn render(&self) {
63+ fn render(&mut self) {
42 // TODO:
64 // TODO:
65+
43 match &self.state {
66 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+ }
45 None => (),
82 None => (),
46 }
83 }
47 }
84 }