170 lines
6.0 KiB
Python
170 lines
6.0 KiB
Python
|
# This is a sample commands.py. You can add your own commands here.
|
||
|
#
|
||
|
# Please refer to commands_full.py for all the default commands and a complete
|
||
|
# documentation. Do NOT add them all here, or you may end up with defunct
|
||
|
# commands when upgrading ranger.
|
||
|
|
||
|
# You always need to import ranger.api.commands here to get the Command class:
|
||
|
from ranger.api.commands import *
|
||
|
|
||
|
# A simple command for demonstration purposes follows.
|
||
|
# -----------------------------------------------------------------------------
|
||
|
|
||
|
# You can import any python module as needed.
|
||
|
import os
|
||
|
|
||
|
# Any class that is a subclass of "Command" will be integrated into ranger as a
|
||
|
# command. Try typing ":my_edit<ENTER>" in ranger!
|
||
|
|
||
|
|
||
|
class my_edit(Command):
|
||
|
# The so-called doc-string of the class will be visible in the built-in
|
||
|
# help that is accessible by typing "?c" inside ranger.
|
||
|
""":my_edit <filename>
|
||
|
|
||
|
A sample command for demonstration purposes that opens a file in an editor.
|
||
|
"""
|
||
|
|
||
|
# The execute method is called when you run this command in ranger.
|
||
|
def execute(self):
|
||
|
# self.arg(1) is the first (space-separated) argument to the function.
|
||
|
# This way you can write ":my_edit somefilename<ENTER>".
|
||
|
if self.arg(1):
|
||
|
# self.rest(1) contains self.arg(1) and everything that follows
|
||
|
target_filename = self.rest(1)
|
||
|
else:
|
||
|
# self.fm is a ranger.core.filemanager.FileManager object and gives
|
||
|
# you access to internals of ranger.
|
||
|
# self.fm.thisfile is a ranger.container.file.File object and is a
|
||
|
# reference to the currently selected file.
|
||
|
target_filename = self.fm.thisfile.path
|
||
|
|
||
|
# This is a generic function to print text in ranger.
|
||
|
self.fm.notify("Let's edit the file " + target_filename + "!")
|
||
|
|
||
|
# Using bad=True in fm.notify allows you to print error messages:
|
||
|
if not os.path.exists(target_filename):
|
||
|
self.fm.notify("The given file does not exist!", bad=True)
|
||
|
return
|
||
|
|
||
|
# This executes a function from ranger.core.acitons, a module with a
|
||
|
# variety of subroutines that can help you construct commands.
|
||
|
# Check out the source, or run "pydoc ranger.core.actions" for a list.
|
||
|
self.fm.edit_file(target_filename)
|
||
|
|
||
|
# The tab method is called when you press tab, and should return a list of
|
||
|
# suggestions that the user will tab through.
|
||
|
# tabnum is 1 for <TAB> and -1 for <S-TAB> by default
|
||
|
def tab(self, tabnum):
|
||
|
# This is a generic tab-completion function that iterates through the
|
||
|
# content of the current directory.
|
||
|
return self._tab_directory_content()
|
||
|
|
||
|
class mkcd(Command):
|
||
|
"""
|
||
|
:mkcd <dirname>
|
||
|
|
||
|
Creates a directory with the name <dirname> and enters it.
|
||
|
"""
|
||
|
|
||
|
def execute(self):
|
||
|
from os.path import join, expanduser, lexists
|
||
|
from os import makedirs
|
||
|
import re
|
||
|
|
||
|
dirname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
|
||
|
if not lexists(dirname):
|
||
|
makedirs(dirname)
|
||
|
|
||
|
match = re.search('^/|^~[^/]*/', dirname)
|
||
|
if match:
|
||
|
self.fm.cd(match.group(0))
|
||
|
dirname = dirname[match.end(0):]
|
||
|
|
||
|
for m in re.finditer('[^/]+', dirname):
|
||
|
s = m.group(0)
|
||
|
if s == '..' or (s.startswith('.') and not self.fm.settings['show_hidden']):
|
||
|
self.fm.cd(s)
|
||
|
else:
|
||
|
## We force ranger to load content before calling `scout`.
|
||
|
self.fm.thisdir.load_content(schedule=False)
|
||
|
self.fm.execute_console('scout -ae ^{}$'.format(s))
|
||
|
else:
|
||
|
self.fm.notify("file/directory exists!", bad=True)
|
||
|
|
||
|
def get_id3tag_cmd(path, key, val):
|
||
|
from ranger.ext.shell_escape import shell_quote
|
||
|
return 'id3tag --%s=%s %s' % (key, shell_quote(val), shell_quote(path))
|
||
|
|
||
|
class id3tag_song(Command):
|
||
|
"""
|
||
|
:id3tag_song <title>
|
||
|
|
||
|
Set current file id3 song tag to <title>
|
||
|
"""
|
||
|
|
||
|
def execute(self):
|
||
|
self.fm.execute_command(get_id3tag_cmd(self.fm.thisfile.path, 'song', self.rest(1)))
|
||
|
|
||
|
class id3tag_artist(Command):
|
||
|
"""
|
||
|
:id3tag_artist <artist>
|
||
|
|
||
|
Set current file id3 artist tag to <artist>
|
||
|
"""
|
||
|
|
||
|
def execute(self):
|
||
|
self.fm.execute_command(get_id3tag_cmd(self.fm.thisfile.path, 'artist', self.rest(1)))
|
||
|
|
||
|
class fzf_select(Command):
|
||
|
"""
|
||
|
:fzf_select
|
||
|
|
||
|
Find a file using fzf.
|
||
|
|
||
|
With a prefix argument select only directories.
|
||
|
|
||
|
See: https://github.com/junegunn/fzf
|
||
|
"""
|
||
|
def execute(self):
|
||
|
import subprocess
|
||
|
if self.quantifier:
|
||
|
# match only directories
|
||
|
command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||
|
-o -type d -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
|
||
|
else:
|
||
|
# match files and directories
|
||
|
command="find -L . \( -path '*/\.*' -o -fstype 'dev' -o -fstype 'proc' \) -prune \
|
||
|
-o -print 2> /dev/null | sed 1d | cut -b3- | fzf +m"
|
||
|
fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
|
||
|
stdout, stderr = fzf.communicate()
|
||
|
if fzf.returncode == 0:
|
||
|
fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
|
||
|
if os.path.isdir(fzf_file):
|
||
|
self.fm.cd(fzf_file)
|
||
|
else:
|
||
|
self.fm.select_file(fzf_file)
|
||
|
|
||
|
class fzf_locate(Command):
|
||
|
"""
|
||
|
:fzf_locate
|
||
|
|
||
|
Find a file using fzf
|
||
|
|
||
|
See: https://github.com/junegunn/fzf
|
||
|
"""
|
||
|
def execute(self):
|
||
|
import subprocess
|
||
|
if self.quantifier:
|
||
|
command="locate home media | fzf -e -i"
|
||
|
else:
|
||
|
command="locate home media | fzf -e -i"
|
||
|
fzf = self.fm.execute_command(command, stdout=subprocess.PIPE)
|
||
|
stdout, stderr = fzf.communicate()
|
||
|
if fzf.returncode == 0:
|
||
|
fzf_file = os.path.abspath(stdout.decode('utf-8').rstrip('\n'))
|
||
|
if os.path.isdir(fzf_file):
|
||
|
self.fm.cd(fzf_file)
|
||
|
else:
|
||
|
self.fm.select_file(fzf_file)
|