-
-
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
Add "--list-languages" command line argument #79
Conversation
src/main.rs
Outdated
|
||
for lang in languages { | ||
print!("{:width$} | ", lang.name, width = longest); | ||
for i in 0..lang.file_extensions.len() { |
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.
Is there a more idiomatic way to list elements separated by a comma (except the last) than an indexed for-loop? I guess after using range loops and not having to deal with indices for so long I feel quite spoiled.
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.
I think you should be able to use lang.file_extensions.join(", ")
.
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.
That worked. Thank you!
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.
Looks great, thank you very much work working on this!
src/main.rs
Outdated
.arg( | ||
Arg::with_name("list languages") | ||
.long("list-languages") | ||
.takes_value(false) |
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.
I think false
is the default value, so .takes_value
can be removed here.
src/main.rs
Outdated
@@ -437,6 +437,12 @@ fn run() -> Result<()> { | |||
.default_value("auto") | |||
.help("When to use the pager"), | |||
) | |||
.arg( | |||
Arg::with_name("list languages") |
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.
I'd prefer if we use list-languages here, just like the .long(..)
name.
src/main.rs
Outdated
@@ -486,6 +492,31 @@ fn run() -> Result<()> { | |||
) | |||
})?; | |||
|
|||
if let Some(_) = app_matches.values_of("list languages") { |
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.
You can use if app_matches.is_present("list-languages")
.
src/main.rs
Outdated
|
||
let longest = match languages.iter() | ||
.map(|s| s.name.len()) | ||
.max() { |
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.
you can use languages.iter().....max().unwrap_or(32)
here instead of the match
statement.
src/main.rs
Outdated
}; | ||
|
||
for lang in languages { | ||
print!("{:width$} | ", lang.name, width = longest); |
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.
Nice! Didn't know this was possible.
src/main.rs
Outdated
|
||
for lang in languages { | ||
print!("{:width$} | ", lang.name, width = longest); | ||
for i in 0..lang.file_extensions.len() { |
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.
I think you should be able to use lang.file_extensions.join(", ")
.
Maybe we should also restrict the length of the file extensions?
😮 |
Yes, we should limit the size of the extensions column. I had no idea some languages could have so many extensions lol. I implemented simple line-wrapping for it. I'm still trying to learn more Rust, so if it is unnecessarily complex I would love some feedback to trim it down. I tried brainstorming a cool and simple iterator solution to this problem but didn't get too far. Here's what it looks like:
|
src/main.rs
Outdated
print!("{:width$}{}", lang.name, separator, width = longest); | ||
|
||
// Line-wrapping for the possible file extension overflow. | ||
let desired_width = 48; |
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.
Maybe this could be desired_width = term_width - longest - padding
?
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.
How should I get the term_width? I looked around and I'm not seeing a function I can call to get that value.
I notice it's set with a literal 90
in run()
; should I just create a constant and apply it to both places?
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.
The term_width
is computed/retrieved in printer.rs
. We should probably move that to the run
function and store it in the Options
struct.
That looks great, thank you very much! Could you please try to resolve the merge conflicts? If you need help with that, let me know.
Your iterative version is completely fine, however... 😄 If you want to use this as an exercise to write iterator-based code, you would probably have to look at One option to do this could be to fold the list of extensions ( I'm pretty sure the functional/iterator-based version will not be completely superior in terms of elegance, but it's worth a try (and a good exercise) 😄 |
We should probably also filter out Languages without any extensions. These are typically syntax definitions which are included by others, e.g. "Regular Expressions (Python)" |
Actually, we should probably just hide the ones that have |
src/main.rs
Outdated
print!("{:width$}{}", lang.name, separator, width = longest); | ||
|
||
// Line-wrapping for the possible file extension overflow. | ||
let desired_width = options.term_width - longest; |
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.
Don't we have to subtract separator.length()
as well?
src/printer.rs
Outdated
@@ -18,8 +17,7 @@ pub struct Printer<'a> { | |||
|
|||
impl<'a> Printer<'a> { | |||
pub fn new(handle: &'a mut Write, options: &'a Options) -> Self { | |||
let (_, term_width) = Term::stdout().size(); | |||
let term_width = term_width as usize; | |||
let term_width = options.term_width; |
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.
Just replace all the usages of term_width
with options.term_width
then
src/main.rs
Outdated
let mut extension = lang.file_extensions.iter().peekable(); | ||
while let Some(word) = extension.next() { | ||
// If we can't fit this word in, then create a line break and align it in. | ||
if word.len() + num_chars >= desired_width { |
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.
Do we have to check for word.len() + 2 + num_chars >= ..
here due to the ", " part which is possibly appended?
src/printer.rs
Outdated
@@ -30,7 +26,7 @@ impl<'a> Printer<'a> { | |||
Printer { | |||
handle, | |||
colors, | |||
term_width, | |||
term_width: options.term_width, |
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.
I think term_width
can be removed from Printer
if it is already included in options
.
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.
Thank you very much!
Thanks for putting up with me! I appreciate it, I learned a lot. |
src/main.rs
Outdated
print!("\n{:width$}{}", "", separator, width = longest); | ||
} | ||
|
||
num_chars += word.len(); |
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.
Actually, this also needs to be num_chars += word.len() + comma_separator.len();
. But I can also change this, if you want.
* removes redundant `.takes_value(false)`. * changes Arg name to "list-languages" to be consistent with long-form. * replaces unnecessary match statement with is_present(). * replaces unnecessary match statement on iter and uses unwrap_or() instead. * replaces for-loop with a call to join().
* Adds separator.length() to calculation for desired width. * Replaces use of term_width with options.term_width. * Adds the comma and space separator to calculation for line-wrapping.
Closes #69