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
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "termframe"
version = "0.8.0-alpha.3"
version = "0.8.0-alpha.4"
edition = "2024"
rust-version = "1.91.0"
description = "Terminal output SVG screenshot tool"
Expand Down
2 changes: 1 addition & 1 deletion doc/help-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion doc/help-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 4 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use clap_complete::Shell;
use enumset_ext::convert::str::EnumSet;

// local imports
use crate::config::{
self, DimensionWithInitial, FontFamilyOption, PaddingOption, Settings, ThemeSetting,
};
use crate::config::{self, DimensionWithInitial, FontFamilyOption, PaddingOption, Settings};

const STYLES: Styles = Styles::styled()
.header(AnsiColor::Green.on_default().bold())
Expand Down Expand Up @@ -127,8 +125,8 @@ pub struct Opt {
pub mode: config::mode::ModeSetting,

/// Color theme.
#[arg(long, overrides_with = "theme")]
pub theme: Option<String>,
#[arg(long, default_value_t = cfg().theme.clone().normalized(), overrides_with = "theme")]
pub theme: config::ThemeSetting,

/// Enable window.
#[arg(long,
Expand Down Expand Up @@ -316,9 +314,7 @@ impl config::Patch for Opt {
settings.rendering.faint_opacity = self.faint_opacity.into();
settings.rendering.line_height = self.line_height.into();
settings.rendering.bold_is_bright = self.bold_is_bright;
if let Some(theme) = &self.theme {
settings.theme = ThemeSetting::Fixed(theme.clone());
}
settings.theme = self.theme.clone();
if let Some(padding) = self.padding {
settings.padding = PaddingOption::Uniform(padding.into());
}
Expand Down
45 changes: 45 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,51 @@ impl ThemeSetting {
},
}
}

/// Normalize the theme setting by converting adaptive themes with identical light and dark themes to fixed themes.
pub fn normalized(self) -> Self {
match self {
Self::Fixed(theme) => Self::Fixed(theme),
Self::Adaptive { light, dark } => {
if light == dark {
Self::Fixed(light)
} else {
Self::Adaptive { light, dark }
}
}
}
}
}

impl fmt::Display for ThemeSetting {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Fixed(theme) => write!(f, "{theme}"),
Self::Adaptive { light, dark } => write!(f, "dark:{dark},light:{light}"),
}
}
}

impl From<&str> for ThemeSetting {
fn from(s: &str) -> Self {
let mut dark = None;
let mut light = None;

for part in s.split(',') {
if let Some((key, value)) = part.split_once(':') {
match key.trim() {
"dark" => dark = Some(value.trim().to_string()),
"light" => light = Some(value.trim().to_string()),
_ => {}
}
}
}

match (dark, light) {
(Some(dark), Some(light)) => Self::Adaptive { light, dark },
_ => Self::Fixed(s.to_string()),
}
}
}

pub type Fonts = Vec<FontFace>;
Expand Down
56 changes: 56 additions & 0 deletions src/config/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,62 @@ fn test_theme_setting_resolve() {
);
}

#[test]
fn test_theme_setting_normalized() {
let fixed = ThemeSetting::Fixed("dark".to_string());
let normalized = fixed.normalized();
assert!(matches!(normalized, ThemeSetting::Fixed(ref s) if s == "dark"));

let adaptive = ThemeSetting::Adaptive {
light: "same".to_string(),
dark: "same".to_string(),
};
let normalized = adaptive.normalized();
assert!(matches!(normalized, ThemeSetting::Fixed(ref s) if s == "same"));

let adaptive = ThemeSetting::Adaptive {
light: "light-theme".to_string(),
dark: "dark-theme".to_string(),
};
let normalized = adaptive.normalized();
assert!(
matches!(normalized, ThemeSetting::Adaptive { ref light, ref dark } if light == "light-theme" && dark == "dark-theme")
);
}

#[test]
fn test_theme_setting_display() {
let fixed = ThemeSetting::Fixed("my-theme".to_string());
assert_eq!(fixed.to_string(), "my-theme");

let adaptive = ThemeSetting::Adaptive {
light: "light-theme".to_string(),
dark: "dark-theme".to_string(),
};
assert_eq!(adaptive.to_string(), "dark:dark-theme,light:light-theme");
}

#[test]
fn test_theme_setting_from_str() {
let fixed = ThemeSetting::from("my-theme");
assert!(matches!(fixed, ThemeSetting::Fixed(ref s) if s == "my-theme"));

let adaptive = ThemeSetting::from("dark:dark-theme,light:light-theme");
assert!(
matches!(adaptive, ThemeSetting::Adaptive { ref light, ref dark } if light == "light-theme" && dark == "dark-theme")
);

let adaptive = ThemeSetting::from("light:light-theme,dark:dark-theme");
assert!(
matches!(adaptive, ThemeSetting::Adaptive { ref light, ref dark } if light == "light-theme" && dark == "dark-theme")
);

let adaptive = ThemeSetting::from("dark:dark-theme,unknown:foo,light:light-theme");
assert!(
matches!(adaptive, ThemeSetting::Adaptive { ref light, ref dark } if light == "light-theme" && dark == "dark-theme")
);
}

#[test]
fn test_font_weight_display() {
assert_eq!(FontWeight::Normal.to_string(), "normal");
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl App {
let theme = if theme == "-" {
AdaptiveTheme::default().resolve(mode)
} else {
log::debug!("use theme {:?}", theme);
let cfg = ThemeConfig::load_hybrid(theme)?;
Rc::new(Theme::from_config(cfg.theme.resolve(mode)))
};
Expand Down