Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI config file #227

Merged
merged 10 commits into from
Nov 9, 2023
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
76 changes: 76 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion book/src/cli-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,45 @@ Refer to your [package manager for details](./cli-installation.html#guidelines-f

## Customization

### Configuration

Numbats configuration file is called `config.toml`, and it needs to be placed in
`<config-path>` described above (`~/.config/numbat/config.toml` on Linux). You
can generate a default configuration by calling

``` bash
numbat --generate-config
```

The most important fields are:

```
[main]
# Controls the welcome message. Can be "long", "short", or "off".
intro-banner = "long"

# Controls the prompt character(s) in the interactive terminal.
prompt = ">>> "

# Whether or not to pretty-print expressions before showing the result.
# Can be "always", "never" or "auto". The latter uses pretty-printing
# only in interactive mode.
pretty-print = "auto"

[exchange-rates]
# When and if to load exchange rates from the European Central Bank for
# currency conversions. Can be "on-startup" to always fetch exchange rates
# in the background when the application is started. With "on-first-use",
# Numbat only fetches exchange rates when they are needed. Exchange rate
# fetching can also be disabled using "never". The latter will lead to
# "unknown identifier" errors when a currency unit is being used.
fetching-policy = "on-startup"
```

### Custom functions, constants, units

If you want to add custom functions, constants, or units to your default environment,
create a `init.nbt` file in your config folder (e.g. `~/.config/numbat/init.nbt` on Linux).
create a `init.nbt` file in your config folder (`~/.config/numbat/init.nbt` on Linux).

### Custom modules

Expand Down
2 changes: 2 additions & 0 deletions numbat-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ dirs = "5"
numbat = { version = "1.7.0", path = "../numbat" }
colored = "2"
itertools = "0.11"
toml = { version = "0.8.6", features = ["parse"] }
serde = { version = "1.0.190", features = ["derive"] }

[dependencies.clap]
version = "4"
Expand Down
76 changes: 76 additions & 0 deletions numbat-cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use clap::ValueEnum;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Eq, Default, Debug, Clone, Copy, ValueEnum)]
#[serde(rename_all = "kebab-case")]
pub enum IntroBanner {
#[default]
Long,
Short,
Off,
}

#[derive(Debug, Clone, Copy, PartialEq, ValueEnum, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PrettyPrintMode {
Always,
Never,
Auto,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct MainConfig {
pub intro_banner: IntroBanner,
pub prompt: String,
pub pretty_print: PrettyPrintMode,
pub load_prelude: bool,
pub load_user_init: bool,
}

impl Default for MainConfig {
fn default() -> Self {
Self {
prompt: ">>> ".to_owned(),
intro_banner: IntroBanner::default(),
pretty_print: PrettyPrintMode::Auto,
load_prelude: true,
load_user_init: true,
}
}
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "kebab-case")]
pub enum ExchangeRateFetchingPolicy {
/// Always fetch exchange rates in the background when the application is started
#[default]
OnStartup,

/// Fetch exchange rates when a currency symbol is used
OnFirstUse,

/// Never fetch exchange rates
Never,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct ExchangeRateConfig {
pub fetching_policy: ExchangeRateFetchingPolicy,
}

impl Default for ExchangeRateConfig {
fn default() -> Self {
Self {
fetching_policy: ExchangeRateFetchingPolicy::default(),
}
}
}

#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default, deny_unknown_fields)]
pub struct Config {
pub main: MainConfig,
pub exchange_rates: ExchangeRateConfig,
}
Loading