Skip to content

Commit

Permalink
[O] Better pathfinding
Browse files Browse the repository at this point in the history
  • Loading branch information
hykilpikonna committed Dec 22, 2024
1 parent f9c9a92 commit 982ac52
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 64 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tracing = { version = "0.1.40", default-features = false }
tracing-subscriber = { version = "0.3.18", default-features = false }
unicode-normalization = { version = "0.1.23", default-features = false }
unicode-segmentation = { version = "1.11.0", default-features = false }
which = { version = "7.0.1", default-features = false }

[workspace.lints.clippy]
arithmetic_side_effects = "warn"
1 change: 1 addition & 0 deletions crates/hyfetch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ toml_edit = { workspace = true, features = [], optional = true }
tracing = { workspace = true, features = ["attributes", "std"] }
tracing-subscriber = { workspace = true, features = ["ansi", "fmt", "smallvec", "std", "tracing-log"] }
unicode-segmentation = { workspace = true, features = [] }
which = { workspace = true, features = [] }

[build-dependencies]
indexmap = { workspace = true, features = ["std"] }
Expand Down
26 changes: 7 additions & 19 deletions crates/hyfetch/src/bin/hyfetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ use hyfetch::color_util::{
use hyfetch::models::Config;
#[cfg(feature = "macchina")]
use hyfetch::neofetch_util::macchina_path;
use hyfetch::neofetch_util::{
self, fastfetch_path, get_distro_ascii, literal_input, ColorAlignment, NEOFETCH_COLORS_AC,
NEOFETCH_COLOR_PATTERNS, TEST_ASCII,
};
use hyfetch::neofetch_util::{self, add_pkg_path, fastfetch_path, get_distro_ascii, literal_input, ColorAlignment, NEOFETCH_COLORS_AC, NEOFETCH_COLOR_PATTERNS, TEST_ASCII};
use hyfetch::presets::{AssignLightness, Preset};
use hyfetch::pride_month;
use hyfetch::types::{AnsiMode, Backend, TerminalTheme};
Expand All @@ -42,6 +39,8 @@ use time::{Month, OffsetDateTime};
use tracing::debug;

fn main() -> Result<()> {
add_pkg_path().expect("failed to add pkg path");

#[cfg(windows)]
if let Err(err) = enable_ansi_support::enable_ansi_support() {
debug!(%err, "could not enable ANSI escape code support");
Expand All @@ -58,20 +57,9 @@ fn main() -> Result<()> {
// Use a custom distro
let distro = options.distro.as_ref();

let backend = options.backend.map_or_else(
|| {
fastfetch_path()
.context("failed to get fastfetch path")
.map(|fastfetch_path| {
if fastfetch_path.is_some() {
Backend::Fastfetch
} else {
Backend::Neofetch
}
})
},
Ok,
)?;
let backend = options.backend.unwrap_or_else(|| {
if fastfetch_path().is_ok() { Backend::Fastfetch } else { Backend::Neofetch }
});

if options.test_print {
let asc = get_distro_ascii(distro, backend).context("failed to get distro ascii")?;
Expand Down Expand Up @@ -956,7 +944,7 @@ fn create_config(
.context("failed to print title prompt")?;

// Check if fastfetch is installed
let fastfetch_path = fastfetch_path().context("failed to get fastfetch path")?;
let fastfetch_path = fastfetch_path().ok();

// Check if macchina is installed
#[cfg(feature = "macchina")]
Expand Down
83 changes: 38 additions & 45 deletions crates/hyfetch/src/neofetch_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::{self, Write as _};
use std::path::{PathBuf};
use std::process::Command;
use std::sync::OnceLock;
use std::{fmt};
use std::{env, fmt};

use aho_corasick::AhoCorasick;
use anyhow::{Context as _, Result};
Expand All @@ -22,7 +22,7 @@ use strum::AsRefStr;
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;
Expand Down Expand Up @@ -145,10 +145,39 @@ where
}
}

/// Add the PyPI pacakge path to the PATH environment variable (for this local process only).
/// This is done so that `which` can find the commands inside the PyPI package.
pub fn add_pkg_path() -> Result<()> {
// Get PATH
let pv = &env::var_os("PATH").context("`PATH` env var is not set or invalid")?;
let mut path = env::split_paths(pv).collect::<Vec<_>>();
let exe = env::current_exe().context("failed to get path of current running executable")?;
let base = exe.parent().unwrap();

// Add from bin: ../git, ../fastfetch, ../scripts
let to_add = ["git", "fastfetch", "scripts", "fastfetch/usr/bin"];
if let Some(parent) = base.parent() {
path.extend(to_add.iter().map(|d| parent.join(d)));
}

// Add from cwd: ./hyfetch/git, ./hyfetch/fastfetch, ./hyfetch/scripts
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")?);

Ok(())
}

/// Gets the absolute path of the [neofetch] command.
///
/// [neofetch]: https://github.com/hykilpikonna/hyfetch#running-updated-original-neofetch
pub fn neofetch_path() -> Result<PathBuf> {
if let Ok(p) = which("neowofetch") {
return Ok(p);
}

// 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 mut file = fs::File::create(&f).context("Failed to create neofetch script file")?;
Expand All @@ -158,43 +187,6 @@ pub fn neofetch_path() -> Result<PathBuf> {
Ok(f)
}

/// Gets the absolute path of the [fastfetch] command.
///
/// [fastfetch]: https://github.com/fastfetch-cli/fastfetch
pub fn fastfetch_path() -> Result<Option<PathBuf>> {
let fastfetch_path = {
#[cfg(not(windows))]
{
find_in_path("fastfetch")
.context("failed to check existence of `fastfetch` in `PATH`")?
}
#[cfg(windows)]
{
find_in_path("fastfetch.exe")
.context("failed to check existence of `fastfetch.exe` in `PATH`")?
}
};

// Fall back to `fastfetch\fastfetch.exe` in directory of current executable
#[cfg(windows)]
let fastfetch_path = fastfetch_path.map_or_else(
|| {
let current_exe_path: PathBuf = env::current_exe()
.and_then(|p| p.normalize().map(|p| p.into()))
.context("failed to get path of current running executable")?;
let current_exe_dir_path = current_exe_path
.parent()
.expect("parent should not be `None`");
let fastfetch_path = current_exe_dir_path.join(r"fastfetch\fastfetch.exe");
find_file(&fastfetch_path)
.with_context(|| format!("failed to check existence of file {fastfetch_path:?}"))
},
|path| Ok(Some(path)),
)?;

Ok(fastfetch_path)
}

/// Gets the absolute path of the [macchina] command.
///
/// [macchina]: https://github.com/Macchina-CLI/macchina
Expand Down Expand Up @@ -427,18 +419,19 @@ where
Ok(out)
}

pub fn fastfetch_path() -> Result<PathBuf> {
which("fastfetch").context("fastfetch command not found")
}

fn make_fastfetch_command<S>(args: &[S]) -> Result<Command>
where
S: AsRef<OsStr>,
{
// Find fastfetch executable
let fastfetch_path = fastfetch_path()
.context("failed to get fastfetch path")?
.context("fastfetch command not found")?;

debug!(?fastfetch_path, "fastfetch path");
let ff_path = fastfetch_path()?;
debug!(?ff_path, "fastfetch path");

let mut command = Command::new(fastfetch_path);
let mut command = Command::new(ff_path);
command.args(args);
Ok(command)
}
Expand Down

0 comments on commit 982ac52

Please sign in to comment.