]> git.armaanb.net Git - charsel.git/blob - charsel
updated readme
[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 [[ ! -z "${#localdirfiles[*]}" ]]
90 then
91   cp -rf $LOCALCHARDIR/../* $CHARDIR
92 fi
93
94 globaldirfiles=($/usr/share/charfiles/)
95 if [[ ! -z "${#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 if [[ $(echo $LANG | grep UTF-8) -ne 0 ]]
193 then
194   echo "Please enable unicode support"
195   exit 4
196 fi
197
198 # Define length of shortcode
199 LENGTH=$(cat $CHARFILE \
200   | cut -f 1 -d ',' -s \
201   | wc -L \
202   | cut -b 1)
203
204 clear
205
206 # Define text formatting
207 bold=$(tput bold)
208 normal=$(tput sgr0)
209
210 if [[ $COLOR == bw ]]
211 then
212   magenta=$(tput setaf 7)
213 else
214   magenta=$(tput setaf 5)
215 fi
216
217 white=$(tput setaf 7)
218
219 # Main program
220 while true
221 do
222   # Format output
223   echo "*---------*---------------*"
224   echo "| ${bold}${magenta}CHARSEL${white}${normal} |" $1
225   echo "*---------*---------------*"
226   echo ""
227
228   if [[ $SHOWALL == true ]]
229   then
230     cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
231       | column -t -N "input","output" --output-separator ' | ' --separator ','
232   else
233     cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
234       | column -t -N "input","output" --output-separator ' | ' --separator ','
235   fi
236
237   echo ""
238   echo "*-------------------------*"
239   echo "| previous shorcode:" $INPUT
240   echo "| previous output:  " $OUTPUT
241   echo "*-------------------------*"
242
243   # User input
244   read -p "| ${bold}input shortcode: ${normal}  " -N $LENGTH INPUT
245
246   if [[ $INPUT == ";"* ]] # Semicolon exts
247   then
248     clear
249     exit 0
250   elif [[ $INPUT == " "* ]]  # Spaces dont count
251   then
252     OUTPUT=""
253     clear
254   else
255     # Finds line number of shortcode
256     LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
257
258     # Outputs character selected above
259     OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
260
261     # Check to see if the shortcode actually exists
262     if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
263     then
264       OUTPUT=""
265     fi
266
267     # Copy output to clipboard
268     if [[ ! $COPY == "no" ]]
269     then
270       echo -n $OUTPUT | xclip -selection clipboard
271     fi
272
273     clear
274   fi
275 done