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