diff --git a/README.md b/README.md index 67c3a94..122cb41 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # spmenu-examples -Example scripts for spmenu \ No newline at end of file +This repository contains spmenu example scripts which may be used to learn how to +do certain things in spmenu. Feel free to copy the scripts somewhere and use +them to learn. The `install.sh` script does this for you if you want all of them. diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..01e0c40 --- /dev/null +++ b/install.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# install.sh +# Install all scripts in the current directory. +# +# See LICENSE file for copyright and license details. +[ "$(id -u)" != "0" ] && printf "Run me as root.\n" && exit 1 +cp -f spmenu* /usr/bin diff --git a/spmenu_16cols.sh b/spmenu_16cols.sh new file mode 100755 index 0000000..983faa0 --- /dev/null +++ b/spmenu_16cols.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# spmenu_16cols.sh +# Prints 16 foreground and 16 background colors +# +# See LICENSE file for copyright and license details. + +printf "\033[0;31mItem 1\n\033[0;32mItem 2\n\033[0;33mItem 3\n\033[0;34mItem 4\n\033[0;35mItem 5\n\033[0;36mItem 6\n\033[0;37mItem 7\n\033[0;38mItem 8\n\033[1;31mItem 9\n\033[1;32mItem 10\n\033[1;33mItem 11\n\033[1;34mItem 12\n\033[1;35mItem 13\n\033[1;36mItem 14\n\033[1;37mItem 15\n\033[1;38mItem 16\n\ +\033[0;41mItem 1\n\033[0;42mItem 2\n\033[0;43mItem 3\n\033[0;44mItem 4\n\033[0;45mItem 5\n\033[0;46mItem 6\n\033[0;47mItem 7\n\033[0;48mItem 8\n\033[1;41mItem 9\n\033[1;42mItem 10\n\033[1;43mItem 11\n\033[1;44mItem 12\n\033[1;45mItem 13\n\033[1;46mItem 14\n\033[1;47mItem 15\n\033[1;48mItem 16\n" | spmenu "$@" diff --git a/spmenu_imageviewer.sh b/spmenu_imageviewer.sh new file mode 100755 index 0000000..79a3c97 --- /dev/null +++ b/spmenu_imageviewer.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# spmenu_imageviewer.sh +# Proof of concept script allowing you to view images in spmenu +# Usage: spmenu_imageviewer.sh image1 image2 ... +# +# See LICENSE file for copyright and license details. +[ ! -f "$1" ] && printf "You must specify an image to view.\n" && exit 1 + +genlist() { + argc="$(printf "%s\n" "$@" | wc -l)" + + for i in $(seq "$argc"); do + arg="$(printf "%s\n" "$@" | sed -n "${i}","${i}"p)" + [ -z "$arg" ] || [ ! -f "$arg" ] && continue + printf "img://%s\t%s\n" "$arg" "$arg" + done +} + +main() { + genlist "$@" | spmenu \ + --hide-mode \ + --hide-match-count \ + --hide-left-arrow \ + --hide-right-arrow \ + --hide-prompt \ + --hide-input \ + --hide-caret \ + --hide-caps \ + --hide-highlighting \ + --image-size 200 \ + --generate-cache \ + --lines 20 \ + --columns 1 +} + +main "$@" diff --git a/spmenu_itemcounter.sh b/spmenu_itemcounter.sh new file mode 100755 index 0000000..80eb32f --- /dev/null +++ b/spmenu_itemcounter.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# spmenu_itemcounter.sh +# Example script which adds a new entry to spmenu every second. +# +# See LICENSE file for copyright and license details. +func() { + rm -f /tmp/fl + + i=0 cnum=0 + while true; do + printf "%s \033[0;3${cnum}m%d\n" "Item" "$i" >> /tmp/fl + i=$((i+1)) + cnum=$((cnum+1)) + + [ "$cnum" -gt "9" ] && cnum="1" + + sleep 1 + done +} +func | spmenu --list-file /tmp/fl --lines 40 --columns 1 --prompt "Updates automatically!" diff --git a/spmenu_musiclist.sh b/spmenu_musiclist.sh new file mode 100755 index 0000000..f5e3a90 --- /dev/null +++ b/spmenu_musiclist.sh @@ -0,0 +1,57 @@ +#!/bin/sh +# spmenu_musiclist.sh +# +# This script lists out songs in a path you give to it and also displays the cover art for that song if it can be found. +# It serves as an example of how image drawing can be done with spmenu. +# +# See LICENSE file for copyright and license details. +[ -z "$dir" ] && dir="$1" +[ -z "$maxcount" ] && maxcount="$2" +[ -z "$startline" ] && startline="$3" +[ -z "$prefix" ] && prefix="/tmp/.list_songs" + +[ -z "$RUNLAUNCHER" ] && RUNLAUNCHER="spmenu" +[ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="--lines 40 --columns 1 --image-size 200 --no-generate-cache" + +create_list() { + [ -z "$i" ] && i=1 + [ -z "$c" ] && c="$(find "$dir"/* -type f | wc -l)" + + while true; do + file="$(find "$dir"/* -type f | sed -n "${i}","${i}"p)" || exit 1 + [ -e "$(dirname "$file")/cover.jpg" ] && \ + cp "$(dirname "$file")"/cover.jpg "$prefix/cover-$i.jpg" || ffmpeg -i "$file" -map 0:1 "$prefix/cover-$i.jpg" -loglevel quiet + [ -e "$prefix/cover-$i.jpg" ] && imageprefix="img://" && image="$prefix/cover-$i.jpg" + [ "$(basename "$file")" = "cover.jpg" ] && i=$(expr $i + 1) && continue + printf "%s%s\t%s\n" "$imageprefix" "$image" "$(basename "$file")" + + i=$(expr $i + 1) + + [ "$i" -eq "$c" ] || [ "$i" -gt "$c" ] && break + done +} + +usage() { + printf "usage: $0 \n" + exit 1 +} + +play() { + [ -n "$sel_file" ] && \ + mpv "$(find "$dir"/* -type f | grep "$sel_file")" +} + +main() { + mkdir -p $prefix + [ -z "$dir" ] && usage + [ -z "$RUNLAUNCHER" ] && RUNLAUNCHER="spmenu" + [ -z "$RUNLAUNCHER_ARGS" ] && RUNLAUNCHER_ARGS="-l 40 -g 1 -is 200 -ngc" + [ -n "$maxcount" ] && c="$maxcount" + [ -n "$startline" ] && c="$startline" + + sel_file="$(create_list | sed "s/\&/\&/g" | $RUNLAUNCHER $RUNLAUNCHER_ARGS | sed "s/\&/\&/g")" + play + rm -f "$prefix"/cover*.jpg # cleanup +} + +main "$@" diff --git a/spmenu_sweden.sh b/spmenu_sweden.sh new file mode 100755 index 0000000..a59a684 --- /dev/null +++ b/spmenu_sweden.sh @@ -0,0 +1,23 @@ +#!/bin/sh +# spmenu_sweden.sh +# Just the swedish flag using SGR sequences, serves as a good example on how to do background colors. +# +# See LICENSE file for copyright and license details. + +[ -z "$fg_blue" ] && fg_blue='\033[1;34m' +[ -z "$fg_yellow" ] && fg_yellow='\033[1;35m' +[ -z "$bg_blue" ] && bg_blue='\033[1;44m' +[ -z "$bg_yellow" ] && bg_yellow='\033[1;45m' +[ -z "$reset" ] && reset='\033[0m' +[ -z "$color1" ] && color1="#006AA7" +[ -z "$color2" ] && color3="#FECC02" + +printf "\n\ +${bg_blue} ${bg_yellow} ${bg_blue}\n\ +${bg_blue} ${bg_yellow} ${bg_blue}\n\ +${bg_yellow} \n\ +${bg_blue} ${bg_yellow} ${bg_blue}\n\ +${bg_blue} ${bg_yellow} ${bg_blue}\n\ +\n\ +${reset}${fg_blue}c${fg_yellow}o${fg_blue}o${fg_yellow}l\n\ +" | spmenu --sgr4 "$color1" --sgr5 "$color3" --sgr12 "$color1" --sgr13 "$color3" --no-alpha --lines 40 --columns 7 "$@"