]> git.armaanb.net Git - charsel.git/blob - charsel
added ';' as an exit
[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 # Define argument functions
23 function usage() {
24   echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
25 A simple terminal character selector
26   -h              show this message
27   -l              show installed charfiles
28   -d              show readme
29       --all       include hidden shortcodes
30
31 Exit status:
32   0    okay,
33   1    charfile does not exist,
34   2    charfile syntax error,
35   3    usage error/invalid option,
36   4    other error"
37 }
38
39 CHARDIR=$HOME/.cache/charsel
40 function list() {
41   echo "The following charfiles are installed:"
42   ls $CHARDIR/charfiles
43 }
44
45 function readme() {
46   cat /usr/share/doc/charsel/README.md
47 }
48
49 function invalid_charfile() {
50   echo "The selected charfile is missing or invalid"
51   list
52   exit 1
53 }
54
55 # Check if Xclip is installed
56 if [[ -x xclip ]]
57 then
58   echo "Please install xclip."
59   exit 4
60 fi
61
62 # Merge global and local charfiles
63 if [[ -d $CHARDIR ]]
64 then
65   rm -rf $CHARDIR/*
66 else
67   mkdir $CHARDIR
68 fi
69
70 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
71 if [[ ! -d $LOCALCHARDIR ]]
72 then
73   mkdir -p $LOCALCHARDIR
74 fi
75
76 localdirfiles=($LOCALCHARDIR/*)
77 if [[ ! -z "${#localdirfiles[*]}" ]]
78 then
79   cp -rf $LOCALCHARDIR/../* $CHARDIR
80 fi
81
82 globaldirfiles=($/usr/share/charfiles/)
83 if [[ ! -z "${#globaldirfiles[*]}" ]]
84 then
85   cp -rf /usr/share/charsel/* $CHARDIR
86 fi
87
88 # Look for arguments
89 while getopts ":lhd" arg
90 do
91   case ${arg} in
92     h)
93       usage
94       exit 0
95       ;;
96     l)
97       list
98       exit 0
99       ;;
100     d)
101       readme
102       exit 0
103       ;;
104     ?)
105       echo "Invalid option"
106       usage
107       exit 3
108       ;;
109     :)
110       break
111       ;;
112   esac
113 done
114 shift $((OPTIND-1))
115
116 # Check if charfile exists
117 CHARFILE="$CHARDIR/charfiles/$1"
118 if [[ ! -f $CHARFILE ]]
119 then
120   invalid_charfile
121 fi
122
123 # VERY basic syntax validation
124 if [[ $(grep , $CHARFILE && grep - $CHARFILE) ]]
125 then
126   echo "valid charfile"
127 else
128   echo "invalid charfile"
129   exit 2
130 fi
131
132 # Define length of shortcode
133 LENGTH=$(cat $CHARFILE \
134   | cut -f 1 -d ',' -s \
135   | wc -L \
136   | cut -b 1)
137
138 clear
139
140 # Main program
141 while true
142 do
143   # Format output
144   echo "*---------*---------------*"
145   echo "| CHARSEL |" $1
146   echo "*---------*---------------*"
147   echo ""
148
149   if [[ $2 == "--all" ]]
150   then
151     cat $CHARFILE | sed -e '/^[ \t]*#/d' | tail -n +2 \
152       | column -t -N input,output --output-separator ' | ' --separator ','
153   else
154     cat $CHARFILE | sed -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
155       | column -t -N input,output --output-separator ' | ' --separator ','
156   fi
157
158   echo ""
159   echo "*-------------------------*"
160   echo "| previous shorcode:" $INPUT
161   echo "| previous output:  " $OUTPUT
162   echo "*-------------------------*"
163
164   # User input
165   read -p "| input shortcode:   " -N $LENGTH INPUT
166
167   # This can definately be simplifed, but it works fine
168   # Finds line number of shortcode
169   LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
170
171   # Outputs character selected above
172   OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
173
174   # Check to see if the shortcode actually exists
175   if [[ $(cut -f 1 -d ',' -s $CHARFILE | grep -wnc $INPUT) == "0" ]]
176   then
177     OUTPUT=""
178   fi
179
180   if [[ $INPUT == ";"* ]]
181   then
182     clear
183     exit 0
184   fi
185
186   # Copy output to clipboard
187   echo $OUTPUT | xclip -selection clipboard
188
189   clear
190 done