@@ -3,7 +3,7 @@ use crossterm::{
33 cursor::position,
44 event::{self, read, Event, EventStream, KeyCode, KeyEvent},
55 execute, queue,
6− style::Print,
6+ style::{Color, Print, SetForegroundColor},
77 terminal::{self, disable_raw_mode, enable_raw_mode},
88};
99use futures::{future::FutureExt, select, StreamExt};
@@ -16,25 +16,37 @@ use anyhow::Error;
1616use crate::{keymap, Args};
1717use helix_core::{Buffer, State};
1818
19−pub struct View<'a> {
19+pub struct BufferComponent<'a> {
2020 x: u16,
2121 y: u16,
22− contents: &'a str,
22+ contents: Vec<&'a str>,
2323}
2424
25−impl View<'_> {
25+impl BufferComponent<'_> {
2626 pub fn render(&self) {
27− execute!(
28− stdout(),
29− cursor::MoveTo(self.x, self.y),
30− Print(self.contents)
31− );
27+ let mut line_count = 0;
28+ for line in &self.contents {
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+ }
3244 }
3345}
3446
3547pub struct Editor {
3648 state: Option<State>,
37− current_line: u16,
49+ first_line: u16,
3850 size: (u16, u16),
3951}
4052
@@ -42,7 +54,7 @@ impl Editor {
4254 pub fn new(mut args: Args) -> Result<Self, Error> {
4355 let mut editor = Editor {
4456 state: None,
45− current_line: 0,
57+ first_line: 0,
4658 size: terminal::size().unwrap(),
4759 };
4860
@@ -65,19 +77,17 @@ impl Editor {
6577
6678 match &self.state {
6779 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− }
80+ let view = BufferComponent {
81+ x: 0,
82+ y: self.first_line,
83+ contents: s
84+ .file()
85+ .lines_at(self.first_line as usize)
86+ .take(self.size.1 as usize)
87+ .map(|x| x.as_str().unwrap())
88+ .collect::<Vec<&str>>(),
89+ };
90+ view.render();
8191 }
8292 None => (),
8393 }