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