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
29 -l show installed charfiles
32 -c check charfile validity
33 -a include hidden shortcodes
37 1 charfile does not exist,
38 2 charfile syntax error,
39 3 usage error/invalid option,
43 CHARDIR=$HOME/.cache/charsel
45 echo "The following charfiles are installed:"
50 cat /usr/share/doc/charsel/README.md
53 function missing_charfile() {
54 echo "The selected charfile is missing or invalid"
59 # Merge global and local charfiles
67 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
68 if [[ ! -d $LOCALCHARDIR ]]
70 mkdir -p $LOCALCHARDIR
73 localdirfiles=($LOCALCHARDIR/*)
74 if [[ ! -z "${#localdirfiles[*]}" ]]
76 cp -rf $LOCALCHARDIR/../* $CHARDIR
79 globaldirfiles=($/usr/share/charfiles/)
80 if [[ ! -z "${#globaldirfiles[*]}" ]]
82 cp -rf /usr/share/charsel/* $CHARDIR
85 # Check if charfile exists
86 CHARFILE="$CHARDIR/charfiles/$2"
87 function existence() {
88 if [[ ! -f $CHARFILE ]]
94 # VERY basic syntax check
96 if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
101 echo "invalid charfile"
107 if [ $# -eq 0 ]; then
111 while getopts ":alchdv" arg
127 echo "charsel" $VERSION
138 echo "Invalid option"
146 # Redefine charfile and check file validity
147 CHARFILE="$CHARDIR/charfiles/$1"
154 echo "Please install xclip."
158 # Define length of shortcode
159 LENGTH=$(cat $CHARFILE \
160 | cut -f 1 -d ',' -s \
166 # Define text formatting
174 echo "*---------*---------------*"
175 echo "| ${bold}CHARSEL${normal} |" $1
176 echo "*---------*---------------*"
179 if [[ $SHOWALL == true ]]
181 cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
182 | column -t -N "input","output" --output-separator ' | ' --separator ','
184 cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
185 | column -t -N "input","output" --output-separator ' | ' --separator ','
189 echo "*-------------------------*"
190 echo "| previous shorcode:" $INPUT
191 echo "| previous output: " $OUTPUT
192 echo "*-------------------------*"
195 read -p "| ${bold}input shortcode: ${normal} " -N $LENGTH INPUT
197 if [[ $INPUT == ";"* ]] # Semicolon exits
201 elif [[ $INPUT == " "* ]] # Spaces dont count
206 # Finds line number of shortcode
207 LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
209 # Outputs character selected above
210 OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
212 # Check to see if the shortcode actually exists
213 if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
218 # Copy output to clipboard
219 echo $OUTPUT | xclip -selection clipboard