From 829cbaf2d0fe95abc9c65b1e34e209fc32952805 Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 17 May 2024 20:22:23 +0200 Subject: Stuff i dont remember, sorry --- src/main.rs | 5 ----- src/model/task.rs | 17 +++++++++++------ src/render.rs | 11 +++++++++-- src/render/tasks.rs | 34 ++++++++++++++++++++++++++++++++++ test.db | Bin 20480 -> 20480 bytes 5 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 src/render/tasks.rs diff --git a/src/main.rs b/src/main.rs index ad4bd25..39e81c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,11 +12,6 @@ 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); diff --git a/src/model/task.rs b/src/model/task.rs index 4fdfd88..e90dd8e 100644 --- a/src/model/task.rs +++ b/src/model/task.rs @@ -1,15 +1,20 @@ -use std::fmt::{self, Display}; - use sqlx::FromRow; #[derive(Debug, FromRow)] pub struct Task { - pub test: String, + pub id: i64, + pub group_id: i64, + pub name: String, + pub description: String, } -impl Display for Task { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "test: {}", self.test) +impl Task { + pub fn new(id: i64, group_id: i64, name: &str, description: &str) -> Task { + Task { + id, group_id, + name: String::from(name), + description: String::from(description), + } } } diff --git a/src/render.rs b/src/render.rs index 8c9ce38..d1d5f19 100644 --- a/src/render.rs +++ b/src/render.rs @@ -5,11 +5,12 @@ 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 crate::{tui, model::Task}; -use self::footer::Footer; +use self::{footer::Footer, tasks::TaskList}; mod footer; +mod tasks; #[derive(Debug, Default)] pub struct App { @@ -61,6 +62,12 @@ impl Widget for &App { Constraint::Length(1), ]) .split(area); + TaskList::new(&vec![ + Task::new(1, 1, "Hej", "Hej med dig hvordan går det"), + Task::new(2, 1, "Lul", "Hej med dig hvordan går det"), + Task::new(3, 1, "Hvordan lige", ""), + Task::new(4, 1, "Hej med dig", ""), + ]).render(chunks[0], buf); Footer::new().render(chunks[1], buf); } } diff --git a/src/render/tasks.rs b/src/render/tasks.rs new file mode 100644 index 0000000..0f6a79a --- /dev/null +++ b/src/render/tasks.rs @@ -0,0 +1,34 @@ +use ratatui::{layout::Constraint, widgets::{Row, Table, Widget}, prelude::Text}; + +use crate::model::Task; + + + +#[derive(Debug)] +pub struct TaskList<'a> { + tasks: &'a[Task] +} + +impl<'a> TaskList<'a> { + pub fn new(tasks: &'a[Task]) -> TaskList<'a> { + TaskList { + tasks + } + } +} + +impl<'a> Widget for &TaskList<'a> { + fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) { + let rows: Vec = self.tasks.into_iter() + .map(|t: &Task| Row::new(vec![Text::from(t.name.as_str()), Text::from(t.description.as_str())])) + .collect(); + + let widths = [ + Constraint::Percentage(50), + Constraint::Fill(1), + ]; + + Table::new(rows, widths) + .render(area, buf); + } +} diff --git a/test.db b/test.db index 386b723..8002edb 100644 Binary files a/test.db and b/test.db differ -- cgit v1.2.3