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