From c4ba21f720710dc09dcf86b02020bd8d93ee7393 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Wed, 29 Dec 2021 20:44:50 +0100 Subject: [PATCH] move numfmt help to a separate file --- src/uu/numfmt/help.md | 42 +++++++++++++++++++++++++++++++++++++ src/uu/numfmt/src/numfmt.rs | 40 +++-------------------------------- src/uucore_procs/src/lib.rs | 41 +++++++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 38 deletions(-) create mode 100644 src/uu/numfmt/help.md diff --git a/src/uu/numfmt/help.md b/src/uu/numfmt/help.md new file mode 100644 index 000000000..51ce1de15 --- /dev/null +++ b/src/uu/numfmt/help.md @@ -0,0 +1,42 @@ + +# numfmt + +## About + +Convert numbers from/to human-readable strings + +## Long Help + +UNIT options: + none no auto-scaling is done; suffixes will trigger an error + + auto accept optional single/two letter suffix: + + 1K = 1000, 1Ki = 1024, 1M = 1000000, 1Mi = 1048576, + + si accept optional single letter suffix: + + 1K = 1000, 1M = 1000000, ... + + iec accept optional single letter suffix: + + 1K = 1024, 1M = 1048576, ... + + iec-i accept optional two-letter suffix: + + 1Ki = 1024, 1Mi = 1048576, ... + +FIELDS supports cut(1) style field ranges: + N N'th field, counted from 1 + N- from N'th field, to end of line + N-M from N'th to M'th field (inclusive) + -M from first to M'th field (inclusive) + - all fields +Multiple fields/ranges can be separated with commas + +FORMAT must be suitable for printing one floating-point argument '%f'. +Optional quote (%'f) will enable --grouping (if supported by current locale). +Optional width value (%10f) will pad output. Optional zero (%010f) width +will zero pad the number. Optional negative values (%-10f) will left align. +Optional precision (%.1f) will override the input determined precision. + diff --git a/src/uu/numfmt/src/numfmt.rs b/src/uu/numfmt/src/numfmt.rs index d259d001d..dbe769ed5 100644 --- a/src/uu/numfmt/src/numfmt.rs +++ b/src/uu/numfmt/src/numfmt.rs @@ -5,8 +5,6 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore N'th M'th - use crate::errors::*; use crate::format::format_and_print; use crate::options::*; @@ -17,47 +15,15 @@ use units::{IEC_BASES, SI_BASES}; use uucore::display::Quotable; use uucore::error::UResult; use uucore::ranges::Range; -use uucore::{format_usage, InvalidEncodingHandling}; +use uucore::{format_usage, help_section, InvalidEncodingHandling}; pub mod errors; pub mod format; pub mod options; mod units; -static ABOUT: &str = "Convert numbers from/to human-readable strings"; -static LONG_HELP: &str = "UNIT options: - none no auto-scaling is done; suffixes will trigger an error - - auto accept optional single/two letter suffix: - - 1K = 1000, 1Ki = 1024, 1M = 1000000, 1Mi = 1048576, - - si accept optional single letter suffix: - - 1K = 1000, 1M = 1000000, ... - - iec accept optional single letter suffix: - - 1K = 1024, 1M = 1048576, ... - - iec-i accept optional two-letter suffix: - - 1Ki = 1024, 1Mi = 1048576, ... - -FIELDS supports cut(1) style field ranges: - N N'th field, counted from 1 - N- from N'th field, to end of line - N-M from N'th to M'th field (inclusive) - -M from first to M'th field (inclusive) - - all fields -Multiple fields/ranges can be separated with commas - -FORMAT must be suitable for printing one floating-point argument '%f'. -Optional quote (%'f) will enable --grouping (if supported by current locale). -Optional width value (%10f) will pad output. Optional zero (%010f) width -will zero pad the number. Optional negative values (%-10f) will left align. -Optional precision (%.1f) will override the input determined precision. -"; +const ABOUT: &str = help_section!("about"); +const LONG_HELP: &str = help_section!("long help"); const USAGE: &str = "{} [OPTION]... [NUMBER]..."; fn handle_args<'a>(args: impl Iterator, options: &NumfmtOptions) -> UResult<()> { diff --git a/src/uucore_procs/src/lib.rs b/src/uucore_procs/src/lib.rs index 3a32dab83..036c24faa 100644 --- a/src/uucore_procs/src/lib.rs +++ b/src/uucore_procs/src/lib.rs @@ -1,7 +1,9 @@ // Copyright (C) ~ Roy Ivy III ; MIT license extern crate proc_macro; -use proc_macro::TokenStream; +use std::{fs::File, io::Read, path::PathBuf}; + +use proc_macro::{Literal, TokenStream, TokenTree}; use quote::quote; //## rust proc-macro background info @@ -34,3 +36,40 @@ pub fn main(_args: TokenStream, stream: TokenStream) -> TokenStream { TokenStream::from(new) } + +#[proc_macro] +pub fn help_section(input: TokenStream) -> TokenStream { + let input: Vec = input.into_iter().collect(); + let value = match &input.get(0) { + Some(TokenTree::Literal(literal)) => literal.to_string(), + _ => panic!("Input to help_section should be a string literal!"), + }; + let input_str: String = value.parse().unwrap(); + let input_str = input_str.to_lowercase().trim_matches('"').to_string(); + + let mut content = String::new(); + let mut path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); + + path.push("help.md"); + + File::open(path) + .unwrap() + .read_to_string(&mut content) + .unwrap(); + + let text = content + .lines() + .skip_while(|&l| { + l.strip_prefix("##") + .map_or(true, |l| l.trim().to_lowercase() != input_str) + }) + .skip(1) + .take_while(|l| !l.starts_with("##")) + .collect::>() + .join("\n") + .trim() + .to_string(); + + let str = TokenTree::Literal(Literal::string(&text)); + str.into() +}