Skip to content

Commit

Permalink
ui update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed May 9, 2024
1 parent 7a24782 commit 8860b52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn main() -> AppResult<()> {

while app.running {
tui.draw(&mut app)?;

match tui.events.next().await? {
Event::Tick => app.tick(),
Event::Key(key_event) => handle_key_events(key_event, &mut app)?,
Expand Down
49 changes: 35 additions & 14 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
use ratatui::{
layout::Alignment,
layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Style},
text::Line,
widgets::{Block, BorderType, Paragraph},
widgets::{Block, Borders, Paragraph},
Frame,
};

use crate::app::App;

pub fn render(app: &mut App, frame: &mut Frame) {
let mut content_lines: Vec<Line> = app.content.iter().map(|s| s.as_str().into()).collect();
let content_lines: Vec<Line> = app.content.iter().map(|s| s.as_str().into()).collect();

content_lines.push(Line::from(format!("{}", app.cursor_position.x)));
content_lines.push(Line::from(format!("{}", app.cursor_position.y)));
let status_bar = Line::from(format!(
"{}:{}",
app.cursor_position.y + 1,
app.cursor_position.x + 1
))
.right_aligned();

let layout = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1),
Constraint::Min(0),
Constraint::Length(1),
])
.split(frame.size());

frame.render_widget(
Paragraph::new(content_lines)
.block(
Block::bordered()
.title("RustEdit")
.title_alignment(Alignment::Center)
.border_type(BorderType::Rounded),
)
Block::new()
.borders(Borders::TOP)
.title("RustEdit")
.title_alignment(Alignment::Center)
.style(Style::default().fg(Color::LightBlue).bg(Color::Black)),
frame.size(),
layout[0],
);

frame.render_widget(
Paragraph::new(content_lines).style(Style::default().fg(Color::LightBlue).bg(Color::Black)),
layout[1],
);

frame.render_widget(
Paragraph::new(status_bar).style(Style::default().fg(Color::LightCyan).bg(Color::Blue)),
layout[2],
);

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

0 comments on commit 8860b52

Please sign in to comment.