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