From 6cb3b0a4120714f782275826526832007b08becb Mon Sep 17 00:00:00 2001 From: pantonshire Date: Sun, 18 Sep 2022 21:59:23 +0100 Subject: [PATCH] add command line option to show full category name This patch adds `clap` as a dependency for command-line argument parsing, and introduces a `-f` flag to allow toggling whether or not the full names of character classes are displayed. --- Cargo.lock | 158 +++++++++++++++++++++++++++++++++++ utfdump_bin/Cargo.toml | 1 + utfdump_bin/src/main.rs | 36 +++++++- utfdump_core/src/chardata.rs | 2 +- 4 files changed, 193 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f903703..312f0a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,24 +2,117 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bytecount" version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c" +[[package]] +name = "clap" +version = "3.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "once_cell", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "heck" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "libc" +version = "0.2.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" + [[package]] name = "libshire" version = "0.1.0" @@ -28,6 +121,18 @@ dependencies = [ "serde", ] +[[package]] +name = "once_cell" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" + +[[package]] +name = "os_str_bytes" +version = "6.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff" + [[package]] name = "papergrid" version = "0.5.1" @@ -87,6 +192,12 @@ version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "1.0.99" @@ -122,6 +233,21 @@ dependencies = [ "syn", ] +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16" + [[package]] name = "unicode-ident" version = "1.0.4" @@ -138,6 +264,7 @@ checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" name = "utfdump" version = "0.1.0" dependencies = [ + "clap", "libshire", "tabled", "utfdump_core", @@ -152,3 +279,34 @@ name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/utfdump_bin/Cargo.toml b/utfdump_bin/Cargo.toml index ef1f19d..3e532f4 100644 --- a/utfdump_bin/Cargo.toml +++ b/utfdump_bin/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" utfdump_core = { path = "../utfdump_core" } libshire = { git = "https://github.com/pantonshire/libshire", branch = "main" } tabled = "0.8.0" +clap = { version = "3.2.22", features = ["derive"] } [build-dependencies] utfdump_core = { path = "../utfdump_core" } diff --git a/utfdump_bin/src/main.rs b/utfdump_bin/src/main.rs index 558677d..c476a32 100644 --- a/utfdump_bin/src/main.rs +++ b/utfdump_bin/src/main.rs @@ -1,5 +1,6 @@ use std::{fmt, io::{self, Read}}; +use clap::Parser; use libshire::strings::CappedString; use tabled::{Tabled, Table, Style}; @@ -7,7 +8,16 @@ use utfdump_core::{chardata::Category, encoded::Data}; const UNICODE_DATA: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/unicode_data_encoded")); +#[derive(Parser)] +#[clap(author, version, about, long_about = None)] +struct Args { + #[clap(short, long, action)] + full_category_names: bool, +} + fn main() { + let args = Args::parse(); + let data = Data::<'static>::from_bytes(UNICODE_DATA).unwrap(); let input = { @@ -32,7 +42,10 @@ fn main() { if let Some(char_data) = data.get(c as u32) { name = Optional::Some(char_data.name()); - category = Optional::Some(char_data.category()); + category = Optional::Some(DisplayCategory { + category: char_data.category(), + full_name: args.full_category_names, + }); let ccc = char_data.ccc(); char_combining_class = Optional::Some(ccc); @@ -68,14 +81,14 @@ fn main() { struct OutRow { #[tabled(rename = "")] display_char: CappedString<8>, - #[tabled(rename = "Codepoint")] + #[tabled(rename = "Code")] codepoint: Codepoint, #[tabled(rename = "UTF-8")] utf_8_bytes: Utf8Bytes, #[tabled(rename = "Name")] name: Optional<&'static str>, #[tabled(rename = "Category")] - category: Optional, + category: Optional, #[tabled(rename = "Combining")] char_combining_class: Optional, } @@ -124,3 +137,20 @@ impl fmt::Display for Utf8Bytes { Ok(()) } } + +#[derive(Debug)] +struct DisplayCategory { + category: Category, + full_name: bool, +} + +impl fmt::Display for DisplayCategory { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.full_name { + write!(f, "{}", self.category.full_name()) + } else { + write!(f, "{}", self.category.abbr()) + } + } +} + diff --git a/utfdump_core/src/chardata.rs b/utfdump_core/src/chardata.rs index 9426bb8..1b5a100 100644 --- a/utfdump_core/src/chardata.rs +++ b/utfdump_core/src/chardata.rs @@ -231,7 +231,7 @@ impl Category { impl fmt::Display for Category { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}: {}", self.abbr(), self.full_name()) + write!(f, "{}", self.abbr()) } }