2 # Charsel - terminal character selector
3 # Copyright Armaan Bhojwani 2020, MIT License
5 # Define argument functions
7 echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
9 A simple terminal character selector
10 -a include hidden shortcodes
11 -b disable color support
12 -c check charfile validity
15 -L show installed charfiles without the message
16 -l show installed charfiles
17 -n dont copy character to clipboard, avoids Xclip dependency
21 1 charfile does not exist,
22 2 charfile syntax error,
23 3 usage error/invalid option,
27 CHARDIR=$HOME/.cache/charsel
29 ls "$CHARDIR"/charfiles
33 cat /usr/share/doc/charsel/README.md
36 function missing_charfile() {
37 echo "The selected charfile is missing or invalid"
42 # Merge global and local charfiles
43 if [[ -d $CHARDIR ]]; then
44 rm -rf "${CHARDIR:?}"/*
49 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
50 [[ -d $LOCALCHARDIR ]] || mkdir -p "$LOCALCHARDIR"
52 localdirfiles="$LOCALCHARDIR"/*
53 [[ -n "${#localdirfiles[*]}" ]] && cp -rf "$LOCALCHARDIR"/../* "$CHARDIR"
55 globaldirfiles=($/usr/share/charfiles/)
56 [[ -n "${#globaldirfiles[*]}" ]] && cp -rf /usr/share/charsel/* "$CHARDIR"
58 # Check if charfile exists
59 CHARFILE="$CHARDIR/charfiles/$2"
60 function existence() {
61 if [[ ! -f $CHARFILE ]]; then
66 # VERY basic syntax check
68 if [[ $(grep , "$CHARFILE" && grep - "$CHARFILE") ]]; then
72 echo "invalid charfile"
78 while getopts ":abcdhlLnvV" arg
100 echo "The following charfiles are installed:"
112 echo "Invalid option"
120 if [ $# -eq 0 ]; then
121 echo "Please enter a valid charfile, or use charsel -h for help."
126 # Redefine charfile and check file validity
127 CHARFILE="$CHARDIR/charfiles/$1"
132 if [[ $COPY == "no" ]]; then
133 if [[ ! -x /usr/bin/xclip ]]; then
134 echo "Please install xclip."
139 # Define length of shortcode
140 LENGTH=$(cut "$CHARFILE" -f 1 -d ',' -s \
146 # Define text formatting
150 highlight=$(tput setaf 5)
151 [[ $COLOR == bw ]] && highlight=$(tput setaf 7)
153 white=$(tput setaf 7)
159 echo "*---------*---------------*"
160 echo "| ${bold}${highlight}CHARSEL${white}${normal} |" "$1"
161 echo "*---------*---------------*"
164 if [[ $SHOWALL == true ]]; then
165 sed "$CHARFILE" -e '/^[ \t]*#/d' | tail -n +2 \
166 | column -t -N "input","output" --output-separator ' | ' --separator ','
168 sed "$CHARFILE" -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
169 | column -t -N "input","output" --output-separator ' | ' --separator ','
173 echo "*-------------------------*"
174 echo "| previous shorcode:" "$(echo $INPUT)"
175 echo "| previous output: " "$OUTPUT"
176 echo "*-------------------------*"
179 read -r -p "| ${bold}input shortcode: ${normal} " -N "$LENGTH" INPUT
181 if [[ $INPUT == ";"* ]]; then # Semicolon exts
185 # Finds line number of shortcode
186 LINENUMBER=$(cut -f 1 -d ',' -s "$CHARFILE" | grep -wn "$INPUT" | cut -d : -f 1)
188 # Outputs character selected above
189 OUTPUT=$(cut -f 2 -d ',' -s "$CHARFILE" | head -"$LINENUMBER" | tail +"$LINENUMBER")
191 # Check to see if the shortcode actually exists
192 if [[ $(cut -f 1 -d ',' -s "$CHARFILE" | grep -wnc "$INPUT") == "0" ]]; then
196 # Copy output to clipboard
197 if [[ ! $COPY == "no" ]]; then
198 echo -n "$OUTPUT" | xclip -selection clipboard