Skip to content

Commit 5e595a5

Browse files
committed
extract info_fields into its own module
1 parent 4d69c11 commit 5e595a5

File tree

7 files changed

+90
-79
lines changed

7 files changed

+90
-79
lines changed

Diff for: src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// `error_chain!` can recurse deeply
22
#![recursion_limit = "1024"]
33

4-
use onefetch::{clap_app, error::*, image_backends, info, language::Language, options};
4+
use onefetch::{
5+
clap_app, error::*, image_backends, info, info_fields, language::Language, options,
6+
};
57

68
use {
79
process::{Command, Stdio},
@@ -59,7 +61,7 @@ fn run() -> Result<()> {
5961
} else {
6062
Vec::new()
6163
},
62-
disabled_fields: info::get_disabled_fields(fields_to_hide)?,
64+
disabled_fields: info_fields::get_disabled_fields(fields_to_hide)?,
6365
no_bold: !matches.is_present("no-bold"),
6466
image,
6567
image_backend,

Diff for: src/onefetch/clap_app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::onefetch::{info::InfoFields, language::Language},
2+
crate::onefetch::{info_fields::InfoFields, language::Language},
33
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
44
strum::{EnumCount, IntoEnumIterator},
55
};

Diff for: src/onefetch/info.rs

+7-75
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,19 @@
11
use {
22
crate::onefetch::{
3-
ascii_art::AsciiArt, commit_info::CommitInfo, error::*, language::Language,
4-
license::Detector, options::Options,
3+
ascii_art::AsciiArt,
4+
commit_info::CommitInfo,
5+
error::*,
6+
language::Language,
7+
license::{Detector, LICENSE_FILES},
8+
options::Options,
59
},
610
colored::{Color, ColoredString, Colorize},
711
git2::Repository,
812
regex::Regex,
9-
std::{ffi::OsStr, fmt::Write, fs, str::FromStr},
10-
strum::{EnumCount, EnumIter, EnumString, IntoStaticStr},
13+
std::{ffi::OsStr, fmt::Write, fs},
1114
tokio::process::Command,
1215
};
1316

