Skip to content
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 a --quote option to put quotation marks around output files/dirs #1366

Closed
wants to merge 3 commits into from

Conversation

scottchiefbaker
Copy link
Contributor

@scottchiefbaker scottchiefbaker commented Aug 23, 2023

Following the rules set by coreutils - quoting I have added a --quote option to put appropriate quotation marks around files/directories that need them.

Note: This fixes #1365

@scottchiefbaker
Copy link
Contributor Author

Also FWIW I've never written any rust before yesterday, so I'm pretty confident my code could be improved. Be gentle

@scottchiefbaker
Copy link
Contributor Author

I would like to request some help with the unit tests. I have written the tests, but they're not working. Specifically it's not engaging the new --quote feature required for the test to complete successfully. Can someone with more experience take a look at my unit tests and tell me what I'm doing wrong.

fn path_needs_quoting(path: &str) -> i8 {
// If it contains any special chars we return single quote
if path.contains(" ") || path.contains("$") || path.contains("\"") {
return 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use an enumeration instead of an i8, where it isn't obvious what the different numbers mean.

format!("\"{}\"", tmp_str)
// No quoting required
} else {
path_str.to_string()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unnecessarily copies the string again even if there is no change


// If the quoted string is new, then we flag that to tweak the offset
// so the colors line up
if tmp_str != path_str {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to compare every character of the string. It would be more efficient if you did something like have return quote_path return an Option that is only Some if quoting happened, and then do:

if let Some(quoted) = quote_path(&path_str) {
    path_str = quoted.into();
    needs_quoting = true;
}

@@ -78,6 +128,11 @@ fn print_entry_colorized<W: Write>(
}

if offset > 0 {

if needs_quoting {
offset += 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. If it is only quoted shouldn't the offset be just 1 more?

And if it is double quoted, and has escaped characters, then the proper offset will depend on how many characters in the parent ended up getting escaped.

// Trying to copy: https://www.gnu.org/software/coreutils/quotes.html
fn path_needs_quoting(path: &str) -> i8 {
// If it contains any special chars we return single quote
if path.contains(" ") || path.contains("$") || path.contains("\"") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottchiefbaker
Copy link
Contributor Author

My code is definitely sub par. This was my proof-of-concept trial. Someone with real Rust experience should tackle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an option to quote files in output for easier copy/pasting
4 participants