]> git.armaanb.net Git - charsel.git/blob - charsel
usability fixes
[charsel.git] / charsel
1 #!/usr/bin/env bash
2
3 # Merge both global and local charfiles
4 CHARDIR=~/.cache/charsel
5
6 if [[ ! -d $CHARDIR ]]
7 then
8   mkdir $CHARDIR/
9 elif [[ ! "$(ls -A $CHARDIR)" ]]
10 then
11   cp -rf /usr/share/charsel/- $CHARDIR/
12   cp -rf ~/.local/share/charsel/- $CHARDIR/
13 fi
14
15 # Check for user inputs
16 if [[ $1 == "list" ]]
17 then
18   echo "The following charfiles are installed"
19   ls $CHARDIR/charfiles
20   exit 0
21 elif [[ $1 == "help" || $1 == "-h" || $1 == "--help" || $# -ne 1 ]]
22 then
23   cat /usr/share/doc/charsel/README.md
24   exit 0
25 fi
26
27 # Define charfile path
28 CHARFILE=$CHARDIR/charfiles/$1
29
30 if [[ ! -f $CHARFILE ]]
31 then
32   echo "Please enter a valid charfile. You can list installed charfiles with \`charsel list\`"
33   exit 1
34 fi
35
36 # Define length of shortcut
37 LENGTH=$(cat $CHARFILE | cut -f 1 -d ',' -s | wc -L | cut -b 1)
38
39 # Clear screen
40 clear
41
42 # Main program
43 while :
44 do
45   # Format charfile into table, showing only the shortcuts below the divider in the charfile
46   cat $CHARFILE | grep -A 100 - | tail -n +2 | column -t -N input,output --output-separator ' | ' --separator ','
47
48   # Print a separator
49   echo ""
50   echo "--------------------------"
51   echo "enter the input shortcode here:"
52
53   # User input
54   read -N $LENGTH INPUT
55
56   # This can definately be simplifed, but it works fine
57   LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -wn $INPUT | cut -d : -f 1)
58   OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
59
60   # Copy output to clipboard
61   echo $OUTPUT | xclip -selection clipboard
62
63   # Clear screen
64   clear
65
66 done