Skip to content

Instantly share code, notes, and snippets.

@squiidz
Created December 1, 2016 04:09
Show Gist options
  • Save squiidz/d8b1fc49a12e150be5b2ab52b99ccdaf to your computer and use it in GitHub Desktop.
Save squiidz/d8b1fc49a12e150be5b2ab52b99ccdaf to your computer and use it in GitHub Desktop.
GMD
#![feature(exact_size_is_empty)]
use std::io::{self, Read};
use std::process::Command;
use std::env;
fn main() {
let mut entry = String::new();
if env::args().is_empty() {
match io::stdin().read_to_string(&mut entry) {
Ok(_) => {},
Err(e) => panic!(e),
};
} else {
entry = env::args().last().unwrap();
}
match create_git_branch(&replace_hyphen(entry)) {
Ok(_) => {},
Err(e) => print!("{}", e),
};
}
fn replace_hyphen(entry: String) -> String {
let mut new_entry = String::new();
for (i, token) in entry.chars().enumerate() {
match token {
'-' | ' ' => { new_entry.push('_') },
_ => {
if token.is_numeric() {
let c = entry.chars().nth(i + 1).unwrap();
if !c.is_numeric() { new_entry.push_str(format!("{}_", token).as_ref()) }
else { new_entry.push(token) }
}else if token != '_'{
new_entry.push(token);
}
},
};
}
new_entry
}
fn create_git_branch(name: &str) -> Result<(), String> {
let output = Command::new("git")
.arg("branch")
.arg(name)
.output().expect("Error calling git");
if output.status.success() {
Ok(println!("branch {} has been created", name))
} else {
let err = String::from_utf8(output.stderr).unwrap();
Err(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment