Merge pull request #2 from teohhanhui/riir

Get distro name from neofetch
This commit is contained in:
Teoh Han Hui 2024-06-28 05:17:12 +08:00 committed by GitHub
commit 6e0e3fd18a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 450 additions and 177 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }

289
Cargo.lock generated
View file

@ -2,55 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.86" version = "1.0.86"
@ -64,32 +15,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3280efcf6d66bc77c2cf9b67dc8acee47a217d9be67dd590b3230dffe663724d" checksum = "3280efcf6d66bc77c2cf9b67dc8acee47a217d9be67dd590b3230dffe663724d"
[[package]] [[package]]
name = "colorchoice" name = "cfg-if"
version = "1.0.1" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
]
[[package]]
name = "env_logger"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]] [[package]]
name = "equivalent" name = "equivalent"
@ -109,23 +38,18 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyfetch" name = "hyfetch"
version = "1.4.11" version = "1.4.11"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bpaf", "bpaf",
"env_logger",
"indexmap", "indexmap",
"log",
"rgb", "rgb",
"shell-words",
"strum", "strum",
"tracing",
"tracing-subscriber",
] ]
[[package]] [[package]]
@ -139,10 +63,10 @@ dependencies = [
] ]
[[package]] [[package]]
name = "is_terminal_polyfill" name = "lazy_static"
version = "1.70.0" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "log" name = "log"
@ -150,6 +74,34 @@ version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "nu-ansi-term"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
dependencies = [
"overload",
"winapi",
]
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "overload"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "pin-project-lite"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.86" version = "1.0.86"
@ -180,6 +132,27 @@ version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "smallvec"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]] [[package]]
name = "strum" name = "strum"
version = "0.26.3" version = "0.26.3"
@ -213,6 +186,73 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "tracing"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
dependencies = [
"log",
"once_cell",
"tracing-core",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b"
dependencies = [
"nu-ansi-term",
"sharded-slab",
"smallvec",
"thread_local",
"tracing-core",
"tracing-log",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
@ -220,80 +260,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]] [[package]]
name = "utf8parse" name = "valuable"
version = "0.2.2" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "windows-sys" name = "winapi"
version = "0.52.0" version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [ dependencies = [
"windows-targets", "winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
] ]
[[package]] [[package]]
name = "windows-targets" name = "winapi-i686-pc-windows-gnu"
version = "0.52.5" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "winapi-x86_64-pc-windows-gnu"
version = "0.52.5" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
[[package]]
name = "windows_i686_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
[[package]]
name = "windows_i686_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"

View file

@ -16,8 +16,9 @@ ansi_colours = { version = "1.2.2", default-features = false }
anyhow = { version = "1.0.86", default-features = false } anyhow = { version = "1.0.86", default-features = false }
bpaf = { version = "0.9.12", default-features = false } bpaf = { version = "0.9.12", default-features = false }
derive_more = { version = "1.0.0-beta.6", default-features = false } derive_more = { version = "1.0.0-beta.6", default-features = false }
env_logger = { version = "0.11.3", default-features = false }
indexmap = { version = "2.2.6", default-features = false } indexmap = { version = "2.2.6", default-features = false }
log = { version = "0.4.21", default-features = false }
rgb = { version = "0.8.37", default-features = false } rgb = { version = "0.8.37", default-features = false }
shell-words = { version = "1.1.0", default-features = false }
strum = { version = "0.26.3", default-features = false } strum = { version = "0.26.3", default-features = false }
tracing = { version = "0.1.40", default-features = false }
tracing-subscriber = { version = "0.3.18", default-features = false }

View file

@ -13,11 +13,12 @@ license = { workspace = true }
anyhow = { workspace = true, features = ["std"] } anyhow = { workspace = true, features = ["std"] }
bpaf = { workspace = true, features = [] } bpaf = { workspace = true, features = [] }
# derive_more = { workspace = true, features = ["std"] } # derive_more = { workspace = true, features = ["std"] }
env_logger = { workspace = true, features = ["auto-color", "humantime", "unstable-kv"] }
indexmap = { workspace = true, features = ["std"] } indexmap = { workspace = true, features = ["std"] }
log = { workspace = true, features = ["kv"] }
rgb = { workspace = true, features = [] } rgb = { workspace = true, features = [] }
shell-words = { workspace = true, features = ["std"] }
strum = { workspace = true, features = ["derive", "std"] } strum = { workspace = true, features = ["derive", "std"] }
tracing = { workspace = true, features = ["attributes", "std"] }
tracing-subscriber = { workspace = true, features = ["ansi", "fmt", "smallvec", "std", "tracing-log"] }
[features] [features]
default = ["autocomplete"] default = ["autocomplete"]

