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 # Define argument functions
26 echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
27 A simple terminal character selector
28 -a include hidden shortcodes
29 -b disable color support
30 -c check charfile validity
33 -L show installed charfiles without the message
34 -l show installed charfiles
35 -n dont copy character to clipboard, avoids Xclip dependency
40 1 charfile does not exist,
41 2 charfile syntax error,
42 3 usage error/invalid option,
46 CHARDIR=$HOME/.cache/charsel
52 cat /usr/share/doc/charsel/README.md
55 function missing_charfile() {
56 echo "The selected charfile is missing or invalid"
61 # Merge global and local charfiles
69 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
70 if [[ ! -d $LOCALCHARDIR ]]
72 mkdir -p $LOCALCHARDIR
75 localdirfiles=($LOCALCHARDIR/*)
76 if [[ ! -z "${#localdirfiles[*]}" ]]
78 cp -rf $LOCALCHARDIR/../* $CHARDIR
81 globaldirfiles=($/usr/share/charfiles/)
82 if [[ ! -z "${#globaldirfiles[*]}" ]]
84 cp -rf /usr/share/charsel/* $CHARDIR
87 # Check if charfile exists
88 CHARFILE="$CHARDIR/charfiles/$2"
89 function existence() {
90 if [[ ! -f $CHARFILE ]]
96 # VERY basic syntax check
98 if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
100 echo "valid charfile"
103 echo "invalid charfile"
109 while getopts ":abcdhlLnv" arg
131 echo "The following charfiles are installed:"
143 echo "charsel" $VERSION
147 echo "Invalid option"
155 if [ $# -eq 0 ]; then
156 echo "Please enter a valid charfile, or use charsel -h for help."
160 # Redefine charfile and check file validity
161 CHARFILE="$CHARDIR/charfiles/$1"
166 if [[ ! $COPY == "no" ]]
170 echo "Please install xclip."
175 if [[ $(echo $LANG | grep UTF-8) -ne 0 ]]
177 echo "Please enable unicode support"
181 # Define length of shortcode
182 LENGTH=$(cat $CHARFILE \
183 | cut -f 1 -d ',' -s \
189 # Define text formatting
193 if [[ $COLOR == bw ]]
195 magenta=$(tput setaf 7)
197 magenta=$(tput setaf 5)
200 white=$(tput setaf 7)
206 echo "*---------*---------------*"
207 echo "| ${bold}${magenta}CHARSEL${white}${normal} |" $1
208 echo "*---------*---------------*"
211 if [[ $SHOWALL == true ]]
213 cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
214 | column -t -N "input","output" --output-separator ' | ' --separator ','
216 cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
217 | column -t -N "input","output" --output-separator ' | ' --separator ','
221 echo "*-------------------------*"
222 echo "| previous shorcode:" $INPUT
223 echo "| previous output: " $OUTPUT
224 echo "*-------------------------*"
227 read -p "| ${bold}input shortcode: ${normal} " -N $LENGTH INPUT
229 if [[ $INPUT == ";"* ]] # Semicolon exts
233 elif [[ $INPUT == " "* ]] # Spaces dont count
238 # Finds line number of shortcode
239 LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
241 # Outputs character selected above
242 OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
244 # Check to see if the shortcode actually exists
245 if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
250 # Copy output to clipboard
251 if [[ ! $COPY == "no" ]]
253 echo -n $OUTPUT | xclip -selection clipboard