Skip to content

Commit 0c9fcbb

Browse files
committed
extract get_ascii_colors from info.rs
1 parent 427029f commit 0c9fcbb

File tree

6 files changed

+117
-115
lines changed

6 files changed

+117
-115
lines changed

Diff for: src/onefetch/info.rs

+59-111
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ use {
88
};
99

1010
pub struct Info {
11-
git_version: String,
1211
git_username: String,
13-
project_name: String,
12+
git_version: String,
13+
repo_name: String,
14+
number_of_tags: usize,
15+
number_of_branches: usize,
1416
head_refs: CommitInfo,
17+
pending_changes: String,
1518
version: String,
1619
creation_date: String,
17-
pub dominant_language: Language,
1820
languages: Vec<(Language, f64)>,
1921
dependencies: String,
2022
authors: Vec<(String, usize, usize)>,
2123
last_change: String,
2224
repo_url: String,
23-
commits: String,
24-
pending: String,
25-
repo_size: String,
25+
number_of_commits: String,
2626
lines_of_code: usize,
27-
number_of_tags: usize,
28-
number_of_branches: usize,
27+
repo_size: String,
2928
license: String,
29+
pub dominant_language: Language,
3030
pub ascii_colors: Vec<Color>,
3131
pub text_colors: TextColor,
3232
pub config: Cli,
@@ -59,7 +59,7 @@ impl std::fmt::Display for Info {
5959
f,
6060
"{}{} {}",
6161
project_str,
62-
self.project_name.color(self.text_colors.info),
62+
self.repo_name.color(self.text_colors.info),
6363
branches_tags_str.color(self.text_colors.info)
6464
)?;
6565
}
@@ -73,12 +73,12 @@ impl std::fmt::Display for Info {
7373
)?;
7474
}
7575

76-
if !self.config.disabled_fields.pending && !self.pending.is_empty() {
76+
if !self.config.disabled_fields.pending && !self.pending_changes.is_empty() {
7777
writeln!(
7878
f,
7979
"{}{}",
8080
&self.get_formatted_subtitle_label("Pending"),
81-
&self.pending.color(self.text_colors.info),
81+
&self.pending_changes.color(self.text_colors.info),
8282
)?;
8383
}
8484

@@ -158,7 +158,7 @@ impl std::fmt::Display for Info {
158158
f,
159159
"{}{}",
160160
&self.get_formatted_subtitle_label("Commits"),
161-
&self.commits.color(self.text_colors.info),
161+
&self.number_of_commits.color(self.text_colors.info),
162162
)?;
163163
}
164164

