[PYTHON] mkseldir --command to mkdir and select it further

An improved version of the default: mkdir command. After mkdir, select that directory.

Updated because I got a comment from the author

commands-mkseldir2.py


from ranger.api.commands import *
class mkseldir(Command):
  """
  :mkseldir <dirname>

  Creates a directory with the name <dirname>, and select it.
  """

  def execute(self):
    from os.path import join, expanduser, lexists
    dirname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
    if not lexists(dirname):
      self.fm.mkdir(dirname)
      if lexists(dirname):
        self.fm.thisdir.load_content(schedule=False)
        self.fm.select_file(dirname)
      else:
        self.fm.notify("Failed to mkdir!", bad=True)
    else:
      self.fm.notify("file/directory exists!", bad=True)

The following is the old version

commands.py


from ranger.api.commands import *

class mkseldir(Command):
  """
  :mkseldir <dirname>

  Creates a directory with the name <dirname>, and select it.
  """

  def execute(self):
    from os.path import join, expanduser, lexists
    dirname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
    if not lexists(dirname):
      self.fm.mkdir(dirname)
      if lexists(dirname):
        item = ranger.fsobject.directory.Directory(dirname, path_is_abs=True)
        item.load()
        self.fm.thisdir.files.append(item)
        self.fm.select_file(dirname)
      else:
        self.fm.notify("Failed to mkdir!", bad=True)
    else:
      self.fm.notify("file/directory exists!", bad=True)

Veedrac told me !!! https://bbs.archlinux.org/viewtopic.php?pid=1155529#p1155529

Recommended Posts

mkseldir --command to mkdir and select it further
How to install Cascade detector and how to use it
Linux mkdir command
Export Qiita's own article and push it to Github
How to use the grep command and frequent samples
How to use Decorator in Django and how to make it
Let's add it to the environment variable from the command ~