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 --all include hidden shortcodes
36 1 charfile does not exist,
37 2 charfile syntax error,
38 3 usage error/invalid option,
42 CHARDIR=$HOME/.cache/charsel
44 echo "The following charfiles are installed:"
49 cat /usr/share/doc/charsel/README.md
52 function invalid_charfile() {
53 echo "The selected charfile is missing or invalid"
58 # Check if Xclip is installed
61 echo "Please install xclip."
65 # Merge global and local charfiles
73 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
74 if [[ ! -d $LOCALCHARDIR ]]
76 mkdir -p $LOCALCHARDIR
79 localdirfiles=($LOCALCHARDIR/*)
80 if [[ ! -z "${#localdirfiles[*]}" ]]
82 cp -rf $LOCALCHARDIR/../* $CHARDIR
85 globaldirfiles=($/usr/share/charfiles/)
86 if [[ ! -z "${#globaldirfiles[*]}" ]]
88 cp -rf /usr/share/charsel/* $CHARDIR
92 while getopts ":lhdv" arg
108 echo "charsel" $VERSION
112 echo "Invalid option"
123 # Check if charfile exists
124 CHARFILE="$CHARDIR/charfiles/$1"
125 if [[ ! -f $CHARFILE ]]
130 # VERY basic syntax validation
131 if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
133 echo "valid charfile"
135 echo "invalid charfile"
139 # Define length of shortcode
140 LENGTH=$(cat $CHARFILE \
141 | cut -f 1 -d ',' -s \
151 echo "*---------*---------------*"
152 echo "| CHARSEL |" $1
153 echo "*---------*---------------*"
156 if [[ $2 == "--all" ]]
158 cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
159 | column -t -N input,output --output-separator ' | ' --separator ','
161 cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
162 | column -t -N input,output --output-separator ' | ' --separator ','
166 echo "*-------------------------*"
167 echo "| previous shorcode:" $INPUT
168 echo "| previous output: " $OUTPUT
169 echo "*-------------------------*"
172 read -p "| input shortcode: " -N $LENGTH INPUT
174 if [[ $INPUT == ";"* ]] # Semicolon exits
178 elif [[ $INPUT == " "* ]] # Spaces dont count
183 # Finds line number of shortcode
184 LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
186 # Outputs character selected above
187 OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
189 # Check to see if the shortcode actually exists
190 if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
195 # Copy output to clipboard
196 echo $OUTPUT | xclip -selection clipboard