diff --git a/Makefile b/Makefile index f831be6..0ba37a3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ BIN ?= bpkg PREFIX ?= /usr/local -CMDS = json install package +CMDS = json install package term install: uninstall install $(BIN) $(PREFIX)/bin diff --git a/bpkg-term b/bpkg-term new file mode 120000 index 0000000..2ee3325 --- /dev/null +++ b/bpkg-term @@ -0,0 +1 @@ +lib/term/term.sh \ No newline at end of file diff --git a/lib/install/install.sh b/lib/install/install.sh index 27d7cb5..36babaa 100755 --- a/lib/install/install.sh +++ b/lib/install/install.sh @@ -138,7 +138,7 @@ bpkg_install () { ## prune existing rm -rf ${name}-${version} && ## shallow clone - git clone --depth=1 ${BPKG_GIT_REMOTE}/${user}/${name}.git ${name}-${version} && + git clone --depth=1 ${BPKG_GIT_REMOTE}/${user}/${name}.git ${name}-${version} > /dev/null 2>&1 && ## move into directory cd ${name}-${version} && ## build diff --git a/lib/term/LICENSE b/lib/term/LICENSE new file mode 100644 index 0000000..3bab1f7 --- /dev/null +++ b/lib/term/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Joseph Werle + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/lib/term/Makefile b/lib/term/Makefile new file mode 100644 index 0000000..a93211a --- /dev/null +++ b/lib/term/Makefile @@ -0,0 +1,14 @@ + +BIN ?= term +PREFIX ?= /usr/local + +install: + cp term.sh $(PREFIX)/bin/$(BIN) + +uninstall: + rm -f $(PREFIX)/bin/$(BIN) + +example.sh: + ./example.sh + +.PHONY: example.sh diff --git a/lib/term/README.md b/lib/term/README.md new file mode 100644 index 0000000..f930869 --- /dev/null +++ b/lib/term/README.md @@ -0,0 +1,100 @@ +term.sh +======= + +Terminal fun written in bash inspired by clibs/term + +## install + +```sh +$ make install +``` + +or + +```sh +$ . term.sh +``` + +## usage + +``` +usage: term [-hV] [args] +``` + +## example + +```sh +$ { term color green; } && { term underline; } && { echo heyaaaa; } +heyaaaa +``` + +## api + +``` +commands: + + write Write a terminal escape code + cursor Perform operation to cursor + color Set terminal color by name (See colors) + background Set terminal background by name (See colors) + move Move to (x, y) + transition Transition to (x, y) + clear
Clear terminal section by name (See sections) + reset Reset the terminal escape code sequence + bright Write bright escape code + dim Write dim escape code + underline Write underline escape code + blink Write blink escape code + reverse Write reverse escape code + hidden Write hidden escape code + +colors: + + black $ term color black + red $ term color red + green $ term color green + yellow $ term color yellow + blue $ term color blue + magenta $ term color magenta + cyan $ term color cyan + white $ term color white + gray|grey $ term color gray + +sections: + + start Start of line + end End of line + up Upper section + down Lower section + line Current line + screen Entire screen +``` + +## histogram + +See `example.sh` + +``` + . + + . + + . + + . + + . + █ + . █ + █ + . █ + █ █ + . █ █ + █ █ + █ █ + . █ █ █ █ █ █ █ █ █ █ █ +``` + +## license + +MIT diff --git a/lib/term/example.sh b/lib/term/example.sh new file mode 100755 index 0000000..653ce2c --- /dev/null +++ b/lib/term/example.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +## include +. term.sh + +## data +declare -a data=( 0 2 3 1 3 3 3 8 2 12 4 2 4 3 ) + +## clean up everything +cleanup () { + ## clear + term clear screen + ## bring back cursor + term cursor show + return 0 +} + +## on SIGINT signal +onsigint () { + cleanup + exit 1 +} + +## clear screen +term clear screen + +## position to top left +term move 0 0 + +## clear line +term clear line + +## hide cursor +term cursor hide + +## catch sigint +trap "onsigint" SIGINT + + +## main loop +{ + let pad=3 + let n=0 + let w=($(tput cols)) + let h=($(tput lines)) + let x=0 + let y=0 + let len="${#data[@]}" + + term clear screen + + term move ${pad} 1 + + ## y axis + for (( n = 0; n < (h - pad - 1); n += 2 )); do + term transition 0 2 + term color gray + printf "." + done + + y=( ${h} - 2 ) + term move ${pad} ${y} + + ## x axis + for (( n = 0; n < (w - pad * 3); n += 6)); do + term color gray + printf "." + term transition 6 0 + done + + x=0 + for (( i = 0; i < len; ++i )); do + let n="${data[$i]}" + while (( n-- )); do + if (( n < 0 )); then + break + fi + let a=( ${x} * 6 + ${pad} ) + let b=( ${h} - ${n} + ${pad} ) + #echo $a $b + term move ${a} ${b} + term reset + printf "█" + done + (( x++ )) + sleep .5 + done + + h=( ${h} - 1) + term move ${w} ${h} +} + +## clean up terminal +cleanup + +## exit +exit $? + diff --git a/lib/term/package.json b/lib/term/package.json new file mode 100644 index 0000000..464fcef --- /dev/null +++ b/lib/term/package.json @@ -0,0 +1,7 @@ +{ + "name": "term", + "version": "0.0.1", + "description": "Terminal utility functions", + "scripts": [ "term.sh" ], + "install": "make install" +} diff --git a/lib/term/term.sh b/lib/term/term.sh new file mode 100755 index 0000000..1362f1d --- /dev/null +++ b/lib/term/term.sh @@ -0,0 +1,293 @@ +#!/bin/bash + +## version +VERSION="0.0.1" + +## coords +let _x=0 +let _y=0 + +## output error to stderr +error () { + printf >&2 "error: %s\n" "${@}" +} + +## output usage +usage () { + echo "usage: term [-hV] [args]" +} + +## write code to terminal +term_write () { + local let c="${1}" + ## ensure + if [ -z "${c}" ]; then + return 1 + fi + printf "\e[${c}" + return 0 +} + +## cursor operations +term_cursor () { + local op="$1" + if [ -z "${op}" ]; then + return 1 + fi + case "${op}" in + hide) term write "?25l" ;; + show) term write "?25h" ;; + *) return 1 ;; + esac + return 0 +} + +## move to (x, y) +term_move () { + local let x="${1}" + local let y="${2}" + + ## ensure + if [ -z "${x}" ] || [ -z "${y}" ]; then + return 1 + fi + + ## set state + (( _x = ${x} )) + (( _y = ${y} )) + + ## write + printf "\e[%d;%d;f" ${y} ${x} + return 0 +} + +term_transition () { + local let x="${1}" + local let y="${2}" + if [ -z "${x}" ] || [ -z "${y}" ]; then + return 1 + fi + + (( x = ${x} + ${_x} )) + (( y = ${y} + ${_y} )) + + term move "${x}" "${y}" + return 0 +} + +## set terminal color +term_color () { + local color="${1}" + local fmt="\e[3%dm" + if [ -z "${color}" ]; then + return 1 + fi + case "${color}" in + black) printf "${fmt}" "0" ;; + red) printf "${fmt}" "1" ;; + green) printf "${fmt}" "2" ;; + yellow) printf "${fmt}" "3" ;; + blue) printf "${fmt}" "4" ;; + magenta) printf "${fmt}" "5" ;; + cyan) printf "${fmt}" "6" ;; + white) printf "${fmt}" "7" ;; + gray|grey) printf "\e[90m" ;; + *) return 1 ;; + esac + return 0 +} + +## set term background color +term_background () { + local color="${1}" + local fmt="\e[4%dm" + if [ -z "${color}" ]; then + return 1 + fi + case "${color}" in + black) printf "${fmt}" "0" ;; + red) printf "${fmt}" "1" ;; + green) printf "${fmt}" "2" ;; + yellow) printf "${fmt}" "3" ;; + blue) printf "${fmt}" "4" ;; + magenta) printf "${fmt}" "5" ;; + cyan) printf "${fmt}" "6" ;; + white) printf "${fmt}" "7" ;; + *) return 1 ;; + esac + return 0 +} + +## reset terminal escape sequence +term_reset () { + term write "0m" +} + +## make terminal bright +term_bright () { + term write "1m" +} + +## make terminal dim +term_dim () { + term write "2m" +} + +## make terminal underlined +term_underline () { + term write "4m" +} + +## make terminal blink +term_blink () { + term write "5m" +} + +## make terminal reverse +term_reverse () { + term write "7m" +} + +## make terminal hidden +term_hidden () { + term write "8m" +} + +## clear a terminal section by name +term_clear () { + local section="${1}" + local fmt="\e[%s" + if [ -z "${section}" ]; then + return 1 + fi + case "${section}" in + start) printf "${fmt}" "1K";; + end) printf "${fmt}" "K";; + line) printf "${fmt}" "2K";; + screen|up) printf "${fmt}" "1J";; + down) printf "${fmt}" "J";; + *) return 1 ;; + esac + return 0 +} + +## +# Term functions +# +# usage: term [-hV] +## + +term () { + local arg="$1" + local cmd="" + shift + + case "${arg}" in + + ## flags + -V|--version) + echo "${VERSION}" + return 0 + ;; + + -h|--help) + usage + + ## commands + { + echo + echo "commands: " + echo + echo " write Write a terminal escape code" + echo " cursor Perform operation to cursor" + echo " color Set terminal color by name (See colors)" + echo " background Set terminal background by name (See colors)" + echo " move Move to (x, y)" + echo " transition Transition to (x, y)" + echo " clear
Clear terminal section by name (See sections)" + echo " reset Reset the terminal escape code sequence" + echo " bright Write bright escape code" + echo " dim Write dim escape code" + echo " underline Write underline escape code" + echo " blink Write blink escape code" + echo " reverse Write reverse escape code" + echo " hidden Write hidden escape code" + } + + ## colors + { + echo + echo "colors:" + echo + + term color black + echo " black $ term color black" + + term color red + echo " red $ term color red" + + term color green + echo " green $ term color green" + + term color yellow + echo " yellow $ term color yellow" + + term color blue + echo " blue $ term color blue" + + term color magenta + echo " magenta $ term color magenta" + + term color cyan + echo " cyan $ term color cyan" + + term color white + echo " white $ term color white" + + term color gray + echo " gray|grey $ term color gray" + + term reset + } + + ## sections + { + echo + echo "sections:" + echo + echo " start Start of line" + echo " end End of line" + echo " up Upper section" + echo " down Lower section" + echo " line Current line" + echo " screen Entire screen" + } + + return 0 + ;; + + *) + cmd="term_${arg}" + if type "${cmd}" > /dev/null 2>&1; then + "${cmd}" "${@}" + return $? + else + if [ ! -z "${arg}" ]; then + error "Unknown argument: \`${arg}'" + fi + usage + return 1 + fi + ;; + esac +} + + +## detect if being sourced and +## export if so else execute +## main function with args +if [[ ${BASH_SOURCE[0]} != $0 ]]; then + export -f term +else + term "${@}" +fi