From 22c8553af68c6f5b2c62ba790e06b249ac82ed2a Mon Sep 17 00:00:00 2001 From: "Julian T." Date: Mon, 29 Apr 2024 22:25:47 +0200 Subject: Add beginnning ui and database --- src/main.rs | 26 +++++++++++++++++---- src/model/mod.rs | 3 +++ src/model/task.rs | 15 ++++++++++++ src/render.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/render/footer.rs | 20 ++++++++++++++++ 5 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 src/model/mod.rs create mode 100644 src/model/task.rs create mode 100644 src/render.rs create mode 100644 src/render/footer.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e5a8fa2..ad4bd25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,27 @@ -use std::io::Result; + +use render::App; +use sqlx::{self, Connection, SqliteConnection}; + +use std::error::Error; mod tui; +mod render; +mod model; + + +#[tokio::main] +async fn main() -> Result<(), Box> { + let mut conn = SqliteConnection::connect("sqlite::memory:").await?; + let task = sqlx::query_as::<_, model::Task>("SELECT 'hej' as test") + .fetch_one(&mut conn).await?; + + println!("{}", task); + + + let mut terminal = tui::init()?; + let res = App::default().run(&mut terminal); + tui::restore()?; -fn main() -> Result<()> { - let mut terminal = tui::init(); - tui::restore(); + res?; Ok(()) } diff --git a/src/model/mod.rs b/src/model/mod.rs new file mode 100644 index 0000000..d869a4f --- /dev/null +++ b/src/model/mod.rs @@ -0,0 +1,3 @@ +mod task; + +pub use task::Task; diff --git a/src/model/task.rs b/src/model/task.rs new file mode 100644 index 0000000..4fdfd88 --- /dev/null +++ b/src/model/task.rs @@ -0,0 +1,15 @@ + +use std::fmt::{self, Display}; + +use sqlx::FromRow; + +#[derive(Debug, FromRow)] +pub struct Task { + pub test: String, +} + +impl Display for Task { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "test: {}", self.test) + } +} diff --git a/src/render.rs b/src/render.rs new file mode 100644 index 0000000..8c9ce38 --- /dev/null +++ b/src/render.rs @@ -0,0 +1,66 @@ +use std::io; + +use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind}; +use ratatui::{ + buffer::Buffer, layout::{Alignment, Constraint, Direction, Layout, Rect}, style::Stylize, symbols::border, text::Text, widgets::{block::Title, Block, Borders, Paragraph, Widget}, Frame +}; + +use crate::tui; + +use self::footer::Footer; + +mod footer; + +#[derive(Debug, Default)] +pub struct App { + exit: bool, +} + +impl App { + pub fn run(&mut self, terminal: &mut tui::Tui) -> io::Result<()> { + while !self.exit { + terminal.draw(|frame| self.render_frame(frame))?; + self.handle_events()?; + } + + Ok(()) + } + + fn render_frame(&self, frame: &mut Frame) { + frame.render_widget(self, frame.size()); + } + + fn handle_events(&mut self) -> io::Result<()> { + match event::read()?{ + Event::Key(key_event) if key_event.kind == KeyEventKind::Press => { + self.handle_key_event(key_event); + } + _ => () + } + Ok(()) + } + + pub fn exit(&mut self) { + self.exit = true; + } + + fn handle_key_event(&mut self, event: KeyEvent) { + match event.code { + KeyCode::Char('q') => self.exit(), + _ => () + } + } +} + +impl Widget for &App { + fn render(self, area: Rect, buf: &mut Buffer) { + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([ + Constraint::Min(1), + Constraint::Length(1), + ]) + .split(area); + Footer::new().render(chunks[1], buf); + } +} diff --git a/src/render/footer.rs b/src/render/footer.rs new file mode 100644 index 0000000..9e74de8 --- /dev/null +++ b/src/render/footer.rs @@ -0,0 +1,20 @@ +use ratatui::{style::{Modifier, Style}, widgets::{Paragraph, Widget}}; + + +#[derive(Debug, Default)] +pub struct Footer; + +impl Footer { + pub fn new() -> Footer { + return Footer {} + } +} + +impl Widget for &Footer { + + fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) { + Paragraph::new("hellow") + .style(Style::default().add_modifier(Modifier::REVERSED)) + .render(area, buf); + } +} -- cgit v1.2.3