@@ -212,26 +212,26 @@ impl Info {
212212
pub fn new(config: Cli) -> Result<Info> {
213213
let repo = Repo::new(&config.repo_path)?;
214214
let workdir = repo.get_work_dir()?;
215-
let (repository_name, repository_url) = repo.get_name_and_url()?;
215+
let (repo_name, repo_url) = repo.get_name_and_url()?;
216216
let head_refs = repo.get_head_refs()?;
217-
let pending = repo.get_pending_changes();
217+
let pending_changes = repo.get_pending_changes()?;
218218
let version = repo.get_version()?;
219219
let git_username = repo.get_git_username()?;
220220
let number_of_tags = repo.get_number_of_tags()?;
221221
let number_of_branches = repo.get_number_of_branches()?;
222222
let git_history = Info::get_git_history(&workdir, config.no_merges);
223-
let creation_date = Info::get_creation_date(&git_history);
223+
let creation_date = Info::get_creation_date(&git_history)?;
224224
let number_of_commits = Info::get_number_of_commits(&git_history);
225225
let authors = Info::get_authors(&git_history, config.number_of_authors);
226-
let last_change = Info::get_date_of_last_commit(&git_history);
226+
let last_change = Info::get_date_of_last_commit(&git_history)?;
227227
let git_version = Info::get_git_version()?;
228-
let repo_size = Info::get_packed_size(&workdir);
229-
let project_license = Detector::new()?.get_project_license(&workdir);
228+
let repo_size = Info::get_packed_size(&workdir)?;
229+
let license = Detector::new()?.get_license(&workdir)?;
230230
let dependencies = deps::DependencyDetector::new().get_dependencies(&workdir)?;
231-
let (languages_stats, lines_of_code) =
231+
let (languages, lines_of_code) =
232232
Language::get_language_statistics(&workdir, &config.excluded)?;
233-
let dominant_language = Language::get_dominant_language(&languages_stats);
234-
let ascii_colors = Info::get_ascii_colors(
233+
let dominant_language = Language::get_dominant_language(&languages);
234+
let ascii_colors = Language::get_ascii_colors(
235235
&config.ascii_language,
236236
&dominant_language,
237237
&config.ascii_colors,
@@ -240,25 +240,25 @@ impl Info {
240240
let text_colors = TextColor::get_text_colors(&config.text_colors, &ascii_colors);
241241

242242
Ok(Info {
243-
git_version,
244243
git_username,
245-
project_name: repository_name,
244+
git_version,
245+
repo_name,
246+
number_of_tags,
247+
number_of_branches,
246248
head_refs,
249+
pending_changes,
247250
version,
248-
creation_date: creation_date?,
249-
dominant_language,
250-
languages: languages_stats,
251+
creation_date,
252+
languages,
251253
dependencies,
252254
authors,
253-
last_change: last_change?,
254-
repo_url: repository_url,
255-
commits: number_of_commits,
256-
pending: pending?,
257-
repo_size: repo_size?,
255+
last_change,
256+
repo_url,
257+
number_of_commits,
258258
lines_of_code,
259-
number_of_tags,
260-
number_of_branches,
261-
license: project_license?,
259+
repo_size,
260+
license,
261+
dominant_language,
262262
ascii_colors,
263263
text_colors,
264264
config,
@@ -311,16 +311,38 @@ impl Info {
311311
authors
312312
}
313313

314-
fn get_git_version() -> Result<String> {
315-
let version = Command::new("git").arg("--version").output()?;
316-
Ok(String::from_utf8_lossy(&version.stdout).replace('\n', ""))
314+
fn get_date_of_last_commit(git_history: &[String]) -> Result<String> {
315+
let last_commit = git_history.first();
316+
317+
let output = match last_commit {
318+
Some(date) => date.split('\t').collect::<Vec<_>>()[0].to_string(),
319+
None => "??".into(),
320+
};
321+
322+
Ok(output)
323+
}
324+
325+
fn get_creation_date(git_history: &[String]) -> Result<String> {
326+
let first_commit = git_history.last();
327+
328+
let output = match first_commit {
329+
Some(creation_time) => creation_time.split('\t').collect::<Vec<_>>()[0].to_string(),
330+
None => "??".into(),
331+
};
332+
333+
Ok(output)
317334
}
318335

319336
fn get_number_of_commits(git_history: &[String]) -> String {
320337
let number_of_commits = git_history.len();
321338
number_of_commits.to_string()
322339
}
323340

341+
fn get_git_version() -> Result<String> {
342+
let version = Command::new("git").arg("--version").output()?;
343+
Ok(String::from_utf8_lossy(&version.stdout).replace('\n', ""))
344+
}
345+
324346
fn get_packed_size(dir: &str) -> Result<String> {
325347
let output = Command::new("git")
326348
.arg("-C")
@@ -366,80 +388,6 @@ impl Info {
366388
}
367389
}
368390

369-
fn get_date_of_last_commit(git_history: &[String]) -> Result<String> {
370-
let last_commit = git_history.first();
371-
372-
let output = match last_commit {
373-
Some(date) => date.split('\t').collect::<Vec<_>>()[0].to_string(),
374-
None => "??".into(),
375-
};
376-
377-
Ok(output)
378-
}
379-
380-
fn get_creation_date(git_history: &[String]) -> Result<String> {
381-
let first_commit = git_history.last();
382-
383-
let output = match first_commit {
384-
Some(creation_time) => creation_time.split('\t').collect::<Vec<_>>()[0].to_string(),
385-
None => "??".into(),
386-
};
387-
388-
Ok(output)
389-
}
390-
391-
fn get_ascii_colors(
392-
ascii_language: &Option<Language>,
393-
dominant_language: &Language,
394-
ascii_colors: &[String],
395-
true_color: bool,
396-
) -> Vec<Color> {
397-
let language = if let Some(ascii_language) = ascii_language {
398-
ascii_language
399-
} else {
400-
&dominant_language
401-
};
402-
403-
let colors = language.get_colors(true_color);
404-
405-
let colors: Vec<Color> = colors
406-
.iter()
407-
.enumerate()
408-
.map(|(index, default_color)| {
409-
if let Some(color_num) = ascii_colors.get(index) {
410-
if let Some(color) = Info::num_to_color(color_num) {
411-
return color;
412-
}
413-
}
414-
*default_color
415-
})
416-
.collect();
417-
colors
418-
}
419-
420-
pub fn num_to_color(num: &str) -> Option<Color> {
421-
let color = match num {
422-
"0" => Color::Black,
423-
"1" => Color::Red,
424-
"2" => Color::Green,
425-
"3" => Color::Yellow,
426-
"4" => Color::Blue,
427-
"5" => Color::Magenta,
428-
"6" => Color::Cyan,
429-
"7" => Color::White,
430-
"8" => Color::BrightBlack,
431-
"9" => Color::BrightRed,
432-
"10" => Color::BrightGreen,
433-
"11" => Color::BrightYellow,
434-
"12" => Color::BrightBlue,
435-
"13" => Color::BrightMagenta,
436-
"14" => Color::BrightCyan,
437-
"15" => Color::BrightWhite,
438-
_ => return None,
439-
};
440-
Some(color)
441-
}
442-
443391
fn get_formatted_subtitle_label(&self, label: &str) -> ColoredString {
444392
let formatted_label = format!(
445393
"{}{} ",

Diff for: src/onefetch/language.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::onefetch::error::*,
2+
crate::onefetch::{error::*, utils::num_to_color},
33
colored::Color,
44
regex::Regex,
55
std::collections::HashMap,
@@ -358,4 +358,33 @@ impl Language {
358358

359359
languages
360360
}
361+
362+
pub fn get_ascii_colors(
363+
ascii_language: &Option<Language>,
364+
dominant_language: &Language,
365+
ascii_colors: &[String],
366+
true_color: bool,
367+
) -> Vec<Color> {
368+
let language = if let Some(ascii_language) = ascii_language {
369+
ascii_language
370+
} else {
371+
&dominant_language
372+
};
373+
374+
let colors = language.get_colors(true_color);
375+
376+
let colors: Vec<Color> = colors
377+
.iter()
378+
.enumerate()
379+
.map(|(index, default_color)| {
380+
if let Some(color_num) = ascii_colors.get(index) {
381+
if let Some(color) = num_to_color(color_num) {
382+
return color;
383+
}
384+
}
385+
*default_color
386+
})
387+
.collect();
388+
colors
389+
}
361390
}

Diff for: src/onefetch/license.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl Detector {
2121
.map_err(|_| "Could not initialize the license detector".into())
2222
}
2323

24-
pub fn get_project_license(&self, dir: &str) -> Result<String> {
24+
pub fn get_license(&self, dir: &str) -> Result<String> {
2525
fn is_license_file<S: AsRef<str>>(file_name: S) -> bool {
2626
LICENSE_FILES.iter().any(|&name| file_name.as_ref().starts_with(name))
2727
}

Diff for: src/onefetch/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ pub mod language;
1111
pub mod license;
1212
pub mod repo;
1313
pub mod text_color;
14+
mod utils;

Diff for: src/onefetch/text_color.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use {crate::onefetch::info::Info, colored::Color};
1+
use {crate::onefetch::utils::num_to_color, colored::Color};
22

33
pub struct TextColor {
44
pub title: Color,
@@ -27,7 +27,7 @@ impl TextColor {
2727
let custom_color = text_colors
2828
.iter()
2929
.map(|color_num| {
30-
let custom = Info::num_to_color(color_num);
30+
let custom = num_to_color(color_num);
3131
match custom {
3232
Some(custom) => custom,
3333
None => Color::White,

Diff for: src/onefetch/utils.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use colored::Color;
2+
3+
pub fn num_to_color(num: &str) -> Option<Color> {
4+
let color = match num {
5+
"0" => Color::Black,
6+
"1" => Color::Red,
7+
"2" => Color::Green,
8+
"3" => Color::Yellow,
9+
"4" => Color::Blue,
10+
"5" => Color::Magenta,
11+
"6" => Color::Cyan,
12+
"7" => Color::White,
13+
"8" => Color::BrightBlack,
14+
"9" => Color::BrightRed,
15+
"10" => Color::BrightGreen,
16+
"11" => Color::BrightYellow,
17+
"12" => Color::BrightBlue,
18+
"13" => Color::BrightMagenta,
19+
"14" => Color::BrightCyan,
20+
"15" => Color::BrightWhite,
21+
_ => return None,
22+
};
23+
Some(color)
24+
}

0 commit comments

Comments
 (0)