]> git.armaanb.net Git - charsel.git/blob - charsel
8270ff3ed0546463f220a653305507f846986a3d
[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   -h         show this message
31   -l         show installed charfiles
32   -L         show installed charfiles without the message
33   -d         show readme
34   -v         print version
35   -c         check charfile validity
36   -a         include hidden shortcodes
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 ":aLlchdv" arg
114 do
115   case ${arg} in
116     h)
117       usage
118       exit 0
119       ;;
120     l)
121       echo "The following charfiles are installed:"
122       list
123       exit 0
124       ;;
125     L)
126       list
127       exit 0
128       ;;
129     d)
130       readme
131       exit 0
132       ;;
133     v)
134       echo "charsel" $VERSION
135       exit 0
136       ;;
137     c)
138       validity
139       exit 0
140       ;;
141     a)
142       SHOWALL="true"
143       ;;
144     ?)
145       echo "Invalid option"
146       usage
147       exit 3
148       ;;
149   esac
150 done
151 shift $((OPTIND-1))
152
153 # Redefine charfile and check file validity
154 CHARFILE="$CHARDIR/charfiles/$1"
155 existence
156 validity
157
158 # Dependency check
159 if [[ -x xclip ]]
160 then
161   echo "Please install xclip."
162   exit 4
163 fi
164
165 # Define length of shortcode
166 LENGTH=$(cat $CHARFILE \
167   | cut -f 1 -d ',' -s \
168   | wc -L \
169   | cut -b 1)
170
171 clear
172
173 # Define text formatting
174 bold=$(tput bold)
175 normal=$(tput sgr0)
176
177 # Main program
178 while true
179 do
180   # Format output
181   echo "*---------*---------------*"
182   echo "| ${bold}CHARSEL${normal} |" $1
183   echo "*---------*---------------*"
184   echo ""
185
186   if [[ $SHOWALL == true ]]
187   then
188     cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
189       | column -t -N "input","output" --output-separator ' | ' --separator ','
190   else
191     cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
192       | column -t -N "input","output" --output-separator ' | ' --separator ','
193   fi
194
195   echo ""
196   echo "*-------------------------*"
197   echo "| previous shorcode:" $INPUT
198   echo "| previous output:  " $OUTPUT
199   echo "*-------------------------*"
200
201   # User input
202   read -p "| ${bold}input shortcode: ${normal}  " -N $LENGTH INPUT
203
204   if [[ $INPUT == ";"* ]] # Semicolon exts
205   then
206     clear
207     exit 0
208   elif [[ $INPUT == " "* ]]  # Spaces dont count
209   then
210     OUTPUT=""
211     clear
212   else
213     # Finds line number of shortcode
214     LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
215
216     # Outputs character selected above
217     OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
218
219     # Check to see if the shortcode actually exists
220     if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
221     then
222       OUTPUT=""
223     fi
224
225     # Copy output to clipboard
226     echo -n $OUTPUT | xclip -selection clipboard
227
228     clear
229   fi
230 done