2024-02-21 19:01:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-03-08 17:01:39 +00:00
|
|
|
# File : .config/ranger/commands.py
|
2024-02-21 19:01:10 +00:00
|
|
|
# Author : Jeff LANCE <email@jefflance.me>
|
|
|
|
# Date : 12.05.2021
|
2024-03-08 19:17:53 +00:00
|
|
|
# Last Modified Date: 08.03.2024 20:17:29
|
2024-03-08 17:01:39 +00:00
|
|
|
# Last Modified By : Jeff Lance <email@jefflance.me>
|
2024-02-21 19:01:10 +00:00
|
|
|
|
2024-03-08 19:17:53 +00:00
|
|
|
|
2024-02-22 16:19:39 +00:00
|
|
|
from ranger.api.commands import Command
|
2024-03-08 17:27:28 +00:00
|
|
|
from plugins.ranger_udisk_menu.mounter import mount
|
2024-03-09 12:00:53 +00:00
|
|
|
from plugins.ranger_tmsu import Tmsu
|
2024-03-08 19:17:53 +00:00
|
|
|
|
|
|
|
import os
|
2024-03-09 12:06:18 +00:00
|
|
|
import ranger.api
|
|
|
|
import ranger.core.linemode
|
2024-03-08 19:17:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
tmsu = Tmsu.findTmsu()
|
2024-02-21 19:01:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2024-02-22 16:19:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class tmsu_tag(Command):
|
|
|
|
""":tmsu_tag
|
|
|
|
|
|
|
|
Tags the current file with tmsu
|
|
|
|
"""
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
cf = self.fm.thisfile
|
2024-03-08 19:17:53 +00:00
|
|
|
tmsu.tag(cf.basename, self.rest(1))
|
|
|
|
|
|
|
|
def tab(self, tabnum):
|
|
|
|
"""Complete with tags"""
|
|
|
|
results = []
|
|
|
|
if self.arg(0) == self.arg(-1):
|
|
|
|
input_tag = ""
|
|
|
|
index = 1
|
|
|
|
else:
|
|
|
|
input_tag = self.arg(-1)
|
|
|
|
index = -1
|
|
|
|
|
|
|
|
if "=" in input_tag:
|
|
|
|
split_value = input_tag.split("=")[1]
|
|
|
|
split_tag = input_tag.split("=")[0]
|
|
|
|
for value in tmsu.values(split_tag):
|
|
|
|
if value.startswith(split_value):
|
|
|
|
results.append(split_tag + "=" + value)
|
|
|
|
else:
|
|
|
|
for tag in tmsu.tags():
|
|
|
|
if tag.startswith(input_tag):
|
|
|
|
results.append(tag)
|
|
|
|
return (self.start(index) + result for result in results)
|
|
|
|
|
|
|
|
|
|
|
|
class tmsu_untag(Command):
|
|
|
|
""":tmsu_untag
|
|
|
|
|
|
|
|
Untags the current file with tmsu
|
|
|
|
"""
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
cf = self.fm.thisfile
|
|
|
|
tmsu.untag(cf.basename, self.rest(1))
|
|
|
|
|
|
|
|
def tab(self, tabnum):
|
|
|
|
"""Complete with tags"""
|
|
|
|
results = []
|
|
|
|
if self.arg(0) == self.arg(-1):
|
|
|
|
input_tag = ""
|
|
|
|
index = 1
|
|
|
|
else:
|
|
|
|
input_tag = self.arg(-1)
|
|
|
|
index = -1
|
|
|
|
cf = self.fm.thisfile
|
|
|
|
|
|
|
|
for tag in tmsu.tags(fileName=cf.basename):
|
|
|
|
if tag.startswith(input_tag):
|
|
|
|
results.append(tag)
|
|
|
|
return (self.start(index) + result for result in results)
|
|
|
|
|
|
|
|
|
|
|
|
class tmsu_ls(Command):
|
|
|
|
""":tmsu_ls
|
|
|
|
|
|
|
|
List tags of the current file with tmsu
|
|
|
|
"""
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
cf = self.fm.thisfile
|
|
|
|
tags = tmsu.tags(cf.basename)
|
|
|
|
self.fm.notify(tags)
|
|
|
|
|
|
|
|
|
|
|
|
@ranger.api.register_linemode # It may be used as a decorator too!
|
|
|
|
class MyLinemode(ranger.core.linemode.LinemodeBase):
|
|
|
|
name = "tmsu_linemode"
|
|
|
|
|
|
|
|
uses_metadata = False
|
|
|
|
|
|
|
|
def filetitle(self, file, metadata):
|
|
|
|
return file.relative_path + str(tmsu.tags(file))
|
2024-02-22 16:19:39 +00:00
|
|
|
|
2024-03-08 19:17:53 +00:00
|
|
|
def infostring(self, file, metadata):
|
|
|
|
return file.user
|