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