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