]> git.armaanb.net Git - charsel.git/blob - charsel
update formatting, refactor
[charsel.git] / charsel
1 #!/usr/bin/env bash
2 # Charsel - terminal character selector
3 # Copyright Armaan Bhojwani 2020, MIT License. See the LICENSE file or
4 # https://opensource.org/licenses/MIT for more information
5
6 VERSION=2.0.8
7
8 # Define argument functions
9 function usage() {
10   echo "Usage: /usr/bin/charsel [OPTION]... [CHARFILE]...
11 A simple terminal character selector
12   -a         include hidden shortcodes
13   -b         disable color support
14   -c         check charfile validity
15   -d         show readme
16   -h         show this message
17   -L         show installed charfiles without the message
18   -l         show installed charfiles
19   -n         dont copy character to clipboard, avoids Xclip dependency
20   -V         check for updates
21   -v         print current version
22
23 Exit status:
24    0         okay,
25    1         charfile does not exist,
26    2         charfile syntax error,
27    3         usage error/invalid option,
28    4         other error"
29 }
30
31 function version_check(){
32   NEW_VERSION=$(curl -s https://codeberg.org/armaan/charsel/raw/branch/master/charsel \
33     | grep VERSION= | tail -c +9)
34   if [[ $VERSION != "$NEW_VERSION" ]]; then
35     echo "an update is available"
36   else
37     echo "you are up to date"
38   fi
39
40 }
41
42 CHARDIR=$HOME/.cache/charsel
43 function list() {
44   ls "$CHARDIR"/charfiles
45 }
46
47 function readme() {
48   cat /usr/share/doc/charsel/README.md
49 }
50
51 function missing_charfile() {
52   echo "The selected charfile is missing or invalid"
53   list
54   exit 1
55 }
56
57 # Merge global and local charfiles
58 if [[ -d $CHARDIR ]]; then
59   rm -rf "${CHARDIR:?}"/*
60 else
61   mkdir "$CHARDIR"
62 fi
63
64 LOCALCHARDIR=$HOME/.local/share/charsel/charfiles
65 [[ -d $LOCALCHARDIR ]] || mkdir -p "$LOCALCHARDIR"
66
67 localdirfiles="$LOCALCHARDIR"/*
68 [[ -n "${#localdirfiles[*]}" ]] && cp -rf "$LOCALCHARDIR"/../* "$CHARDIR"
69
70 globaldirfiles=($/usr/share/charfiles/)
71 [[ -n "${#globaldirfiles[*]}" ]] && cp -rf /usr/share/charsel/* "$CHARDIR"
72
73 # Check if charfile exists
74 CHARFILE="$CHARDIR/charfiles/$2"
75 function existence() {
76   if [[ ! -f $CHARFILE ]]; then
77     missing_charfile
78   fi
79 }
80
81 # VERY basic syntax check
82 function validity() {
83   if [[ $(grep , "$CHARFILE" && grep - "$CHARFILE") ]]; then
84     echo "valid charfile"
85   else
86     existence
87     echo "invalid charfile"
88     exit 2
89   fi
90 }
91
92 # Look for arguments
93 while getopts ":abcdhlLnvV" arg
94 do
95   case ${arg} in
96     a)
97       SHOWALL="true"
98       ;;
99     b)
100       COLOR="bw"
101       ;;
102     c)
103       validity
104       exit 0
105       ;;
106     d)
107       readme
108       exit 0
109       ;;
110     h)
111       usage
112       exit 0
113       ;;
114     l)
115       echo "The following charfiles are installed:"
116       list
117       exit 0
118       ;;
119     L)
120       list
121       exit 0
122       ;;
123     n)
124       COPY="no"
125       ;;
126     v)
127       echo "charsel" $VERSION
128       exit 0
129       ;;
130     V)
131       version_check
132       exit 0
133       ;;
134     ?)
135       echo "Invalid option"
136       usage
137       exit 3
138       ;;
139   esac
140 done
141 shift $((OPTIND-1))
142
143 if [ $# -eq 0 ]; then
144   echo "Please enter a valid charfile, or use charsel -h for help."
145   charsel -l
146   exit 3
147 fi
148 # Redefine charfile and check file validity
149 CHARFILE="$CHARDIR/charfiles/$1"
150 existence
151 validity
152
153 # Dependency check
154 if [[ $COPY == "no" ]]; then
155   if [[ -x /usr/bin/xclip ]]; then
156     echo "Please install xclip."
157     exit 4
158   fi
159 fi
160
161 # Define length of shortcode
162 LENGTH=$(cut "$CHARFILE" -f 1 -d ',' -s \
163   | wc -L \
164   | cut -b 1)
165
166 clear
167
168 # Define text formatting
169 bold=$(tput bold)
170 normal=$(tput sgr0)
171
172 magenta=$(tput setaf 5)
173 [[ $COLOR == bw ]] && magenta=$(tput setaf 7)
174
175 white=$(tput setaf 7)
176
177 # Main program
178 while true
179 do
180   # Format output
181   echo "*---------*---------------*"
182   echo "| ${bold}${magenta}CHARSEL${white}${normal} |" "$1"
183   echo "*---------*---------------*"
184   echo ""
185
186   if [[ $SHOWALL == true ]]; then
187     sed "$CHARFILE" -e '/^[ \t]*#/d' | tail -n +2 \
188       | column -t -N "input","output" --output-separator ' | ' --separator ','
189   else
190     sed "$CHARFILE" -e '/^[ \t]*#/d' | grep -A 100 - | tail -n +2 \
191       | column -t -N "input","output" --output-separator ' | ' --separator ','
192   fi
193
194   echo ""
195   echo "*-------------------------*"
196   echo "| previous shorcode:" "$(echo $INPUT)"
197   echo "| previous output:  " "$OUTPUT"
198   echo "*-------------------------*"
199
200   # User input
201   read -r -p "| ${bold}input shortcode: ${normal}  " -N "$LENGTH" INPUT
202
203   if [[ $INPUT == ";"* ]]; then # Semicolon exts
204     clear
205     exit 0
206   else
207     # Finds line number of shortcode
208     LINENUMBER=$(cut -f 1 -d ',' -s "$CHARFILE" | grep -wn "$INPUT" | cut -d : -f 1)
209
210     # Outputs character selected above
211     OUTPUT=$(cut -f 2 -d ',' -s "$CHARFILE" | head -"$LINENUMBER" | tail +"$LINENUMBER")
212
213     # Check to see if the shortcode actually exists
214     if [[ $(cut -f 1 -d ',' -s "$CHARFILE" | grep -wnc "$INPUT") == "0" ]]; then
215       OUTPUT=""
216     fi
217
218     # Copy output to clipboard
219     if [[ ! $COPY == "no" ]]; then
220       echo -n "$OUTPUT" | xclip -selection clipboard
221     fi
222
223     clear
224   fi
225 done