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

Trim final newline from :pipe output #10912

Open
clo4 opened this issue Jun 10, 2024 · 4 comments · May be fixed by #10952
Open

Trim final newline from :pipe output #10912

clo4 opened this issue Jun 10, 2024 · 4 comments · May be fixed by #10952
Labels
A-command Area: Commands C-enhancement Category: Improvements

Comments

@clo4
Copy link
Contributor

clo4 commented Jun 10, 2024

I want to use | / :pipe more often but it inserts trailing whitespace that the command generated. This limits its usefulness because when I want to run the same command on every selection, everything gets messed up because of the additional whitespace. It's not too much effort to clean it up, but it's annoying and I've never actually wanted the whitespace to be inserted. For reference, this is the way that Vim/NeoVim handle it.

My preference would be to have the trailing whitespace trimming on by default, but I'm open to the idea that it could be a new command like pipe_trim or something. If it's decided to go ahead with this then I'll implement it.

@clo4 clo4 added the C-enhancement Category: Improvements label Jun 10, 2024
@kirawi
Copy link
Member

kirawi commented Jun 10, 2024

#8366 already implements a command to trim, so you could just pipe and execute that if/when it is merged

@clo4
Copy link
Contributor Author

clo4 commented Jun 10, 2024

That’s a possible solution, but it means that the vast majority of the time you pipe to another command you’ll have to :trim too. IMO this is a behaviour that should be implemented by the pipe command for ease of use.

I’d also be open to it only trimming a single trailing newline too. But as it is, stuff like piping to uniq or sort or case conversion utils or a formatter will all insert a new line into your document because these tools do the “correct” thing for interactive shell usage. Both Vim and Kakoune do remove the final newline, for good reason :)

@clo4 clo4 changed the title Trim trailing whitespace from pipe output Trim final newline from :pipe output Jun 10, 2024
@RoloEdits
Copy link
Contributor

I've played around with this and it seems like it is indeed a bit more intuitive when the newline is trimmed(perhaps a placebo). I can make a pr so that it can be tested.

I'm unsure if I implemented the change in full completeness, as the exiting logic is kind of hard to understand how any other branch could yield a result, and so only changed the one area I thought could, and it appears to work fine.

        let output = if let Some(output) = shell_output.as_ref() {
            output.clone()
        } else {
            let fragment = range.slice(text);
            match shell_impl(shell, cmd, pipe.then(|| fragment.into())) {
                Ok(result) => {
                    if !pipe {
                        shell_output = Some(result.clone());
                    }
-                    result
+                    Tendril::from(result.trim_end())
                }
                Err(err) => {
                    cx.editor.set_error(err.to_string());
                    return;
                }
            }
        };

@clo4
Copy link
Contributor Author

clo4 commented Jun 14, 2024

That's the exact change I made in my branch too 😁 I agree, it does feel much nicer to use, don't think it's a placebo. I reckon make a PR with it!

@RoloEdits RoloEdits linked a pull request Jun 14, 2024 that will close this issue
@kirawi kirawi added the A-command Area: Commands label Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-command Area: Commands C-enhancement Category: Improvements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants