@@ -5,6 +5,70 @@ use libmacchina::{
55use pfetch_logo_parser:: { Color , Logo , LogoPart } ;
66use std:: { env, fmt:: Display , str:: FromStr } ;
77
8+ struct Config {
9+ pf_ascii : Option < String > ,
10+ pf_col1 : String ,
11+ pf_col2 : String ,
12+ pf_col3 : String ,
13+ pf_color : String ,
14+ pf_sep : String ,
15+ pf_pad1 : String ,
16+ pf_pad2 : String ,
17+ pf_pad3 : String ,
18+ user : String ,
19+ hostname : String ,
20+ pf_source : String ,
21+ pf_fast_pkg_count : bool ,
22+ pf_info : Vec < PfetchInfo > ,
23+ pf_custom_logos : String ,
24+ print_version : bool ,
25+ }
26+
27+ impl Config {
28+ fn load ( ) -> Self {
29+ // source file specified by env: PF_SOURCE
30+ if let Ok ( filepath) = dotenvy:: var ( "PF_SOURCE" ) {
31+ let _ = dotenvy:: from_path ( filepath) ;
32+ }
33+ Self {
34+ pf_ascii : dotenvy:: var ( "PF_ASCII" ) . ok ( ) ,
35+ pf_col1 : dotenvy:: var ( "PF_COL1" ) . unwrap_or_default ( ) ,
36+ pf_col2 : dotenvy:: var ( "PF_COL2" ) . unwrap_or_default ( ) ,
37+ pf_col3 : dotenvy:: var ( "PF_COL3" ) . unwrap_or_default ( ) ,
38+ pf_color : dotenvy:: var ( "PF_COLOR" ) . unwrap_or_default ( ) ,
39+ pf_sep : dotenvy:: var ( "PF_SEP" ) . unwrap_or_default ( ) ,
40+ pf_pad1 : dotenvy:: var ( "PF_PAD1" ) . unwrap_or_default ( ) ,
41+ pf_pad2 : dotenvy:: var ( "PF_PAD2" ) . unwrap_or_default ( ) ,
42+ pf_pad3 : dotenvy:: var ( "PF_PAD3" ) . unwrap_or_default ( ) ,
43+ user : dotenvy:: var ( "USER" ) . unwrap_or_default ( ) ,
44+ hostname : dotenvy:: var ( "HOSTNAME" ) . unwrap_or_default ( ) ,
45+ pf_source : dotenvy:: var ( "PF_SOURCE" ) . unwrap_or_default ( ) ,
46+ pf_fast_pkg_count : dotenvy:: var ( "PF_FAST_PKG_COUNT" ) . is_ok ( ) ,
47+ pf_info : match dotenvy:: var ( "PF_INFO" ) {
48+ Ok ( pfetch_infos) => pfetch_infos
49+ . trim ( )
50+ . split ( ' ' )
51+ . map ( PfetchInfo :: from_str)
52+ . filter_map ( |i| i. ok ( ) )
53+ . collect ( ) ,
54+ Err ( _) => vec ! [
55+ PfetchInfo :: Ascii ,
56+ PfetchInfo :: Title ,
57+ PfetchInfo :: Os ,
58+ PfetchInfo :: Host ,
59+ PfetchInfo :: Kernel ,
60+ PfetchInfo :: Uptime ,
61+ PfetchInfo :: Pkgs ,
62+ PfetchInfo :: Memory ,
63+ ] ,
64+ } ,
65+ pf_custom_logos : dotenvy:: var ( "PF_CUSTOM_LOGOS" ) . unwrap_or_default ( ) ,
66+ print_version : std:: env:: args ( )
67+ . any ( |arg| arg. starts_with ( "-v" ) || arg. starts_with ( "--v" ) ) ,
68+ }
69+ }
70+ }
71+
872#[ derive( Debug , PartialEq ) ]
973enum PfetchInfo {
1074 Ascii ,
@@ -203,59 +267,17 @@ fn get_info(
203267}
204268
205269fn main ( ) {
206- // parse arguements
207- if std :: env :: args ( ) . any ( |arg| arg . starts_with ( "-v" ) || arg . starts_with ( "--v" ) ) {
270+ let config = Config :: load ( ) ;
271+ if config . print_version {
208272 println ! ( "pfetch-rs {}" , env!( "CARGO_PKG_VERSION" ) ) ;
209273 std:: process:: exit ( 0 ) ;
210274 } else if std:: env:: args ( ) . len ( ) > 1 {
275+ // TODO: change this for raw logos
211276 println ! ( "pfetch show system information" ) ;
212277 println ! ( "pfetch -v show version" ) ;
213278 std:: process:: exit ( 0 ) ;
214279 }
215280
216- // source file specified by env: PF_SOURCE
217- if let Ok ( filepath) = dotenvy:: var ( "PF_SOURCE" ) {
218- let _ = dotenvy:: from_path ( filepath) ;
219- }
220- // Check if SKIP_SLOW is enabled
221- let skip_slow_package_managers = dotenvy:: var ( "PF_FAST_PKG_COUNT" ) . is_ok ( ) ;
222-
223- let enabled_pf_info_base: Vec < PfetchInfo > = match dotenvy:: var ( "PF_INFO" ) {
224- Ok ( pfetch_infos) => pfetch_infos
225- . trim ( )
226- . split ( ' ' )
227- . map ( PfetchInfo :: from_str)
228- . filter_map ( |i| i. ok ( ) )
229- . collect ( ) ,
230- Err ( _) => vec ! [
231- PfetchInfo :: Ascii ,
232- PfetchInfo :: Title ,
233- PfetchInfo :: Os ,
234- PfetchInfo :: Host ,
235- PfetchInfo :: Kernel ,
236- PfetchInfo :: Uptime ,
237- PfetchInfo :: Pkgs ,
238- PfetchInfo :: Memory ,
239- ] ,
240- } ;
241-
242- // insert blank lines before and after palettes
243- let mut enabled_pf_info: Vec < PfetchInfo > = vec ! [ ] ;
244- let mut ascii_enabled: bool = false ;
245- for info in enabled_pf_info_base {
246- match info {
247- PfetchInfo :: Palette => {
248- enabled_pf_info. push ( PfetchInfo :: BlankLine ) ;
249- enabled_pf_info. push ( PfetchInfo :: Palette ) ;
250- enabled_pf_info. push ( PfetchInfo :: BlankLine ) ;
251- }
252- PfetchInfo :: Ascii => {
253- ascii_enabled = true ;
254- }
255- i => enabled_pf_info. push ( i) ,
256- }
257- }
258-
259281 let readouts = Readouts {
260282 general_readout : GeneralReadout :: new ( ) ,
261283 package_readout : PackageReadout :: new ( ) ,
@@ -265,35 +287,30 @@ fn main() {
265287
266288 let os = pfetch:: os ( & GeneralReadout :: new ( ) ) . unwrap_or_default ( ) ;
267289
268- let logo_override = env:: var ( "PF_ASCII" ) ;
269-
270- let logo_name = logo_override. unwrap_or ( match env:: consts:: OS {
290+ let logo_name = config. pf_ascii . unwrap_or ( match env:: consts:: OS {
271291 "linux" => os. clone ( ) ,
272292 other => other. to_owned ( ) ,
273293 } ) ;
274294
275295 let mut logo = pfetch:: logo ( & logo_name) ;
276296
277297 // color overrides
278- if let Ok ( newcolor) = dotenvy:: var ( "PF_COL1" ) {
279- if let Ok ( newcolor) = Color :: from_str ( & newcolor) {
280- logo. primary_color = newcolor;
281- }
298+ if let Ok ( newcolor) = Color :: from_str ( & config. pf_col1 ) {
299+ logo. primary_color = newcolor;
282300 }
283301
284- if let Ok ( newcolor) = dotenvy:: var ( "PF_COL3" ) {
285- if newcolor == "COL1" {
286- logo. secondary_color = logo. primary_color ;
287- } else if let Ok ( newcolor) = Color :: from_str ( & newcolor) {
288- logo. secondary_color = newcolor;
289- }
302+ if config. pf_col3 == "COL1" {
303+ logo. secondary_color = logo. primary_color ;
304+ } else if let Ok ( newcolor) = Color :: from_str ( & config. pf_col3 ) {
305+ logo. secondary_color = newcolor;
290306 }
291307
292- let gathered_pfetch_info: Vec < ( Color , String , String ) > = enabled_pf_info
308+ let gathered_pfetch_info: Vec < ( Color , String , String ) > = config
309+ . pf_info
293310 . iter ( )
294311 . filter_map ( |info| match info {
295312 PfetchInfo :: Os => Some ( ( logo. primary_color , info. to_string ( ) , os. clone ( ) ) ) ,
296- _ => get_info ( info, & readouts, skip_slow_package_managers ) . map ( |info_str| match info {
313+ _ => get_info ( info, & readouts, config . pf_fast_pkg_count ) . map ( |info_str| match info {
297314 PfetchInfo :: Title => ( logo. secondary_color , info_str, "" . to_string ( ) ) ,
298315 PfetchInfo :: BlankLine => ( logo. primary_color , "" . to_string ( ) , "" . to_string ( ) ) ,
299316 PfetchInfo :: Palette => ( logo. primary_color , info_str, "" . to_string ( ) ) ,
@@ -302,5 +319,12 @@ fn main() {
302319 } )
303320 . collect ( ) ;
304321
305- pfetch ( gathered_pfetch_info, logo, ascii_enabled) ;
322+ pfetch (
323+ gathered_pfetch_info,
324+ logo,
325+ config
326+ . pf_info
327+ . iter ( )
328+ . any ( |x| matches ! ( & x, PfetchInfo :: Ascii ) ) ,
329+ ) ;
306330}
0 commit comments