mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-02 05:57:46 +00:00
fmt: refactor width and goal calculation
This commit is contained in:
parent
f5206ce783
commit
96ca5e609e
1 changed files with 23 additions and 19 deletions
|
@ -6,7 +6,6 @@
|
||||||
// spell-checker:ignore (ToDO) PSKIP linebreak ostream parasplit tabwidth xanti xprefix
|
// spell-checker:ignore (ToDO) PSKIP linebreak ostream parasplit tabwidth xanti xprefix
|
||||||
|
|
||||||
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
|
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
|
||||||
use std::cmp;
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{stdin, stdout, Write};
|
use std::io::{stdin, stdout, Write};
|
||||||
use std::io::{BufReader, BufWriter, Read, Stdout};
|
use std::io::{BufReader, BufWriter, Read, Stdout};
|
||||||
|
@ -84,27 +83,32 @@ impl FmtOptions {
|
||||||
let prefix = matches.get_one::<String>(OPT_PREFIX).map(String::from);
|
let prefix = matches.get_one::<String>(OPT_PREFIX).map(String::from);
|
||||||
let anti_prefix = matches.get_one::<String>(OPT_SKIP_PREFIX).map(String::from);
|
let anti_prefix = matches.get_one::<String>(OPT_SKIP_PREFIX).map(String::from);
|
||||||
|
|
||||||
let mut width = 75;
|
let width_opt = matches.get_one::<usize>(OPT_WIDTH);
|
||||||
let mut goal = 70;
|
let goal_opt = matches.get_one::<usize>(OPT_GOAL);
|
||||||
if let Some(w) = matches.get_one::<usize>(OPT_WIDTH) {
|
let (width, goal) = match (width_opt, goal_opt) {
|
||||||
width = *w;
|
(Some(&w), Some(&g)) => {
|
||||||
|
if g > w {
|
||||||
|
return Err(USimpleError::new(1, "GOAL cannot be greater than WIDTH."));
|
||||||
|
}
|
||||||
|
(w, g)
|
||||||
|
}
|
||||||
|
(Some(&w), None) => {
|
||||||
|
let g = (w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100).min(w - 3);
|
||||||
|
(w, g)
|
||||||
|
}
|
||||||
|
(None, Some(&g)) => {
|
||||||
|
let w = (g * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO).max(g + 3);
|
||||||
|
(w, g)
|
||||||
|
}
|
||||||
|
(None, None) => (75, 70),
|
||||||
|
};
|
||||||
|
|
||||||
if width > MAX_WIDTH {
|
if width > MAX_WIDTH {
|
||||||
return Err(USimpleError::new(
|
return Err(USimpleError::new(
|
||||||
1,
|
1,
|
||||||
format!("invalid width: '{}': Numerical result out of range", width),
|
format!("invalid width: '{}': Numerical result out of range", width),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
goal = cmp::min(width * DEFAULT_GOAL_TO_WIDTH_RATIO / 100, width - 3);
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(g) = matches.get_one::<usize>(OPT_GOAL) {
|
|
||||||
goal = *g;
|
|
||||||
if !matches.contains_id(OPT_WIDTH) {
|
|
||||||
width = cmp::max(goal * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO, goal + 3);
|
|
||||||
} else if goal > width {
|
|
||||||
return Err(USimpleError::new(1, "GOAL cannot be greater than WIDTH."));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut tabwidth = 8;
|
let mut tabwidth = 8;
|
||||||
if let Some(s) = matches.get_one::<String>(OPT_TAB_WIDTH) {
|
if let Some(s) = matches.get_one::<String>(OPT_TAB_WIDTH) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue