3 #######################################################################
5 # (C) Copyright Armaan Bhojwani, 2020
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 ########################################################################
24 # Environment variables
26 # Define argument functions
28 echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
29 A simple terminal character selector
30 -a include hidden shortcodes
31 -b disable color support
32 -c check charfile validity
35 -L show installed charfiles without the message
36 -l show installed charfiles
41 1 charfile does not exist,
42 2 charfile syntax error,
43 3 usage error/invalid option,
47 CHARDIR=$HOME/.cache/charsel
53 cat /usr/share/doc/charsel/README.md
56 function missing_charfile() {
57 echo "The selected charfile is missing or invalid"
62 # Merge global and local charfiles
70 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
71 if [[ ! -d $LOCALCHARDIR ]]
73 mkdir -p $LOCALCHARDIR
76 localdirfiles=($LOCALCHARDIR/*)
77 if [[ ! -z "${#localdirfiles[*]}" ]]
79 cp -rf $LOCALCHARDIR/../* $CHARDIR
82 globaldirfiles=($/usr/share/charfiles/)
83 if [[ ! -z "${#globaldirfiles[*]}" ]]
85 cp -rf /usr/share/charsel/* $CHARDIR
88 # Check if charfile exists
89 CHARFILE="$CHARDIR/charfiles/$2"
90 function existence() {
91 if [[ ! -f $CHARFILE ]]
97 # VERY basic syntax check
99 if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
101 echo "valid charfile"
104 echo "invalid charfile"
110 if [ $# -eq 0 ]; then
114 while getopts ":baLlchdv" arg
122 echo "The following charfiles are installed:"
135 echo "charsel" $VERSION
149 echo "Invalid option"
157 # Redefine charfile and check file validity
158 CHARFILE="$CHARDIR/charfiles/$1"
165 echo "Please install xclip."
169 if [[ $(echo $LANG | grep UTF-8) -ne 0 ]]
171 echo "Please enable unicode support"
175 # Define length of shortcode
176 LENGTH=$(cat $CHARFILE \
177 | cut -f 1 -d ',' -s \
183 # Define text formatting
187 if [[ $COLOR == bw ]]
189 magenta=$(tput setaf 7)
191 magenta=$(tput setaf 5)
194 white=$(tput setaf 7)
200 echo "*---------*---------------*"
201 echo "| ${bold}${magenta}CHARSEL${white}${normal} |" $1
202 echo "*---------*---------------*"
205 if [[ $SHOWALL == true ]]
207 cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
208 | column -t -N "input","output" --output-separator ' | ' --separator ','
210 cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
211 | column -t -N "input","output" --output-separator ' | ' --separator ','
215 echo "*-------------------------*"
216 echo "| previous shorcode:" $INPUT
217 echo "| previous output: " $OUTPUT
218 echo "*-------------------------*"
221 read -p "| ${bold}input shortcode: ${normal} " -N $LENGTH INPUT
223 if [[ $INPUT == ";"* ]] # Semicolon exts
227 elif [[ $INPUT == " "* ]] # Spaces dont count
232 # Finds line number of shortcode
233 LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
235 # Outputs character selected above
236 OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
238 # Check to see if the shortcode actually exists
239 if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
244 # Copy output to clipboard
245 echo -n $OUTPUT | xclip -selection clipboard