Compare commits

..

No commits in common. "8ff9ce269d0bb5f18e290405792c3c6cf443cc57" and "51e7dc047a51c94ea0a7cced298ef52203916667" have entirely different histories.

4 changed files with 19 additions and 52 deletions

View file

@ -49,17 +49,12 @@ $ ./aliveline
## Configuration ## Configuration
Aliveline tries to find config at: Aliveline tries to find config at:
- `$ALIVELINE_CONFIG` - `$XDG_CONFIG_DIR/aliveline/config.toml`
- `$XDG_CONFIG_HOME/aliveline/config.toml`
- `$XDG_CONFIG_HOME/.aliveline`
- `$HOME/.config/aliveline/config.toml`
- `$HOME/.config/.aliveline`
- `$HOME/.aliveline/config.toml`
- `$HOME/.aliveline`
- `./config.toml` (relative to working directory) - `./config.toml` (relative to working directory)
If config isn't found, Aliveline uses default values defined in [config.toml](https://2ndbeam.ru/git/2ndbeam/aliveline/src/branch/master/config.toml). If config isn't found, Aliveline uses default values defined in [config.toml](https://2ndbeam.ru/git/2ndbeam/aliveline/src/branch/master/config.toml).
Keep in mind that default paths are relative, and in this case logs will be written to and read from current working directory. Keep in mind that default paths are relative, and in this case logs will be written to and read from current working directory.
## Contribution ## Contribution
You can contribute to Aliveline by creating an issue/PR on [Codeberg mirror](https://codeberg.org/2ndbeam/aliveline) ~~You can contribute to Aliveline by creating issue on this repository, then we'll discuss it.~~
I guess, it is not an option right now, since account creation is disabled on my Forgejo instance, but you may contact me via email: 2ndbeam@disroot.org

View file

@ -20,16 +20,14 @@ impl Default for Colors {
} }
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)] #[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(default)]
pub struct Config { pub struct Config {
/// directory, where config is located /// directory, where config is located
#[serde(skip)] #[serde(skip)]
pub conf_path: PathBuf, pub conf_path: PathBuf,
pub log_path: PathBuf, pub log_path: PathBuf,
#[serde(default)]
pub colors: Colors, pub colors: Colors,
#[serde(default)]
pub event_colors: Vec<u32>, pub event_colors: Vec<u32>,
#[serde(default)]
pub text_colors: Vec<u32>, pub text_colors: Vec<u32>,
} }
@ -92,13 +90,13 @@ impl Config {
} }
} }
pub fn load(path: PathBuf) -> Option<Self> { pub fn load(path: PathBuf) -> Self {
if let Ok(toml_string) = std::fs::read_to_string(path.clone()) { if let Ok(toml_string) = std::fs::read_to_string(path.clone()) {
if let Ok(mut conf) = toml::from_str::<Config>(&toml_string) { if let Ok(mut conf) = toml::from_str::<Config>(&toml_string) {
conf.conf_path = path.parent().unwrap().into(); conf.conf_path = path.parent().unwrap().into();
return Some(conf); return conf;
} }
} }
None Config::new(path)
} }
} }

View file

@ -4,47 +4,16 @@ use std::{hash::{DefaultHasher, Hash, Hasher}, path::PathBuf};
pub mod config; pub mod config;
pub mod log; pub mod log;
fn try_config_path(var: &'static str, path: &[&'static str]) -> Option<Config> {
match std::env::var(var) {
Ok(path_start) => {
let mut pathbuf = PathBuf::from(path_start);
for entry in path.iter() {
pathbuf.push(entry);
}
Config::load(pathbuf)
},
Err(_) => None,
}
}
/// This function tries to load config from several places (in order):
/// - $ALIVELINE_CONFIG
/// - $XDG_CONFIG_HOME/aliveline/config.toml
/// - $XDG_CONFIG_HOME/.aliveline
/// - $HOME/.config/aliveline/config.toml
/// - $HOME/.config/.aliveline
/// - $HOME/.aliveline
/// as a last resort it defaults to current working directory (./config.toml)
pub fn load_config() -> Config { pub fn load_config() -> Config {
let places = [ if let Ok(path_str) = std::env::var("XDG_CONFIG_HOME") {
("ALIVELINE_CONFIG", vec![]), let mut path = PathBuf::from(path_str);
("XDG_CONFIG_HOME", vec!["aliveline", "config.toml"]), path.push("aliveline");
("XDG_CONFIG_HOME", vec![".aliveline"]), path.push("config.toml");
("HOME", vec![".config", "aliveline", "config.toml"]), return Config::load(path);
("HOME", vec![".config", ".aliveline"]),
("HOME", vec![".aliveline", "config.toml"]),
("HOME", vec![".aliveline"]),
];
for (place_var, place) in places {
if let Some(conf) = try_config_path(place_var, place.as_slice()) {
println!("Found config at ${place_var} / {place:?}");
return conf;
}
println!("Config not found at ${place_var} / {place:?}");
} }
println!("Using last resort config path ./config.toml");
Config::new(PathBuf::from("./config.toml")) Config::new(PathBuf::from("./config.toml"))
} }
/// Get random-like color id in range 0..16 by computing string hash /// Get random-like color id in range 0..16 by computing string hash
pub fn color_id_from_name(name: String) -> i32 { pub fn color_id_from_name(name: String) -> i32 {
let mut s = DefaultHasher::new(); let mut s = DefaultHasher::new();

View file

@ -250,7 +250,12 @@ fn main() -> Result<(), Box<dyn Error>> {
None => None, None => None,
} }
}; };
ui.set_previous_event_name(prev_event_name.unwrap_or_default().to_shared_string()); if let Some(name) = prev_event_name {
println!("{name}");
ui.set_previous_event_name(name.to_shared_string());
} else {
ui.set_previous_event_name(SharedString::new());
}
} }
}); });