Initialize Minecraft text generator project with basic structure and CLI functionality
This commit is contained in:
64
crates/bin/src/cli.rs
Normal file
64
crates/bin/src/cli.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
use clap::Parser;
|
||||
use exporters::McFunctionExporter;
|
||||
use lib::{StructureExporter, TextBuilder, TtfFont};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
/// The text you want to generate
|
||||
#[arg(short, long)]
|
||||
text: String,
|
||||
|
||||
/// Path to the .ttf font file
|
||||
#[arg(short, long)]
|
||||
font: PathBuf,
|
||||
|
||||
/// How many blocks deep the text should be
|
||||
#[arg(short, long, default_value_t = 1)]
|
||||
depth: u32,
|
||||
|
||||
/// The Minecraft block ID to use for the text body
|
||||
#[arg(short, long, default_value = "minecraft:quartz_block")]
|
||||
block: String,
|
||||
|
||||
/// Output file path (without extension)
|
||||
#[arg(short, long, default_value = "output")]
|
||||
out: PathBuf,
|
||||
|
||||
/// Height of the text in blocks
|
||||
#[arg(long, default_value_t = 16.0)]
|
||||
size: f32,
|
||||
}
|
||||
|
||||
pub fn run() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
tracing::info!("loading font from: {:?}", cli.font);
|
||||
let font_bytes = fs::read(&cli.font)?;
|
||||
|
||||
let font = TtfFont::from_bytes(&font_bytes, cli.size)
|
||||
.map_err(|e| anyhow::anyhow!(e))?;
|
||||
|
||||
tracing::info!("generating voxel grid for text: '{}'", cli.text);
|
||||
let builder = TextBuilder::new(&font).with_depth(cli.depth);
|
||||
let grid = builder.generate(&cli.text);
|
||||
|
||||
tracing::info!(
|
||||
"grid generated: {}x{}x{}",
|
||||
grid.width, grid.height, grid.depth
|
||||
);
|
||||
|
||||
let exporter = McFunctionExporter::new(&cli.block, "minecraft:obsidian");
|
||||
|
||||
let output_bytes = exporter.export(&grid)?;
|
||||
|
||||
let mut out_path = cli.out.clone();
|
||||
out_path.set_extension(exporter.file_extension());
|
||||
|
||||
fs::write(&out_path, output_bytes)?;
|
||||
tracing::info!("saved to: {:?}", out_path);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
1
crates/bin/src/lib.rs
Normal file
1
crates/bin/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod cli;
|
||||
10
crates/bin/src/main.rs
Normal file
10
crates/bin/src/main.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
fn main() {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.init();
|
||||
|
||||
minecraft_text_generator::cli::run().unwrap_or_else(|e| {
|
||||
tracing::error!("{e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user