tui done
This commit is contained in:
parent
234b17a8b0
commit
3681aa2a22
15 changed files with 1109 additions and 143 deletions
49
src/app.rs
49
src/app.rs
|
|
@ -1,9 +1,9 @@
|
|||
use std::io;
|
||||
|
||||
use chrono::{Datelike, Local};
|
||||
use crossterm::event::{self, Event, KeyEvent, KeyEventKind};
|
||||
use ratatui::{
|
||||
DefaultTerminal, Frame,
|
||||
crossterm::event::{self, KeyEvent, KeyEventKind},
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
};
|
||||
|
||||
|
|
@ -20,17 +20,17 @@ pub enum FocusedComponent {
|
|||
DayPopup,
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
pub struct App<'a> {
|
||||
year: Year,
|
||||
month: Month,
|
||||
days: Days,
|
||||
day_popup: Popup,
|
||||
day_popup: Popup<'a>,
|
||||
|
||||
focused: FocusedComponent,
|
||||
exit: bool,
|
||||
}
|
||||
|
||||
impl App {
|
||||
impl App<'_> {
|
||||
pub fn new() -> Self {
|
||||
let mut app = App {
|
||||
year: Year::default(),
|
||||
|
|
@ -38,7 +38,7 @@ impl App {
|
|||
days: Days::default(),
|
||||
day_popup: Popup::default(),
|
||||
|
||||
focused: FocusedComponent::DayPopup,
|
||||
focused: FocusedComponent::Days,
|
||||
exit: false,
|
||||
};
|
||||
|
||||
|
|
@ -75,13 +75,13 @@ impl App {
|
|||
let mut db = DB::new().unwrap();
|
||||
|
||||
while !self.exit {
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
terminal.draw(|frame| self.draw(frame, &mut db))?;
|
||||
self.handle_events(&mut db)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
fn draw(&mut self, frame: &mut Frame, db: &mut DB) {
|
||||
let main_area = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(vec![
|
||||
|
|
@ -100,7 +100,7 @@ impl App {
|
|||
|
||||
self.year.ready_to_render(main_area[0]);
|
||||
self.month.ready_to_render(main_area[1]);
|
||||
self.days.ready_to_render(main_area[2]);
|
||||
self.days.ready_to_render(main_area[2], db);
|
||||
self.day_popup.ready_to_render(popup_area);
|
||||
|
||||
frame.render_widget(&self.year, main_area[0]);
|
||||
|
|
@ -136,7 +136,9 @@ impl App {
|
|||
|
||||
fn handle_events(&mut self, db: &mut DB) -> io::Result<()> {
|
||||
match event::read()? {
|
||||
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
|
||||
ratatui::crossterm::event::Event::Key(key_event)
|
||||
if key_event.kind == KeyEventKind::Press =>
|
||||
{
|
||||
let app_event_option = self.handle_input(key_event, db);
|
||||
|
||||
if let Some(app_event_vec) = app_event_option {
|
||||
|
|
@ -160,13 +162,40 @@ impl App {
|
|||
self.month.minus_month();
|
||||
}
|
||||
},
|
||||
AppEvent::Reload => {
|
||||
self.days.reload_day_counts(db);
|
||||
}
|
||||
AppEvent::MonthSet(month) => {
|
||||
self.days.reload(month, self.year.year, 1);
|
||||
}
|
||||
AppEvent::YearSet(year) => {
|
||||
self.days.reload(self.month.month, year, 1);
|
||||
}
|
||||
_ => (),
|
||||
|
||||
AppEvent::AddEvent(start, end) => {
|
||||
self.switch_focus(FocusedComponent::DayPopup);
|
||||
self.day_popup.add_event(
|
||||
start,
|
||||
end,
|
||||
db.get_tags().expect("Database error"),
|
||||
);
|
||||
}
|
||||
|
||||
AppEvent::DaySelected(day) => {
|
||||
self.switch_focus(FocusedComponent::DayPopup);
|
||||
let events =
|
||||
db.get_day(day).expect("Database error").unwrap_or_default();
|
||||
let tag_names = events
|
||||
.iter()
|
||||
.map(|e| match e.tag {
|
||||
Some(id) => {
|
||||
db.get_tag_name(id).expect("Database error").unwrap()
|
||||
}
|
||||
None => "Tag deleted".to_owned(),
|
||||
})
|
||||
.collect();
|
||||
self.day_popup.show_day(day, events, tag_names);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue