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