knqyf263 / pet

Simple command-line snippet manager
MIT License
4.58k stars 230 forks source link

command to edit does not work on Windows unless configuring a Unix-like editor #314

Open cderv opened 2 months ago

cderv commented 2 months ago

Privileged issue

Issue Content

Related to

but it does not work on Powershell nor CMD/

❯ pet configure
'+0' n’est pas reconnu en tant que commande interne
ou externe, un programme exécutable ou un fichier de commandes.
exit status 1

(error in french, but basically command run is not windows compatible)

This is because the function to edit file is not Windows compatible https://github.com/knqyf263/pet/blob/f88549cbb1e7eb4853bf7929b659e083076bfc92/cmd/util.go#L17-L22

Configuring the editor can help if using one supportin +<line> work - like a Unix-like one

[General]
  Editor = "vim"

(vim or equivalent can be made installed with Scoop)

Then I can make it work with CMD (default).

It could be good to be able to support for Windows notepad or any other program like VSCOE (Editor = "code").

RamiAwar commented 2 months ago

Hmm interesting, wonder why we don't clean up the args when using the generic command from conf.

Will look into this one. Tricky to change tho as it might break for people depending on it

jaroslawhartman commented 1 month ago

I can confirm similar behaviour on MacOS - I have VSCode configured for edit and it's opening +0 file as well: image

This seems to be "working as designed", i.e. when added a print statement, it shows clearly that +0 came intentionally:

go run . configure
code +0 /Users/jhartman/.config/pet/config.toml

This must have been thought as an aid to move the cursor to the end of the snippets file, +3 lines after the last line, see:

func new(cmd *cobra.Command, args []string) (err error) {
...
            return createAndEditSnippet(newSnippet, snippets, lineCount+3)
...

As per vi man:

vi Command-Line Options

You can open file for editing, optionally at line n or at the first line matching pattern:

+[num] Start editing at line number num, or the last line of the file if num is omitted.


Eventually, I'd fix this in an easiest possible way:

func editFile(command, file string) error {
    command += " " + file
    return run(command, os.Stdin, os.Stdout)
}

Raised a PR for this: https://github.com/knqyf263/pet/pull/319

Best regards, Jarek

RamiAwar commented 1 month ago

Indeed @jaroslawhartman , the +0 is added for editors like vi, vim, nano, etc. to jump to the end of the file where the new snippet is inserted and put the cursor in a place where the user can just start typing.

I think the most straightforward solution to this would be to add some conditionals there, depending on the editor that's being used.