Skip to content

Support for editing multiple files using click.edit #2067

@yashrathi-git

Description

@yashrathi-git

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions