栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

mac终端iterm2配置imgcat插件直接打开图片

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mac终端iterm2配置imgcat插件直接打开图片

imgcat官网

地址

安装imgcat

在mac下的terminal或者iterm2上运行:

curl -L https://iterm2.com/shell_integration/install_shell_integration_and_utilities.sh | bash
imgcat源码shell脚本安装
#!/bin/bash

# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
#  ST, and for all ESCs in  to be replaced with ESC ESC. It
# only accepts ESC backslash for ST. We use TERM instead of TMUX because TERM
# gets passed through ssh.
function print_osc() {
    if [[ $TERM == screen* ]]; then
        printf "33Ptmux;3333]"
    else
        printf "33]"
    fi
}

# More of the tmux workaround described above.
function print_st() {
    if [[ $TERM == screen* ]]; then
        printf "a33\"
    else
        printf "a"
    fi
}

function load_version() {
    if [ -z ${IMGCAT_BASE64_VERSION+x} ]; then
        IMGCAT_BASE64_VERSION=$(base64 --version 2>&1)
        export IMGCAT_BASE64_VERSION
    fi
}

function b64_encode() {
    load_version
    if [[ $IMGCAT_BASE64_VERSION =~ GNU ]]; then
        # Disable line wrap
        base64 -w0
    else
        base64
    fi
}

function b64_decode() {
    load_version
    if [[ $IMGCAT_BASE64_VERSION =~ fourmilab ]]; then
        BASE64ARG=-d
    elif [[ $IMGCAT_BASE64_VERSION =~ GNU ]]; then
        BASE64ARG=-di
    else
        BASE64ARG=-D
    fi
    base64 $BASE64ARG
}

# print_image filename inline base64contents print_filename
#   filename: Filename to convey to client
#   inline: 0 or 1
#   base64contents: Base64-encoded contents
#   print_filename: If non-empty, print the filename
#                   before outputting the image
function print_image() {
    print_osc
    printf '1337;File='
    if [[ -n $1 ]]; then
        printf "name=%s;" "$(printf "%s" "$1" | b64_encode)"
    fi

    printf "%s" "$3" | b64_decode | wc -c | awk '{printf "size=%d",$1}'
    printf ";inline=%s" "$2"
    printf ":"
    printf "%s" "$3"
    print_st
    printf 'n'
    if [[ -n $4 ]]; then
        echo "$1"
    fi
}

function error() {
    echo "ERROR: $*" 1>&2
}

function show_help() {
    echo "Usage: imgcat [-p] filename ..." 1>&2
    echo "   or: cat filename | imgcat" 1>&2
}

function check_dependency() {
    if ! (builtin command -V "$1" >/dev/null 2>&1); then
        echo "imgcat: missing dependency: can't find $1" 1>&2
        exit 1
    fi
}

## Main

if [ -t 0 ]; then
    has_stdin=f
else
    has_stdin=t
fi

# Show help if no arguments and no stdin.
if [ $has_stdin = f ] && [ $# -eq 0 ]; then
    show_help
    exit
fi

check_dependency awk
check_dependency base64
check_dependency wc

# Look for command line flags.
while [ $# -gt 0 ]; do
    case "$1" in
    -h | --h | --help)
        show_help
        exit
        ;;
    -p | --p | --print)
        print_filename=1
        ;;
    -u | --u | --url)
        check_dependency curl
        encoded_image=$(curl -s "$2" | b64_encode) || (
            error "No such file or url $2"
            exit 2
        )
        has_stdin=f
        print_image "$2" 1 "$encoded_image" "$print_filename"
        set -- "${@:1:1}" "-u" "${@:3}"
        if [ "$#" -eq 2 ]; then
            exit
        fi
        ;;
    -*)
        error "Unknown option flag: $1"
        show_help
        exit 1
        ;;
    *)
        if [ -r "$1" ]; then
            has_stdin=f
            print_image "$1" 1 "$(b64_encode <"$1")" "$print_filename"
        else
            error "imgcat: $1: No such file or directory"
            exit 2
        fi
        ;;
    esac
    shift
done

# Read and print stdin
if [ $has_stdin = t ]; then
    print_image "" 1 "$(cat | b64_encode)" ""
fi

exit 0
使用

使用也非常简单:

imgcat yourphoto
例子

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号