Skip to content

Commit

Permalink
optional light-dark
Browse files Browse the repository at this point in the history
  • Loading branch information
wetfloo committed Jan 10, 2025
1 parent f4e2bd2 commit 6473891
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 36 deletions.
14 changes: 10 additions & 4 deletions crates/hyfetch/src/bin/hyfetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ fn main() -> Result<()> {
};
let theme = if auto_detect_light_dark {
let res = det_bg();
res?.map(|bg| bg.theme()).unwrap_or(config.light_dark)
res?.map(|bg| bg.theme())
.unwrap_or(config.light_dark.unwrap_or_default())
} else {
config.light_dark
config.light_dark.unwrap_or_default()
};

// Check if it's June (pride month)
Expand Down Expand Up @@ -148,7 +149,12 @@ fn main() -> Result<()> {
} else if let Some(lightness) = options.lightness {
color_profile.with_lightness(AssignLightness::Replace(lightness))
} else {
color_profile.with_lightness_adaptive(config.lightness(), theme)
color_profile.with_lightness_adaptive(
config
.lightness
.unwrap_or_else(|| Config::default_lightness(theme)),
theme,
)
};
debug!(?color_profile, "lightened color profile");

Expand Down Expand Up @@ -1024,7 +1030,7 @@ fn create_config(
let config = Config {
preset,
mode: color_mode,
light_dark: theme,
light_dark: Some(theme),
auto_detect_light_dark: Some(det_bg.is_some()),
lightness: Some(lightness),
color_align,
Expand Down
27 changes: 19 additions & 8 deletions crates/hyfetch/src/font_logo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ const FONT_LOGOS: &str = include_str!("../../../hyfetch/data/font_logos.json");

pub fn get_font_logo(backend: Backend) -> Result<String> {
// Check if the cache file exists and return its contents if it does
let cache_path = get_cache_path().context("Failed to get cache path")?.join("font_logo");
let cache_path = get_cache_path()
.context("Failed to get cache path")?
.join("font_logo");
if cache_path.exists() {
let mut cached_logo = String::new();
File::open(cache_path).context("Failed to open cache file")?
.read_to_string(&mut cached_logo).context("Failed to read from cache file")?;
File::open(cache_path)
.context("Failed to open cache file")?
.read_to_string(&mut cached_logo)
.context("Failed to read from cache file")?;
return Ok(cached_logo);
}

// Deserialize the JSON into a HashMap
let font_logos: HashMap<String, String> = serde_json::from_str::<HashMap<String, String>>(FONT_LOGOS)
.context("Failed to deserialize font logos JSON file")?
.into_iter().map(|(k, v)| (k.to_lowercase(), v)).collect();
let font_logos: HashMap<String, String> =
serde_json::from_str::<HashMap<String, String>>(FONT_LOGOS)
.context("Failed to deserialize font logos JSON file")?
.into_iter()
.map(|(k, v)| (k.to_lowercase(), v))
.collect();

// Get the distro name
let distro = get_distro_name(backend).context("Failed to get distro name")?.to_lowercase();
let distro = get_distro_name(backend)
.context("Failed to get distro name")?
.to_lowercase();

// Find the most likely matching distro from font_logos
let matched_distro = font_logos.keys().find(|&k| distro.contains(k))
Expand All @@ -41,7 +50,9 @@ pub fn get_font_logo(backend: Backend) -> Result<String> {

// Write the logo to the cache file
let mut cache_file = File::create(cache_path).context("Failed to create cache file")?;
cache_file.write_all(logo.as_bytes()).context("Failed to write logo to cache file")?;
cache_file
.write_all(logo.as_bytes())
.context("Failed to write logo to cache file")?;

Ok(logo.clone())
}
7 changes: 1 addition & 6 deletions crates/hyfetch/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Config {
pub preset: Preset,
pub mode: AnsiMode,
pub auto_detect_light_dark: Option<bool>,
pub light_dark: TerminalTheme,
pub light_dark: Option<TerminalTheme>,
pub lightness: Option<Lightness>,
pub color_align: ColorAlignment,
pub backend: Backend,
Expand All @@ -32,11 +32,6 @@ impl Config {
},
}
}

pub fn lightness(&self) -> Lightness {
self.lightness
.unwrap_or_else(|| Self::default_lightness(self.light_dark))
}
}

mod args_serde {
Expand Down
51 changes: 33 additions & 18 deletions crates/hyfetch/src/neofetch_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ use std::ffi::OsStr;
#[cfg(feature = "macchina")]
use std::fs;
use std::io::{self, Write as _};
use std::path::{PathBuf};
use std::path::PathBuf;
use std::process::Command;
use std::sync::OnceLock;
use std::{env, fmt};

use crate::ascii::{RawAsciiArt, RecoloredAsciiArt};
use crate::color_util::{printc, NeofetchAsciiIndexedColor, PresetIndexedColor};
use crate::distros::Distro;
use crate::types::{AnsiMode, Backend};
#[cfg(windows)]
use crate::utils::find_file;
use crate::utils::{find_in_path, get_cache_path, input, process_command_status};
use aho_corasick::AhoCorasick;
#[cfg(windows)]
use anyhow::anyhow;
use anyhow::{Context as _, Result};
use indexmap::IndexMap;
use itertools::Itertools as _;
#[cfg(windows)]
use anyhow::anyhow;
#[cfg(windows)]
use crate::utils::find_file;
#[cfg(windows)]
use std::path::Path;
#[cfg(windows)]
use normpath::PathExt as _;
#[cfg(windows)]
use same_file::is_same_file;
use serde::{Deserialize, Serialize};
#[cfg(windows)]
use std::path::Path;
use strum::AsRefStr;
#[cfg(feature = "macchina")]
use toml_edit::{value, DocumentMut, Item, Table};
use tracing::debug;
use unicode_segmentation::UnicodeSegmentation as _;
use which::which;
use crate::ascii::{RawAsciiArt, RecoloredAsciiArt};
use crate::color_util::{printc, NeofetchAsciiIndexedColor, PresetIndexedColor};
use crate::distros::Distro;
use crate::types::{AnsiMode, Backend};
use crate::utils::{find_in_path, get_cache_path, input, process_command_status};

pub const TEST_ASCII: &str = r####################"
### |\___/| ###
Expand Down Expand Up @@ -170,8 +170,14 @@ pub fn add_pkg_path() -> Result<()> {
path.extend(to_add.iter().map(|d| PathBuf::from("hyfetch").join(d)));

// Set PATH
env::set_var("PATH", env::join_paths(path).context("failed to join paths")?);
debug!("Added PyPI package path to PATH, PATH={}", env::var("PATH")?);
env::set_var(
"PATH",
env::join_paths(path).context("failed to join paths")?,
);
debug!(
"Added PyPI package path to PATH, PATH={}",
env::var("PATH")?
);

Ok(())
}
Expand All @@ -185,7 +191,9 @@ pub fn neofetch_path() -> Result<PathBuf> {
}

// Instead of doing that, let's write the neofetch script to a temp file
let f: PathBuf = get_cache_path().context("Failed to get cache path")?.join("nf_script.sh");
let f: PathBuf = get_cache_path()
.context("Failed to get cache path")?
.join("nf_script.sh");
let mut file = fs::File::create(&f).context("Failed to create neofetch script file")?;
file.write_all(NEOFETCH_SCRIPT.as_bytes())
.context("Failed to write neofetch script to file")?;
Expand Down Expand Up @@ -342,14 +350,21 @@ fn bash_path() -> Result<PathBuf> {
match is_same_file(&bash_path, Path::new(&windir).join(r"System32\bash.exe")) {
Ok(false) => return Ok(bash_path),
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(bash_path),
_ => {}
_ => {},
}
}
}

if let Some(bash_path) = find_in_path("git.exe").context("failed to find `git.exe` in `PATH`")? {
if let Some(bash_path) =
find_in_path("git.exe").context("failed to find `git.exe` in `PATH`")?
{
if bash_path.ends_with(r"Git\cmd\git.exe") {
let pth = bash_path.parent().unwrap().parent().unwrap().join(r"bin\bash.exe");
let pth = bash_path
.parent()
.unwrap()
.parent()
.unwrap()
.join(r"bin\bash.exe");
if pth.is_file() {
return Ok(pth);
}
Expand Down

0 comments on commit 6473891

Please sign in to comment.