2 # Charsel - terminal character selector
3 # Copyright Armaan Bhojwani 2020, MIT License. See the LICENSE file or
4 # https://opensource.org/licenses/MIT for more information
8 # Define argument functions
10 echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
11 A simple terminal character selector
12 -a include hidden shortcodes
13 -b disable color support
14 -c check charfile validity
17 -L show installed charfiles without the message
18 -l show installed charfiles
19 -n dont copy character to clipboard, avoids Xclip dependency
21 -v print current version
25 1 charfile does not exist,
26 2 charfile syntax error,
27 3 usage error/invalid option,
31 function version_check(){
32 NEW_VERSION=$(curl -s https://codeberg.org/armaan/charsel/raw/branch/master/charsel \
33 | grep VERSION= | tail -c +9)
34 if [[ $VERSION != "$NEW_VERSION" ]]; then
35 echo "an update is available"
37 echo "you are up to date"
42 CHARDIR=$HOME/.cache/charsel
44 ls "$CHARDIR"/charfiles
48 cat /usr/share/doc/charsel/README.md
51 function missing_charfile() {
52 echo "The selected charfile is missing or invalid"
57 # Merge global and local charfiles
58 if [[ -d $CHARDIR ]]; then
59 rm -rf "${CHARDIR:?}"/*
64 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
65 [[ -d $LOCALCHARDIR ]] || mkdir -p "$LOCALCHARDIR"
67 localdirfiles="$LOCALCHARDIR"/*
68 [[ -n "${#localdirfiles[*]}" ]] && cp -rf "$LOCALCHARDIR"/../* "$CHARDIR"
70 globaldirfiles=($/usr/share/charfiles/)
71 [[ -n "${#globaldirfiles[*]}" ]] && cp -rf /usr/share/charsel/* "$CHARDIR"
73 # Check if charfile exists
74 CHARFILE="$CHARDIR/charfiles/$2"
75 function existence() {
76 if [[ ! -f $CHARFILE ]]; then
81 # VERY basic syntax check
83 if [[ $(grep , "$CHARFILE" && grep - "$CHARFILE") ]]; then
87 echo "invalid charfile"
93 while getopts ":abcdhlLnvV" arg
115 echo "The following charfiles are installed:"
127 echo "charsel" $VERSION
135 echo "Invalid option"
143 if [ $# -eq 0 ]; then
144 echo "Please enter a valid charfile, or use charsel -h for help."
148 # Redefine charfile and check file validity
149 CHARFILE="$CHARDIR/charfiles/$1"
154 if [[ $COPY == "no" ]]; then
155 if [[ -x /usr/bin/xclip ]]; then
156 echo "Please install xclip."
161 # Define length of shortcode
162 LENGTH=$(cut "$CHARFILE" -f 1 -d ',' -s \
168 # Define text formatting
172 magenta=$(tput setaf 5)
173 [[ $COLOR == bw ]] && magenta=$(tput setaf 7)
175 white=$(tput setaf 7)
181 echo "*---------*---------------*"
182 echo "| ${bold}${magenta}CHARSEL${white}${normal} |" "$1"
183 echo "*---------*---------------*"
186 if [[ $SHOWALL == true ]]; then
187 sed "$CHARFILE" -e '/^[ \t]*#/d' | tail -n +2 \
188 | column -t -N "input","output" --output-separator ' | ' --separator ','
190 sed "$CHARFILE" -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
191 | column -t -N "input","output" --output-separator ' | ' --separator ','
195 echo "*-------------------------*"
196 echo "| previous shorcode:" "$(echo $INPUT)"
197 echo "| previous output: " "$OUTPUT"
198 echo "*-------------------------*"
201 read -r -p "| ${bold}input shortcode: ${normal} " -N "$LENGTH" INPUT
203 if [[ $INPUT == ";"* ]]; then # Semicolon exts
207 # Finds line number of shortcode
208 LINENUMBER=$(cut -f 1 -d ',' -s "$CHARFILE" | grep -wn "$INPUT" | cut -d : -f 1)
210 # Outputs character selected above
211 OUTPUT=$(cut -f 2 -d ',' -s "$CHARFILE" | head -"$LINENUMBER" | tail +"$LINENUMBER")
213 # Check to see if the shortcode actually exists
214 if [[ $(cut -f 1 -d ',' -s "$CHARFILE" | grep -wnc "$INPUT") == "0" ]]; then
218 # Copy output to clipboard
219 if [[ ! $COPY == "no" ]]; then
220 echo -n "$OUTPUT" | xclip -selection clipboard