Cleaned up imports a bit and added rustfmt.toml

This commit is contained in:
2023-08-08 10:50:04 -06:00
parent 0d4e283c21
commit 155675b596
6 changed files with 182 additions and 179 deletions
+10
View File
@@ -0,0 +1,10 @@
tab_spaces=2
edition = "2021"
reorder_imports = true
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
reorder_modules = true
merge_derives = true
use_field_init_shorthand = true
format_macro_matchers = true
format_macro_bodies = true
+5 -9
View File
@@ -7,8 +7,8 @@ use crate::app::radarr::RadarrData;
use super::network::RadarrEvent; use super::network::RadarrEvent;
pub mod radarr;
pub(crate) mod key_binding; pub(crate) mod key_binding;
pub mod radarr;
#[derive(Debug)] #[derive(Debug)]
pub struct App { pub struct App {
@@ -22,11 +22,7 @@ pub struct App {
} }
impl App { impl App {
pub fn new( pub fn new(network_tx: Sender<RadarrEvent>, tick_until_poll: u64, config: AppConfig) -> Self {
network_tx: Sender<RadarrEvent>,
tick_until_poll: u64,
config: AppConfig
) -> Self {
App { App {
network_tx: Some(network_tx), network_tx: Some(network_tx),
tick_until_poll, tick_until_poll,
@@ -66,7 +62,7 @@ impl Default for App {
tick_until_poll: 0, tick_until_poll: 0,
tick_count: 0, tick_count: 0,
config: AppConfig::default(), config: AppConfig::default(),
data: Data::default() data: Data::default(),
} }
} }
} }
@@ -85,7 +81,7 @@ pub struct AppConfig {
pub struct RadarrConfig { pub struct RadarrConfig {
pub host: String, pub host: String,
pub port: Option<u16>, pub port: Option<u16>,
pub api_token: String pub api_token: String,
} }
impl Default for RadarrConfig { impl Default for RadarrConfig {
@@ -93,7 +89,7 @@ impl Default for RadarrConfig {
RadarrConfig { RadarrConfig {
host: "localhost".to_string(), host: "localhost".to_string(),
port: Some(7878), port: Some(7878),
api_token: "".to_string() api_token: "".to_string(),
} }
} }
} }
+4 -3
View File
@@ -10,12 +10,12 @@ use crate::event::Key;
pub enum InputEvent<T> { pub enum InputEvent<T> {
KeyEvent(T), KeyEvent(T),
Tick Tick,
} }
pub struct Events { pub struct Events {
_tx: Sender<InputEvent<Key>>, _tx: Sender<InputEvent<Key>>,
rx: Receiver<InputEvent<Key>> rx: Receiver<InputEvent<Key>>,
} }
impl Events { impl Events {
@@ -27,7 +27,8 @@ impl Events {
thread::spawn(move || { thread::spawn(move || {
let mut last_tick = Instant::now(); let mut last_tick = Instant::now();
loop { loop {
let timeout = tick_rate.checked_sub(last_tick.elapsed()) let timeout = tick_rate
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0)); .unwrap_or_else(|| Duration::from_secs(0));
if event::poll(timeout).unwrap() { if event::poll(timeout).unwrap() {
if let CrosstermEvent::Key(key) = event::read().unwrap() { if let CrosstermEvent::Key(key) = event::read().unwrap() {
+7 -4
View File
@@ -6,14 +6,14 @@ use crossterm::event::{KeyCode, KeyEvent};
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Key { pub enum Key {
Char(char), Char(char),
Unknown Unknown,
} }
impl Display for Key { impl Display for Key {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self { match *self {
Key::Char(c) => write!(f, "<{}>", c), Key::Char(c) => write!(f, "<{}>", c),
_ => write!(f, "<{:?}>", self) _ => write!(f, "<{:?}>", self),
} }
} }
} }
@@ -25,7 +25,7 @@ impl From<KeyEvent> for Key {
code: KeyCode::Char(c), code: KeyCode::Char(c),
.. ..
} => Key::Char(c), } => Key::Char(c),
_ => Key::Unknown _ => Key::Unknown,
} }
} }
} }
@@ -43,6 +43,9 @@ mod tests {
#[test] #[test]
fn test_key_from() { fn test_key_from() {
assert_eq!(Key::from(KeyEvent::from(KeyCode::Char('q'))), Key::Char('q')) assert_eq!(
Key::from(KeyEvent::from(KeyCode::Char('q'))),
Key::Char('q')
)
} }
} }
+8 -15
View File
@@ -1,16 +1,17 @@
use std::io; use std::io;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Duration, Instant};
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use crossterm::execute; use crossterm::execute;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}; use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use log::{debug, info}; use log::{debug, info};
use tokio::sync::{mpsc, Mutex};
use tokio::sync::mpsc::Receiver; use tokio::sync::mpsc::Receiver;
use tui::Terminal; use tokio::sync::{mpsc, Mutex};
use tui::backend::CrosstermBackend; use tui::backend::CrosstermBackend;
use tui::Terminal;
use utils::init_logging_config; use utils::init_logging_config;
@@ -29,9 +30,7 @@ mod utils;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Cli { struct Cli {}
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@@ -41,11 +40,7 @@ async fn main() -> Result<()> {
let config = confy::load("managarr", "config")?; let config = confy::load("managarr", "config")?;
let (sync_network_tx, sync_network_rx) = mpsc::channel(500); let (sync_network_tx, sync_network_rx) = mpsc::channel(500);
let app = Arc::new(Mutex::new(App::new( let app = Arc::new(Mutex::new(App::new(sync_network_tx, 5000 / 250, config)));
sync_network_tx,
5000 / 250,
config
)));
let app_nw = Arc::clone(&app); let app_nw = Arc::clone(&app);
@@ -91,9 +86,7 @@ async fn simple_ui(app: &Arc<Mutex<App>>) -> Result<()> {
break; break;
} }
} }
InputEvent::Tick => { InputEvent::Tick => app.on_tick().await,
app.on_tick().await
}
} }
} }
+2 -2
View File
@@ -10,13 +10,13 @@ pub(crate) mod radarr;
#[derive(Debug, Eq, PartialEq, Hash)] #[derive(Debug, Eq, PartialEq, Hash)]
pub enum RadarrEvent { pub enum RadarrEvent {
HealthCheck, HealthCheck,
GetOverview GetOverview,
} }
pub struct Network<'a> { pub struct Network<'a> {
pub client: Client, pub client: Client,
pub app: &'a Arc<Mutex<App>> pub app: &'a Arc<Mutex<App>>,
} }
impl<'a> Network<'a> { impl<'a> Network<'a> {