1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-15 09:44:22 +00:00
starship/src/configs/custom.rs
Tilmann Meyer 2233683410
feat: add error messaging (#1576)
This creates a custom logger for the log crate which logs everything to a file (/tmp/starship/session_$STARSHIP_SESSION_KEY.log) and it logs everything above Warn to stderr, but only if the log file does not contain the line that should be logged resulting in an error or warning to be only logged at the first starship invocation after opening the shell.
2020-09-28 16:38:50 -04:00

94 lines
2.5 KiB
Rust

use crate::config::{ModuleConfig, RootModuleConfig, VecOr};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, Default, PartialEq)]
pub struct Files<'a>(pub Vec<&'a str>);
#[derive(Clone, Default, PartialEq)]
pub struct Extensions<'a>(pub Vec<&'a str>);
#[derive(Clone, Default, PartialEq)]
pub struct Directories<'a>(pub Vec<&'a str>);
#[derive(Clone, ModuleConfig)]
pub struct CustomConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub command: &'a str,
pub when: Option<&'a str>,
pub shell: VecOr<&'a str>,
pub description: &'a str,
pub style: &'a str,
pub disabled: bool,
pub files: Files<'a>,
pub extensions: Extensions<'a>,
pub directories: Directories<'a>,
}
impl<'a> RootModuleConfig<'a> for CustomConfig<'a> {
fn new() -> Self {
CustomConfig {
format: "[$symbol$output]($style) ",
symbol: "",
command: "",
when: None,
shell: VecOr::default(),
description: "<custom config>",
style: "green bold",
disabled: false,
files: Files::default(),
extensions: Extensions::default(),
directories: Directories::default(),
}
}
}
impl<'a> ModuleConfig<'a> for Files<'a> {
fn from_config(config: &'a toml::Value) -> Option<Self> {
let mut files = Vec::new();
for item in config.as_array()? {
if let Some(file) = item.as_str() {
files.push(file);
} else {
log::warn!("Unexpected file {:?}", item);
}
}
Some(Files(files))
}
}
impl<'a> ModuleConfig<'a> for Extensions<'a> {
fn from_config(config: &'a toml::Value) -> Option<Self> {
let mut extensions = Vec::new();
for item in config.as_array()? {
if let Some(file) = item.as_str() {
extensions.push(file);
} else {
log::warn!("Unexpected extension {:?}", item);
}
}
Some(Extensions(extensions))
}
}
impl<'a> ModuleConfig<'a> for Directories<'a> {
fn from_config(config: &'a toml::Value) -> Option<Self> {
let mut directories = Vec::new();
for item in config.as_array()? {
if let Some(file) = item.as_str() {
directories.push(file);
} else {
log::warn!("Unexpected directory {:?}", item);
}
}
Some(Directories(directories))
}
}