Skip to content

Instantly share code, notes, and snippets.

@tomoe-mami
Last active December 15, 2015 12:49

Revisions

  1. tomoe-mami revised this gist Mar 30, 2013. 1 changed file with 12 additions and 8 deletions.
    20 changes: 12 additions & 8 deletions lsthumb.sh
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,21 @@

    WIDTH=10
    HEIGHT=4
    LABEL="yes"
    LABEL="yes" # show filename under thumbnail
    VIDEO="no" # don't show video thumbnail. only show generic icon
    CHAR="."

    if [ ! "$TERMINOLOGY" = "1" ]; then
    echo "This script only works in Terminology" >&2
    exit 1
    fi

    while getopts ":w:h:n" opt; do
    while getopts ":w:h:nv" opt; do
    case "$opt" in
    w) WIDTH="$OPTARG" ;;
    h) HEIGHT="$OPTARG" ;;
    n) LABEL="no" ;;
    v) VIDEO="yes" ;;
    :)
    echo "Option -$OPTARG requires a value" >&2
    exit 1
    @@ -84,14 +86,16 @@ function get_thumb {
    # there are still more file formats supported by terminology.
    # these are just few i used most
    case "$EXT" in
    jpg|jpeg|png|gif|svg|pdf)
    THUMB="$ABSPATH";;
    jpg|jpeg|png|gif|bmp|ico|xcf|svg|pdf)
    THUMB="$1";;

    avi|mp4|mkv|asf|wmv|ogv|mpg|rmvb|flv)
    THUMB="video-x-generic" ;;

    swf)
    THUMB="application-x-shockwave-flash" ;;
    if [ "$VIDEO" = "yes" ]; then
    THUMB="$1"
    else
    THUMB="video-x-generic"
    fi
    ;;

    mp3|aac|flac|ogg|wma|wav|ape)
    THUMB="audio-x-generic" ;;
  2. tomoe-mami revised this gist Mar 30, 2013. 1 changed file with 93 additions and 34 deletions.
    127 changes: 93 additions & 34 deletions lsthumb.sh
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,37 @@
    #!/bin/sh

    WIDTH=6
    HEIGHT=3
    WIDTH=10
    HEIGHT=4
    LABEL="yes"
    CHAR="."

    if [ ! "$TERMINOLOGY" = "1" ]; then
    echo "This script only works in Terminology" >&2
    exit 1
    fi

    while getopts ":w:h:n" opt; do
    case "$opt" in
    w) WIDTH="$OPTARG" ;;
    h) HEIGHT="$OPTARG" ;;
    n) LABEL="no" ;;
    :)
    echo "Option -$OPTARG requires a value" >&2
    exit 1
    ;;
    esac
    done

    shift $((OPTIND - 1))

    if [ $WIDTH -lt 1 ]; then
    WIDTH=10
    fi

    if [ $HEIGHT -lt 1 ]; then
    HEIGHT=4
    fi

    if [ -z "$*" ]; then
    PARAM="./*"
    else
    @@ -34,63 +62,94 @@ function declare_inline_media {
    echo -ne "\e}it$CHAR$WIDTH;$HEIGHT;$1\0"
    }

    # $1 = amount
    function move_left {
    echo -ne "\e[${1}D"
    # $1 = direction (left/right/up/down)
    # $2 = amount
    function move {
    local OP
    case "$1" in
    left) OP="D" ;;
    right) OP="C" ;;
    up) OP="A" ;;
    down) OP="B" ;;
    esac
    echo -ne "\e[${2:-1}$OP"
    }

    # $1 = amount
    function move_right {
    echo -ne "\e[${1}C"
    }
    # $1 = absolute path
    function get_thumb {
    if [ -d "$1" ]; then
    THUMB="folder"
    else
    EXT="${1##*.}"
    # there are still more file formats supported by terminology.
    # these are just few i used most
    case "$EXT" in
    jpg|jpeg|png|gif|svg|pdf)
    THUMB="$ABSPATH";;

    # $1 = amount
    function move_up {
    echo -ne "\e[${1}A"
    }
    avi|mp4|mkv|asf|wmv|ogv|mpg|rmvb|flv)
    THUMB="video-x-generic" ;;

    swf)
    THUMB="application-x-shockwave-flash" ;;

    mp3|aac|flac|ogg|wma|wav|ape)
    THUMB="audio-x-generic" ;;

    zip|rar|ace|gz|bz2|tbz2|tgz|cbz|cbr|7z|xz)
    THUMB="application-x-archive" ;;

    # $1 = amount
    function move_down {
    echo -ne "\e[${1}B"
    txt|srt|edc|c|h|sh|py|rb|lua|go|php|cpp|h|hpp|cc|hh|md|vim|conf|ini|am|ac|in|xml|html|xhtml|rss|atom|README|TODO|NOTES|ChangeLog|NEWS|AUTHORS|COPYING|INSTALL)
    THUMB="text-x-generic" ;;

    *) THUMB="empty";;
    esac
    fi
    }

    get_info

    COL=1
    for FILENAME in $PARAM; do
    ABSPATH="$(realpath "$FILENAME")"
    BASENAME="$(basename "$ABSPATH")"

    if [ -d "$ABSPATH" ]; then
    THUMB="folder"
    else
    BASENAME="$(basename "$ABSPATH")"
    EXT="${BASENAME##*.}"
    case "$EXT" in
    jpg|jpeg|png|gif|bmp|tif|ico|svg|pdf)
    THUMB="$ABSPATH";;

    *) THUMB="empty";;
    esac
    fi
    get_thumb "$ABSPATH"

    declare_inline_media "$THUMB"

    for (( ROW = 0 ; ROW < $HEIGHT ; ROW++ )); do
    print_block
    move_left $WIDTH
    move_down 1
    move left $WIDTH
    move down
    done

    COL=$(( $COL + $WIDTH + 2 ));
    if [ "$LABEL" = "yes" ]; then
    CAPTION="${BASENAME:0:$WIDTH}"
    LEFT_PAD=$(( ($WIDTH + ${#CAPTION}) / 2 ))
    printf "%*s" $LEFT_PAD "$CAPTION"
    move left $LEFT_PAD
    fi

    COL=$(( $COL + $WIDTH + 3 ));

    if [ $COL -ge $TERM_WIDTH ]; then
    COL=1
    if [ "$LABEL" = "yes" ]; then
    echo
    fi
    echo
    else
    move_right $(( $WIDTH + 1))
    move_up $HEIGHT
    move right $(( $WIDTH + 2 ))
    move up $HEIGHT
    fi

    done

    for (( ROW = 0 ; ROW < $HEIGHT ; ROW++)); do echo; done
    if [ $COL -gt $WIDTH -a $COL -lt $TERM_WIDTH ]; then
    move down $HEIGHT
    if [ "$LABEL" = "yes" ]; then
    echo
    fi
    fi
    echo
  3. tomoe-mami created this gist Mar 28, 2013.
    96 changes: 96 additions & 0 deletions lsthumb.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    #!/bin/sh

    WIDTH=6
    HEIGHT=3
    CHAR="."

    if [ -z "$*" ]; then
    PARAM="./*"
    else
    PARAM=$*
    fi

    function get_info {
    local ORIG_STTY="$(stty -g)"
    stty -echo
    echo -ne "\e}qs\0"
    read RESPONSE
    stty "$ORIG_STTY"

    TERM_WIDTH="$(echo "$RESPONSE" | cut -d';' -f1)"
    TERM_HEIGHT="$(echo "$RESPONSE" | cut -d';' -f2)"
    }

    function print_block {
    echo -ne "\e}ib\0"
    for (( c = 0 ; c < $WIDTH ; c++ )); do
    echo -n "$CHAR"
    done
    echo -ne "\e}ie\0"
    }

    # $1 = path
    function declare_inline_media {
    echo -ne "\e}it$CHAR$WIDTH;$HEIGHT;$1\0"
    }

    # $1 = amount
    function move_left {
    echo -ne "\e[${1}D"
    }

    # $1 = amount
    function move_right {
    echo -ne "\e[${1}C"
    }

    # $1 = amount
    function move_up {
    echo -ne "\e[${1}A"
    }

    # $1 = amount
    function move_down {
    echo -ne "\e[${1}B"
    }

    get_info

    COL=1
    for FILENAME in $PARAM; do
    ABSPATH="$(realpath "$FILENAME")"

    if [ -d "$ABSPATH" ]; then
    THUMB="folder"
    else
    BASENAME="$(basename "$ABSPATH")"
    EXT="${BASENAME##*.}"
    case "$EXT" in
    jpg|jpeg|png|gif|bmp|tif|ico|svg|pdf)
    THUMB="$ABSPATH";;

    *) THUMB="empty";;
    esac
    fi

    declare_inline_media "$THUMB"

    for (( ROW = 0 ; ROW < $HEIGHT ; ROW++ )); do
    print_block
    move_left $WIDTH
    move_down 1
    done

    COL=$(( $COL + $WIDTH + 2 ));

    if [ $COL -ge $TERM_WIDTH ]; then
    COL=1
    echo
    else
    move_right $(( $WIDTH + 1))
    move_up $HEIGHT
    fi

    done

    for (( ROW = 0 ; ROW < $HEIGHT ; ROW++)); do echo; done