incorporated run-time configfile selection using trap
This commit is contained in:
parent
dd24908816
commit
758108c925
|
|
@ -15,3 +15,5 @@ env_logger = "0.4.*"
|
|||
dotenvy = "0.15.7"
|
||||
log = "0.4"
|
||||
signal-hook = "0.3.*"
|
||||
toml = "0.5.*"
|
||||
clap = { version = "4.2.*", features = ["derive"] }
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread::{self};
|
||||
use clap::Parser;
|
||||
use mpris::PlayerFinder;
|
||||
use structs::cli::Cli;
|
||||
use structs::{config::Config, data::Data};
|
||||
use crate::update_players::update_players;
|
||||
use crate::update_message::update_message;
|
||||
|
|
@ -20,9 +22,6 @@ fn handle_signal(data: &Data, pf: &PlayerFinder) {
|
|||
}
|
||||
}
|
||||
|
||||
//todo: load different config giles depending on CLI argument
|
||||
//so i can specify 3 files to use with my polybar config
|
||||
|
||||
fn main() {
|
||||
//dotenvy::dotenv().expect("Failed to read .env file");
|
||||
std::env::set_var("RUST_LOG", "error");
|
||||
|
|
@ -30,7 +29,8 @@ fn main() {
|
|||
panic!("{}", e);
|
||||
}
|
||||
|
||||
let mut cfg: Config = confy::load("polybar-now-playing", None).unwrap(); //TODO: error handling
|
||||
let cli = Cli::parse();
|
||||
let mut cfg: Config = confy::load("polybar-now-playing", cli.config_file.as_str()).unwrap(); //TODO: error handling
|
||||
cfg.priorities_to_lower();
|
||||
let mut data: Data = Data::default();
|
||||
let term = Arc::new(AtomicBool::new(false));
|
||||
|
|
|
|||
|
|
@ -25,7 +25,9 @@ fn build_string(cfg: &Config, data: &mut Data) -> String {
|
|||
b.append(' ');
|
||||
} else {
|
||||
cutoff(&cfg.metadata_fields, cfg.break_character, &mut data.display_text);
|
||||
if cfg.render_prefix {
|
||||
b.append(data.display_prefix.clone());
|
||||
}
|
||||
b.append(' ');
|
||||
b.append(format!(" %{{T{}}}", cfg.font_index));
|
||||
let mut idx = 0; let len = cfg.metadata_fields.len() as i32;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
use clap::Parser;
|
||||
|
||||
/// Program which finds the active mpris player.
|
||||
///
|
||||
/// Most configuration is done through config files.
|
||||
#[derive(Parser)]
|
||||
pub struct Cli {
|
||||
/// The name of the config file to use.
|
||||
#[arg(short = 'c', long = "config", default_value = "default")]
|
||||
pub config_file: String,
|
||||
}
|
||||
|
|
@ -21,6 +21,16 @@ impl Field {
|
|||
}
|
||||
}
|
||||
|
||||
// I shouldn't need this; remove when done with testing FIXME
|
||||
impl Default for Field {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
field: Default::default(),
|
||||
num_chars: Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Rating {
|
||||
pub nil: char,
|
||||
|
|
@ -49,15 +59,16 @@ impl Default for Rating {
|
|||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub font_index: u8,
|
||||
pub update_delay: Duration,
|
||||
pub metadata_separator: char,
|
||||
pub array_separator: char,
|
||||
pub hide_output: bool,
|
||||
pub render_prefix: bool,
|
||||
pub break_character: char,
|
||||
pub player_priorities: Vec<String>,
|
||||
pub rating_icons: Rating,
|
||||
pub metadata_fields: Vec<Field>,
|
||||
pub player_prefixes: HashMap<String, char>,
|
||||
pub rating_icons: Rating,
|
||||
pub player_priorities: Vec<String>,
|
||||
pub break_character: char,
|
||||
pub update_delay: Duration,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
|
|
@ -68,11 +79,12 @@ impl Default for Config {
|
|||
metadata_separator: '|',
|
||||
array_separator: '+',
|
||||
hide_output: true,
|
||||
render_prefix: true,
|
||||
metadata_fields: vec![Field::constructor("xesam:title", 40), Field::constructor("xesam:artist", 20)],
|
||||
player_prefixes: default_player_prefixes(),
|
||||
rating_icons: Rating::default(),
|
||||
player_priorities: vec![ms("clementine"), ms("spotify"), ms("deadbeef"), ms("mpv"), ms("vlc"), ms("firefox"), ms("chromium")],
|
||||
break_character: '-',
|
||||
player_prefixes: default_player_prefixes(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod config;
|
||||
pub mod data;
|
||||
pub mod cli;
|
||||
Loading…
Reference in New Issue