quick-scripts/unish.sh

181 lines
3.6 KiB
Bash

#!/bin/bash
# # unish
#
# A bash script to have a limited interactive version of the `unicode` command to view the unicode info.
#
# ## Usage
#
# bash unish.sh symbol
# bash unish.sh 0xhexval
#
# where
#
# code-point-in-hex is like 0x1ff
#
# ## Navigation
#
# The default key configuration is:
#
# | Key | Effect |
# |------------------+-----------------|
# | j/k | +1/-1 |
# | J/K | +10/-10 |
# | h/l | +100/-100 |
# | H/L | +1000/-1000 |
# | C-f/C-v | +10000/-10000 |
# | Space/Backspace | +100000/-100000 |
#
# This can be changed by changing the variables in the beginning of the script.
#
# Press Esc or q to quit.
# Configure keys
n_1='j'
p_1='k'
n_10='J'
p_10='K'
n_100='l'
p_100='h'
n_1k='L'
p_1k='H'
n_10k=$'\6' # C-f
p_10k=$'\2' # C-b
n_100k=$'\0a' # Enter
p_100k=$'\177' # Backspace
# Unicode bounds
MINVAL=0x0
MAXVAL=0x10FFFF
function is_whole_num {
# Returns zero if argument has only digits
# Otherwise non-zero value is returned
if (( "$#" == 0 ))
then
echo "Error: is_whole_num (function): No arguments" >&2
return 1
fi
# https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash
case "$1" in
("" | *[!0-9]*)
return 1
esac
return 0
}
# Function change offset
function incr {
# Args: value, offset
# Return value: No change if new value is out of bounds,
# otherwise changed value
if (( "$#" < 2 ))
then
echo "Error: incr (function): Needs two arguments" >&2
return 1
fi
val="$1"
offset="$2"
nval=$((val + offset))
# Bring it back if value went out of bounds
if [[ "$nval" -gt "$MAXVAL" || "$nval" -lt "$MINVAL" ]]
then
nval="$val"
fi
# Return new value
echo "$nval"
}
# Obtain required arguments
if (( "$#" < 1 ))
then
chr=65
elif (( "$#" == 1 ))
then
chr="$1"
fi
# Initialize variable storing user response
response=
# The event loop
while [ "$response" != $'\E' ] && [ "$response" != "q" ]
do
# clear
# str="$(printf "0x%x" "$chr")"
# echo "str: $str"
echo -e "\n\nCodepoint: $chr\n"
unicode "$(printf "0x%x" "$chr")"
# Print a line
# https://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash
printf '=%.0s' {1..70}
read -rsn1 response
case "$response" in
"$n_1")
chr=$(incr "$chr" 1)
# echo "$chr"
;;
"$p_1")
chr=$(incr "$chr" -1)
# echo "$chr"
;;
"$n_10")
chr=$(incr "$chr" 10)
# echo "$chr"
;;
"$p_10")
chr=$(incr "$chr" -10)
# echo "$chr"
;;
"$n_100")
chr=$(incr "$chr" 100)
# echo "$chr"
;;
"$p_100")
chr=$(incr "$chr" -100)
# echo "$chr"
;;
"$n_1k")
chr=$(incr "$chr" 1000)
# echo "$chr"
;;
"$p_1k")
chr=$(incr "$chr" -1000)
# echo "$chr"
;;
"$n_10k")
chr=$(incr "$chr" 10000)
# echo "$chr"
;;
"$p_10k")
chr=$(incr "$chr" -10000)
# echo "$chr"
;;
"$n_100k")
chr=$(incr "$chr" 100000)
# echo "$chr"
;;
"$p_100k")
chr=$(incr "$chr" -100000)
# echo "$chr"
;;
*) # echo "$chr"
;;
esac
done
#
#
# # Todo:
# # - config file to customize key bindings
# # - add number modifier
# # - use getopt to accept other options of cal