nushell / nufmt

MIT License
67 stars 8 forks source link

Alternative specification for pipe formatting #60

Open CabalCrow opened 4 months ago

CabalCrow commented 4 months ago

The specification.md describes the default formatting for pipelines to be a wall of lines.

def "apply to" [
    file: path
    modification: closure
] {
    $file
    | path expand
    | open --raw
    | from toml
    | do $modification
    | save --force $file
}

In the nushell discord fdncred mentioned how this creates issues with copy pasting code to the terminal on windows. Because of this I would like to suggest alternative formatting & indenting:

def "apply to" [
    file: path
    modification: closure
] {
    $file |
        path expand |
        open --raw |
        from toml |
        do $modification |
        save --force $file
}

This python style indenting still showcases that a pipeline is continued, despite that the pipe symbol is not at the start of the line.

fdncred commented 4 months ago

Agreed.

AucaCoyan commented 4 months ago

Hi! Yes, this is really useful. When I wrote some lines in the specification I though this as a parameter "do you want the pipelines to be connected?" and exactly these scripts are the perfect example 😊

amtoine commented 4 months ago

i think this should be modifiable in some settings, i personally quite strongly dislike the second way :rofl:

and i would write the following instead

def "apply to" [
    file: path
    modification: closure
] {
    $file
        | path expand
        | open --raw
        | from toml
        | do $modification
        | save --force $file
}
CabalCrow commented 4 months ago

i think this should be modifiable in some settings, i personally quite strongly dislike the second way 🤣

and i would write the following instead

def "apply to" [
    file: path
    modification: closure
] {
    $file
        | path expand
        | open --raw
        | from toml
        | do $modification
        | save --force $file
}

This falls into the same issue for windows users - you can't have a | at the start of the line, indent or not.

amtoine commented 4 months ago

@CabalCrow

'tis why i propose to make in configurable :)

CabalCrow commented 4 months ago

@amtoine Having it configurable, is good, but I'm also talking about what the default formatting should be. I believe that you would want default formatting that could work properly regardless of OS, since nushell aims to work regardless of OS.

AucaCoyan commented 4 months ago

This falls into the same issue for windows users - you can't have a | at the start of the line, indent or not.

What do you mean with Windows users? I can copy and paste that script in my nushell terminal and it works 🤩. Do you have trouble with that script?

amtoine commented 4 months ago

yeah i think Nushell should be able to handle copy pasting any way to format pipelines, the parser shouldn't care at all :confused:

if that's not possible, i agree the default should work the best for everyone -even though it's Windows which is annoying again, ugh...-, i would just turn that default off as soon as i start a Nushell project :wink:

my gut feeling is that, putting the | at the end of lines would like doing the following in Rust

            let vals = rec.
                values().
                cloned().
                enumerate().
                map(|(i, v)| {
                    if i == id {
                        mutate_value_cell(&v, &cell_path, cell).unwrap()
                    } else {
                        v
                    }
                }).
                collect();

instead of the conventional

            let vals = rec
                .values()
                .cloned()
                .enumerate()
                .map(|(i, v)| {
                    if i == id {
                        mutate_value_cell(&v, &cell_path, cell).unwrap()
                    } else {
                        v
                    }
                })
                .collect();

anyways :wink:

amtoine commented 4 months ago

@AucaCoyan if there is no issue anymore with copy pasting pipelines on Windows, then it's all good :relieved:

CabalCrow commented 4 months ago

On 24/06/02 04:00AM, Antoine Stevan wrote:

@AucaCoyan if there is no issue anymore with copy pasting pipelines on Windows, then it's all good :relieved:

fdncred shared the issue. I'm not on windows so I can't talk about the issue first hand.

fdncred commented 4 months ago

Here is an example @AucaCoyan, if your terminal does not support bracketed paste, such as Windows Terminal, when you paste the middle portion of the custom command, something like this

    $file
        | path expand
        | open --raw
        | from toml
        | do $modification
        | save --force $file

nushell/reedline pastes one line at a time. So, it looks like this to nushell and each line tries to be executed giving you a different result than what you want.

$file # it runs this
    | path expand # then tries to run this
    | open --raw # then tries to run this, and so on

alternatively, if you paste this

    $file |
        path expand |
        open --raw |
        from toml |
        do $modification |
        save --force $file

nushell sees

$file | # oh, this statement isn't complete, don't execute it yet
    path expand | # oh, there's another pipe, so this statement isn't complete, don't execute yet
   open --raw | # oh, another pipe, don't execute yet

So, all I was saying is that I prefer the pipes at the end because Windows users won't have a problem with copying and pasting pieces out of the script/custom command. You are correct that the entire custom command can be pasted fine but that's because it's in a { } block but if you try to play with just the middle pieces, it won't work.

You can see here that I pasted the entire custom command and it worked just fine. But if I want just the part in question, it pukes because the pipes are in the wrong place for windows to understand what to do. image But if the pipes are at the end, windows has no issues with it image

AucaCoyan commented 4 months ago

Thanks for the clarification! I tried also in Windows and had the same result in Windows Terminal. Because I've been using nushell in windows for so long I didn't expect it to "magically work" in linux 😋. BTW, is there any other terminal in windows that supports bracketed paste? Wezterm and Rio also couldn't resolve the job

fdncred commented 4 months ago

I'm not sure. I'm hopeful that once Ghostty gets fully supported in Windows that it will be I'm not sure. I think in most terminals you have to enable the feature though.