Skip to content

Commit 149c3e4

Browse files
committed
updated --disable flag behaviour by removing use of vec.contains and using bool struct instead
1 parent 450a651 commit 149c3e4

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

Diff for: src/main.rs

+50-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Info {
4646
license: String,
4747
custom_logo: Language,
4848
custom_colors: Vec<String>,
49-
disable_fields: Vec<InfoFields>,
49+
disable_fields: InfoFieldOn,
5050
}
5151

5252
impl fmt::Display for Info {
@@ -57,7 +57,7 @@ impl fmt::Display for Info {
5757
None => Color::White,
5858
};
5959

60-
if !self.disable_fields.contains(&InfoFields::Project) {
60+
if !self.disable_fields.project {
6161
writeln!(
6262
buffer,
6363
"{}{}",
@@ -66,7 +66,7 @@ impl fmt::Display for Info {
6666
)?;
6767
}
6868

69-
if !self.disable_fields.contains(&InfoFields::HEAD) {
69+
if !self.disable_fields.head {
7070
writeln!(
7171
buffer,
7272
"{}{}",
@@ -75,7 +75,7 @@ impl fmt::Display for Info {
7575
)?;
7676
}
7777

78-
if !self.disable_fields.contains(&InfoFields::Version) {
78+
if !self.disable_fields.version {
7979
writeln!(
8080
buffer,
8181
"{}{}",
@@ -84,7 +84,7 @@ impl fmt::Display for Info {
8484
)?;
8585
}
8686

87-
if !self.disable_fields.contains(&InfoFields::Created) {
87+
if !self.disable_fields.created {
8888
writeln!(
8989
buffer,
9090
"{}{}",
@@ -93,7 +93,7 @@ impl fmt::Display for Info {
9393
)?;
9494
}
9595

96-
if !self.disable_fields.contains(&InfoFields::Languages) && !self.languages.is_empty() {
96+
if !self.disable_fields.languages && !self.languages.is_empty() {
9797
if self.languages.len() > 1 {
9898
let title = "Languages: ";
9999
let pad = " ".repeat(title.len());
@@ -118,7 +118,7 @@ impl fmt::Display for Info {
118118
};
119119
}
120120

121-
if !self.disable_fields.contains(&InfoFields::Authors) && !self.authors.is_empty() {
121+
if !self.disable_fields.authors && !self.authors.is_empty() {
122122
let title = if self.authors.len() > 1 {
123123
"Authors: "
124124
} else {
@@ -134,7 +134,7 @@ impl fmt::Display for Info {
134134
}
135135
}
136136

137-
if !self.disable_fields.contains(&InfoFields::LastChange) {
137+
if !self.disable_fields.last_change {
138138
writeln!(
139139
buffer,
140140
"{}{}",
@@ -143,11 +143,11 @@ impl fmt::Display for Info {
143143
)?;
144144
}
145145

146-
if !self.disable_fields.contains(&InfoFields::Repo) {
146+
if !self.disable_fields.repo {
147147
writeln!(buffer, "{}{}", "Repo: ".color(color).bold(), self.repo)?;
148148
}
149149

150-
if !self.disable_fields.contains(&InfoFields::Commits) {
150+
if !self.disable_fields.commits {
151151
writeln!(
152152
buffer,
153153
"{}{}",
@@ -156,7 +156,7 @@ impl fmt::Display for Info {
156156
)?;
157157
}
158158

159-
if !self.disable_fields.contains(&InfoFields::LinesOfCode) {
159+
if !self.disable_fields.lines_of_code {
160160
writeln!(
161161
buffer,
162162
"{}{}",
@@ -165,7 +165,7 @@ impl fmt::Display for Info {
165165
)?;
166166
}
167167

168-
if !self.disable_fields.contains(&InfoFields::Size) {
168+
if !self.disable_fields.size {
169169
writeln!(
170170
buffer,
171171
"{}{}",
@@ -174,7 +174,7 @@ impl fmt::Display for Info {
174174
)?;
175175
}
176176

177-
if !self.disable_fields.contains(&InfoFields::License) {
177+
if !self.disable_fields.license {
178178
writeln!(
179179
buffer,
180180
"{}{}",
@@ -320,6 +320,22 @@ impl fmt::Display for CommitInfo {
320320
}
321321
}
322322

323+
#[derive(Default)]
324+
struct InfoFieldOn {
325+
project: bool,
326+
head: bool,
327+
version: bool,
328+
created: bool,
329+
languages: bool,
330+
authors: bool,
331+
last_change: bool,
332+
repo: bool,
333+
commits: bool,
334+
lines_of_code: bool,
335+
size: bool,
336+
license: bool,
337+
}
338+
323339
#[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)]
324340
#[strum(serialize_all = "snake_case")]
325341
enum InfoFields {
@@ -509,15 +525,31 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
509525
.unwrap()
510526
.to_lowercase())
511527
.unwrap_or(Language::Unknown);
512-
let disable_fields: Vec<InfoFields> = matches.values_of("disable_field")
528+
let mut disable_fields = InfoFieldOn { ..Default::default() };
529+
530+
matches.values_of("disable_field")
513531
.unwrap()
514532
.map(String::from)
515-
.filter_map(|field: String| {
533+
.for_each(|field: String| {
516534
let item = InfoFields::from_str(field.to_lowercase().as_str())
517535
.unwrap_or(InfoFields::UnrecognizedField);
518-
if item == InfoFields::UnrecognizedField { None } else { Some(item) }
519-
})
520-
.collect();
536+
537+
match item {
538+
InfoFields::Project => disable_fields.project = true,
539+
InfoFields::HEAD => disable_fields.head = true,
540+
InfoFields::Version => disable_fields.version = true,
541+
InfoFields::Created => disable_fields.created = true,
542+
InfoFields::Languages => disable_fields.languages = true,
543+
InfoFields::Authors => disable_fields.authors = true,
544+
InfoFields::LastChange => disable_fields.last_change = true,
545+
InfoFields::Repo => disable_fields.repo = true,
546+
InfoFields::Commits => disable_fields.commits = true,
547+
InfoFields::LinesOfCode => disable_fields.lines_of_code = true,
548+
InfoFields::Size => disable_fields.size = true,
549+
InfoFields::License => disable_fields.license = true,
550+
_ => (),
551+
}
552+
});
521553

522554
let tokei_langs = project_languages(&dir);
523555
let languages_stat = get_languages_stat(&tokei_langs).ok_or(Error::SourceCodeNotFound)?;

0 commit comments

Comments
 (0)