Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ plugin:

You can configure centerpiece through yaml or nix.

You can specify alternative configuration locations through:
- the `--config` flag
- the `CENTERPIECE_CONFIGURATION_FILE` environment variable

## Using yml

1. Create a `config.yml` file in `~/.config/centerpiece/config.yml`.
Expand Down
2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ edition = "2021"
[dependencies]
# general
anyhow = { version = "1.0.78", features = ["backtrace"] }
clap = { version = "4.5.1", features = ["derive"] }
clap = { version = "4.5.1", features = ["derive", "env"] }
log = { version = "0.4.20", features = ["kv_unstable_serde"] }
simple_logger = { version = "4.3.3", features = [
"colors",
Expand Down
12 changes: 10 additions & 2 deletions client/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use clap::Parser;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Default)]
#[command(author, version = CliArgs::version(), about, long_about=None) ]
#[command(next_line_help = true)]
pub(crate) struct CliArgs {}
pub(crate) struct CliArgs {
#[clap(
short,
long,
help = "The location of the configuration file",
env = "CENTERPIECE_CONFIGURATION_FILE"
)]
pub(crate) config: Option<String>,
}

impl CliArgs {
/// Surface current version together with the current git revision and date,
Expand Down
18 changes: 12 additions & 6 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod plugin;
mod settings;

pub fn main() -> iced::Result {
let _args = crate::cli::CliArgs::parse();
let args = crate::cli::CliArgs::parse();
simple_logger::init_with_level(log::Level::Info).unwrap();
Centerpiece::run(Centerpiece::settings())
Centerpiece::run(Centerpiece::settings(args))
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -38,22 +38,27 @@ impl Application for Centerpiece {
type Message = Message;
type Executor = iced::executor::Default;
type Theme = iced::Theme;
type Flags = ();
type Flags = crate::cli::CliArgs;

fn new(_flags: ()) -> (Self, iced::Command<Message>) {
fn new(flags: crate::cli::CliArgs) -> (Self, iced::Command<Message>) {
let _ = iced::font::load(
include_bytes!("../assets/FiraCode/FiraCodeNerdFont-Regular.ttf").as_slice(),
);
let _ = iced::font::load(
include_bytes!("../assets/FiraCode/FiraCodeNerdFont-Light.ttf").as_slice(),
);

let settings = crate::settings::Settings::try_from(flags).unwrap_or_else(|_| {
eprintln!("There is an issue with the settings, please check the configuration file.");
std::process::exit(0);
});

(
Self {
query: String::from(""),
active_entry_index: 0,
plugins: vec![],
settings: settings::Settings::new(),
settings,
},
iced::Command::perform(async {}, move |()| Message::Loaded),
)
Expand Down Expand Up @@ -262,7 +267,7 @@ impl Application for Centerpiece {
}

impl Centerpiece {
fn settings() -> iced::Settings<()> {
fn settings(flags: crate::cli::CliArgs) -> iced::Settings<crate::cli::CliArgs> {
let default_text_size = REM;

let default_font = iced::Font {
Expand Down Expand Up @@ -293,6 +298,7 @@ impl Centerpiece {
window,
default_font,
default_text_size,
flags,
..Default::default()
}
}
Expand Down
5 changes: 5 additions & 0 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ pub fn config_directory() -> anyhow::Result<String> {
Ok(std::env::var("XDG_CONFIG_HOME").unwrap_or(config_in_home))
}

pub fn centerpiece_default_config_path() -> anyhow::Result<String> {
let config_directory = crate::plugin::utils::centerpiece_config_directory()?;
Ok(format!("{config_directory}/config.yml"))
}

pub fn centerpiece_config_directory() -> anyhow::Result<String> {
let config_directory = config_directory()?;
Ok(format!("{config_directory}/centerpiece"))
Expand Down
31 changes: 31 additions & 0 deletions client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,34 @@ impl Settings {
config_result.unwrap()
}
}

impl std::convert::TryFrom<crate::cli::CliArgs> for Settings {
type Error = anyhow::Error;

fn try_from(args: crate::cli::CliArgs) -> Result<Self, Self::Error> {
let maybe_config_file_path = args.config;
let config_file_path = maybe_config_file_path.unwrap_or_else(|| {
crate::plugin::utils::centerpiece_default_config_path().unwrap_or_else(|error| {
log::error!(
error = log::error!("{:?}", error);
"Unable to find default config file.",
);
panic!();
})
});
let config_file_result = std::fs::File::open(config_file_path);
if config_file_result.is_err() {
log::info!("No custom config file found, falling back to default.");
return Ok(Self::default());
}
let config_file = config_file_result?;
let config_result = serde_yaml::from_reader(config_file);
if let Err(ref error) = config_result {
log::error!(
error = log::error!("{:?}", error);
"Config file does not match settings struct.",
);
}
Ok(config_result?)
}
}