]> git.armaanb.net Git - charsel.git/blob - charsel
made syntax checker more usable
[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.1
23
24 # Define argument functions
25 function usage() {
26   echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
27 A simple terminal character selector
28   -h              show this message
29   -l              show installed charfiles
30   -d              show readme
31   -v              print version
32   -c              check charfile validity
33       --all       include hidden shortcodes
34
35 Exit status:
36   0    okay,
37   1    charfile does not exist,
38   2    charfile syntax error,
39   3    usage error/invalid option,
40   4    other error"
41 }
42
43 CHARDIR=$HOME/.cache/charsel
44 function list() {
45   echo "The following charfiles are installed:"
46   ls $CHARDIR/charfiles
47 }
48
49 function readme() {
50   cat /usr/share/doc/charsel/README.md
51 }
52
53 function missing_charfile() {
54   echo "The selected charfile is missing or invalid"
55   list
56   exit 1
57 }
58
59 # Merge global and local charfiles
60 if [[ -d $CHARDIR ]]
61 then
62   rm -rf $CHARDIR/*
63 else
64   mkdir $CHARDIR
65 fi
66
67 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
68 if [[ ! -d $LOCALCHARDIR ]]
69 then
70   mkdir -p $LOCALCHARDIR
71 fi
72
73 localdirfiles=($LOCALCHARDIR/*)
74 if [[ ! -z "${#localdirfiles[*]}" ]]
75 then
76   cp -rf $LOCALCHARDIR/../* $CHARDIR
77 fi
78
79 globaldirfiles=($/usr/share/charfiles/)
80 if [[ ! -z "${#globaldirfiles[*]}" ]]
81 then
82   cp -rf /usr/share/charsel/* $CHARDIR
83 fi
84
85 # Check if charfile exists
86 CHARFILE="$CHARDIR/charfiles/$2"
87 function existence() {
88   if [[ ! -f $CHARFILE ]]
89   then
90     missing_charfile
91   fi
92 }
93
94 # VERY basic syntax check
95 function validity() {
96   if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
97   then
98     echo "valid charfile"
99   else
100     existence
101     echo "invalid charfile"
102     exit 2
103   fi
104 }
105
106 # Look for arguments
107 while getopts ":lchdv" arg
108 do
109   case ${arg} in
110     h)
111       usage
112       exit 0
113       ;;
114     l)
115       list
116       exit 0
117       ;;
118     d)
119       readme
120       exit 0
121       ;;
122     v)
123       echo "charsel" $VERSION
124       exit 0
125       ;;
126     c)
127       validity
128       exit 0
129       ;;
130     ?)
131       echo "Invalid option"
132       usage
133       exit 3
134       ;;
135     :)
136       break
137       ;;
138   esac
139 done
140 shift $((OPTIND-1))
141
142 # Redefine charfile and check file validity
143 CHARFILE="$CHARDIR/charfiles/$1"
144 existence
145 validity
146
147 # Dependency check
148 if [[ -x xclip ]]
149 then
150   echo "Please install xclip."
151   exit 4
152 fi
153
154 # Define length of shortcode
155 LENGTH=$(cat $CHARFILE \
156   | cut -f 1 -d ',' -s \
157   | wc -L \
158   | cut -b 1)
159
160 clear
161
162 # Main program
163 while true
164 do
165   # Format output
166   echo "*---------*---------------*"
167   echo "| CHARSEL |" $1
168   echo "*---------*---------------*"
169   echo ""
170
171   if [[ $2 == "--all" ]]
172   then
173     cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
174       | column -t -N input,output --output-separator ' | ' --separator ','
175   else
176     cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
177       | column -t -N input,output --output-separator ' | ' --separator ','
178   fi
179
180   echo ""
181   echo "*-------------------------*"
182   echo "| previous shorcode:" $INPUT
183   echo "| previous output:  " $OUTPUT
184   echo "*-------------------------*"
185
186   # User input
187   read -p "| input shortcode:   " -N $LENGTH INPUT
188
189   if [[ $INPUT == ";"* ]] # Semicolon exits
190   then
191     clear
192     exit 0
193   elif [[ $INPUT == " "* ]]  # Spaces dont count
194   then
195     OUTPUT=""
196     clear
197   else
198     # Finds line number of shortcode
199     LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
200
201     # Outputs character selected above
202     OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
203
204     # Check to see if the shortcode actually exists
205     if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
206     then
207       OUTPUT=""
208     fi
209
210     # Copy output to clipboard
211     echo $OUTPUT | xclip -selection clipboard
212
213     clear
214   fi
215 done