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 ########################################################################
22 # Define argument functions
24 echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
25 A simple terminal character selector
27 -l show installed charfiles
29 --all include hidden shortcodes
33 1 charfile does not exist,
34 2 charfile syntax error,
35 3 usage error/invalid option,
39 CHARDIR=$HOME/.cache/charsel
41 echo "The following charfiles are installed:"
46 cat /usr/share/doc/charsel/README.md
49 function invalid_charfile() {
50 echo "The selected charfile is missing or invalid"
55 # Check if Xclip is installed
58 echo "Please install xclip."
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
89 while getopts ":lhd" arg
105 echo "Invalid option"
116 # Check if charfile exists
117 CHARFILE="$CHARDIR/charfiles/$1"
118 if [[ ! -f $CHARFILE ]]
123 # VERY basic syntax validation
124 if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
126 echo "valid charfile"
128 echo "invalid charfile"
132 # Define length of shortcode
133 LENGTH=$(cat $CHARFILE \
134 | cut -f 1 -d ',' -s \
144 echo "*---------*---------------*"
145 echo "| CHARSEL |" $1
146 echo "*---------*---------------*"
149 if [[ $2 == "--all" ]]
151 cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
152 | column -t -N input,output --output-separator ' | ' --separator ','
154 cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
155 | column -t -N input,output --output-separator ' | ' --separator ','
159 echo "*-------------------------*"
160 echo "| previous shorcode:" $INPUT
161 echo "| previous output: " $OUTPUT
162 echo "*-------------------------*"
165 read -p "| input shortcode: " -N $LENGTH INPUT
167 # This can definately be simplifed, but it works fine
168 # Finds line number of shortcode
169 LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
171 # Outputs character selected above
172 OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
174 # Check to see if the shortcode actually exists
175 if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
180 if [[ $INPUT == ";"* ]]
186 # Copy output to clipboard
187 echo $OUTPUT | xclip -selection clipboard