started documentation effort
This commit is contained in:
27
src/main.rs
27
src/main.rs
@@ -1,10 +1,10 @@
|
||||
//! This file contains all driver code for the program.
|
||||
use core::time;
|
||||
use std::process::exit;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread;
|
||||
use clap::Parser;
|
||||
use log::{error, info};
|
||||
use log::{error, info, warn};
|
||||
use mpris::PlayerFinder;
|
||||
use structs::cli::Cli;
|
||||
use structs::{config::Config, data::Data};
|
||||
@@ -19,13 +19,18 @@ mod print_text;
|
||||
mod structs;
|
||||
mod print_players;
|
||||
|
||||
/// This function deals with an incoming (USR1) signal.
|
||||
/// It is hard-coded to play/payse the active player.
|
||||
///
|
||||
/// input:
|
||||
/// data: Data struct for active configuration.
|
||||
fn handle_signal(data: &Data) {
|
||||
if let Some(p) = &data.current_player {
|
||||
match p.checked_play_pause() {
|
||||
Ok(b) => {
|
||||
match b {
|
||||
true => info!("Player play/paused succesfully!"),
|
||||
false => info!("Failed to send play/pause signal!"),
|
||||
false => warn!("Failed to send play/pause signal!"),
|
||||
}
|
||||
},
|
||||
Err(e) => error!("{e}"),
|
||||
@@ -33,26 +38,35 @@ fn handle_signal(data: &Data) {
|
||||
}
|
||||
}
|
||||
|
||||
/// This function contains the default maim loop body of the program.
|
||||
/// It updates the active player, updates the output strings based on this, and finally formats and outputs these strings to stdout.
|
||||
///
|
||||
/// input:
|
||||
/// pf: PlayerFinder instance for the program
|
||||
/// cfg: Configuration of the program
|
||||
/// data: mutable Data struct, active state of the program
|
||||
/// r: pre-computed rating strings
|
||||
fn default_loop(pf: &PlayerFinder, cfg: &Config, data: &mut Data, r: &Vec<String>) {
|
||||
update_players(pf, cfg, data);
|
||||
update_message(cfg, data, r);
|
||||
print_text(cfg, data);
|
||||
}
|
||||
|
||||
/// Main function. Mostly concerned with initialisation.
|
||||
fn main() {
|
||||
// logging initialisation
|
||||
std::env::set_var("RUST_LOG", "error");
|
||||
if let Err(e) = env_logger::init() {
|
||||
error!("{e}");
|
||||
return
|
||||
}
|
||||
|
||||
// Cli flags, Config, Data, and PlayerFinder initialisation
|
||||
let cli = Cli::parse();
|
||||
match confy::load::<Config>("polybar-now-playing", cli.config_file.as_str()) {
|
||||
Ok(cfg) => {
|
||||
let mut data: Data = Data::default();
|
||||
let rating_strings = cfg.build_rating_strings();
|
||||
|
||||
let term = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let pf: PlayerFinder;
|
||||
match PlayerFinder::new() {
|
||||
@@ -63,11 +77,14 @@ fn main() {
|
||||
},
|
||||
}
|
||||
|
||||
// signal interception initialisation
|
||||
let term = Arc::new(AtomicBool::new(false));
|
||||
if let Err(e) = signal_hook::flag::register(signal_hook::consts::SIGUSR1, Arc::clone(&term)) {
|
||||
error!("{e}");
|
||||
return
|
||||
}
|
||||
|
||||
// main body loop
|
||||
loop {
|
||||
thread::sleep(time::Duration::from_millis(cfg.update_delay));
|
||||
match cli.debug {
|
||||
|
||||
Reference in New Issue
Block a user