Skip to content
dodopod edited this page Mar 6, 2022 · 7 revisions

It is fairly easy to implement a custom linemode (see this section of the ranger guide for an explanation what is a linemode).

Let's try to implement a linemode displaying a filename with a comment (added via :meta) on the left side and the file owner on the right. So it would look like this:

my_video.mp4 (sweet cats)                             john

Each linemode is a class implementing the LinemodeBase interface, so we start with:

class MyLinemode(LinemodeBase):

A linemode must have a name:

    name = "awesome_linemode"

...then we specify that we will be using the metadata (see: :meta) and ranger should load it:

    uses_metadata = True

We may use required_metadata list to specify which metadata tags are necessary for our linemode to make sense (it will fall back to the default linemode if any of them is not set):

    required_metadata = ["comment"]

Now we will define two methods, one for each side of the line. Each takes two arguments: a file object (FSObject) and an object with the metadata.

    def filetitle(self, file, metadata):
        return file.relative_path + " (" + metadata.comment + ")"
    def infostring(self, file, metadata):
        return file.user

It's almost over. What's left is registering the brand new linemode:

register_linemode(MyLinemode)

To summarize:

import ranger.api
import ranger.core.linemode

@ranger.api.register_linemode     # It may be used as a decorator too!
class MyLinemode(ranger.core.linemode.LinemodeBase):
    name = "awesome_linemode"

    uses_metadata = True
    required_metadata = ["comment"]

    def filetitle(self, file, metadata):
        return file.relative_path + " (" + metadata.comment + ")"

    def infostring(self, file, metadata):
        return file.user

To actually see it in action, you need to tag some files with the "comment" tag using :meta and set an appropriate linemode:

:meta comment sweet cats
:linemode awesome_linemode

You may also set the default linemode in your rc.conf with :default_linemode.

Clone this wiki locally