]> git.armaanb.net Git - charsel.git/blob - charsel
d2e7525e0c246a9b4a4a165d024e169ce22c9d74
[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 # Merge global and local charfiles
23 CHARDIR=$HOME/.cache/charsel
24
25 if [[ ! -d $CHARDIR ]]
26 then
27   mkdir $CHARDIR/
28 elif [[ ! "$(ls -A $CHARDIR)" ]]
29 then
30   cp -rf /usr/share/charsel/- $CHARDIR/
31   cp -rf $HOME/.local/share/charsel/- $CHARDIR/
32 fi
33
34 # Check for xclip
35 if [[ ! -f "$(which xclip)" ]]
36 then
37   echo "Please install xclip."
38   exit 1
39 fi
40
41 # Check for user inputs
42 if [[ $1 == "-l" \
43   || $1 == "--list" ]]
44 then
45   echo "The following charfiles are installed:"
46   ls $CHARDIR/charfiles
47   exit 0
48 elif [[ $1 == "-h" \
49   || $1 == "--help" ]]
50 then
51   cat /usr/share/doc/charsel/usage
52   exit 0
53 elif [[ $1 == "-d" \
54   || $1 == "--doc" ]]
55 then
56   cat /usr/share/doc/charsel/README.md
57   exit 0
58 fi
59
60 # Define charfile path
61 CHARFILE=$CHARDIR/charfiles/$1
62
63 # Check if given charfile exists
64 if [[ ! -f $CHARFILE ]]
65 then
66   echo "Please enter a valid charfile."
67   charsel -l
68   exit 1
69 fi
70
71 # Define length of shortcode
72 LENGTH=$(cat $CHARFILE \
73   | cut -f 1 -d ',' -s \
74   | wc -L \
75   | cut -b 1)
76
77 clear
78
79 # Main program
80 while true
81 do
82   # Format output
83   echo "*---------*---------------*"
84   echo "| CHARSEL |" $1
85   echo "*---------*---------------*"
86   echo ""
87
88   if [[ $2 == "--all" ]]
89   then
90     cat $CHARFILE \
91       | sed -e '/^[ \t]*#/d' \
92       | tail -n +2 \
93       | column -t -N input,output --output-separator ' | ' --separator ','
94   else
95     cat $CHARFILE \
96       | sed -e '/^[ \t]*#/d' \
97       | grep -A 100 - \
98       | tail -n +2 \
99       | column -t -N input,output --output-separator ' | ' --separator ','
100   fi
101
102   echo ""
103   echo "*-------------------------*"
104   echo "| previous shorcode:" $INPUT
105   echo "| previous output:  " $OUTPUT
106   echo "*-------------------------*"
107
108   # User input
109   read -p "| input shortcode:   " -N $LENGTH INPUT
110
111   # This can definately be simplifed, but it works fine
112   # Finds line number of shortcode
113   LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE \
114     | grep -wn $INPUT \
115     | cut -d : -f 1)
116
117   # Outputs charachter selected above
118   OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE \
119     | head -$LINENUMBER \
120     | tail +$LINENUMBER)
121
122   # Check to see if the shortcode actually exists
123   if [[ $(cut -f 1 -d ',' -s $CHARFILE \
124     | grep -wnc $INPUT) == "0" ]]
125   then
126     OUTPUT=""
127   fi
128
129   # Copy output to clipboard
130   echo $OUTPUT \
131     | xclip -selection clipboard
132
133   clear
134
135 done