Last active
February 15, 2020 18:22
-
-
Save thugcee/9bbcae52554579c7d58b2ec80efaf971 to your computer and use it in GitHub Desktop.
Custom command for the ranger file manager for simple creation of annotations for files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import (absolute_import, division, print_function) | |
# You can import any python module as needed. | |
import os | |
# You always need to import ranger.api.commands here to get the Command class: | |
from ranger.api.commands import Command | |
class annotate_file(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. | |
""":annotate_file <note> .. <content> | |
This command creates .note.txt file for the currently selected file | |
""" | |
# 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 | |
note = self.rest(1) | |
else: | |
# if the is no note content given then exit | |
return | |
# 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 + ".note.txt" | |
# This is a generic function to print text in ranger. | |
self.fm.notify("Creating note file for " + target_filename) | |
with open(target_filename, "w") as target_file: | |
target_file.write(note) | |
# Using bad=True in fm.notify allows you to print error messages: | |
if not os.path.exists(target_filename): | |
self.fm.notify("Creating note file has failed!", 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment