click icon indicating copy to clipboard operation
click copied to clipboard

Support for editing multiple files using `click.edit`

Open yashrathi-git opened this issue 2 years ago • 0 comments

Hello,

Most of the editors(like nano, vim, vi, vscode etc) have support for multiple tabs. The only editor that I can recall that don't support multiple tabs is notepad. Something like this could be really helpful and also could be easily implemented into click:

import click
click.edit(filenames = {'path/to/file1', 'path/to/file2'})

Instead of calling editor using single file as argument it provides multiple files as arguments here.

So it calls something like:

vim "/path/to/file/1" "/path/to/file/2"

All editors don't support tabs, but most of them do.

Right now implementing something like this without changes to click would look like:

from click._termui_impl import Editor

class CustomEditor(Editor):
    def edit_multiple(self, filenames: Sequence[str]):
        import subprocess

        editor = self.get_editor()
        filenames = ' '.join(f'"{name}"' for name in filenames)
        try:
            c = subprocess.Popen(f'{editor} {filenames}', shell=True)
            exit_code = c.wait()
            if exit_code != 0:
                raise click.ClickException(
                    "{editor}: Editing failed".format(editor=editor)
                )

        except OSError as e:
            raise click.ClickException(
                "{editor}: Editing failed: {e}".format(editor=editor, e=e)
            )


def edit_multiple(filenames: Sequence[str], editor: Optional[str] = None):
    ed = CustomEditor(editor=editor)
    ed.edit_multiple(filenames)

This looks really bad because we have to import from protected file and there is a lot of repetitive code.

Thank you

yashrathi-git avatar Sep 14 '21 15:09 yashrathi-git