mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 04:27:45 +00:00
fmt: style modifications suggested by Arcterus
This commit is contained in:
parent
6228bb4b85
commit
5d2a2b6a0b
2 changed files with 194 additions and 175 deletions
67
fmt/fmt.rs
67
fmt/fmt.rs
|
@ -97,21 +97,22 @@ fn uumain(args: Vec<String>) -> int {
|
|||
return 0
|
||||
}
|
||||
|
||||
let mut fmt_opts = FmtOptions { crown : false
|
||||
, tagged : false
|
||||
, mail : false
|
||||
, uniform : false
|
||||
, split_only : false
|
||||
, use_prefix : false
|
||||
, prefix : String::new()
|
||||
, xprefix : false
|
||||
, prefix_len : 0
|
||||
, use_anti_prefix : false
|
||||
, anti_prefix : String::new()
|
||||
, xanti_prefix: false
|
||||
, width : 78
|
||||
, goal : 72
|
||||
, tabwidth : 8
|
||||
let mut fmt_opts = FmtOptions {
|
||||
crown : false,
|
||||
tagged : false,
|
||||
mail : false,
|
||||
uniform : false,
|
||||
split_only : false,
|
||||
use_prefix : false,
|
||||
prefix : String::new(),
|
||||
xprefix : false,
|
||||
prefix_len : 0,
|
||||
use_anti_prefix : false,
|
||||
anti_prefix : String::new(),
|
||||
xanti_prefix : false,
|
||||
width : 78,
|
||||
goal : 72,
|
||||
tabwidth : 8,
|
||||
};
|
||||
|
||||
if matches.opt_present("t") { fmt_opts.tagged = true; }
|
||||
|
@ -123,27 +124,38 @@ fn uumain(args: Vec<String>) -> int {
|
|||
if matches.opt_present("X") { fmt_opts.xanti_prefix = true; }
|
||||
|
||||
match matches.opt_str("p") {
|
||||
Some(s) => { fmt_opts.prefix = s; fmt_opts.use_prefix = true; fmt_opts.prefix_len = fmt_opts.prefix.as_slice().char_len() },
|
||||
Some(s) => {
|
||||
fmt_opts.prefix = s;
|
||||
fmt_opts.use_prefix = true;
|
||||
fmt_opts.prefix_len = fmt_opts.prefix.as_slice().char_len()
|
||||
}
|
||||
None => ()
|
||||
};
|
||||
|
||||
match matches.opt_str("P") {
|
||||
Some(s) => { fmt_opts.anti_prefix = s; fmt_opts.use_anti_prefix = true; },
|
||||
Some(s) => {
|
||||
fmt_opts.anti_prefix = s;
|
||||
fmt_opts.use_anti_prefix = true;
|
||||
}
|
||||
None => ()
|
||||
};
|
||||
|
||||
match matches.opt_str("w") {
|
||||
Some(s) => { fmt_opts.width = match from_str(s.as_slice()) {
|
||||
Some(s) => {
|
||||
fmt_opts.width =
|
||||
match from_str(s.as_slice()) {
|
||||
Some(t) => t,
|
||||
None => { crash!(1, "Invalid WIDTH specification: `{}'", s); }
|
||||
};
|
||||
fmt_opts.goal = std::cmp::min(fmt_opts.width * 92 / 100, fmt_opts.width - 4);
|
||||
},
|
||||
}
|
||||
None => ()
|
||||
};
|
||||
|
||||
match matches.opt_str("g") {
|
||||
Some(s) => { fmt_opts.goal = match from_str(s.as_slice()) {
|
||||
Some(s) => {
|
||||
fmt_opts.goal =
|
||||
match from_str(s.as_slice()) {
|
||||
Some(t) => t,
|
||||
None => { crash!(1, "Invalid GOAL specification: `{}'", s); }
|
||||
};
|
||||
|
@ -152,15 +164,18 @@ fn uumain(args: Vec<String>) -> int {
|
|||
} else if fmt_opts.goal > fmt_opts.width {
|
||||
crash!(1, "GOAL cannot be greater than WIDTH.");
|
||||
}
|
||||
},
|
||||
}
|
||||
None => ()
|
||||
};
|
||||
|
||||
match matches.opt_str("T") {
|
||||
Some(s) => fmt_opts.tabwidth = match from_str(s.as_slice()) {
|
||||
Some(s) => {
|
||||
fmt_opts.tabwidth =
|
||||
match from_str(s.as_slice()) {
|
||||
Some(t) => t,
|
||||
None => { crash!(1, "Invalid TABWIDTH specification: `{}'", s); }
|
||||
},
|
||||
};
|
||||
}
|
||||
None => ()
|
||||
};
|
||||
|
||||
|
@ -179,8 +194,10 @@ fn uumain(args: Vec<String>) -> int {
|
|||
let mut ostream = box BufferedWriter::new(stdout_raw()) as Box<Writer>;
|
||||
|
||||
for i in files.iter().map(|x| x.as_slice()) {
|
||||
let mut fp = match open_file(i) {
|
||||
Err(e) => { show_warning!("{}: {}",i,e);
|
||||
let mut fp =
|
||||
match open_file(i) {
|
||||
Err(e) => {
|
||||
show_warning!("{}: {}",i,e);
|
||||
continue;
|
||||
}
|
||||
Ok(f) => f
|
||||
|
|
|
@ -98,6 +98,10 @@ impl<'a> FileLines<'a> {
|
|||
|
||||
(false, 0)
|
||||
}
|
||||
|
||||
fn displayed_length(&self, s: &str) -> uint {
|
||||
s.char_len() + (self.opts.tabwidth - 1) * s.chars().filter(|x| x == &'\t').count()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator<Line> for FileLines<'a> {
|
||||
|
@ -154,19 +158,16 @@ impl<'a> Iterator<Line> for FileLines<'a> {
|
|||
let pfxEnd = poffset + self.opts.prefix.len();
|
||||
let nSlice = n.as_slice().slice_from(pfxEnd);
|
||||
let nSlice2 = nSlice.trim_left();
|
||||
|
||||
(pfxEnd + nSlice.len() - nSlice2.len(), pfxEnd, poffset)
|
||||
} else {
|
||||
let nSlice = n.as_slice().trim_left();
|
||||
|
||||
(nLen - nSlice.len(), 0, 0)
|
||||
};
|
||||
|
||||
// indent length
|
||||
let indLen =
|
||||
if indEnd > 0 {
|
||||
let nSlice = n.as_slice().slice(pfxEnd, indEnd);
|
||||
nSlice.char_len() + (self.opts.tabwidth - 1) * nSlice.chars().filter(|x| x == &'\t').count()
|
||||
self.displayed_length(n.as_slice().slice(pfxEnd, indEnd))
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
@ -174,8 +175,7 @@ impl<'a> Iterator<Line> for FileLines<'a> {
|
|||
// prefix indent length
|
||||
let pfxIndLen =
|
||||
if pfxIndEnd > 0 {
|
||||
let nSlice = n.as_slice().slice_to(pfxIndEnd);
|
||||
nSlice.char_len() + (self.opts.tabwidth - 1) * nSlice.chars().filter(|x| x == &'\t').count()
|
||||
self.displayed_length(n.as_slice().slice_to(pfxIndEnd))
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
@ -194,12 +194,13 @@ impl<'a> Iterator<Line> for FileLines<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
Some(FormatLine(FileLine { line: n
|
||||
, indent_end: indEnd
|
||||
, prefix_end: pfxEnd
|
||||
, pfxind_end: pfxIndEnd
|
||||
, indent_len: indLen
|
||||
, pfxind_len: pfxIndLen
|
||||
Some(FormatLine(FileLine {
|
||||
line : n,
|
||||
indent_end : indEnd,
|
||||
prefix_end : pfxEnd,
|
||||
pfxind_end : pfxIndEnd,
|
||||
indent_len : indLen,
|
||||
pfxind_len : pfxIndLen,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -245,11 +246,11 @@ impl<'a> ParagraphStream<'a> {
|
|||
// or with a sequence of printable ASCII chars (33 to 126, inclusive,
|
||||
// except colon) followed by a colon.
|
||||
if line.indent_end > 0 {
|
||||
return false;
|
||||
false
|
||||
} else {
|
||||
let lSlice = line.line.as_slice();
|
||||
if lSlice.starts_with("From ") {
|
||||
return true;
|
||||
true
|
||||
} else {
|
||||
let colonPosn =
|
||||
match lSlice.find(':') {
|
||||
|
@ -260,8 +261,7 @@ impl<'a> ParagraphStream<'a> {
|
|||
// header field must be nonzero length
|
||||
if colonPosn == 0 { return false; }
|
||||
|
||||
return lSlice.slice_to(colonPosn).chars()
|
||||
.all(|x| match x as uint {
|
||||
return lSlice.slice_to(colonPosn).chars().all(|x| match x as uint {
|
||||
y if y < 33 || y > 126 => false,
|
||||
_ => true
|
||||
});
|
||||
|
@ -396,16 +396,17 @@ impl<'a> Iterator<Result<Paragraph,String>> for ParagraphStream<'a> {
|
|||
// NoFormatLine.
|
||||
self.next_mail = in_mail;
|
||||
|
||||
Some(Ok(Paragraph { lines: pLines
|
||||
, init_str: init_str
|
||||
, init_len: init_len
|
||||
, init_end: init_end
|
||||
, indent_str: indent_str
|
||||
, indent_len: indent_len
|
||||
, indent_end: indent_end
|
||||
, pfxind_str: pfxind_str
|
||||
, pfxind_len: pfxind_len
|
||||
, mail_header: in_mail
|
||||
Some(Ok(Paragraph {
|
||||
lines : pLines,
|
||||
init_str : init_str,
|
||||
init_len : init_len,
|
||||
init_end : init_end,
|
||||
indent_str : indent_str,
|
||||
indent_len : indent_len,
|
||||
indent_end : indent_end,
|
||||
pfxind_str : pfxind_str,
|
||||
pfxind_len : pfxind_len,
|
||||
mail_header : in_mail
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -525,6 +526,7 @@ impl<'a> Iterator<&'a str> for WordSplit<'a> {
|
|||
// eventually we will want to annotate where the sentence boundaries are
|
||||
// so that we can give preference to splitting lines appropriately
|
||||
self.string.slice(old_position, self.position)
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue