Last active
September 17, 2022 15:10
-
-
Save yzhong52/128efcf352fd4d7921d3cd7f78d23644 to your computer and use it in GitHub Desktop.
Ascii chord CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FRETBOARD: &str = "◯ ◯ ◯ ◯ ◯ ◯ | |
┌─┬─┬─┬─┬─┐ | |
│ │ │ │ │ │ | |
├─┼─┼─┼─┼─┤ | |
│ │ │ │ │ │ | |
├─┼─┼─┼─┼─┤ | |
│ │ │ │ │ │ | |
└─┴─┴─┴─┴─┘"; | |
use clap::Parser; | |
/// A CLI to show you how to play a guitar chord | |
#[derive(Parser, Debug)] | |
#[clap(version, about)] | |
struct Args { | |
/// Name of the chord | |
#[clap()] | |
name: String, | |
} | |
fn main() { | |
let args = Args::parse(); | |
println!("This is how you play '{}' chord: \n{}", args.name, FRETBOARD); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let args = Args::parse(); | |
let chords: HashMap<&str, &str> = | |
HashMap::from([("C", "x32010"), ("G", "320003"), ("D", "xx0232")]); | |
match chords.get(&args.name[..]) { | |
None => println!("Unknown chord '{}'", args.name), | |
Some(pattern) => { | |
let mut board: Vec<char> = FRETBOARD.chars().collect(); | |
for (i, ch) in pattern.chars().enumerate() { | |
let idx: usize = i * 2; | |
if ch == 'x' { | |
board[idx] = ch | |
} else { | |
let value: usize = ch.to_digit(10).unwrap() as usize; | |
board[idx] = ' '; | |
board[idx + 24 * value] = '◯' | |
} | |
} | |
println!( | |
"This is how you play '{}' chord: \n{}", | |
args.name, | |
board.iter().collect::<String>() | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment