Ranger: sxiv python wrapper.

master
Pierre Neidhardt 2013-03-05 20:08:30 +01:00
parent d9938427c7
commit afa49b608f
1 changed files with 31 additions and 7 deletions

View File

@ -1,6 +1,24 @@
#!/usr/bin/env python
################################################################################
# Compatible with ranger 1.6.*
#
# This script does not use ranger API, it is completely independent. Maybe it
# would be better and faster ro use ranger API, most notably to fetch the file
# list. The ranger 1.5.5 implementation of the sxiv app was as follow in
# ranger/default/apps.py:
#
# def app_sxiv(self, c):
# c.flags = 'd' + c.flags
# if len(c.files) is 1 and self.fm.env.cwd:
# images = [f.basename for f in self.fm.env.cwd.files if f.image]
# try:
# position = images.index(c.file.basename) + 1
# except:
# return None
# return 'sxiv', '-n', str(position), images
# return 'sxiv', c
#
#
# If only one file is selected, this script searches image files in a directory,
# opens them all with sxiv and sets the first argument to the first image
# displayed by sxiv.
@ -17,9 +35,7 @@
# all image to window, use
#
# mime ^image, has sxiv, X, flag f = path/to/this/script -fs -- "$@"
# TODO: support for mimetypes.
# result = [a for a in filelist if v=mimetypes.guess_type(a)[0] and type(v) is str and v.find('image') != -1 ]
################################################################################
import sys
import os
@ -30,6 +46,11 @@ import re
def usage():
print("Usage: " + re.sub(r".*/", "", sys.argv[0]) + " PICTURES")
def is_image(a):
v=mimetypes.guess_type(a)[0]
if type(v) is str and v.find('image') != -1:
return True
return False
def sxiv_singlefile(inputfile):
# Turn to an absolute path
@ -41,10 +62,11 @@ def sxiv_singlefile(inputfile):
filename = inputfile
## Note: os.path.join seems to be slow.
result = [ inputdir + a for a in filelist if re.search('.(bmp|gif|jpe?g|png)$', a, re.IGNORECASE) != None ]
result = [inputdir + a for a in filelist if is_image(a) ]
list.sort(result)
## We get the index of the first argument to know where sxiv should start the display.
## We get the index of the first argument to know where sxiv should start
## the display.
try:
count = result.index(inputfile) + 1
except ValueError:
@ -66,12 +88,12 @@ def sxiv_multifile(arglist):
result = parameters + result
result = ["sxiv"] + result
print(result)
subprocess.call(result)
## MAIN
################################################################################
## Parse arguments.
if len(sys.argv) == 1:
usage_exit()
@ -100,3 +122,5 @@ elif len(arglist) >= 2:
sys.exit(0)
################################################################################
# End