on master help sign in
file now rendered line by line
c3a23a1Jan Hrastnik
helix-term/src/editor.rs+34−24
@@ -3,7 +3,7 @@ use crossterm::{
3 cursor::position,
3 cursor::position,
4 event::{self, read, Event, EventStream, KeyCode, KeyEvent},
4 event::{self, read, Event, EventStream, KeyCode, KeyEvent},
5 execute, queue,
5 execute, queue,
6− style::Print,
6+ style::{Color, Print, SetForegroundColor},
7 terminal::{self, disable_raw_mode, enable_raw_mode},
7 terminal::{self, disable_raw_mode, enable_raw_mode},
8};
8};
9use futures::{future::FutureExt, select, StreamExt};
9use futures::{future::FutureExt, select, StreamExt};
@@ -16,25 +16,37 @@ 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> {
19+pub struct BufferComponent<'a> {
20 x: u16,
20 x: u16,
21 y: u16,
21 y: u16,
22− contents: &'a str,
22+ contents: Vec<&'a str>,
23}
23}
24
24
25−impl View<'_> {
25+impl BufferComponent<'_> {
26 pub fn render(&self) {
26 pub fn render(&self) {
27− execute!(
27+ let mut line_count = 0;
28− stdout(),
29− cursor::MoveTo(self.x, self.y),
30− Print(self.contents)
28+ for line in &self.contents {
31− );
29+ execute!(
30+ stdout(),
31+ SetForegroundColor(Color::DarkCyan),
32+ cursor::MoveTo(self.x, self.y + line_count),
33+ Print((line_count + 1).to_string())
34+ );
35+ execute!(
36+ stdout(),
37+ SetForegroundColor(Color::Reset),
38+ cursor::MoveTo(self.x + 2, self.y + line_count),
39+ Print(line)
40+ )
41+ .unwrap();
42+ line_count += 1;
43+ }
32 }
44 }
33}
45}
34
46
35pub struct Editor {
47pub struct Editor {
36 state: Option<State>,
48 state: Option<State>,
37− current_line: u16,
49+ first_line: u16,
38 size: (u16, u16),
50 size: (u16, u16),
39}
51}
40
52
@@ -42,7 +54,7 @@ impl Editor {
42 pub fn new(mut args: Args) -> Result<Self, Error> {
54 pub fn new(mut args: Args) -> Result<Self, Error> {
43 let mut editor = Editor {
55 let mut editor = Editor {
44 state: None,
56 state: None,
45− current_line: 0,
57+ first_line: 0,
46 size: terminal::size().unwrap(),
58 size: terminal::size().unwrap(),
47 };
59 };
48
60
@@ -65,19 +77,17 @@ impl Editor {
65
77
66 match &self.state {
78 match &self.state {
67 Some(s) => {
79 Some(s) => {
68− for line in s
80+ let view = BufferComponent {
81+ x: 0,
82+ y: self.first_line,
83+ contents: s
69− .file()
84+ .file()
70− .lines_at(self.current_line as usize)
85+ .lines_at(self.first_line as usize)
71− .take(self.size.1 as usize)
86+ .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(),
87+ .map(|x| x.as_str().unwrap())
88+ .collect::<Vec<&str>>(),
77− };
89+ };
78− view.render();
90+ view.render();
79− self.current_line += 1;
80− }
81 }
91 }
82 None => (),
92 None => (),
83 }
93 }