View file

@ -1,14 +1,73 @@
use anyhow::Result; use anyhow::{Context, Result};
use hyfetch::cli_options::options; use hyfetch::cli_options::options;
use log::debug; use hyfetch::neofetch_util::get_distro_ascii;
use tracing::debug;
fn main() -> Result<()> { fn main() -> Result<()> {
env_logger::init();
let options = options().fallback_to_usage().run(); let options = options().fallback_to_usage().run();
debug!(options:?; "CLI options");
init_tracing_subsriber(options.debug).context("Failed to init tracing subscriber")?;
debug!(?options, "CLI options");
// TODO
if options.test_print {
println!(
"{}",
get_distro_ascii(options.distro.as_ref()).context("Failed to get distro ascii")?
);
return Ok(());
}
// TODO // TODO
Ok(()) Ok(())
} }
fn init_tracing_subsriber(debug: bool) -> Result<()> {
use std::env;
use std::str::FromStr;
use tracing::Level;
use tracing_subscriber::filter::{LevelFilter, Targets};
use tracing_subscriber::fmt::Subscriber;
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::util::SubscriberInitExt as _;
let builder = Subscriber::builder();
// Remove the default max level filter from the subscriber; it will be added to
// the `Targets` filter instead if no filter is set in `RUST_LOG`.
// Replacing the default `LevelFilter` with an `EnvFilter` would imply this,
// but we can't replace the builder's filter with a `Targets` filter yet.
let builder = builder.with_max_level(LevelFilter::TRACE);
let subscriber = builder.finish();
let subscriber = {
let targets = match env::var("RUST_LOG") {
Ok(var) => Targets::from_str(&var)
.map_err(|e| {
eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e);
})
.unwrap_or_default(),
Err(env::VarError::NotPresent) => {
let targets = Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL);
if debug {
targets.with_target(env!("CARGO_CRATE_NAME"), Level::DEBUG)
} else {
targets
}
},
Err(e) => {
eprintln!("Ignoring `RUST_LOG`: {}", e);
Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
},
};
subscriber.with(targets)
};
subscriber
.try_init()
.context("Failed to set the global default subscriber")
}

View file

