]> git.armaanb.net Git - charsel.git/blob - charsel
Added -n, reorganized flags
[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 if [ $# -eq 0 ]; then
110   usage
111   exit 3
112 fi
113 while getopts ":abcdhlLnv" arg
114 do
115   case ${arg} in
116     a)
117       SHOWALL="true"
118       ;;
119     b)
120       COLOR="bw"
121       ;;
122     c)
123       validity
124       exit 0
125       ;;
126     d)
127       readme
128       exit 0
129       ;;
130     h)
131       usage
132       exit 0
133       ;;
134     l)
135       echo "The following charfiles are installed:"
136       list
137       exit 0
138       ;;
139     L)
140       list
141       exit 0
142       ;;
143     n)
144       COPY="no"
145       ;; 
146     v)
147       echo "charsel" $VERSION
148       exit 0
149       ;;
150     ?)
151       echo "Invalid option"
152       usage
153       exit 3
154       ;;
155   esac
156 done
157 shift $((OPTIND-1))
158
159 # Redefine charfile and check file validity
160 CHARFILE="$CHARDIR/charfiles/$1"
161 existence
162 validity
163
164 # Dependency check
165 if [[ ! $COPY == "no" ]]
166 then
167   if [[ -x xclip ]]
168   then
169     echo "Please install xclip."
170     exit 4
171   fi
172 fi
173
174 if [[ $(echo $LANG | grep UTF-8) -ne 0 ]]
175 then
176   echo "Please enable unicode support"
177   exit 4
178 fi
179
180 # Define length of shortcode
181 LENGTH=$(cat $CHARFILE \
182   | cut -f 1 -d ',' -s \
183   | wc -L \
184   | cut -b 1)
185
186 clear
187
188 # Define text formatting
189 bold=$(tput bold)
190 normal=$(tput sgr0)
191
192 if [[ $COLOR == bw ]]
193 then
194   magenta=$(tput setaf 7)
195 else
196   magenta=$(tput setaf 5)
197 fi
198
199 white=$(tput setaf 7)
200
201 # Main program
202 while true
203 do
204   # Format output
205   echo "*---------*---------------*"
206   echo "| ${bold}${magenta}CHARSEL${white}${normal} |" $1
207   echo "*---------*---------------*"
208   echo ""
209
210   if [[ $SHOWALL == true ]]
211   then
212     cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
213       | column -t -N "input","output" --output-separator ' | ' --separator ','
214   else
215     cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
216       | column -t -N "input","output" --output-separator ' | ' --separator ','
217   fi
218
219   echo ""
220   echo "*-------------------------*"
221   echo "| previous shorcode:" $INPUT
222   echo "| previous output:  " $OUTPUT
223   echo "*-------------------------*"
224
225   # User input
226   read -p "| ${bold}input shortcode: ${normal}  " -N $LENGTH INPUT
227
228   if [[ $INPUT == ";"* ]] # Semicolon exts
229   then
230     clear
231     exit 0
232   elif [[ $INPUT == " "* ]]  # Spaces dont count
233   then
234     OUTPUT=""
235     clear
236   else
237     # Finds line number of shortcode
238     LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
239
240     # Outputs character selected above
241     OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
242
243     # Check to see if the shortcode actually exists
244     if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
245     then
246       OUTPUT=""
247     fi
248
249     # Copy output to clipboard
250     if [[ ! $COPY == "no" ]]
251     then
252       echo -n $OUTPUT | xclip -selection clipboard
253     fi
254
255     clear
256   fi
257 done