sxiv
sxiv (Simple X Image Viewer) 是一款用 C 语言编写的轻量级且可脚本化的图像查看器。
安装
安装以下软件包之一
用法
分配键盘快捷键
sxiv 支持外部按键事件。首先,你需要按下 Ctrl-x 将下一个按键发送给外部按键处理器。外部按键处理器需要一个可执行文件 ~/.config/sxiv/exec/key-handler,它通过参数传递按下的按键组合,并通过标准输入 (stdin) 传递当前标记的图像名称(如果没有标记,则传递当前选中的图像)。在安装 sxiv 时会附带一个示例按键处理器:/usr/share/sxiv/exec/key-handler。该示例是自定义快捷键的绝佳起点。
$ mkdir -p ~/.config/sxiv/exec/ $ cp /usr/share/sxiv/exec/key-handler ~/.config/sxiv/exec/key-handler
请确保将该脚本标记为 可执行。
在以下示例中,我们将添加快捷键 Ctrl+d 以执行 mv filename ~/.trash,Ctrl+c 使用 xclip 将当前图像名称复制到剪贴板,以及 Ctrl+w 使用 nitrogen 设置当前壁纸。显然,某些命令在作为参数传递单个图像时才有意义,因此你可能需要修改脚本以处理传递多个图像的情况。
~/.config/sxiv/exec/key-handler
#!/bin/sh
while read file
do
case "$1" in
"C-d")
mv "$file" ~/.trash ;;
"C-r")
convert -rotate 90 "$file" "$file" ;;
"C-c")
echo -n "$file" | xclip -selection clipboard ;;
"C-w")
nitrogen --save --set-zoom-fill "$file" ;;
esac
done
如果 .trash 文件夹不存在,请创建它
$ mkdir ~/.trash
mv "$2" ~/.trash。技巧与提示
在打开单个文件后浏览目录中的图像
sxiv 的开发者曾多次被要求使其程序能够浏览参数中给定文件名所在目录的所有图像(参见 [1] 和 [2])。有一些分支实现了这一行为:doronbehar's, qsmodo's 和 sammoth's。
或者,你可以使用官方版本的 sxiv,将 这个脚本 放在 /usr/local/bin 中并这样调用它
$ scriptname a_single_image.jpg
如脚本注释所示,该脚本可用于在 ranger 中打开图像时实现此行为。
这个来自 [3] 的 shell 脚本在且仅在传递的文件参数是一个文件夹时,以缩略图模式启动 sxiv
~/bin/sxiv.sh
#!/bin/sh
if command -v sxiv >/dev/null 2>&1; then
if [ -d "${@: -1}" ] || [ -h "${@: -1}" ]; then
sxiv -t "$@"
else
sxiv "$@"
fi
elif command -v feh >/dev/null 2>&1; then
feh "$@"
else
echo "Please install SXIV or FEH!"
fi
在状态栏中显示图像尺寸
将以下可执行脚本放入 ~/.config/sxiv/exec/image-info,并确保你已经安装了 exiv2 软件包
~/.config/sxiv/exec/image-info
#!/bin/sh
# Example for ~/.config/sxiv/exec/image-info
# Called by sxiv(1) whenever an image gets loaded,
# with the name of the image file as its first argument.
# The output is displayed in sxiv's status bar.
s=" | " # field separator
filename=$(basename "$1")
filesize=$(du -Hh "$1" | cut -f 1)
# The '[0]' stands for the first frame of a multi-frame file, e.g. gif.
geometry=$(identify -format '%wx%h' "$1[0]")
tags=$(exiv2 -q pr -pi "$1" | awk '$1~"Keywords" { printf("%s,", $4); }')
tags=${tags%,}
echo "${filesize}${s}${geometry}${tags:+$s}${tags}${s}${filename}"