]> git.armaanb.net Git - charsel.git/blob - charsel
800b964b55d63d71183b439088c9c2d051a705f5
[charsel.git] / charsel
1 #!/usr/bin/env bash
2
3 #######################################################################
4
5 # (C) Copyright Armaan Bhojwani, 2020
6
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.
11
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.
16
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/>.
19
20 ########################################################################
21
22 VERSION=2.0.8
23
24 # Define argument functions
25 function usage() {
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
31   -d         show readme
32   -h         show this message
33   -L         show installed charfiles without the message
34   -l         show installed charfiles
35   -n         dont copy character to clipboard, avoids Xclip dependency
36   -V         check for updates
37   -v         print current version
38
39 Exit status:
40    0         okay,
41    1         charfile does not exist,
42    2         charfile syntax error,
43    3         usage error/invalid option,
44    4         other error"
45 }
46
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" ]]
51   then
52     echo "an update is available"
53   else
54     echo "you are up to date"
55   fi
56
57 }
58
59 CHARDIR=$HOME/.cache/charsel
60 function list() {
61   ls "$CHARDIR"/charfiles
62 }
63
64 function readme() {
65   cat /usr/share/doc/charsel/README.md
66 }
67
68 function missing_charfile() {
69   echo "The selected charfile is missing or invalid"
70   list
71   exit 1
72 }
73
74 # Merge global and local charfiles
75 if [[ -d $CHARDIR ]]
76 then
77   rm -rf "${CHARDIR:?}"/*
78 else
79   mkdir "$CHARDIR"
80 fi
81
82 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
83 if [[ ! -d $LOCALCHARDIR ]]
84 then
85   mkdir -p "$LOCALCHARDIR"
86 fi
87
88 localdirfiles="$LOCALCHARDIR"/*
89 if [[ -n "${#localdirfiles[*]}" ]]
90 then
91   cp -rf "$LOCALCHARDIR"/../* "$CHARDIR"
92 fi
93
94 globaldirfiles=($/usr/share/charfiles/)
95 if [[ -n "${#globaldirfiles[*]}" ]]
96 then
97   cp -rf /usr/share/charsel/* "$CHARDIR"
98 fi
99
100 # Check if charfile exists
101 CHARFILE="$CHARDIR/charfiles/$2"
102 function existence() {
103   if [[ ! -f $CHARFILE ]]
104   then
105     missing_charfile
106   fi
107 }
108
109 # VERY basic syntax check
110 function validity() {
111   if [[ $(grep , "$CHARFILE" && grep - "$CHARFILE") ]]
112   then
113     echo "valid charfile"
114   else
115     existence
116     echo "invalid charfile"
117     exit 2
118   fi
119 }
120
121 # Look for arguments
122 while getopts ":abcdhlLnvV" arg
123 do
124   case ${arg} in
125     a)
126       SHOWALL="true"
127       ;;
128     b)
129       COLOR="bw"
130       ;;
131     c)
132       validity
133       exit 0
134       ;;
135     d)
136       readme
137       exit 0
138       ;;
139     h)
140       usage
141       exit 0
142       ;;
143     l)
144       echo "The following charfiles are installed:"
145       list
146       exit 0
147       ;;
148     L)
149       list
150       exit 0
151       ;;
152     n)
153       COPY="no"
154       ;; 
155     v)
156       echo "charsel" $VERSION
157       exit 0
158       ;;
159     V)
160       version_check
161       exit 0
162       ;;
163     ?)
164       echo "Invalid option"
165       usage
166       exit 3
167       ;;
168   esac
169 done
170 shift $((OPTIND-1))
171
172 if [ $# -eq 0 ]; then
173   echo "Please enter a valid charfile, or use charsel -h for help."
174   charsel -l
175   exit 3
176 fi
177 # Redefine charfile and check file validity
178 CHARFILE="$CHARDIR/charfiles/$1"
179 existence
180 validity
181
182 # Dependency check
183 if [[ ! $COPY == "no" ]]
184 then
185   if [[ -x xclip ]]
186   then
187     echo "Please install xclip."
188     exit 4
189   fi
190 fi
191
192 # Define length of shortcode
193 LENGTH=$(cut "$CHARFILE" -f 1 -d ',' -s \
194   | wc -L \
195   | cut -b 1)
196
197 clear
198
199 # Define text formatting
200 bold=$(tput bold)
201 normal=$(tput sgr0)
202
203 if [[ $COLOR == bw ]]
204 then
205   magenta=$(tput setaf 7)
206 else
207   magenta=$(tput setaf 5)
208 fi
209
210 white=$(tput setaf 7)
211
212 # Main program
213 while true
214 do
215   # Format output
216   echo "*---------*---------------*"
217   echo "| ${bold}${magenta}CHARSEL${white}${normal} |" "$1"
218   echo "*---------*---------------*"
219   echo ""
220
221   if [[ $SHOWALL == true ]]
222   then
223     sed "$CHARFILE" -e '/^[ \t]*#/d' | tail -n +2 \
224       | column -t -N "input","output" --output-separator ' | ' --separator ','
225   else
226     sed "$CHARFILE" -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
227       | column -t -N "input","output" --output-separator ' | ' --separator ','
228   fi
229
230   echo ""
231   echo "*-------------------------*"
232   echo "| previous shorcode:" "$INPUT"
233   echo "| previous output:  " "$OUTPUT"
234   echo "*-------------------------*"
235
236   # User input
237   read -p "| ${bold}input shortcode: ${normal}  " -N "$LENGTH" INPUT
238
239   if [[ $INPUT == ";"* ]] # Semicolon exts
240   then
241     clear
242     exit 0
243   elif [[ $INPUT == " "* ]]  # Spaces dont count
244   then
245     OUTPUT=""
246     clear
247   else
248     # Finds line number of shortcode
249     LINENUMBER=$(cut -f 1 -d ',' -s "$CHARFILE" | grep -wn "$INPUT" | cut -d : -f 1)
250
251     # Outputs character selected above
252     OUTPUT=$(cut -f 2 -d ',' -s "$CHARFILE" | head -"$LINENUMBER" | tail +"$LINENUMBER")
253
254     # Check to see if the shortcode actually exists
255     if [[ $(cut -f 1 -d ',' -s "$CHARFILE" | grep -wnc "$INPUT") == "0" ]]
256     then
257       OUTPUT=""
258     fi
259
260     # Copy output to clipboard
261     if [[ ! $COPY == "no" ]]
262     then
263       echo -n "$OUTPUT" | xclip -selection clipboard
264     fi
265
266     clear
267   fi
268 done