mirror of
https://github.com/JakeStanger/ironbar.git
synced 2024-11-24 00:27:41 +03:00
feat(config): use default fallback with config instructions
When no config file is found, the bar will now automatically instead load a hard-coded default consisting of the `focused` and `clock` modules, and a `label` informing you the bar is not configured. Instructions are also printed to the log.
This commit is contained in:
parent
1a272e00fb
commit
738b9e3da7
@ -21,6 +21,7 @@ use crate::modules::tray::TrayModule;
|
||||
use crate::modules::upower::UpowerModule;
|
||||
#[cfg(feature = "workspaces")]
|
||||
use crate::modules::workspaces::WorkspacesModule;
|
||||
use cfg_if::cfg_if;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -109,6 +110,35 @@ pub struct Config {
|
||||
pub monitors: Option<HashMap<String, MonitorConfig>>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "clock")] {
|
||||
let end = Some(vec![ModuleConfig::Clock(Box::default())]);
|
||||
}
|
||||
else {
|
||||
let end = None;
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
position: Default::default(),
|
||||
height: default_bar_height(),
|
||||
margin: Default::default(),
|
||||
popup_gap: default_popup_gap(),
|
||||
icon_theme: None,
|
||||
ironvar_defaults: None,
|
||||
start: Some(vec![ModuleConfig::Label(
|
||||
LabelModule::new("ℹ️ Using default config".to_string()).into(),
|
||||
)]),
|
||||
center: Some(vec![ModuleConfig::Focused(Box::default())]),
|
||||
end,
|
||||
anchor_to_edges: default_true(),
|
||||
monitors: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fn default_bar_height() -> i32 {
|
||||
42
|
||||
}
|
||||
|
@ -2,7 +2,6 @@
|
||||
pub enum ExitCode {
|
||||
GtkDisplay = 1,
|
||||
CreateBars = 2,
|
||||
Config = 3,
|
||||
}
|
||||
|
||||
pub const ERR_OUTPUTS: &str = "GTK and Wayland are reporting a different set of outputs - this is a severe bug and should never happen";
|
||||
|
25
src/main.rs
25
src/main.rs
@ -47,7 +47,7 @@ use tokio::task::{block_in_place, spawn_blocking};
|
||||
|
||||
use crate::error::ExitCode;
|
||||
use clients::wayland;
|
||||
use tracing::{debug, error, info};
|
||||
use tracing::{debug, error, info, warn};
|
||||
use universal_config::ConfigLoader;
|
||||
|
||||
const GTK_APP_ID: &str = "dev.jstanger.ironbar";
|
||||
@ -159,18 +159,19 @@ pub fn load_interface(app: &Application) {
|
||||
|display| display,
|
||||
);
|
||||
|
||||
let config_res = env::var("IRONBAR_CONFIG").map_or_else(
|
||||
|_| ConfigLoader::new("ironbar").find_and_load(),
|
||||
ConfigLoader::load,
|
||||
);
|
||||
let mut config = env::var("IRONBAR_CONFIG")
|
||||
.map_or_else(
|
||||
|_| ConfigLoader::new("ironbar").find_and_load(),
|
||||
ConfigLoader::load,
|
||||
)
|
||||
.unwrap_or_else(|err| {
|
||||
error!("Failed to load config: {}", err);
|
||||
warn!("Falling back to the default config");
|
||||
info!("If this is your first time using Ironbar, you should create a config in ~/.config/ironbar/");
|
||||
info!("More info here: https://github.com/JakeStanger/ironbar/wiki/configuration-guide");
|
||||
|
||||
let mut config: Config = match config_res {
|
||||
Ok(config) => config,
|
||||
Err(err) => {
|
||||
error!("{:?}", err);
|
||||
exit(ExitCode::Config as i32)
|
||||
}
|
||||
};
|
||||
Config::default()
|
||||
});
|
||||
|
||||
debug!("Loaded config file");
|
||||
|
||||
|
@ -34,6 +34,17 @@ pub struct ClockModule {
|
||||
pub common: Option<CommonConfig>,
|
||||
}
|
||||
|
||||
impl Default for ClockModule {
|
||||
fn default() -> Self {
|
||||
ClockModule {
|
||||
format: default_format(),
|
||||
format_popup: default_popup_format(),
|
||||
locale: default_locale(),
|
||||
common: Some(CommonConfig::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn default_format() -> String {
|
||||
String::from("%d/%m/%Y %H:%M")
|
||||
}
|
||||
|
@ -32,6 +32,18 @@ pub struct FocusedModule {
|
||||
pub common: Option<CommonConfig>,
|
||||
}
|
||||
|
||||
impl Default for FocusedModule {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
show_icon: crate::config::default_true(),
|
||||
show_title: crate::config::default_true(),
|
||||
icon_size: default_icon_size(),
|
||||
truncate: None,
|
||||
common: Some(CommonConfig::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fn default_icon_size() -> i32 {
|
||||
32
|
||||
}
|
||||
|
@ -17,6 +17,15 @@ pub struct LabelModule {
|
||||
pub common: Option<CommonConfig>,
|
||||
}
|
||||
|
||||
impl LabelModule {
|
||||
pub(crate) fn new(label: String) -> Self {
|
||||
Self {
|
||||
label,
|
||||
common: Some(CommonConfig::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Module<Label> for LabelModule {
|
||||
type SendMessage = String;
|
||||
type ReceiveMessage = ();
|
||||
|
Loading…
Reference in New Issue
Block a user