@ -1,13 +1,14 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use anyhow::Context;
#[cfg(feature = "autocomplete")] #[cfg(feature = "autocomplete")]
use bpaf::ShellComp; use bpaf::ShellComp;
use bpaf::{construct, long, OptionParser, Parser}; use bpaf::{construct, long, OptionParser, Parser};
use strum::VariantNames; use strum::VariantNames;
use crate::presets::Preset; use crate::presets::Preset;
use crate::types::AnsiMode; use crate::types::{AnsiMode, Backend};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Options { pub struct Options {
@ -15,17 +16,17 @@ pub struct Options {
pub config_file: Option<PathBuf>, pub config_file: Option<PathBuf>,
pub preset: Option<Preset>, pub preset: Option<Preset>,
pub mode: Option<AnsiMode>, pub mode: Option<AnsiMode>,
// pub backend: Option<Backend>, pub backend: Option<Backend>,
// pub backend_args: Option<String>, pub backend_args: Vec<String>,
// pub colors_scale: Option<f32>, pub colors_scale: Option<f32>,
// pub colors_set_lightness: Option<f32>, pub colors_set_lightness: Option<f32>,
// pub colors_use_overlay: bool, pub colors_use_overlay: bool,
// pub june: bool, pub june: bool,
// pub debug: bool, pub debug: bool,
// pub test_distro: Option<String>, pub distro: Option<String>,
// pub ascii_file: Option<PathBuf>, pub ascii_file: Option<PathBuf>,
// pub test_print: bool, pub test_print: bool,
// pub ask_exit: bool, pub ask_exit: bool,
} }
pub fn options() -> OptionParser<Options> { pub fn options() -> OptionParser<Options> {
@ -44,10 +45,12 @@ pub fn options() -> OptionParser<Options> {
PRESET={{{}}}", PRESET={{{}}}",
Preset::VARIANTS.join(",") Preset::VARIANTS.join(",")
)) ))
.argument::<String>("PRESET"); .argument("PRESET");
#[cfg(feature = "autocomplete")] #[cfg(feature = "autocomplete")]
let preset = preset.complete(complete_preset); let preset = preset.complete(complete_preset);
let preset = preset.parse(|s| Preset::from_str(&s)).optional(); let preset = preset
.parse(|s| Preset::from_str(&s).with_context(|| format!("Failed to parse preset `{s}`")))
.optional();
let mode = long("mode") let mode = long("mode")
.short('m') .short('m')
.help(&*format!( .help(&*format!(
@ -55,18 +58,84 @@ PRESET={{{}}}",
MODE={{{}}}", MODE={{{}}}",
AnsiMode::VARIANTS.join(",") AnsiMode::VARIANTS.join(",")
)) ))
.argument::<String>("MODE"); .argument("MODE");
#[cfg(feature = "autocomplete")] #[cfg(feature = "autocomplete")]
let mode = mode.complete(complete_mode); let mode = mode.complete(complete_mode);
let mode = mode.parse(|s| AnsiMode::from_str(&s)).optional(); let mode = mode
// TODO .parse(|s| AnsiMode::from_str(&s).with_context(|| format!("Failed to parse mode `{s}`")))
.optional();
let backend = long("backend")
.short('b')
.help(&*format!(
"Choose a *fetch backend
BACKEND={{{}}}",
Backend::VARIANTS.join(",")
))
.argument("BACKEND");
#[cfg(feature = "autocomplete")]
let backend = backend.complete(complete_backend);
let backend = backend
.parse(|s| Backend::from_str(&s).with_context(|| format!("Failed to parse backend `{s}`")))
.optional();
let backend_args = long("args")
.help("Additional arguments pass-through to backend")
.argument::<String>("ARGS")
.parse(|s| shell_words::split(&s).context("Failed to split args for shell"))
.fallback(vec![]);
let colors_scale = long("c-scale")
.help("Lighten colors by a multiplier")
.argument("SCALE")
.optional();
let colors_set_lightness = long("c-set-l")
.help("Set lightness value of the colors")
.argument("LIGHT")
.optional();
let colors_use_overlay = long("c-overlay")
.help("Use experimental overlay color adjusting instead of HSL lightness")
.switch();
let june = long("june").help("Show pride month easter egg").switch();
let debug = long("debug").help("Debug mode").switch();
let distro = long("distro")
.help("Test for a specific distro")
.argument("DISTRO")
.optional();
let test_distro = long("test-distro")
.help("Test for a specific distro")
.argument("DISTRO")
.optional();
let distro = construct!([distro, test_distro]);
let ascii_file = long("ascii-file")
.help("Use a specific file for the ascii art")
.argument("ASCII_FILE");
#[cfg(feature = "autocomplete")]
let ascii_file = ascii_file.complete_shell(ShellComp::Nothing);
let ascii_file = ascii_file.optional();
let test_print = long("test-print")
.help("Print the ascii distro and exit")
.switch()
.hide();
let ask_exit = long("ask-exit")
.help("Ask for input before exiting")
.switch()
.hide();
construct!(Options { construct!(Options {
config, config,
config_file, config_file,
preset, preset,
mode, mode,
// TODO backend,
backend_args,
colors_scale,
colors_set_lightness,
colors_use_overlay,
june,
debug,
distro,
ascii_file,
// hidden
test_print,
ask_exit,
}) })
.to_options() .to_options()
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
@ -100,6 +169,20 @@ fn complete_mode(input: &String) -> Vec<(String, Option<String>)> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
#[cfg(feature = "autocomplete")]
fn complete_backend(input: &String) -> Vec<(String, Option<String>)> {
Backend::VARIANTS
.iter()
.filter_map(|&name| {
if name.starts_with(input) {
Some((name.to_owned(), None))
} else {
None
}
})
.collect::<Vec<_>>()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -1,4 +1,5 @@
pub mod cli_options; pub mod cli_options;
pub mod color_util; pub mod color_util;
pub mod neofetch_util;
pub mod presets; pub mod presets;
pub mod types; pub mod types;

View file

@ -0,0 +1,128 @@
use std::borrow::Cow;
use std::ffi::OsStr;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt as _;
use std::path::PathBuf;
use std::process::Command;
use std::{env, fmt};
use anyhow::{anyhow, Context, Result};
use tracing::debug;
/// Gets the absolute path of the neofetch command.
pub fn get_command_path() -> Result<PathBuf> {
if let Some(workspace_dir) = option_env!("CARGO_WORKSPACE_DIR") {
let path = PathBuf::from(workspace_dir);
if path.exists() {
let path = path.join("neofetch");
match path.try_exists() {
Ok(true) => {
return path.canonicalize().context("Failed to canonicalize path");
},
Ok(false) => {
Err(anyhow!("{path:?} does not exist or is not readable"))?;
},
Err(err) => {
Err(err)
.with_context(|| format!("Failed to check for existence of {path:?}"))?;
},
}
}
}
let Ok(path_env) = env::var("PATH") else {
return Err(anyhow!("`PATH` env var is not set or invalid"));
};
for search_path in env::split_paths(&path_env) {
let path = search_path.join("neowofetch");
if !path.is_file() {
continue;
}
return path.canonicalize().context("Failed to canonicalize path");
}
Err(anyhow!("neofetch command not found"))
}
/// Gets the distro ascii of the current distro. Or if distro is specified, get
/// the specific distro's ascii art instead.
#[tracing::instrument(level = "debug")]
pub fn get_distro_ascii<S>(distro: Option<S>) -> Result<String>
where
S: AsRef<str> + fmt::Debug,
{
let distro: Cow<_> = if let Some(distro) = distro.as_ref() {
distro.as_ref().into()
} else {
get_distro_name()
.context("Failed to get distro name")?
.into()
};
debug!(%distro, "distro name");
todo!()
}
/// Runs neofetch command, returning the piped stdout output.
#[tracing::instrument(level = "debug")]
fn run_neofetch_command_piped<S>(args: &[S]) -> Result<String>
where
S: AsRef<OsStr> + fmt::Debug,
{
let mut command = make_neofetch_command(args).context("Failed to make neofetch command")?;
let output = command
.output()
.context("Failed to execute neofetch as child process")?;
debug!(?output, "neofetch output");
if !output.status.success() {
let err = if let Some(code) = output.status.code() {
anyhow!("neofetch process exited with status code: {code}")
} else {
#[cfg(unix)]
{
anyhow!(
"neofetch process terminated by signal: {}",
output
.status
.signal()
.expect("either one of status code or signal should be set")
)
}
#[cfg(not(unix))]
unimplemented!("status code not expected to be `None` on non-Unix platforms")
};
Err(err)?;
}
let out = String::from_utf8(output.stdout)
.context("Failed to process neofetch output as it contains invalid UTF-8")?
.trim()
.to_owned();
Ok(out)
}
fn make_neofetch_command<S>(args: &[S]) -> Result<Command>
where
S: AsRef<OsStr>,
{
#[cfg(not(windows))]
{
let mut command = Command::new("bash");
command.arg(get_command_path().context("Failed to get neofetch command path")?);
command.args(args);
Ok(command)
}
#[cfg(windows)]
{
todo!()
}
}
#[tracing::instrument(level = "debug")]
fn get_distro_name() -> Result<String> {
run_neofetch_command_piped(&["ascii_distro_name"])
.context("Failed to get distro name from neofetch")
}

View file

@ -7,3 +7,12 @@ pub enum AnsiMode {
Ansi256, Ansi256,
Rgb, Rgb,
} }
#[derive(Clone, Eq, PartialEq, Hash, Debug, EnumString, VariantNames)]
#[strum(serialize_all = "kebab-case")]
pub enum Backend {
Qwqfetch,
Neofetch,
Fastfetch,
FastfetchOld,
}