]> git.armaanb.net Git - charsel.git/blob - charsel
Minor changes
[charsel.git] / charsel
1 #!/usr/bin/env bash
2
3 # (C) Copyright Armaan Bhojwani, 2020
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18 # Merge both global and local charfiles
19
20 CHARDIR=~/.cache/charsel
21
22 if [[ ! -d $CHARDIR ]]
23 then
24   mkdir $CHARDIR/
25 elif [[ ! "$(ls -A $CHARDIR)" ]]
26 then
27   cp -rf /usr/share/charsel/- $CHARDIR/
28   cp -rf $HOME/.local/share/charsel/- $CHARDIR/
29 fi
30
31 # Check for user inputs
32 if [[ $1 == "list" \
33   || $1 == "-l" \
34   || $1 == "--list" ]]
35 then
36   echo "The following charfiles are installed"
37   ls $CHARDIR/charfiles
38   exit 0
39 elif [[ $1 == "help" \
40   || $1 == "-h" \
41   || $1 == "--help" \
42   || $# -ne 1 ]]
43 then
44   cat /usr/share/doc/charsel/README.md
45   exit 0
46 fi
47
48 # Define charfile path
49 CHARFILE=$CHARDIR/charfiles/$1
50
51 # Check if given charfile exists
52 if [[ ! -f $CHARFILE ]]
53 then
54   echo "Please enter a valid charfile."
55   charsel list
56   exit 1
57 fi
58
59 # Define length of shortcode
60 LENGTH=$(cat $CHARFILE \
61   | cut -f 1 -d ',' -s \
62   | wc -L \
63   | cut -b 1)
64
65 clear
66
67 # Main program
68 while :
69 do
70   # Format output
71   echo "*---------*---------------*"
72   echo "| CHARSEL | " $1
73   echo "*---------*---------------*"
74   echo ""
75
76   cat $CHARFILE \
77     | sed -e '/^[ \t]*#/d' \
78     | grep -A 100 - \
79     | tail -n +2 \
80     | column -t -N input,output --output-separator ' | ' --separator ','
81
82   echo ""
83   echo "*-------------------------*"
84   echo "| previous shorcode:" $INPUT
85   echo "| previous output:  " $OUTPUT
86   echo "*-------------------------*"
87   
88   # User input
89   read -p "| input shortcode:   " -N $LENGTH INPUT
90
91   # This can definately be simplifed, but it works fine
92   # Finds line number of shortcode
93   LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE \
94     | grep -wn $INPUT \
95     | cut -d : -f 1)
96   
97   # Outputs charachter selected above
98   OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE \
99     | head -$LINENUMBER \
100     | tail +$LINENUMBER)
101   
102   # Check to see if the shortcode actually exists
103   if [[ $(cut -f 1 -d ',' -s $CHARFILE \
104     | grep -wnc $INPUT) == "0" ]]
105   then
106     OUTPUT="" 
107   fi
108
109   # Copy output to clipboard
110   echo $OUTPUT | xclip -selection clipboard
111
112   clear
113
114 done