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