This commit is contained in:
Daniel Flanagan 2024-04-20 19:50:18 -05:00
parent a94d498c7e
commit 38912b506b
8 changed files with 47 additions and 16 deletions

View file

@ -108,10 +108,11 @@ impl CleanupArgs {
mod ui { mod ui {
use crate::cli::prelude::*; use crate::cli::prelude::*;
#[derive(Parser)] #[derive(Parser, Debug)]
pub struct Args {} pub struct Args {}
impl Args { impl Args {
#[instrument]
pub async fn run(&self, tasks: SharedTasks) -> Result<()> { pub async fn run(&self, tasks: SharedTasks) -> Result<()> {
crate::tui::Tui::run(tasks).await crate::tui::Tui::run(tasks).await
} }

View file

@ -5,6 +5,7 @@ use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use reqwest_tracing::TracingMiddleware; use reqwest_tracing::TracingMiddleware;
use serde::de; use serde::de;
#[derive(Debug)]
pub struct Client { pub struct Client {
client: ClientWithMiddleware, client: ClientWithMiddleware,
base_url: Url, base_url: Url,

View file

@ -6,6 +6,7 @@ use reqwest::{
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug)]
pub struct GitLab { pub struct GitLab {
client: Client, client: Client,
} }

View file

@ -13,6 +13,7 @@ use serde::Deserialize;
use tokio::spawn; use tokio::spawn;
use tracing::debug; use tracing::debug;
#[derive(Debug)]
pub struct Jira { pub struct Jira {
client: Client, client: Client,
} }

View file

@ -26,5 +26,7 @@ async fn main() -> Result<()> {
// https://docs.rs/tracing-appender/latest/tracing_appender/non_blocking/struct.WorkerGuard.html // https://docs.rs/tracing-appender/latest/tracing_appender/non_blocking/struct.WorkerGuard.html
let _log_guard = observe::setup_logging(&cli.logs_directory, &cli.tracing_env_filter)?; let _log_guard = observe::setup_logging(&cli.logs_directory, &cli.tracing_env_filter)?;
info!("Starting taskr...");
cli.exec().await cli.exec().await
} }

View file

@ -1,5 +1,7 @@
#![allow(unused_imports)] #![allow(unused_imports)]
pub use tracing::instrument;
pub use crate::config::Config; pub use crate::config::Config;
pub use crate::result::Result; pub use crate::result::Result;
pub use regex::Regex; pub use regex::Regex;

View file

@ -18,6 +18,7 @@ use crate::{gitlab::GitLab, jira::Jira, result::Result};
pub type Db = sled::Db; pub type Db = sled::Db;
#[derive(Debug)]
pub struct Tasks { pub struct Tasks {
pub data_dir: PathBuf, pub data_dir: PathBuf,
pub config_file_path: PathBuf, pub config_file_path: PathBuf,
@ -35,6 +36,7 @@ impl Tasks {
where where
D: AsRef<Path>, D: AsRef<Path>,
{ {
debug!("Creating Tasks instance...");
let db = OnceLock::new(); let db = OnceLock::new();
let config = OnceLock::new(); let config = OnceLock::new();
let gitlab = OnceLock::new(); let gitlab = OnceLock::new();

View file

@ -13,7 +13,7 @@ use ratatui::{
}; };
use signal_hook::consts::*; use signal_hook::consts::*;
use signal_hook_tokio::Signals; use signal_hook_tokio::Signals;
use std::io::stdout; use std::io::{stdout, Stdout};
pub struct Tui {} pub struct Tui {}
@ -21,17 +21,28 @@ enum Event {
CrosstermEvent(crossterm::event::Event), CrosstermEvent(crossterm::event::Event),
// CrosstermError(Arc<dyn std::error::Error>), // CrosstermError(Arc<dyn std::error::Error>),
Quit, Quit,
Tick, Update,
Render,
} }
impl Tui { impl Tui {
pub async fn run(tasks: SharedTasks) -> Result<()> { #[instrument]
fn setup_screen() -> Result<()> {
debug!("Setting up stdout for TUI...");
stdout().execute(EnterAlternateScreen)?; stdout().execute(EnterAlternateScreen)?;
enable_raw_mode()?; enable_raw_mode()?;
Ok(())
}
fn new_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
debug!("Initializing TUI Terminal with Crossterm backend...");
let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?; let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
terminal.clear()?; terminal.clear()?;
return Ok(terminal);
}
pub async fn run(tasks: SharedTasks) -> Result<()> {
Self::setup_screen()?;
let mut terminal = Self::new_terminal()?;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<Event>(); let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<Event>();
let mut signals = Signals::new(&[SIGHUP, SIGTERM, SIGINT, SIGQUIT])?; let mut signals = Signals::new(&[SIGHUP, SIGTERM, SIGINT, SIGQUIT])?;
@ -39,13 +50,16 @@ impl Tui {
let signal_sender = tx.clone(); let signal_sender = tx.clone();
tokio::spawn(async move { tokio::spawn(async move {
// await a signal - once we receive one send the quit event // await a signal - once we receive one send the quit event
signals.next().await; let sig = signals.next().await;
eprintln!("{sig:#?}");
println!("{sig:#?}");
info!("{sig:#?}");
signal_sender.send(Event::Quit).unwrap(); signal_sender.send(Event::Quit).unwrap();
}); });
let tick_rate = std::time::Duration::from_millis(1000); let tick_rate = std::time::Duration::from_millis(1000);
let event_publisher_loop = tokio::spawn(async move { let _event_publisher_loop = tokio::spawn(async move {
let mut crossterm_event_stream = crossterm::event::EventStream::new(); let mut crossterm_event_stream = crossterm::event::EventStream::new();
let mut interval = tokio::time::interval(tick_rate); let mut interval = tokio::time::interval(tick_rate);
@ -68,7 +82,7 @@ impl Tui {
} }
} }
_ = delay => { _ = delay => {
tx.send(Event::Tick).unwrap(); tx.send(Event::Update).unwrap();
} }
} }
} }
@ -77,6 +91,8 @@ impl Tui {
loop { loop {
let event = rx.recv().await; let event = rx.recv().await;
let mut should_draw = false;
match event { match event {
Some(Event::CrosstermEvent(e)) => match e { Some(Event::CrosstermEvent(e)) => match e {
event::Event::Key(key) => { event::Event::Key(key) => {
@ -92,18 +108,23 @@ impl Tui {
} }
_ => {} _ => {}
}, },
Some(Event::Tick) => {} Some(Event::Update) => {}
Some(Event::Quit) => break, Some(Event::Quit) => break,
Some(Event::Render) => {
should_draw = true;
}
None => {} None => {}
} }
terminal.draw(|frame| { if should_draw {
let area = frame.size(); terminal.draw(|frame| {
frame.render_widget( let area = frame.size();
Paragraph::new("Hello Ratatui! (press 'q' to quit)").white(), frame.render_widget(
area, Paragraph::new("Hello Ratatui! (press 'q' to quit)").white(),
); area,
})?; );
})?;
}
} }
signal_handle.close(); signal_handle.close();