diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/colors.h | 45 | ||||
-rw-r--r-- | src/lib/colors.list | 42 | ||||
-rw-r--r-- | src/lib/colors.sh | 44 | ||||
-rw-r--r-- | src/lib/generate_colors.sh | 44 | ||||
-rw-r--r-- | src/lib/glyphs.sh | 30 | ||||
-rw-r--r-- | src/lib/xilib.sh | 34 | ||||
-rw-r--r-- | src/lib/xitui.sh | 451 |
7 files changed, 690 insertions, 0 deletions
diff --git a/src/lib/colors.h b/src/lib/colors.h new file mode 100644 index 0000000..e7bc26d --- /dev/null +++ b/src/lib/colors.h @@ -0,0 +1,45 @@ +/* + * colors.h + * + * list of ansi color codes + * provided by xiutils + * + */ + +#define RESET "[0m" +#define BOLD "[0;1m" +#define BLACK "[0;30m" +#define RED "[0;31m" +#define GREEN "[0;32m" +#define YELLOW "[0;33m" +#define BLUE "[0;34m" +#define MAGENTA "[0;35m" +#define CYAN "[0;36m" +#define WHITE "[0;37m" +#define DEFAULT "[0;39m" +#define BG_BLACK "[40m" +#define BG_RED "[41m" +#define BG_GREEN "[42m" +#define BG_YELLOW "[43m" +#define BG_BLUE "[44m" +#define BG_MAGENTA "[45m" +#define BG_CYAN "[46m" +#define BG_WHITE "[47m" +#define BG_DEFAULT "[49m" +#define LIGHT_BLACK "[0;90m" +#define LIGHT_RED "[0;91m" +#define LIGHT_GREEN "[0;92m" +#define LIGHT_YELLOW "[0;93m" +#define LIGHT_BLUE "[0;94m" +#define LIGHT_MAGENTA "[0;95m" +#define LIGHT_CYAN "[0;96m" +#define LIGHT_WHITE "[0;97m" +#define LIGHT_DEFAULT "[0;99m" +#define BOLDBLACK "[1;30m" +#define BOLDRED "[1;31m" +#define BOLDGREEN "[1;32m" +#define BOLDYELLOW "[1;33m" +#define BOLDBLUE "[1;34m" +#define BOLDMAGENTA "[1;35m" +#define BOLDCYAN "[1;36m" +#define BOLDWHITE "[1;37m" diff --git a/src/lib/colors.list b/src/lib/colors.list new file mode 100644 index 0000000..0829a63 --- /dev/null +++ b/src/lib/colors.list @@ -0,0 +1,42 @@ +RESET "\033[0m" + +BOLD "\033[0;1m" + +BLACK "\033[0;30m" +RED "\033[0;31m" +GREEN "\033[0;32m" +YELLOW "\033[0;33m" +BLUE "\033[0;34m" +MAGENTA "\033[0;35m" +CYAN "\033[0;36m" +WHITE "\033[0;37m" +DEFAULT "\033[0;39m" + +BG_BLACK "\033[40m" +BG_RED "\033[41m" +BG_GREEN "\033[42m" +BG_YELLOW "\033[43m" +BG_BLUE "\033[44m" +BG_MAGENTA "\033[45m" +BG_CYAN "\033[46m" +BG_WHITE "\033[47m" +BG_DEFAULT "\033[49m" + +LIGHT_BLACK "\033[0;90m" +LIGHT_RED "\033[0;91m" +LIGHT_GREEN "\033[0;92m" +LIGHT_YELLOW "\033[0;93m" +LIGHT_BLUE "\033[0;94m" +LIGHT_MAGENTA "\033[0;95m" +LIGHT_CYAN "\033[0;96m" +LIGHT_WHITE "\033[0;97m" +LIGHT_DEFAULT "\033[0;99m" + +BOLDBLACK "\033[1;30m" +BOLDRED "\033[1;31m" +BOLDGREEN "\033[1;32m" +BOLDYELLOW "\033[1;33m" +BOLDBLUE "\033[1;34m" +BOLDMAGENTA "\033[1;35m" +BOLDCYAN "\033[1;36m" +BOLDWHITE "\033[1;37m" diff --git a/src/lib/colors.sh b/src/lib/colors.sh new file mode 100644 index 0000000..8e5de11 --- /dev/null +++ b/src/lib/colors.sh @@ -0,0 +1,44 @@ +# +# colors.h +# +# list of ansi color codes +# provided by xiutils +# + +export RESET=$(printf "[0m") +export BOLD=$(printf "[0;1m") +export BLACK=$(printf "[0;30m") +export RED=$(printf "[0;31m") +export GREEN=$(printf "[0;32m") +export YELLOW=$(printf "[0;33m") +export BLUE=$(printf "[0;34m") +export MAGENTA=$(printf "[0;35m") +export CYAN=$(printf "[0;36m") +export WHITE=$(printf "[0;37m") +export DEFAULT=$(printf "[0;39m") +export BG_BLACK=$(printf "[40m") +export BG_RED=$(printf "[41m") +export BG_GREEN=$(printf "[42m") +export BG_YELLOW=$(printf "[43m") +export BG_BLUE=$(printf "[44m") +export BG_MAGENTA=$(printf "[45m") +export BG_CYAN=$(printf "[46m") +export BG_WHITE=$(printf "[47m") +export BG_DEFAULT=$(printf "[49m") +export LIGHT_BLACK=$(printf "[0;90m") +export LIGHT_RED=$(printf "[0;91m") +export LIGHT_GREEN=$(printf "[0;92m") +export LIGHT_YELLOW=$(printf "[0;93m") +export LIGHT_BLUE=$(printf "[0;94m") +export LIGHT_MAGENTA=$(printf "[0;95m") +export LIGHT_CYAN=$(printf "[0;96m") +export LIGHT_WHITE=$(printf "[0;97m") +export LIGHT_DEFAULT=$(printf "[0;99m") +export BOLDBLACK=$(printf "[1;30m") +export BOLDRED=$(printf "[1;31m") +export BOLDGREEN=$(printf "[1;32m") +export BOLDYELLOW=$(printf "[1;33m") +export BOLDBLUE=$(printf "[1;34m") +export BOLDMAGENTA=$(printf "[1;35m") +export BOLDCYAN=$(printf "[1;36m") +export BOLDWHITE=$(printf "[1;37m") diff --git a/src/lib/generate_colors.sh b/src/lib/generate_colors.sh new file mode 100644 index 0000000..6e01d58 --- /dev/null +++ b/src/lib/generate_colors.sh @@ -0,0 +1,44 @@ +#!/bin/bash +DIST=$1 + +headerfile=${DIST}/colors.h +shlib=${DIST}/colors.sh + +cat > $shlib << "EOF" +# +# colors.h +# +# list of ansi color codes +# provided by xiutils +# + +EOF + +cat > $headerfile << "EOF" +/* + * colors.h + * + * list of ansi color codes + * provided by xiutils + * + */ + +EOF + +append_header() { + echo "#define $1 $2" >> $headerfile +} + +append_sh() { + printf 'export %s=$(printf %s)\n' $1 $2 >> $shlib +} + +while IFS= read -r line; do + echo "$line" | grep -q "." || continue + name=$(echo $line | awk '{ print $1 }') + code=$(echo $line | awk '{ print $2 }') + + append_header $name $code + append_sh $name $code + +done < "$2" diff --git a/src/lib/glyphs.sh b/src/lib/glyphs.sh new file mode 100644 index 0000000..b4ecccd --- /dev/null +++ b/src/lib/glyphs.sh @@ -0,0 +1,30 @@ +# +# Assorted Unicode glyphs that can be repurposed +# + +export CHECKMARK="✔" +export CROSSMARK="✘" +export CROSSMARK="✘" +export HOURGLASS="⧖" +export LARGE_CIRCLE="◯" +export REFRESH="🗘" +export TABCHAR="╚═" +export XI="Ξ" + +export LOGO_SMALL="████████ +▀ ▀ + █▄▄█ + █▀▀█ +▄ ▄ +████████" + +export LOGO="████████████████ +▀ ▀ + + + █▄▄▄▄▄▄█ + █▀▀▀▀▀▀█ + + +▄ ▄ +████████████████" diff --git a/src/lib/xilib.sh b/src/lib/xilib.sh new file mode 100644 index 0000000..57dd10c --- /dev/null +++ b/src/lib/xilib.sh @@ -0,0 +1,34 @@ +#!/bin/sh + +# format a number into a bytes, kibibytes, mebibytes, or gibibytes +# +format_bytes () { + case "1" in + "$(($1>=1<<30))") printf "$(($1>>30))GiB";; + "$(($1>=1<<20))") printf "$(($1>>20))MiB";; + "$(($1>=1<<10))") printf "$(($1>>10))kiB";; + *) printf "$1B";; + esac +} + +# ensure that the user is a root user +# +checkroot () { + [ "$(id -u)" = "0" ] || { + printf "${RED}Please run as root!${RESET}\n" + exit 1 + } +} + +# reverse the order of lines +# +reverse_lines () { + local result= + while IFS= read -r line; do + result="$line + $result" + done + echo "$result" +} + + diff --git a/src/lib/xitui.sh b/src/lib/xitui.sh new file mode 100644 index 0000000..dcfaf02 --- /dev/null +++ b/src/lib/xitui.sh @@ -0,0 +1,451 @@ +#!/bin/sh + +################################################################# +#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ # +#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████# +# # +# # +# # +# # +# # +# # +# # +#█ ███████ ███████ ███████ ███████ ███████ ███████ ███████ █████# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ # +#█ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#█ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █ █ ███ █# +#█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █# +#███████ ███████ ███████ ███████ ███████ ███████ ███████ ███████# +################################################################# + +. /usr/lib/colors.sh + +COLOR_FG=${WHITE} +COLOR_FG2=${LIGHT_BLUE} +COLOR_PTRN=${LIGHT_BLUE} +COLOR_BG=${BG_WHITE} + +t_init () { + export LINES=$(tput lines) + export COLUMNS=$(tput cols) + t_styl_cur l + tput clear + t_set_tty + t_gen_ptrn + +} + +t_set_tty () { + export SAVED_TTY_SETTINGS=$(stty -g) + stty raw -echo +} + +t_clean () { + stty $SAVED_TTY_SETTINGS + tput clear + t_yes_cur +} + +readc () { + stty -echo -icanon min 1 time 0 + s="$(dd bs=1 count=1 of=/dev/stdout 2>/dev/null)" + stty -icanon min 0 time 0 + [ "$s" = "" ] && { + s="$s$(dd bs=1 count=2 of=/dev/stdout 2>/dev/null)" + } + printf "$s" +} + + +t_cls () { + tput clear +} + +t_no_cur () { + tput civis +} + +t_yes_cur () { + tput cnorm +} + +t_set_cur () { + printf "[$1;$2H" +} + +max_line_length () { + local max=-1 + while IFS= read -r line; do + [ "${#line}" -gt "$max" ] && max=${#line} + done + echo "$max" +} + +t_styl_cur () { + case "$1" in + H) printf "\033[2 q";; + H0) printf "\033[1 q";; + l) printf "\033[6 q";; + l0) printf "\033[5 q";; + _) printf "\033[4 q";; + _0) printf "\033[3 q";; + *) printf"\033[0 q]";; + esac > $(tty) +} + + +t_gen_ptrn () { + export P="" + for l in $(seq 7); do + for c in $(seq $COLUMNS); do + case "$l" in + 1) [ "$((c%8))" != "0" ] ;; + 2) [ "$((c%8))" = "1" ] || [ "$((c%8))" = "7" ] ;; + 3) [ "$((c%2))" = "1" ] || [ "$((c%8))" = "4" ] ;; + 4) [ "$((c%2))" = "1" ] ;; + 5) [ "$((c%2))" != "0" ] || [ "$((c%8))" = "2" ] ;; + 6) [ "$((c%8))" = "5" ] || [ "$((c%8))" = "7" ] ;; + 7) [ "$((c%8))" != "6" ] ;; + *) false ;; + esac && P="$P█" || P="$P " + done + P="$P\n" + done +} + +t_prnt_ptrn () { + printf "$@$P" +} + +t_cls_ptrn () { + printf "[7;0H" + printf " %.0s" $(seq $(( (LINES-14) * COLUMNS))) + t_drw_ptrn +} + +t_drw_ptrn () { + t_drw_txt_clr "${COLOR_PTRN}" 1 1 "$P" + t_drw_txt_clr "${COLOR_PTRN}" 1 $((LINES-6)) "$(printf "$P" | rev)" +} + +# draws inside the given area +# t_drw_box x y w h +# +t_drw_box () { + local x=$1 y=$2 w=$3 h=$4 + + + l="$(printf "${COLOR_FG2}║" + printf " %.0s" $(seq $((w-2))) + printf "${COLOR_FG2}║" + )" + b="$(printf "═%.0s" $(seq $((w-2))))" + + printf "[${y};${x}H${COLOR_FG2}╔$b╗" + for i in $(seq $((y+1)) $((y+h-1))); do + printf "[${i};${x}H$l" + done + printf "[$((y+h-1));${x}H${COLOR_FG2}╚$b╝" +} + +# draw text at location +# t_drw_txt x y [txt...] +# +t_drw_txt () { + local x=$1 y=$2 + shift 2 + + echo "$@" | while IFS= read -r line; do + printf "[${y};${x}H$line" + y=$((y+1)) + done +} + +# draw colored text +# +t_drw_txt_clr () { + local clr=$1 x=$2 y=$3 + shift 3 + + echo "$@" | while IFS= read -r line; do + printf "[${y};${x}H${clr}$line" + y=$((y+1)) + done +} + +t_msg () { + h_txt_w_h "$@\n" + h_cntr $w $h + t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2)) + t_drw_txt_clr "${COLOR_FG}" $x $y "$@" +} + +h_drw_btns () { + local sel=$1 i=0 + while read -r btn; do + [ "$sel" = "$i" ] && styl="${COLOR_BG}" || styl="${COLOR_FG2}" + h_drw_btn "${styl}" ${btn} + i=$((i+1)) + done +} + +h_drw_btn () { + styl="$1" x=$2 y=$3 ; shift 3; txt="$@" + t_drw_txt $x $y "${COLOR_FG2}${styl}$txt${RESET}" +} + +h_cntr() { + local w=$1 h=$2 + x=$(( ( (COLUMNS+w)/2 ) - $1 )) + y=$(( ( (LINES+h)/2 ) - $2 )) +} + +h_txt_w_h() { + w=$(printf "$*" | max_line_length) + h=$(printf "$*" | wc -l) +} + +h_t_result() { + local s=$1 i=0; shift + for opt in "$@"; do + case " $s " in + *" $i "*) echo "$opt";; + esac + i=$((i+1)) + done +} + +t_dialog() { + # TODO these are messy af + local msg="$1"; shift + local btns_len="$#" w_btns=$(echo "$*" | wc -c) + + h_txt_w_h "$msg\n $*\n" + h_cntr $w $h + + local btn_y=$((y+h-1)) \ + btn_x=$(( ((COLUMNS+w_btns) / 2) - w_btns)) + + btns="" + for btn in "$@"; do + btns="$btns$btn_x $btn_y $btn\n" + btn_x=$((btn_x+${#btn}+1)) + done + + t_drw_box $((x-1)) $((y-1)) $((w+2)) $((h+2)) + t_drw_txt_clr ${COLOR_FG} $x $y "$msg" + + local sel="0" + while true; do + printf "$btns" | h_drw_btns $sel + + case "$(readc)" in + '
') break;; + '[B'|'[C'|' ') + sel=$(((sel+1)%btns_len));; + '[A'|'[D') + sel=$(((sel+btns_len-1)%btns_len));; + esac + + done + export T_RESULT=$(h_t_result "$sel" "$@") +} + +t_radio() { + local msg="$1"; shift + + local btns_len="$#" w_btns=$(echo "$*" | wc -c) + text=`{ + printf "$msg\n" + for b in "$@"; do + printf "╚═$b${RESET}\n" + done + }` + + t_msg "$text" + + x=$((x+2)) y=$((y+1)) + local py=$((y)) + btns="" + for btn in "$@"; do + btns="$btns$x $y $btn\n" + y=$((y+1)) + done + + local sel="0" + while true; do + printf "$btns" | h_drw_btns $sel + + case "$(readc)" in + '
') break;; + '[B'|'[C'|' ') + sel=$(((sel+1)%btns_len));; + '[A'|'[D') + sel=$(((sel+btns_len-1)%btns_len));; + esac + + done + export T_RESULT=$(h_t_result "$sel" "$@") +} + +t_check () { + local msg="$1"; shift + local btns_len="$#" + btns_len=$((btns_len+1)) + + text=`{ + printf "$msg\n" + for b in "$@"; do + printf "[ ] $b${RESET}\n" + done + printf " <OK>${RESET}\n" + }` + + t_msg "$text" + + x=$((x+4)) y=$((y+1)) + local py=$((y)) + btns="" + for btn in "$@"; do + btns="$btns$x $y $btn\n" + y=$((y+1)) + done + btns="$btns$x $y <OK>\n" + + local sel="0" checked="" + while true; do + printf "$btns" | h_drw_btns $sel + case "$(readc)" in + '[B'|'[C'|' ') + sel=$(((sel+1)%btns_len)) + ;; + '[A'|'[D') + sel=$(((sel+btns_len-1)%btns_len));; + ' '|'
') + [ "$((sel+1))" = "$btns_len" ] && break || { + case " $checked " in + *$sel*) + checked=$(echo "$checked" | sed "s/$sel//") + t_drw_txt $((x-3)) $((py+sel)) " " + ;; + *) + checked="$checked$sel " + t_drw_txt $((x-3)) $((py+sel)) "x" + ;; + esac + };; + esac + done + export T_RESULT=$(h_t_result "$checked" "$@") +} + +t_prompt () { + t_dialog "$@" "<ok>" +} + +t_yesno () { + t_dialog "$@" "<yes>" "<no>" + [ "$T_RESULT" = "<yes>" ] +} + +t_input () { + stty $SAVED_TTY_SETTINGS + t_msg "$@ + +>" > $(tty) + t_yes_cu + read var + t_set_tty + t_no_cur + export T_RESULT="$var" +} + +t_input_hidden () { + stty $SAVED_TTY_SETTINGS + stty -echo + t_msg "$@ + +>" > $(tty) + t_yes_cur + read var + t_set_tty + t_no_cur + export T_RESULT="$var" +} + +t_tail() { + t_drw_txt 0 7 "$(tail -$((LINES-14)) $1)" +} + +t_paged_radio () { + perpage=9 + heading=$1 + shift + + T_RESULT="more..." + page=0 + max_pages=$((($#/perpage)+1)) + while true; do + start=$((page*perpage)) + current=$(echo $@ | tr ' ' '\n' | tail -$(($#-start)) | head -$perpage) + current="$current more..." + + t_cls_ptrn + t_radio "$heading" $current + [ "$T_RESULT" = "more..." ] && page=$(((page+1)%max_pages)) || return 0 + done +} + + +t_demo () { + t_init + t_no_cur + + + t_cls_ptrn + t_prompt "Hello world?" + t_cls_ptrn + + t_paged_radio "Pick a number" $(seq 34) + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + exit 1 + + t_radio "Pick one:" "toast" "bread" "bread but fishy" "empty sandwich" + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + t_check "Please select your order:" "yeast" "fish and chips" "twigs" "tuna" "pizza" "aspic" + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + t_dialog "Can a match box?" "<Yes>" "<No>" "<No but a tin can>" + t_prompt "You selected the following: $T_RESULT" + t_cls_ptrn + + #t_cls_ptrn + t_input "Enter your name" + t_prompt "Hello $T_RESULT" + t_cls_ptrn + + file=$2.sh + [ -f $file ] && { + t_tail $file + } || { + t_prompt "The file $file does not exist" + } + t_prompt "Clear" + t_cls_ptrn + t_prompt "Exit?" + + t_clean +} + +"$@" |