Skip to content

Commit

Permalink
feat: change binary to chara & add shell completions
Browse files Browse the repository at this point in the history
  • Loading branch information
latipun7 committed Mar 1, 2023
1 parent 7d87c99 commit 72548cb
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 77 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ categories = [
"rendering",
]

[[bin]]
name = "chara"

[dependencies]
clap = { version = "4.1.6", features = ["derive"] }
clap_complete = "4.1.4"
rust-embed = { version = "6.4.2", features = ["interpolate-folder-path"] }
textwrap = { version = "0.16.0", features = ["terminal_size"] }
unicode-width = "0.1.10"
Expand Down
125 changes: 125 additions & 0 deletions src/bin/chara.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use std::io::{stdin, stdout, Read};

use charasay::{format_character, list_chara};
use clap::{Command, CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Generator, Shell};
use rand::seq::SliceRandom;
use textwrap::termwidth;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about)]
struct Cli {
#[command(subcommand)]
subcommands: Subcommands,
}

#[derive(Subcommand, Debug)]
enum Subcommands {
/// Make the character say something.
Say {
/// Messages that chara want to say/think. If empty, read from STDIN.
message: Vec<String>,

/// Choose random chara.
#[arg(short, long)]
random: bool,

/// Print all available chara.
#[arg(short, long)]
all: bool,

/// Make chara only thinking about it, not saying it.
#[arg(short, long)]
think: bool,

/// Max width of speech bubble. Default to terminal width.
#[arg(short, long)]
width: Option<usize>,

/// Which chara should say/think
#[arg(short = 'f', long = "file")]
chara: Option<String>,
},

/// Generate completions for shell. Default to current shell.
Completions {
/// Shell syntax to use. Infer current shell when missing, fallback to bash.
#[arg(short, long, value_enum)]
shell: Option<Shell>,
},

/// TODO: Convert pixel-arts PNG to chara files.
Convert,
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, "chara".to_string(), &mut stdout());
}

fn main() {
let cli = Cli::parse();

// Run subcommands if match
match cli.subcommands {
Subcommands::Say {
message,
random,
all,
think,
width,
chara,
} => {
let mut messages = message.join(" ");

if messages.is_empty() {
let mut buffer = String::new();

stdin()
.read_to_string(&mut buffer)
.unwrap_or_else(|err| todo!("Log ERROR: {:#?}", err));

messages = buffer.trim_end().to_string();
}

let max_width = match width {
Some(w) => w,
None => termwidth() - 6,
};

if all {
let charas = list_chara();
for chara in charas {
println!("\n\n{}", chara);
println!(
"{}",
format_character(messages.as_str(), &chara, max_width, think)
);
}
} else {
let chara = if random {
let charas = list_chara();
charas.choose(&mut rand::thread_rng()).unwrap().to_owned()
} else {
chara.unwrap_or("cow".to_string())
};

println!(
"{}",
format_character(messages.as_str(), &chara, max_width, think)
);
}
}

Subcommands::Completions { shell } => {
let mut cmd = Cli::command();
let gen = match shell {
Some(s) => s,
None => Shell::from_env().unwrap_or(Shell::Bash),
};

print_completions(gen, &mut cmd);
}

Subcommands::Convert => {}
}
}
77 changes: 0 additions & 77 deletions src/main.rs

This file was deleted.

0 comments on commit 72548cb

Please sign in to comment.