Skip to content

Commit ff46c8f

Browse files
authored
Merge pull request hykilpikonna#14 from teohhanhui/riir
Add more scaffolding
2 parents 475b10b + 5921368 commit ff46c8f

File tree

6 files changed

+118
-27
lines changed

6 files changed

+118
-27
lines changed

Diff for: crates/hyfetch/src/bin/hyfetch.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use anyhow::{Context, Result};
77
use chrono::Datelike;
88
use hyfetch::cli_options::options;
99
use hyfetch::models::Config;
10-
use hyfetch::neofetch_util::get_distro_ascii;
10+
use hyfetch::neofetch_util::{self, get_distro_ascii};
1111
use hyfetch::presets::AssignLightness;
1212
use hyfetch::utils::get_cache_path;
1313
use tracing::debug;
@@ -21,10 +21,15 @@ fn main() -> Result<()> {
2121

2222
// TODO
2323

24+
// Use a custom distro
25+
let distro = options.distro.as_ref();
26+
27+
// TODO
28+
2429
if options.test_print {
2530
println!(
2631
"{}",
27-
get_distro_ascii(options.distro.as_ref()).context("failed to get distro ascii")?
32+
get_distro_ascii(distro).context("failed to get distro ascii")?
2833
);
2934
return Ok(());
3035
}
@@ -63,7 +68,12 @@ fn main() -> Result<()> {
6368
}
6469
}
6570

66-
// TODO
71+
// Use a custom distro
72+
let distro = options.distro.as_ref().or(config.distro.as_ref());
73+
74+
let color_mode = options.mode.unwrap_or(config.mode);
75+
let backend = options.backend.unwrap_or(config.backend);
76+
let args = options.args.as_ref().or(config.args.as_ref());
6777

6878
// Get preset
6979
let preset = options.preset.unwrap_or(config.preset);
@@ -76,11 +86,27 @@ fn main() -> Result<()> {
7686
} else if let Some(lightness) = options.lightness {
7787
color_profile.with_lightness(AssignLightness::Replace(lightness))
7888
} else {
79-
color_profile.with_lightness_dl(config.lightness(), config.light_dark)
89+
color_profile.with_lightness_dl(config.lightness(), config.light_dark, options.overlay)
8090
};
8191
debug!(?color_profile, "lightened color profile");
8292

83-
// TODO
93+
let asc = if let Some(path) = options.ascii_file {
94+
fs::read_to_string(&path).with_context(|| format!("failed to read ascii from {path:?}"))?
95+
} else {
96+
get_distro_ascii(distro).context("failed to get distro ascii")?
97+
};
98+
let asc = config
99+
.color_align
100+
.recolor_ascii(asc, color_profile, color_mode, config.light_dark);
101+
neofetch_util::run(asc, backend, args).context("failed to run")?;
102+
103+
if options.ask_exit {
104+
print!("Press any key to exit...");
105+
let mut buf = String::new();
106+
io::stdin()
107+
.read_line(&mut buf)
108+
.context("failed to read line from input")?;
109+
}
84110

85111
Ok(())
86112
}

