Skip to content

Commit 6cf5ff2

Browse files
committed
Start unifying option parsing
first step towards #62
1 parent 1523bcb commit 6cf5ff2

File tree

2 files changed

+88
-61
lines changed

2 files changed

+88
-61
lines changed

pfetch-logo-parser/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ impl FromStr for Color {
3737
type Err = String;
3838

3939
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
40+
if s.is_empty() {
41+
return Err("No string given".to_string());
42+
}
4043
Ok(Color(s.parse::<u8>().ok()))
4144
}
4245
}

src/main.rs

Lines changed: 85 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,70 @@ use pfetch_logo_parser::{Color, Logo, LogoPart};
66
use std::{env, fmt::Display, str::FromStr};
77
use unicode_width::UnicodeWidthStr;
88

9+
struct Config {
10+
pf_ascii: Option<String>,
11+
pf_col1: String,
12+
pf_col2: String,
13+
pf_col3: String,
14+
pf_color: String,
15+
pf_sep: String,
16+
pf_pad1: String,
17+
pf_pad2: String,
18+
pf_pad3: String,
19+
user: String,
20+
hostname: String,
21+
pf_source: String,
22+
pf_fast_pkg_count: bool,
23+
pf_info: Vec<PfetchInfo>,
24+
pf_custom_logos: String,
25+
print_version: bool,
26+
}
27+
28+
impl Config {
29+
fn load() -> Self {
30+
// source file specified by env: PF_SOURCE
31+
if let Ok(filepath) = dotenvy::var("PF_SOURCE") {
32+
let _ = dotenvy::from_path(filepath);
33+
}
34+
Self {
35+
pf_ascii: dotenvy::var("PF_ASCII").ok(),
36+
pf_col1: dotenvy::var("PF_COL1").unwrap_or_default(),
37+
pf_col2: dotenvy::var("PF_COL2").unwrap_or_default(),
38+
pf_col3: dotenvy::var("PF_COL3").unwrap_or_default(),
39+
pf_color: dotenvy::var("PF_COLOR").unwrap_or_default(),
40+
pf_sep: dotenvy::var("PF_SEP").unwrap_or_default(),
41+
pf_pad1: dotenvy::var("PF_PAD1").unwrap_or_default(),
42+
pf_pad2: dotenvy::var("PF_PAD2").unwrap_or_default(),
43+
pf_pad3: dotenvy::var("PF_PAD3").unwrap_or_default(),
44+
user: dotenvy::var("USER").unwrap_or_default(),
45+
hostname: dotenvy::var("HOSTNAME").unwrap_or_default(),
46+
pf_source: dotenvy::var("PF_SOURCE").unwrap_or_default(),
47+
pf_fast_pkg_count: dotenvy::var("PF_FAST_PKG_COUNT").is_ok(),
48+
pf_info: match dotenvy::var("PF_INFO") {
49+
Ok(pfetch_infos) => pfetch_infos
50+
.trim()
51+
.split(' ')
52+
.map(PfetchInfo::from_str)
53+
.filter_map(|i| i.ok())
54+
.collect(),
55+
Err(_) => vec![
56+
PfetchInfo::Ascii,
57+
PfetchInfo::Title,
58+
PfetchInfo::Os,
59+
PfetchInfo::Host,
60+
PfetchInfo::Kernel,
61+
PfetchInfo::Uptime,
62+
PfetchInfo::Pkgs,
63+
PfetchInfo::Memory,
64+
],
65+
},
66+
pf_custom_logos: dotenvy::var("PF_CUSTOM_LOGOS").unwrap_or_default(),
67+
print_version: std::env::args()
68+
.any(|arg| arg.starts_with("-v") || arg.starts_with("--v")),
69+
}
70+
}
71+
}
72+
973
#[derive(Debug, PartialEq)]
1074
enum PfetchInfo {
1175
Ascii,
@@ -204,59 +268,17 @@ fn get_info(
204268
}
205269

206270
fn main() {
207-
// parse arguements
208-
if std::env::args().any(|arg| arg.starts_with("-v") || arg.starts_with("--v")) {
271+
let config = Config::load();
272+
if config.print_version {
209273
println!("pfetch-rs {}", env!("CARGO_PKG_VERSION"));
210274
std::process::exit(0);
211275
} else if std::env::args().len() > 1 {
276+
// TODO: change this for raw logos
212277
println!("pfetch show system information");
213278
println!("pfetch -v show version");
214279
std::process::exit(0);
215280
}
216281

217-
// source file specified by env: PF_SOURCE
218-
if let Ok(filepath) = dotenvy::var("PF_SOURCE") {
219-
let _ = dotenvy::from_path(filepath);
220-
}
221-
// Check if SKIP_SLOW is enabled
222-
let skip_slow_package_managers = dotenvy::var("PF_FAST_PKG_COUNT").is_ok();
223-
224-
let enabled_pf_info_base: Vec<PfetchInfo> = match dotenvy::var("PF_INFO") {
225-
Ok(pfetch_infos) => pfetch_infos
226-
.trim()
227-
.split(' ')
228-
.map(PfetchInfo::from_str)
229-
.filter_map(|i| i.ok())
230-
.collect(),
231-
Err(_) => vec![
232-
PfetchInfo::Ascii,
233-
PfetchInfo::Title,
234-
PfetchInfo::Os,
235-
PfetchInfo::Host,
236-
PfetchInfo::Kernel,
237-
PfetchInfo::Uptime,
238-
PfetchInfo::Pkgs,
239-
PfetchInfo::Memory,
240-
],
241-
};
242-
243-
// insert blank lines before and after palettes
244-
let mut enabled_pf_info: Vec<PfetchInfo> = vec![];
245-
let mut ascii_enabled: bool = false;
246-
for info in enabled_pf_info_base {
247-
match info {
248-
PfetchInfo::Palette => {
249-
enabled_pf_info.push(PfetchInfo::BlankLine);
250-
enabled_pf_info.push(PfetchInfo::Palette);
251-
enabled_pf_info.push(PfetchInfo::BlankLine);
252-
}
253-
PfetchInfo::Ascii => {
254-
ascii_enabled = true;
255-
}
256-
i => enabled_pf_info.push(i),
257-
}
258-
}
259-
260282
let readouts = Readouts {
261283
general_readout: GeneralReadout::new(),
262284
package_readout: PackageReadout::new(),
@@ -266,35 +288,30 @@ fn main() {
266288

267289
let os = pfetch::os(&GeneralReadout::new()).unwrap_or_default();
268290

269-
let logo_override = env::var("PF_ASCII");
270-
271-
let logo_name = logo_override.unwrap_or(match env::consts::OS {
291+
let logo_name = config.pf_ascii.unwrap_or(match env::consts::OS {
272292
"linux" => os.clone(),
273293
other => other.to_owned(),
274294
});
275295

276296
let mut logo = pfetch::logo(&logo_name);
277297

278298
// color overrides
279-
if let Ok(newcolor) = dotenvy::var("PF_COL1") {
280-
if let Ok(newcolor) = Color::from_str(&newcolor) {
281-
logo.primary_color = newcolor;
282-
}
299+
if let Ok(newcolor) = Color::from_str(&config.pf_col1) {
300+
logo.primary_color = newcolor;
283301
}
284302

285-
if let Ok(newcolor) = dotenvy::var("PF_COL3") {
286-
if newcolor == "COL1" {
287-
logo.secondary_color = logo.primary_color;
288-
} else if let Ok(newcolor) = Color::from_str(&newcolor) {
289-
logo.secondary_color = newcolor;
290-
}
303+
if config.pf_col3 == "COL1" {
304+
logo.secondary_color = logo.primary_color;
305+
} else if let Ok(newcolor) = Color::from_str(&config.pf_col3) {
306+
logo.secondary_color = newcolor;
291307
}
292308

293-
let gathered_pfetch_info: Vec<(Color, String, String)> = enabled_pf_info
309+
let gathered_pfetch_info: Vec<(Color, String, String)> = config
310+
.pf_info
294311
.iter()
295312
.filter_map(|info| match info {
296313
PfetchInfo::Os => Some((logo.primary_color, info.to_string(), os.clone())),
297-
_ => get_info(info, &readouts, skip_slow_package_managers).map(|info_str| match info {
314+
_ => get_info(info, &readouts, config.pf_fast_pkg_count).map(|info_str| match info {
298315
PfetchInfo::Title => (logo.secondary_color, info_str, "".to_string()),
299316
PfetchInfo::BlankLine => (logo.primary_color, "".to_string(), "".to_string()),
300317
PfetchInfo::Palette => (logo.primary_color, info_str, "".to_string()),
@@ -303,5 +320,12 @@ fn main() {
303320
})
304321
.collect();
305322

306-
pfetch(gathered_pfetch_info, logo, ascii_enabled);
323+
pfetch(
324+
gathered_pfetch_info,
325+
logo,
326+
config
327+
.pf_info
328+
.iter()
329+
.any(|x| matches!(&x, PfetchInfo::Ascii)),
330+
);
307331
}

0 commit comments

Comments
 (0)