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
34 2 usage error/invalid option"
37 CHARDIR=$HOME/.cache/charsel
39 echo "The following charfiles are installed:"
44 cat /usr/share/doc/charsel/README.md
47 function invalid_charfile() {
48 echo "The selected charfile is missing or invalid"
54 while getopts ":lhd" arg
81 # Check if Xclip is installed
84 echo "Please install xclip."
88 # Merge global and local charfiles
96 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
97 if [[ ! -d $LOCALCHARDIR ]]
99 mkdir -p $LOCALCHARDIR
102 localdirfiles=($LOCALCHARDIR/*)
103 if [[ ! -z "${#localdirfiles[*]}" ]]
105 cp -rf $LOCALCHARDIR/../* $CHARDIR
108 globaldirfiles=($/usr/share/charfiles/)
109 if [[ ! -z "${#globaldirfiles[*]}" ]]
111 cp -rf /usr/share/charsel/* $CHARDIR
114 # Check if charfile exists
115 CHARFILE="$CHARDIR/charfiles/$1"
116 if [[ ! -f $CHARFILE ]]
121 # Define length of shortcode
122 LENGTH=$(cat $CHARFILE \
123 | cut -f 1 -d ',' -s \
133 echo "*---------*---------------*"
134 echo "| CHARSEL |" $1
135 echo "*---------*---------------*"
138 if [[ $2 == "--all" ]]
140 cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
141 | column -t -N input,output --output-separator ' | ' --separator ','
143 cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
144 | column -t -N input,output --output-separator ' | ' --separator ','
148 echo "*-------------------------*"
149 echo "| previous shorcode:" $INPUT
150 echo "| previous output: " $OUTPUT
151 echo "*-------------------------*"
154 read -p "| input shortcode: " -N $LENGTH INPUT
156 # This can definately be simplifed, but it works fine
157 # Finds line number of shortcode
158 LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
160 # Outputs character selected above
161 OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
163 # Check to see if the shortcode actually exists
164 if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
169 # Copy output to clipboard
170 echo $OUTPUT | xclip -selection clipboard