Diff for: crates/hyfetch/src/cli_options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Options {
1919
pub preset: Option<Preset>,
2020
pub mode: Option<AnsiMode>,
2121
pub backend: Option<Backend>,
22-
pub args: Vec<String>,
22+
pub args: Option<Vec<String>>,
2323
pub scale: Option<f32>,
2424
pub lightness: Option<Lightness>,
2525
pub overlay: bool,
@@ -107,7 +107,7 @@ BACKEND={{{}}}",
107107
.help("Additional arguments pass-through to backend")
108108
.argument::<String>("ARGS")
109109
.parse(|s| shell_words::split(&s).context("ARGS should be valid command-line arguments"))
110-
.fallback(vec![]);
110+
.optional();
111111
let scale = long("c-scale")
112112
.help("Lighten colors by a multiplier")
113113
.argument("SCALE")

Diff for: crates/hyfetch/src/models.rs

+45-11
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ pub struct Config {
1313
lightness: Option<Lightness>,
1414
pub color_align: ColorAlignment,
1515
pub backend: Backend,
16+
#[serde(default)]
1617
#[serde(with = "self::args_serde_with")]
17-
pub args: Vec<String>,
18+
pub args: Option<Vec<String>>,
1819
pub distro: Option<String>,
1920
pub pride_month_disable: bool,
2021
}
@@ -39,48 +40,81 @@ mod args_serde_with {
3940
use serde::de::{self, value, Deserialize, Deserializer, SeqAccess, Visitor};
4041
use serde::ser::Serializer;
4142

42-
pub(super) fn serialize<S>(value: &Vec<String>, serializer: S) -> Result<S::Ok, S::Error>
43+
pub(super) fn serialize<S>(
44+
value: &Option<Vec<String>>,
45+
serializer: S,
46+
) -> Result<S::Ok, S::Error>
4347
where
4448
S: Serializer,
4549
{
46-
serializer.serialize_str(&shell_words::join(value))
50+
match value {
51+
Some(value) => serializer.serialize_some(&shell_words::join(value)),
52+
None => serializer.serialize_none(),
53+
}
4754
}
4855

49-
pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
56+
pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<String>>, D::Error>
5057
where
5158
D: Deserializer<'de>,
5259
{
5360
struct StringOrVec;
5461

62+
struct OptionVisitor;
63+
5564
impl<'de> Visitor<'de> for StringOrVec {
5665
type Value = Vec<String>;
5766

5867
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
5968
formatter.write_str("string or list of strings")
6069
}
6170

71+
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
72+
where
73+
E: de::Error,
74+
{
75+
shell_words::split(s).map_err(de::Error::custom)
76+
}
77+
78+
fn visit_seq<S>(self, seq: S) -> Result<Self::Value, S::Error>
79+
where
80+
S: SeqAccess<'de>,
81+
{
82+
Deserialize::deserialize(value::SeqAccessDeserializer::new(seq))
83+
}
84+
}
85+
86+
impl<'de> Visitor<'de> for OptionVisitor {
87+
type Value = Option<Vec<String>>;
88+
89+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
90+
formatter.write_str("option")
91+
}
92+
93+
#[inline]
6294
fn visit_unit<E>(self) -> Result<Self::Value, E>
6395
where
6496
E: de::Error,
6597
{
66-
Ok(vec![])
98+
Ok(None)
6799
}
68100

69-
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
101+
#[inline]
102+
fn visit_none<E>(self) -> Result<Self::Value, E>
70103
where
71104
E: de::Error,
72105
{
73-
shell_words::split(s).map_err(de::Error::custom)
106+
Ok(None)
74107
}
75108

76-
fn visit_seq<S>(self, seq: S) -> Result<Self::Value, S::Error>
109+
#[inline]
110+
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
77111
where
78-
S: SeqAccess<'de>,
112+
D: Deserializer<'de>,
79113
{
80-
Deserialize::deserialize(value::SeqAccessDeserializer::new(seq))
114+
deserializer.deserialize_any(StringOrVec).map(Some)
81115
}
82116
}
83117

84-
deserializer.deserialize_any(StringOrVec)
118+
deserializer.deserialize_option(OptionVisitor)
85119
}
86120
}

Diff for: crates/hyfetch/src/neofetch_util.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ use tracing::debug;
1515

1616
use crate::color_util::{NeofetchAsciiIndexedColor, PresetIndexedColor};
1717
use crate::distros::Distro;
18+
use crate::presets::ColorProfile;
19+
use crate::types::{AnsiMode, Backend, LightDark};
1820

19-
const NEOFETCH_COLOR_PATTERN: &str = r"\$\{c[0-9]\}";
21+
const NEOFETCH_COLOR_PATTERN: &str = r"\$\{c[0-6]\}";
2022
static NEOFETCH_COLOR_RE: OnceLock<Regex> = OnceLock::new();
2123

