mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-09-14 19:16:17 +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
73
fmt/fmt.rs
73
fmt/fmt.rs
|
@ -49,7 +49,7 @@ struct FmtOptions {
|
||||||
prefix_len : uint,
|
prefix_len : uint,
|
||||||
use_anti_prefix : bool,
|
use_anti_prefix : bool,
|
||||||
anti_prefix : String,
|
anti_prefix : String,
|
||||||
xanti_prefix: bool,
|
xanti_prefix : bool,
|
||||||
uniform : bool,
|
uniform : bool,
|
||||||
width : uint,
|
width : uint,
|
||||||
goal : uint,
|
goal : uint,
|
||||||
|
@ -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,44 +124,58 @@ 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); }
|
||||||
};
|
};
|
||||||
if ! matches.opt_present("w") {
|
if !matches.opt_present("w") {
|
||||||
fmt_opts.width = std::cmp::max(fmt_opts.goal * 100 / 92, fmt_opts.goal + 4);
|
fmt_opts.width = std::cmp::max(fmt_opts.goal * 100 / 92, fmt_opts.goal + 4);
|
||||||
} 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
|
||||||
|
@ -204,7 +221,7 @@ fn uumain(args: Vec<String>) -> int {
|
||||||
// handle "init" portion
|
// handle "init" portion
|
||||||
silent_unwrap!(ostream.write(para.init_str.as_bytes()));
|
silent_unwrap!(ostream.write(para.init_str.as_bytes()));
|
||||||
para.init_len
|
para.init_len
|
||||||
} else if ! para.mail_header {
|
} else if !para.mail_header {
|
||||||
// for non-(crown, tagged) that's the same as a normal indent
|
// for non-(crown, tagged) that's the same as a normal indent
|
||||||
silent_unwrap!(ostream.write(pIndent.as_bytes()));
|
silent_unwrap!(ostream.write(pIndent.as_bytes()));
|
||||||
pIndentLen
|
pIndentLen
|
||||||
|
|
|
@ -65,14 +65,14 @@ impl<'a> FileLines<'a> {
|
||||||
|
|
||||||
// returns true if this line should be formatted
|
// returns true if this line should be formatted
|
||||||
fn match_prefix(&self, line: &str) -> (bool, uint) {
|
fn match_prefix(&self, line: &str) -> (bool, uint) {
|
||||||
if ! self.opts.use_prefix { return (true, 0u); }
|
if !self.opts.use_prefix { return (true, 0u); }
|
||||||
|
|
||||||
FileLines::match_prefix_generic(self.opts.prefix.as_slice(), line, self.opts.xprefix)
|
FileLines::match_prefix_generic(self.opts.prefix.as_slice(), line, self.opts.xprefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true if this line should be formatted
|
// returns true if this line should be formatted
|
||||||
fn match_anti_prefix(&self, line: &str) -> bool {
|
fn match_anti_prefix(&self, line: &str) -> bool {
|
||||||
if ! self.opts.use_anti_prefix { return true; }
|
if !self.opts.use_anti_prefix { return true; }
|
||||||
|
|
||||||
match FileLines::match_prefix_generic(self.opts.anti_prefix.as_slice(), line, self.opts.xanti_prefix) {
|
match FileLines::match_prefix_generic(self.opts.anti_prefix.as_slice(), line, self.opts.xanti_prefix) {
|
||||||
(true, _) => false,
|
(true, _) => false,
|
||||||
|
@ -85,7 +85,7 @@ impl<'a> FileLines<'a> {
|
||||||
return (true, 0);
|
return (true, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ! exact {
|
if !exact {
|
||||||
// we do it this way rather than byte indexing to support unicode whitespace chars
|
// we do it this way rather than byte indexing to support unicode whitespace chars
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
while (i < line.len()) && line.char_at(i).is_whitespace() {
|
while (i < line.len()) && line.char_at(i).is_whitespace() {
|
||||||
|
@ -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> {
|
||||||
|
@ -122,13 +126,13 @@ impl<'a> Iterator<Line> for FileLines<'a> {
|
||||||
// if this line does not match the prefix,
|
// if this line does not match the prefix,
|
||||||
// emit the line unprocessed and iterate again
|
// emit the line unprocessed and iterate again
|
||||||
let (pmatch, poffset) = self.match_prefix(n.as_slice());
|
let (pmatch, poffset) = self.match_prefix(n.as_slice());
|
||||||
if ! pmatch {
|
if !pmatch {
|
||||||
return Some(NoFormatLine(n, false));
|
return Some(NoFormatLine(n, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
// if this line matches the anti_prefix
|
// if this line matches the anti_prefix
|
||||||
// (NOTE definition of match_anti_prefix is TRUE if we should process)
|
// (NOTE definition of match_anti_prefix is TRUE if we should process)
|
||||||
if ! self.match_anti_prefix(n.as_slice()) {
|
if !self.match_anti_prefix(n.as_slice()) {
|
||||||
return Some(NoFormatLine(n, false));
|
return Some(NoFormatLine(n, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
};
|
};
|
||||||
|
@ -185,7 +185,7 @@ impl<'a> Iterator<Line> for FileLines<'a> {
|
||||||
// [?!.]\t\t is. We could expand tabs to two spaces to force detection of tab as
|
// [?!.]\t\t is. We could expand tabs to two spaces to force detection of tab as
|
||||||
// sentence ending
|
// sentence ending
|
||||||
if self.opts.uniform {
|
if self.opts.uniform {
|
||||||
let tabinds: Vec<uint> = n.as_slice().slice_from(indEnd).char_indices().filter_map(|(i,c)| if c == '\t' { Some(i) } else { None }).collect();
|
let tabinds: Vec<uint> = n.as_slice().slice_from(indEnd).char_indices().filter_map(|(i, c)| if c == '\t' { Some(i) } else { None }).collect();
|
||||||
unsafe {
|
unsafe {
|
||||||
let nmut = n.as_mut_bytes();
|
let nmut = n.as_mut_bytes();
|
||||||
for i in tabinds.iter() {
|
for i in tabinds.iter() {
|
||||||
|
@ -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
|
||||||
});
|
});
|
||||||
|
@ -335,7 +335,7 @@ impl<'a> Iterator<Result<Paragraph,String>> for ParagraphStream<'a> {
|
||||||
// these will be overwritten in the 2nd line of crown or tagged mode, but
|
// these will be overwritten in the 2nd line of crown or tagged mode, but
|
||||||
// we are not guaranteed to get to the 2nd line, e.g., if the next line
|
// we are not guaranteed to get to the 2nd line, e.g., if the next line
|
||||||
// is a NoFormatLine or None. Thus, we set sane defaults the 1st time around
|
// is a NoFormatLine or None. Thus, we set sane defaults the 1st time around
|
||||||
indent_str.push_str(fl.line.as_slice().slice(fl.prefix_end,fl.indent_end));
|
indent_str.push_str(fl.line.as_slice().slice(fl.prefix_end, fl.indent_end));
|
||||||
indent_len = fl.indent_len;
|
indent_len = fl.indent_len;
|
||||||
indent_end = fl.indent_end;
|
indent_end = fl.indent_end;
|
||||||
|
|
||||||
|
@ -356,10 +356,10 @@ impl<'a> Iterator<Result<Paragraph,String>> for ParagraphStream<'a> {
|
||||||
}
|
}
|
||||||
} else if in_mail {
|
} else if in_mail {
|
||||||
// lines following mail headers must begin with spaces
|
// lines following mail headers must begin with spaces
|
||||||
if (self.opts.use_prefix && fl.pfxind_end == 0) || (! self.opts.use_prefix && fl.indent_end == 0) {
|
if (self.opts.use_prefix && fl.pfxind_end == 0) || (!self.opts.use_prefix && fl.indent_end == 0) {
|
||||||
break; // this line does not begin with spaces
|
break; // this line does not begin with spaces
|
||||||
}
|
}
|
||||||
} else if ! second_done && (self.opts.crown || self.opts.tagged) {
|
} else if !second_done && (self.opts.crown || self.opts.tagged) {
|
||||||
// now we have enough info to handle crown margin and tagged mode
|
// now we have enough info to handle crown margin and tagged mode
|
||||||
if pfxind_len != fl.pfxind_len {
|
if pfxind_len != fl.pfxind_len {
|
||||||
// in both crown and tagged modes we require that pfxind is the same
|
// in both crown and tagged modes we require that pfxind is the same
|
||||||
|
@ -370,7 +370,7 @@ impl<'a> Iterator<Result<Paragraph,String>> for ParagraphStream<'a> {
|
||||||
} else {
|
} else {
|
||||||
// this is part of the same paragraph, get the indent info from this line
|
// this is part of the same paragraph, get the indent info from this line
|
||||||
indent_str.clear();
|
indent_str.clear();
|
||||||
indent_str.push_str(fl.line.as_slice().slice(fl.prefix_end,fl.indent_end));
|
indent_str.push_str(fl.line.as_slice().slice(fl.prefix_end, fl.indent_end));
|
||||||
indent_len = fl.indent_len;
|
indent_len = fl.indent_len;
|
||||||
indent_end = fl.indent_end;
|
indent_end = fl.indent_end;
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -500,7 +501,7 @@ impl<'a> Iterator<&'a str> for WordSplit<'a> {
|
||||||
// note that this preserves the invariant that self.position points to
|
// note that this preserves the invariant that self.position points to
|
||||||
// non-whitespace character OR end of string
|
// non-whitespace character OR end of string
|
||||||
self.position =
|
self.position =
|
||||||
match self.string.slice_from(ws_start).find(|x: char| ! x.is_whitespace()) {
|
match self.string.slice_from(ws_start).find(|x: char| !x.is_whitespace()) {
|
||||||
None => self.length,
|
None => self.length,
|
||||||
Some(s) => s + ws_start
|
Some(s) => s + ws_start
|
||||||
};
|
};
|
||||||
|
@ -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