14-
const LICENSE_FILES: [&str; 3] = ["LICENSE", "LICENCE", "COPYING"];
15-
16-
#[derive(Default)]
17-
pub struct InfoFieldOn {
18-
pub git_info: bool,
19-
pub project: bool,
20-
pub head: bool,
21-
pub version: bool,
22-
pub created: bool,
23-
pub languages: bool,
24-
pub authors: bool,
25-
pub last_change: bool,
26-
pub repo: bool,
27-
pub commits: bool,
28-
pub pending: bool,
29-
pub lines_of_code: bool,
30-
pub size: bool,
31-
pub license: bool,
32-
}
33-
34-
#[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)]
35-
#[strum(serialize_all = "snake_case")]
36-
pub enum InfoFields {
37-
GitInfo,
38-
Project,
39-
HEAD,
40-
Version,
41-
Created,
42-
Languages,
43-
Authors,
44-
LastChange,
45-
Repo,
46-
Commits,
47-
Pending,
48-
LinesOfCode,
49-
Size,
50-
License,
51-
UnrecognizedField,
52-
}
53-
5417
pub struct Info {
5518
git_version: String,
5619
git_username: String,
@@ -73,37 +36,6 @@ pub struct Info {
7336
config: Options,
7437
}
7538

76-
pub fn get_disabled_fields(fields_to_hide: Vec<String>) -> Result<InfoFieldOn> {
77-
let mut disabled_fields = InfoFieldOn {
78-
..Default::default()
79-
};
80-
81-
for field in fields_to_hide.iter() {
82-
let item = InfoFields::from_str(field.to_lowercase().as_str())
83-
.unwrap_or(InfoFields::UnrecognizedField);
84-
85-
match item {
86-
InfoFields::GitInfo => disabled_fields.git_info = true,
87-
InfoFields::Project => disabled_fields.project = true,
88-
InfoFields::HEAD => disabled_fields.head = true,
89-
InfoFields::Version => disabled_fields.version = true,
90-
InfoFields::Created => disabled_fields.created = true,
91-
InfoFields::Languages => disabled_fields.languages = true,
92-
InfoFields::Authors => disabled_fields.authors = true,
93-
InfoFields::LastChange => disabled_fields.last_change = true,
94-
InfoFields::Repo => disabled_fields.repo = true,
95-
InfoFields::Pending => disabled_fields.pending = true,
96-
InfoFields::Commits => disabled_fields.commits = true,
97-
InfoFields::LinesOfCode => disabled_fields.lines_of_code = true,
98-
InfoFields::Size => disabled_fields.size = true,
99-
InfoFields::License => disabled_fields.license = true,
100-
_ => (),
101-
}
102-
}
103-
104-
Ok(disabled_fields)
105-
}
106-
10739
impl std::fmt::Display for Info {
10840
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
10941
let mut buf = String::new();

Diff for: src/onefetch/info_fields.rs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use {
2+
crate::onefetch::error::*,
3+
std::str::FromStr,
4+
strum::{EnumCount, EnumIter, EnumString, IntoStaticStr},
5+
};
6+
7+
#[derive(Default)]
8+
pub struct InfoFieldOn {
9+
pub git_info: bool,
10+
pub project: bool,
11+
pub head: bool,
12+
pub version: bool,
13+
pub created: bool,
14+
pub languages: bool,
15+
pub authors: bool,
16+
pub last_change: bool,
17+
pub repo: bool,
18+
pub commits: bool,
19+
pub pending: bool,
20+
pub lines_of_code: bool,
21+
pub size: bool,
22+
pub license: bool,
23+
}
24+
25+
#[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)]
26+
#[strum(serialize_all = "snake_case")]
27+
pub enum InfoFields {
28+
GitInfo,
29+
Project,
30+
HEAD,
31+
Version,
32+
Created,
33+
Languages,
34+
Authors,
35+
LastChange,
36+
Repo,
37+
Commits,
38+
Pending,
39+
LinesOfCode,
40+
Size,
41+
License,
42+
UnrecognizedField,
43+
}
44+
45+
pub fn get_disabled_fields(fields_to_hide: Vec<String>) -> Result<InfoFieldOn> {
46+
let mut disabled_fields = InfoFieldOn {
47+
..Default::default()
48+
};
49+
50+
for field in fields_to_hide.iter() {
51+
let item = InfoFields::from_str(field.to_lowercase().as_str())
52+
.unwrap_or(InfoFields::UnrecognizedField);
53+
54+
match item {
55+
InfoFields::GitInfo => disabled_fields.git_info = true,
56+
InfoFields::Project => disabled_fields.project = true,
57+
InfoFields::HEAD => disabled_fields.head = true,
58+
InfoFields::Version => disabled_fields.version = true,
59+
InfoFields::Created => disabled_fields.created = true,
60+
InfoFields::Languages => disabled_fields.languages = true,
61+
InfoFields::Authors => disabled_fields.authors = true,
62+
InfoFields::LastChange => disabled_fields.last_change = true,
63+
InfoFields::Repo => disabled_fields.repo = true,
64+
InfoFields::Pending => disabled_fields.pending = true,
65+
InfoFields::Commits => disabled_fields.commits = true,
66+
InfoFields::LinesOfCode => disabled_fields.lines_of_code = true,
67+
InfoFields::Size => disabled_fields.size = true,
68+
InfoFields::License => disabled_fields.license = true,
69+
_ => (),
70+
}
71+
}
72+
73+
Ok(disabled_fields)
74+
}

Diff for: src/onefetch/license.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use askalono::{Store, TextData};
22

33
use crate::onefetch::error::*;
44

5+
pub const LICENSE_FILES: [&str; 3] = ["LICENSE", "LICENCE", "COPYING"];
6+
57
static CACHE_DATA: &[u8] = include_bytes!("../../resources/licenses/cache.bin.zstd");
68
const MIN_THRESHOLD: f32 = 0.8;
79

Diff for: src/onefetch/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod commit_info;
44
pub mod error;
55
pub mod image_backends;
66
pub mod info;
7+
pub mod info_fields;
78
pub mod language;
89
pub mod license;
910
pub mod options;

Diff for: src/onefetch/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::onefetch::{image_backends::ImageBackend, info::InfoFieldOn, language::Language},
2+
crate::onefetch::{image_backends::ImageBackend, info_fields::InfoFieldOn, language::Language},
33
image::DynamicImage,
44
};
55

0 commit comments

Comments
 (0)