2224
#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
23-
#[serde(rename_all = "lowercase", tag = "mode")]
25+
#[serde(tag = "mode")]
26+
#[serde(rename_all = "lowercase")]
2427
pub enum ColorAlignment {
2528
Horizontal {
2629
fore_back: Option<(NeofetchAsciiIndexedColor, NeofetchAsciiIndexedColor)>,
@@ -34,6 +37,19 @@ pub enum ColorAlignment {
3437
},
3538
}
3639

40+
impl ColorAlignment {
41+
/// Uses the color alignment to recolor an ascii art.
42+
pub fn recolor_ascii(
43+
&self,
44+
asc: String,
45+
color_profile: ColorProfile,
46+
color_mode: AnsiMode,
47+
term: LightDark,
48+
) -> String {
49+
todo!()
50+
}
51+
}
52+
3753
/// Gets the absolute path of the neofetch command.
3854
pub fn get_command_path() -> Result<PathBuf> {
3955
if let Ok(workspace_dir) = env::var("CARGO_WORKSPACE_DIR") {
@@ -93,6 +109,10 @@ where
93109
todo!()
94110
}
95111

112+
pub fn run(asc: String, backend: Backend, args: Option<&Vec<String>>) -> Result<()> {
113+
todo!()
114+
}
115+
96116
/// Gets distro ascii width and height, ignoring color code.
97117
pub fn ascii_size<S>(asc: S) -> (u8, u8)
98118
where
@@ -101,9 +121,7 @@ where
101121
let asc = asc.as_ref();
102122

103123
let Some(width) = NEOFETCH_COLOR_RE
104-
.get_or_init(|| {
105-
Regex::new(NEOFETCH_COLOR_PATTERN).expect("neofetch color regex should not be invalid")
106-
})
124+
.get_or_init(|| Regex::new(NEOFETCH_COLOR_PATTERN).unwrap())
107125
.replace_all(asc, "")
108126
.split('\n')
109127
.map(|line| line.len())

Diff for: crates/hyfetch/src/presets.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,16 @@ impl ColorProfile {
484484

485485
/// Creates a new color profile, with the colors set to the specified HSL
486486
/// lightness value, with respect to dark/light terminals.
487-
pub fn with_lightness_dl(&self, lightness: Lightness, term: LightDark) -> Self {
487+
pub fn with_lightness_dl(
488+
&self,
489+
lightness: Lightness,
490+
term: LightDark,
491+
use_overlay: bool,
492+
) -> Self {
493+
if use_overlay {
494+
todo!()
495+
}
496+
488497
match term {
489498
LightDark::Dark => self.with_lightness(AssignLightness::ClampMin(lightness)),
490499
LightDark::Light => self.with_lightness(AssignLightness::ClampMax(lightness)),

Diff for: crates/hyfetch/src/types.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use serde::{Deserialize, Serialize};
22
use strum::{EnumString, VariantNames};
33

4-
#[derive(Clone, Eq, PartialEq, Hash, Debug, Deserialize, EnumString, Serialize, VariantNames)]
4+
#[derive(
5+
Copy, Clone, Eq, PartialEq, Hash, Debug, Deserialize, EnumString, Serialize, VariantNames,
6+
)]
57
#[serde(rename_all = "lowercase")]
68
#[strum(serialize_all = "lowercase")]
79
pub enum AnsiMode {
@@ -11,14 +13,16 @@ pub enum AnsiMode {
1113
Rgb,
1214
}
1315

14-
#[derive(Clone, Eq, PartialEq, Hash, Debug, Deserialize, Serialize)]
16+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Deserialize, Serialize)]
1517
#[serde(rename_all = "lowercase")]
1618
pub enum LightDark {
1719
Light,
1820
Dark,
1921
}
2022

21-
#[derive(Clone, Eq, PartialEq, Hash, Debug, Deserialize, EnumString, Serialize, VariantNames)]
23+
#[derive(
24+
Copy, Clone, Eq, PartialEq, Hash, Debug, Deserialize, EnumString, Serialize, VariantNames,
25+
)]
2226
#[serde(rename_all = "kebab-case")]
2327
#[strum(serialize_all = "kebab-case")]
2428
pub enum Backend {

0 commit comments

Comments
 (0)