Skip to content

Commit

Permalink
Applied 'cargo fmt'
Browse files Browse the repository at this point in the history
  • Loading branch information
ZapAnton committed Oct 31, 2019
1 parent 0ea794b commit 615eecd
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 58 deletions.
10 changes: 6 additions & 4 deletions src/ascii_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ mod test {
let colors_shim = Vec::new();

assert_eq!(
Tokens("").render(&colors_shim, 0, 0, true),
Tokens("").render(&colors_shim, 0, 0, true),
"\u{1b}[1;37m\u{1b}[0m"
);

Expand Down Expand Up @@ -316,11 +316,11 @@ mod test {
"\u{1b}[37m \u{1b}[0m"
);
}

#[test]
fn truncate() {
assert_eq!(
Tokens("").truncate(0, 0).collect::<Vec<_>>(),
Tokens("").truncate(0, 0).collect::<Vec<_>>(),
Tokens("").collect::<Vec<_>>()
);

Expand Down Expand Up @@ -350,7 +350,9 @@ mod test {
);

assert_eq!(
Tokens(" {1} {5} {9} a").truncate(4, 10).collect::<Vec<_>>(),
Tokens(" {1} {5} {9} a")
.truncate(4, 10)
.collect::<Vec<_>>(),
Tokens("{1}{5} {9} a").collect::<Vec<_>>()
);
}
Expand Down
6 changes: 2 additions & 4 deletions src/image_backends/kitty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ impl KittyBackend {
if allowed_bytes.contains(&byte) {
buf.push(byte);
}
if buf.starts_with(&[0x1B, b'_', b'G'])
&& buf.ends_with(&[0x1B, b'\\'])
{
if buf.starts_with(&[0x1B, b'_', b'G']) && buf.ends_with(&[0x1B, b'\\']) {
sender.send(()).unwrap();
return;
}
Expand Down Expand Up @@ -108,7 +106,7 @@ impl super::ImageBackend for KittyBackend {
image.width() as usize * image.height() as usize * 4,
raw_image.len()
);

let encoded_image = base64::encode(&raw_image); // image data is base64 encoded
let mut image_data = Vec::<u8>::new();
for chunk in encoded_image.as_bytes().chunks(4096) {
Expand Down
112 changes: 87 additions & 25 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::fmt::Write;
use std::fs;
use std::process::Command;

use colored::{Color, Colorize, ColoredString};
use colored::{Color, ColoredString, Colorize};
use git2::Repository;
use license;
use image::DynamicImage;
use license;

use crate::image_backends;
use crate::language::Language;
use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};
use crate::image_backends;

type Result<T> = std::result::Result<T, crate::Error>;

Expand Down Expand Up @@ -44,32 +44,60 @@ impl std::fmt::Display for Info {
Some(&c) => c,
None => Color::White,
};
if !self.disable_fields.git_info{
if !self.disable_fields.git_info {
let git_info_length;
if self.git_username != "" {
git_info_length = self.git_username.len() + self.git_version.len() + 3;
write!(&mut buf, "{} ~ ", &self.get_formatted_info_label(&self.git_username, color))?;
write!(
&mut buf,
"{} ~ ",
&self.get_formatted_info_label(&self.git_username, color)
)?;
} else {
git_info_length = self.git_version.len();
}
write_buf(&mut buf, &self.get_formatted_info_label(&self.git_version, color), "")?;
write_buf(
&mut buf,
&self.get_formatted_info_label(&self.git_version, color),
"",
)?;
let separator = "-".repeat(git_info_length);
write_buf(&mut buf, &self.get_formatted_info_label("", color), &separator)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("", color),
&separator,
)?;
}
if !self.disable_fields.project {
write_buf(&mut buf, &self.get_formatted_info_label("Project: ", color), &self.project_name)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Project: ", color),
&self.project_name,
)?;
}

if !self.disable_fields.head {
write_buf(&mut buf, &self.get_formatted_info_label("HEAD: ", color), &self.current_commit)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("HEAD: ", color),
&self.current_commit,
)?;
}

if !self.disable_fields.version {
write_buf(&mut buf, &self.get_formatted_info_label("Version: ", color), &self.version)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Version: ", color),
&self.version,
)?;
}

if !self.disable_fields.created {
write_buf(&mut buf, &self.get_formatted_info_label("Created: ", color), &self.creation_date)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Created: ", color),
&self.creation_date,
)?;
}

if !self.disable_fields.languages && !self.languages.is_empty() {
Expand All @@ -88,7 +116,7 @@ impl std::fmt::Display for Info {
iter.collect()
}
};

for (cnt, language) in languages.iter().enumerate() {
let formatted_number = format!("{:.*}", 2, language.1);
if cnt != 0 && cnt % 3 == 0 {
Expand All @@ -99,7 +127,11 @@ impl std::fmt::Display for Info {
}
writeln!(buf, "{}{}", &self.get_formatted_info_label(title, color), s)?;
} else {
write_buf(&mut buf, &self.get_formatted_info_label("Language: ", color), &self.dominant_language)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Language: ", color),
&self.dominant_language,
)?;
};
}

Expand All @@ -122,7 +154,6 @@ impl std::fmt::Display for Info {
let title = " ".repeat(title.len());

for author in self.authors.iter().skip(1) {

writeln!(
buf,
"{}{}% {} {}",
Expand All @@ -135,27 +166,51 @@ impl std::fmt::Display for Info {
}

if !self.disable_fields.last_change {
write_buf(&mut buf, &self.get_formatted_info_label("Last change: ", color), &self.last_change)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Last change: ", color),
&self.last_change,
)?;
}

if !self.disable_fields.repo {
write_buf(&mut buf, &self.get_formatted_info_label("Repo: ", color), &self.repo)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Repo: ", color),
&self.repo,
)?;
}

if !self.disable_fields.commits {
write_buf(&mut buf, &self.get_formatted_info_label("Commits: ", color), &self.commits)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Commits: ", color),
&self.commits,
)?;
}

