Skip to content

Commit

Permalink
line numbers added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed May 15, 2024
1 parent 8b8d9da commit ee15f97
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Default for App {
fn default() -> Self {
Self {
running: true,
content: Vec::new(),
content: vec![String::new()],
cursor_position: Position { x: 0, y: 0 },
cursor_offset: Position { x: 0, y: 0 },
opened_filename: String::new(),
Expand Down
23 changes: 21 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::app::App;

pub fn render(app: &mut App, frame: &mut Frame) {
let content_lines: Vec<Line> = app.content.iter().map(|s| s.as_str().into()).collect();
let line_numbers: Vec<Line> = (1..=app.content.len())
.map(|i| Line::from(format!("{:>4} ", i)))
.collect();

let filename_status = Line::from(format!("Filename: {}", app.opened_filename))
.left_aligned()
Expand All @@ -33,6 +36,11 @@ pub fn render(app: &mut App, frame: &mut Frame) {
])
.split(frame.size());

let content_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Length(5), Constraint::Min(0)])
.split(layout[1]);

let status_bar_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
Expand All @@ -52,6 +60,17 @@ pub fn render(app: &mut App, frame: &mut Frame) {
layout[0],
);

frame.render_widget(
Paragraph::new(line_numbers)
.style(
Style::default()
.fg(Color::Rgb(64, 96, 128))
.bg(Color::Rgb(32, 32, 64)),
)
.scroll((app.cursor_offset.y as u16, app.cursor_offset.x as u16)),
content_layout[0],
);

frame.render_widget(
Paragraph::new(content_lines)
.style(
Expand All @@ -60,7 +79,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
.bg(Color::Rgb(32, 32, 64)),
)
.scroll((app.cursor_offset.y as u16, app.cursor_offset.x as u16)),
layout[1],
content_layout[1],
);

frame.render_widget(
Expand All @@ -84,7 +103,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
frame.render_widget(Line::from("Press Ctrl + C to quit").centered(), layout[3]);

frame.set_cursor(
app.cursor_position.x as u16,
app.cursor_position.x as u16 + 5,
app.cursor_position.y as u16 + 1,
);
}

0 comments on commit ee15f97

Please sign in to comment.