-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nl: implement -d/--section-delimiter #5158
Conversation
2e60c32
to
37cd034
Compare
src/uu/nl/src/nl.rs
Outdated
match s { | ||
_ if s == self.delimiter.repeat(3) => SectionDelimiter::Header, | ||
_ if s == self.delimiter.repeat(2) => SectionDelimiter::Body, | ||
_ if s == self.delimiter => SectionDelimiter::Footer, | ||
_ => SectionDelimiter::None, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, so this is more of a brainstorm than actual feedback, because this expression is very cool and clever but also a bit weird. Maybe it could be:
if s.trim_left(self.delimiter).is_empty() {
match s.len() / self.delimiter.len() {
3 => SectionDelimiter::Header,
2 => SectionDelimiter::Body,
1 => SectionDelimiter::Footer,
_ => SectionDelimiter::None,
}
This avoids the allocations of the strings from repeat
as well. But I'm not actually sure it's better.
src/uu/nl/src/nl.rs
Outdated
if self.delimiter.is_empty() { | ||
return SectionDelimiter::None; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does making self.delimiter
an Option<SectionDelimiter>
make sense? Or maybe the whole SectionDelimiterParser
?
src/uu/nl/src/nl.rs
Outdated
|
||
impl SectionDelimiterParser { | ||
fn new(delimiter: &str) -> Self { | ||
let delimiter = if delimiter.chars().count() == 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let delimiter = if delimiter.chars().count() == 1 { | |
let delimiter = if delimiter.len() == 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, len
returns the number of bytes, not the number of chars (for example, "ä".len()
returns 2
). And my intention was to get the number of chars. On the other hand, using len
leads to the same output as GNU nl when specifying ä
as delimiter. Which is incorrect, in my opinion ;-) I guess I have to ask in the GNU coreutils project what the expected output should be when using a single non-ASCII char.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh I see. Maybe it should even be unicode width?
37cd034
to
8b9a946
Compare
@tertsdiepraam I addressed your feedback in the latest push in the following way:
In addition I got rid of the |
Footer, | ||
} | ||
|
||
impl SectionDelimiter { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is beautiful now!
CI failures seem unrelated, but I'm rerunning them just to be sure. |
8b9a946
to
db34255
Compare
Somehow I overlooked that this PR got approved a while ago :| |
This PR implements
-d
/--section-delimiter
that allows you to define a section delimiter other than the default\:
. If the specified delimiter is a single character,:
is added to the delimiter.