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