Skip to content

Commit

Permalink
apply rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed May 5, 2024
1 parent 8efa0e5 commit fa3f1cb
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![build](https://github.com/tamada/gibo-wrapper/actions/workflows/build.yaml/badge.svg)](https://github.com/tamada/gibo-wrapper/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/tamada/gibo-wrapper/badge.svg?branch=main)](https://coveralls.io/github/tamada/gibo-wrapper?branch=main)

[![gibo-wrapper](https://img.shields.io/badge/gibo--wrapper-v-blue)](https://github.com/tamada/gibo-wrapper/releases/tag/v)
[![gibo-wrapper](https://img.shields.io/badge/gibo--wrapper-v${VERSION}-blue)](https://github.com/tamada/gibo-wrapper/releases/tag/v${VERSION})
[![Unlicense license](http://img.shields.io/badge/license-Unlicense-blue.svg?style=flat)](LICENSE)

[`gibo`](https://github.com/simonwhitaker/gibo) is a great tool for creating `.gitignore` files.
Expand Down
33 changes: 24 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::fmt::{Display, Formatter, Result};

#[derive(Parser, Debug, PartialEq)]
#[clap(
version, author, about,
version,
author,
about,
arg_required_else_help = true,
long_about = "gibo-wrapper acts like gibo and improves gibo by adding the following features.
1. current-list command for dumping the boilerplates while keeping the prologue of .gitignore file.
Expand All @@ -22,10 +24,20 @@ pub struct CliOpts {
pub(crate) enum GiboCommand {
#[command(about = "Dump a boilerplate")]
Dump {
#[clap(short, long, default_value_t = false, help = "keep the prologue of the .gitignore")]
#[clap(
short,
long,
default_value_t = false,
help = "keep the prologue of the .gitignore"
)]
keep_prologue: bool,

#[clap(short, long = "remove-duplication", default_value_t = false, help = "remove the duplicated boilerplate names")]
#[clap(
short,
long = "remove-duplication",
default_value_t = false,
help = "remove the duplicated boilerplate names"
)]
remove_duplication: bool,

#[clap(help = "the boilerplate names to dump")]
Expand Down Expand Up @@ -66,10 +78,13 @@ mod tests {
#[test]
fn test_gibo_command() {
let cli1 = CliOpts::parse_from(&["gibo-wrapper", "dump", "+macos", "linux", "-k", "-r"]);
assert_eq!(cli1.command, GiboCommand::Dump {
keep_prologue: true,
remove_duplication: true,
args: vec!["+macos".to_string(), "linux".to_string()],
});
assert_eq!(
cli1.command,
GiboCommand::Dump {
keep_prologue: true,
remove_duplication: true,
args: vec!["+macos".to_string(), "linux".to_string()],
}
);
}
}
}
11 changes: 6 additions & 5 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub fn find_prologue(dir: &PathBuf) -> Vec<String> {
}
}
items

}

fn find_boilerplate_name(line: String) -> Option<String> {
Expand Down Expand Up @@ -56,8 +55,8 @@ pub fn current_list(dir: &PathBuf) -> Vec<String> {
for line in reader.lines() {
if let Ok(line) = line {
match find_boilerplate_name(line) {
Some(name) => items.push(name),
None => {},
Some(name) => items.push(name),
None => {}
}
}
}
Expand All @@ -75,12 +74,14 @@ mod tests {
Some("Python".to_string())
);
assert_eq!(
find_boilerplate_name("### Generated by gibo (https://github.com/simonwhitaker/gibo)".to_string()),
find_boilerplate_name(
"### Generated by gibo (https://github.com/simonwhitaker/gibo)".to_string()
),
None
);
assert_eq!(
find_boilerplate_name("### https://raw.github.com/github/gitignore/4488915eec0b3a45b5c63ead28f286819c0917de/Global/Linux.gitignore".to_string()),
Some("Linux".to_string())
);
}
}
}
50 changes: 36 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::{path::PathBuf, process::Command};
use clap::Parser;
use cli::GiboCommand::{Dump, CurrentList, List, Root, Search, Update, Version};
use cli::GiboCommand::{CurrentList, Dump, List, Root, Search, Update, Version};
use std::{path::PathBuf, process::Command};

mod cli;
mod list;
mod terminal;

fn call_gibo_command(command: String, args: Vec<String>) {
let _ = Command::new("gibo")
.arg(command)
.args(args)
.spawn();
let _ = Command::new("gibo").arg(command).args(args).spawn();
}

fn print_prologue(dir: &PathBuf) {
Expand All @@ -25,7 +22,11 @@ pub fn dedup_ignorecase(args: &mut Vec<String>) {
}

fn remove_items_from_list_ignorecase(list: &mut Vec<String>, removal: &Vec<String>) {
let r = removal.clone().iter().map(|e| e.to_lowercase()).collect::<Vec<String>>();
let r = removal
.clone()
.iter()
.map(|e| e.to_lowercase())
.collect::<Vec<String>>();
list.retain(|e| !r.contains(&e.to_lowercase()));
}

Expand All @@ -50,21 +51,28 @@ impl DumpArgs {
ordinal.push(arg);
}
}
Self { ordinal, appendage, removal, dir }
Self {
ordinal,
appendage,
removal,
dir,
}
}
fn new(args: Vec<String>) -> Self {
Self::new_with_dir(args, PathBuf::from("."))
}

fn resultant_args(&self, remove_duplication: bool) -> Vec<String> {
let mut new_args = vec![];
if !self.appendage.is_empty() { // append mode
if !self.appendage.is_empty() {
// append mode
let current = list::current_list(&self.dir);
new_args.extend(current);
new_args.extend(self.ordinal.clone());
new_args.extend(self.appendage.clone());
}
if !self.removal.is_empty() { // remove mode
if !self.removal.is_empty() {
// remove mode
remove_items_from_list_ignorecase(&mut new_args, &self.removal);
}
if remove_duplication {
Expand All @@ -78,7 +86,11 @@ fn main() {
let app = cli::CliOpts::parse();
let dir = PathBuf::from(".");
match app.command {
Dump { keep_prologue, remove_duplication, args } => {
Dump {
keep_prologue,
remove_duplication,
args,
} => {
let dump_args = DumpArgs::new(args);
if keep_prologue {
print_prologue(&dir);
Expand All @@ -102,9 +114,19 @@ mod tests {
#[test]
fn test_dump_args() {
let args1 = DumpArgs::new_with_dir(vec!["+emacs".to_string()], PathBuf::from("testdata"));
assert_eq!(args1.resultant_args(false), vec!["macOS", "Linux", "Windows", "emacs"]);
assert_eq!(
args1.resultant_args(false),
vec!["macOS", "Linux", "Windows", "emacs"]
);

let args2 = DumpArgs::new_with_dir(vec!["+emacs".to_string(), "macos".to_string(), "_windows".to_string()], PathBuf::from("testdata"));
let args2 = DumpArgs::new_with_dir(
vec![
"+emacs".to_string(),
"macos".to_string(),
"_windows".to_string(),
],
PathBuf::from("testdata"),
);
assert_eq!(args2.resultant_args(true), vec!["macOS", "Linux", "emacs"]);
}
}
}
22 changes: 19 additions & 3 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,31 @@ mod tests {
use super::*;
#[test]
fn test_print_in_column_string() {
let v1 = vec!["macOS", "Linux", "Windows", "Go", "VisualStudioCode", "JetBrains"];
let v1 = vec![
"macOS",
"Linux",
"Windows",
"Go",
"VisualStudioCode",
"JetBrains",
];
let r1 = print_in_column_string(v1.iter().map(|s| s.to_string()).collect(), 125);
assert_eq!(r1.len(), 1);
assert_eq!(r1[0], "macOS Linux Windows Go VisualStudioCode JetBrains");

let v2 = vec!["macOS", "Linux", "Windows", "Go", "VisualStudioCode", "JetBrains", "Rust", "NetBeans"];
let v2 = vec![
"macOS",
"Linux",
"Windows",
"Go",
"VisualStudioCode",
"JetBrains",
"Rust",
"NetBeans",
];
let r2 = print_in_column_string(v2.iter().map(|s| s.to_string()).collect(), 125);
assert_eq!(r2.len(), 2);
assert_eq!(r2[0], "macOS Linux Windows Go VisualStudioCode JetBrains Rust");
assert_eq!(r2[1], "NetBeans");
}
}
}

0 comments on commit fa3f1cb

Please sign in to comment.