summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2022-06-27 01:14:42 +0100
committerdavidovski <david@davidovski.xyz>2022-06-27 01:14:42 +0100
commit7bba6cd7612293796e905885f9ed3072877798ab (patch)
treedd01942dbdd474a4df62ecf74479f5e78d83f1c8 /src/lib
parent79fb6f41104cd0d88891598ddfc3c30d1bb0352d (diff)
added shmk, building all with shmk
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/colors.h45
-rw-r--r--src/lib/colors.list42
-rw-r--r--src/lib/colors.sh44
-rw-r--r--src/lib/generate_colors.sh44
-rw-r--r--src/lib/glyphs.sh30
-rw-r--r--src/lib/xilib.sh34
-rw-r--r--src/lib/xitui.sh451
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 ""
+#define BOLD ""
+#define BLACK ""
+#define RED ""
+#define GREEN ""
+#define YELLOW ""
+#define BLUE ""
+#define MAGENTA ""
+#define CYAN ""
+#define WHITE ""
+#define DEFAULT ""
+#define BG_BLACK ""
+#define BG_RED ""
+#define BG_GREEN ""
+#define BG_YELLOW ""
+#define BG_BLUE ""
+#define BG_MAGENTA ""
+#define BG_CYAN ""
+#define BG_WHITE ""
+#define BG_DEFAULT ""
+#define LIGHT_BLACK ""
+#define LIGHT_RED ""
+#define LIGHT_GREEN ""
+#define LIGHT_YELLOW ""
+#define LIGHT_BLUE ""
+#define LIGHT_MAGENTA ""
+#define LIGHT_CYAN ""
+#define LIGHT_WHITE ""
+#define LIGHT_DEFAULT ""
+#define BOLDBLACK ""
+#define BOLDRED ""
+#define BOLDGREEN ""
+#define BOLDYELLOW ""
+#define BOLDBLUE ""
+#define BOLDMAGENTA ""
+#define BOLDCYAN ""
+#define BOLDWHITE ""
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 "")
+export BOLD=$(printf "")
+export BLACK=$(printf "")
+export RED=$(printf "")
+export GREEN=$(printf "")
+export YELLOW=$(printf "")
+export BLUE=$(printf "")
+export MAGENTA=$(printf "")
+export CYAN=$(printf "")
+export WHITE=$(printf "")
+export DEFAULT=$(printf "")
+export BG_BLACK=$(printf "")
+export BG_RED=$(printf "")
+export BG_GREEN=$(printf "")
+export BG_YELLOW=$(printf "")
+export BG_BLUE=$(printf "")
+export BG_MAGENTA=$(printf "")
+export BG_CYAN=$(printf "")
+export BG_WHITE=$(printf "")
+export BG_DEFAULT=$(printf "")
+export LIGHT_BLACK=$(printf "")
+export LIGHT_RED=$(printf "")
+export LIGHT_GREEN=$(printf "")
+export LIGHT_YELLOW=$(printf "")
+export LIGHT_BLUE=$(printf "")
+export LIGHT_MAGENTA=$(printf "")
+export LIGHT_CYAN=$(printf "")
+export LIGHT_WHITE=$(printf "")
+export LIGHT_DEFAULT=$(printf "")
+export BOLDBLACK=$(printf "")
+export BOLDRED=$(printf "")
+export BOLDGREEN=$(printf "")
+export BOLDYELLOW=$(printf "")
+export BOLDBLUE=$(printf "")
+export BOLDMAGENTA=$(printf "")
+export BOLDCYAN=$(printf "")
+export BOLDWHITE=$(printf "")
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 ""
+ 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;;
+ ''|''|' ')
+ sel=$(((sel+1)%btns_len));;
+ ''|'')
+ 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;;
+ ''|''|' ')
+ sel=$(((sel+1)%btns_len));;
+ ''|'')
+ 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
+ ''|''|' ')
+ sel=$(((sel+1)%btns_len))
+ ;;
+ ''|'')
+ 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
+}
+
+"$@"