if !self.disable_fields.lines_of_code {
write_buf(&mut buf, &self.get_formatted_info_label("Lines of code: ", color), &self.number_of_lines)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Lines of code: ", color),
&self.number_of_lines,
)?;
}

if !self.disable_fields.size {
write_buf(&mut buf, &self.get_formatted_info_label("Size: ", color), &self.repo_size)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("Size: ", color),
&self.repo_size,
)?;
}

if !self.disable_fields.license {
write_buf(&mut buf, &self.get_formatted_info_label("License: ", color), &self.license)?;
write_buf(
&mut buf,
&self.get_formatted_info_label("License: ", color),
&self.license,
)?;
}

writeln!(
Expand Down Expand Up @@ -184,7 +239,14 @@ impl std::fmt::Display for Info {

if let Some(custom_image) = &self.custom_image {
if let Some(backend) = image_backends::get_best_backend() {
writeln!(f, "{}", backend.add_image(info_lines.map(|s| format!("{}{}", center_pad, s)).collect(), custom_image))?;
writeln!(
f,
"{}",
backend.add_image(
info_lines.map(|s| format!("{}{}", center_pad, s)).collect(),
custom_image
)
)?;
} else {
panic!("No image backend found")
}
Expand Down Expand Up @@ -223,7 +285,7 @@ impl Info {
colors: Vec<String>,
disabled: InfoFieldOn,
bold_flag: bool,
custom_image: Option<DynamicImage>
custom_image: Option<DynamicImage>,
) -> Result<Info> {
let authors = Info::get_authors(&dir, 3);
let (git_v, git_user) = Info::get_git_info(&dir);
Expand Down Expand Up @@ -306,12 +368,12 @@ impl Info {
authors
}

fn get_git_info(dir: &str) -> (String, String){
fn get_git_info(dir: &str) -> (String, String) {
let version = Command::new("git")
.arg("--version")
.output()
.expect("Failed to execute git.");
let version = String::from_utf8_lossy(&version.stdout).replace('\n',"");
let version = String::from_utf8_lossy(&version.stdout).replace('\n', "");

let username = Command::new("git")
.arg("-C")
Expand All @@ -321,7 +383,7 @@ impl Info {
.arg("user.name")
.output()
.expect("Failed to execute git.");
let username = String::from_utf8_lossy(&username.stdout).replace('\n',"");
let username = String::from_utf8_lossy(&username.stdout).replace('\n', "");
(version, username)
}

Expand Down
8 changes: 7 additions & 1 deletion src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,13 @@ impl Language {
Language::Idris => vec![Color::Red],
Language::Java => vec![Color::BrightBlue, Color::Red],
Language::JavaScript => vec![Color::BrightYellow],
Language::Julia => vec![Color::BrightWhite, Color::Blue, Color::BrightGreen, Color::Red, Color::BrightMagenta],
Language::Julia => vec![
Color::BrightWhite,
Color::Blue,
Color::BrightGreen,
Color::Red,
Color::BrightMagenta,
],
Language::Kotlin => vec![Color::Blue, Color::Yellow, Color::Magenta],
Language::Lisp => vec![Color::Yellow],
Language::Lua => vec![Color::Blue],
Expand Down
56 changes: 32 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Result<T> = result::Result<T, Error>;

#[derive(Default)]
pub struct InfoFieldOn {
git_info:bool,
git_info: bool,
project: bool,
head: bool,
version: bool,
Expand Down Expand Up @@ -143,35 +143,37 @@ fn main() -> Result<()> {
.help(&format!(
"Specifies a preferred color set. Unspecified colors will remain as default.
Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
"0".black(),
"1".red(),
"2".green(),
"3".yellow(),
"4".blue(),
"5".magenta(),
"6".cyan(),
"7".white(),
"8".bright_black(),
"9".bright_red(),
"10".bright_green(),
"11".bright_yellow(),
"12".bright_blue(),
"13".bright_magenta(),
"14".bright_cyan(),
"15".bright_white(),
)))
"0".black(),
"1".red(),
"2".green(),
"3".yellow(),
"4".blue(),
"5".magenta(),
"6".cyan(),
"7".white(),
"8".bright_black(),
"9".bright_red(),
"10".bright_green(),
"11".bright_yellow(),
"12".bright_blue(),
"13".bright_magenta(),
"14".bright_cyan(),
"15".bright_white(),
)),
)
.arg(
Arg::with_name("no-bold")
.short("b")
.long("no-bold")
.help("Turns off bold formatting for the logo and all labels")
)
.help("Turns off bold formatting for the logo and all labels"),
)
.arg(
Arg::with_name("languages")
.short("l")
.long("languages")
.help("Prints out supported languages"),
).arg(
)
.arg(
Arg::with_name("image")
.short("i")
.long("image")
Expand All @@ -181,8 +183,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
.get_matches();

if matches.is_present("languages") {
let iterator = Language::iter()
.filter(|x| *x != Language::Unknown);
let iterator = Language::iter().filter(|x| *x != Language::Unknown);

for l in iterator {
println!("{}", l);
Expand Down Expand Up @@ -238,7 +239,14 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
None
};

let info = Info::new(&dir, custom_logo, custom_colors, disable_fields, bold_flag, custom_image)?;
let info = Info::new(
&dir,
custom_logo,
custom_colors,
disable_fields,
bold_flag,
custom_image,
)?;

print!("{}", info);
Ok(())
Expand Down

0 comments on commit 615eecd

Please sign in to comment.