-
-
Notifications
You must be signed in to change notification settings - Fork 147
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
(WIP) Support configuration file #312
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
no_colors = false | ||
lang = "en_us.UTF-8" | ||
ascii_only = true | ||
palette = "8" | ||
|
||
|
||
[toggles] | ||
color_durations = true | ||
color_terminated = true | ||
Comment on lines
+1
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this checked in for testing purposes? Eventually, I think it could be good to have an example config file with the default options (and comments explaining them) checked into the repo, so that users can see how to configure the console. But, I don't know if it should be "live" --- it might be better to name the example something like "console.example.toml" or similar, so that running the console from the repo directory doesn't actually use those settings... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I added it unintentionally.
Comment on lines
+1
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if it makes sense to match the layout of the config structs exactly in the config file. instead, i think it might be good to group the different configurations into TOML tables, maybe something like this: [charset]
lang = "en_us.UTF-8"
ascii_only = false
[colors]
enabled = true
palette = "8"
[colors.enable]
durations = true
terminated = true or something like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds good, I'll try it. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
use crate::view::Palette; | ||
use clap::{ArgGroup, Parser as Clap, ValueHint}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::env; | ||
use std::fs; | ||
use std::path::Path; | ||
use std::process::Command; | ||
use std::str::FromStr; | ||
use std::time::Duration; | ||
|
@@ -28,7 +32,7 @@ pub struct Config { | |
#[clap(long = "log", env = "RUST_LOG", default_value = "off")] | ||
pub(crate) env_filter: tracing_subscriber::EnvFilter, | ||
|
||
#[clap(flatten)] | ||
#[clap(skip)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason I skipped it:
|
||
pub(crate) view_options: ViewOptions, | ||
|
||
/// How long to continue displaying completed tasks and dropped resources | ||
|
@@ -66,7 +70,7 @@ pub struct Config { | |
#[derive(Debug)] | ||
struct RetainFor(Option<Duration>); | ||
|
||
#[derive(Clap, Debug, Clone)] | ||
#[derive(Clap, Debug, Clone, Default, Deserialize, Serialize)] | ||
#[clap(group = ArgGroup::new("colors").conflicts_with("no-colors"))] | ||
pub struct ViewOptions { | ||
/// Disable ANSI colors entirely. | ||
|
@@ -107,7 +111,7 @@ pub struct ViewOptions { | |
} | ||
|
||
/// Toggles on and off color coding for individual UI elements. | ||
#[derive(Clap, Debug, Copy, Clone)] | ||
#[derive(Clap, Debug, Copy, Clone, Deserialize, Serialize)] | ||
pub struct ColorToggles { | ||
/// Disable color-coding for duration units. | ||
#[clap(long = "no-duration-colors", parse(from_flag = std::ops::Not::not), group = "colors")] | ||
|
@@ -118,9 +122,37 @@ pub struct ColorToggles { | |
pub(crate) color_terminated: bool, | ||
} | ||
|
||
impl Default for ColorToggles { | ||
fn default() -> Self { | ||
Self { | ||
color_durations: true, | ||
color_terminated: true, | ||
} | ||
} | ||
} | ||
|
||
// === impl Config === | ||
|
||
impl Config { | ||
pub fn from_config() -> color_eyre::Result<Self> { | ||
let xdg_config_path = env::var_os("XDG_CONFIG_HOME").map(|mut base| { | ||
base.push("/tokio-console/console.toml"); | ||
base | ||
}); | ||
let xdg_view_opt = xdg_config_path.and_then(ViewOptions::from_config); | ||
let current_view_opt = ViewOptions::from_config("console.toml"); | ||
|
||
let config = Config::parse(); | ||
|
||
match xdg_view_opt.or(current_view_opt) { | ||
None => Ok(config), | ||
Some(view_opt) => Ok(Self { | ||
view_options: view_opt, | ||
..config | ||
}), | ||
} | ||
Comment on lines
+147
to
+153
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eventually, I think what we'll want is a function on the
does that make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that makes sense. Ok, I'll try it again. |
||
} | ||
Comment on lines
+137
to
+154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm worried:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we might want to look into using the On macOS, we might also want to additionally check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
pub fn trace_init(&mut self) -> color_eyre::Result<()> { | ||
let filter = std::mem::take(&mut self.env_filter); | ||
use tracing_subscriber::prelude::*; | ||
|
@@ -166,6 +198,16 @@ impl Config { | |
// === impl ViewOptions === | ||
|
||
impl ViewOptions { | ||
pub(crate) fn from_config<P>(path: P) -> Option<Self> | ||
where | ||
P: AsRef<Path> + std::fmt::Debug, | ||
{ | ||
match fs::read_to_string(&path) { | ||
Err(_) => None, | ||
Ok(conf) => toml::from_str(&conf).ok(), | ||
} | ||
} | ||
|
||
pub fn is_utf8(&self) -> bool { | ||
self.lang.ends_with("UTF-8") && !self.ascii_only | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if it makes sense for the config file to have separate keys for
no_colors
andpalette
, if we can also writepalette = "off"
to disable colors. i'd prefer to not have two separate ways to write the same thing in the config file.