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
37 -v print current version
41 1 charfile does not exist,
42 2 charfile syntax error,
43 3 usage error/invalid option,
47 function version_check(){
48 NEW_VERSION=$(curl -s https://codeberg.org/armaan/charsel/raw/branch/master/charsel \
49 | grep VERSION= | tail -c +9)
50 if [[ $VERSION != "$NEW_VERSION" ]]
52 echo "an update is available"
54 echo "you are up to date"
59 CHARDIR=$HOME/.cache/charsel
61 ls "$CHARDIR"/charfiles
65 cat /usr/share/doc/charsel/README.md
68 function missing_charfile() {
69 echo "The selected charfile is missing or invalid"
74 # Merge global and local charfiles
77 rm -rf "${CHARDIR:?}"/*
82 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
83 if [[ ! -d $LOCALCHARDIR ]]
85 mkdir -p "$LOCALCHARDIR"
88 localdirfiles="$LOCALCHARDIR"/*
89 if [[ -n "${#localdirfiles[*]}" ]]
91 cp -rf "$LOCALCHARDIR"/../* "$CHARDIR"
94 globaldirfiles=($/usr/share/charfiles/)
95 if [[ -n "${#globaldirfiles[*]}" ]]
97 cp -rf /usr/share/charsel/* "$CHARDIR"
100 # Check if charfile exists
101 CHARFILE="$CHARDIR/charfiles/$2"
102 function existence() {
103 if [[ ! -f $CHARFILE ]]
109 # VERY basic syntax check
110 function validity() {
111 if [[ $(grep , "$CHARFILE" && grep - "$CHARFILE") ]]
113 echo "valid charfile"
116 echo "invalid charfile"
122 while getopts ":abcdhlLnvV" arg
144 echo "The following charfiles are installed:"
156 echo "charsel" $VERSION
164 echo "Invalid option"
172 if [ $# -eq 0 ]; then
173 echo "Please enter a valid charfile, or use charsel -h for help."
177 # Redefine charfile and check file validity
178 CHARFILE="$CHARDIR/charfiles/$1"
183 if [[ ! $COPY == "no" ]]
187 echo "Please install xclip."
192 # Define length of shortcode
193 LENGTH=$(cut "$CHARFILE" -f 1 -d ',' -s \
199 # Define text formatting
203 if [[ $COLOR == bw ]]
205 magenta=$(tput setaf 7)
207 magenta=$(tput setaf 5)
210 white=$(tput setaf 7)
216 echo "*---------*---------------*"
217 echo "| ${bold}${magenta}CHARSEL${white}${normal} |" "$1"
218 echo "*---------*---------------*"
221 if [[ $SHOWALL == true ]]
223 sed "$CHARFILE" -e '/^[ \t]*#/d' | tail -n +2 \
224 | column -t -N "input","output" --output-separator ' | ' --separator ','
226 sed "$CHARFILE" -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
227 | column -t -N "input","output" --output-separator ' | ' --separator ','
231 echo "*-------------------------*"
232 echo "| previous shorcode:" "$INPUT"
233 echo "| previous output: " "$OUTPUT"
234 echo "*-------------------------*"
237 read -p "| ${bold}input shortcode: ${normal} " -N "$LENGTH" INPUT
239 if [[ $INPUT == ";"* ]] # Semicolon exts
243 elif [[ $INPUT == " "* ]] # Spaces dont count
248 # Finds line number of shortcode
249 LINENUMBER=$(cut -f 1 -d ',' -s "$CHARFILE" | grep -wn "$INPUT" | cut -d : -f 1)
251 # Outputs character selected above
252 OUTPUT=$(cut -f 2 -d ',' -s "$CHARFILE" | head -"$LINENUMBER" | tail +"$LINENUMBER")
254 # Check to see if the shortcode actually exists
255 if [[ $(cut -f 1 -d ',' -s "$CHARFILE" | grep -wnc "$INPUT") == "0" ]]
260 # Copy output to clipboard
261 if [[ ! $COPY == "no" ]]
263 echo -n "$OUTPUT" | xclip -selection clipboard