core: add sign pattern command line argument handling

This commit is contained in:
Matthias Schiffer 2024-01-04 14:21:12 +01:00
parent 6da921cca3
commit 9fd5689ebb
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
4 changed files with 75 additions and 4 deletions

View file

@ -6,7 +6,9 @@ use std::{
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
use indexmap::IndexSet;
use regex::RegexSet;
use serde::{Deserialize, Serialize};
use crate::{
@ -147,11 +149,13 @@ pub struct Config {
pub viewer_info_path: PathBuf,
/// Path of viewer entities file
pub viewer_entities_path: PathBuf,
/// Sign text filter patterns
pub sign_patterns: RegexSet,
}
impl Config {
/// Crates a new [Config] from [command line arguments](super::Args)
pub fn new(args: &super::Args) -> Self {
pub fn new(args: &super::Args) -> Result<Self> {
let num_threads = match args.jobs {
Some(0) => num_cpus::get(),
Some(threads) => threads,
@ -168,7 +172,9 @@ impl Config {
.iter()
.collect();
Config {
let sign_patterns = Self::sign_patterns(args).context("Failed to parse sign patterns")?;
Ok(Config {
num_threads,
region_dir,
level_dat_path,
@ -178,7 +184,20 @@ impl Config {
entities_path_final,
viewer_info_path,
viewer_entities_path,
}
sign_patterns,
})
}
/// Parses the sign prefixes and sign filters into a [RegexSet]
fn sign_patterns(args: &super::Args) -> Result<RegexSet> {
let prefix_patterns: Vec<_> = args
.sign_prefix
.iter()
.map(|prefix| format!("^{}", regex::escape(prefix)))
.collect();
Ok(RegexSet::new(
prefix_patterns.iter().chain(args.sign_filter.iter()),
)?)
}
/// Constructs the path to an input region file

View file

@ -47,6 +47,18 @@ pub struct Args {
/// Enable verbose messages
#[arg(short, long)]
pub verbose: bool,
/// Prefix for text of signs to show on the map
#[arg(long)]
pub sign_prefix: Vec<String>,
/// Regular expression for text of signs to show on the map
///
/// --sign-prefix and --sign-filter allow to filter for signs to display;
/// by default, none are visible. The options may be passed multiple times,
/// and a sign will be visible if it matches any pattern.
///
/// To make all signs visible, pass an empty string to either option.
#[arg(long)]
pub sign_filter: Vec<String>,
/// Minecraft save directory
pub input_dir: PathBuf,
/// MinedMap data directory
@ -64,7 +76,7 @@ fn setup_threads(num_threads: usize) -> Result<()> {
/// MinedMap CLI main function
pub fn cli() -> Result<()> {
let args = Args::parse();
let config = Config::new(&args);
let config = Config::new(&args)?;
tracing_subscriber::fmt()
.with_max_level(if args.verbose {