]> git.armaanb.net Git - charsel.git/blob - src/charsel
fixed excess copying
[charsel.git] / src / charsel
1 #!/usr/bin/env bash
2 # Merge both global and local charfiles
3 CHARDIR=~/.cache/charsel
4
5 if [[ ! "$(ls -A $CHARDIR)" || ! -d $CHARDIR ]]; then
6   mkdir $CHARDIR/
7   cp -R /usr/share/charsel/* $CHARDIR/
8   cp -R ~/.local/share/charsel/* $CHARDIR/
9 fi
10
11 # Check if user provided an input
12 if [ $1 = "list" ]; then
13   echo "The following charfiles are installed"
14   ls $CHARDIR/charfiles
15   exit
16 elif [[ $1 = "help" || $1 = "-h" || $1 = "--help" || $# -ne 1 ]]; then
17   cat /usr/share/doc/charsel/README.md
18   exit
19 else
20
21 # Clear screen
22 clear
23
24 # Define charfile path
25 CHARFILE=$CHARDIR/charfiles/$1
26
27 # Define length of shortcut
28 LENGTH=$(cat $CHARFILE | cut -f 1 -d ',' -s | wc -L | cut -b 1)
29
30 # MOTD
31 cat $CHARDIR/motd
32
33 # Main program
34 while :
35 do
36   # Put charfile into table, showing only the shortcuts below the divider in the charfile
37   cat $CHARFILE | grep -A 100 - | tail -n +2 | column -t  --output-separator ' | ' --separator ','
38
39   # Automatically enter input
40   read -N $LENGTH INPUT
41   
42   # Navigate to the right characther
43   LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE | grep -n $INPUT | cut -d : -f 1)
44   OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE | head -$LINENUMBER | tail +$LINENUMBER)
45   # Clear screen
46   clear
47
48   # Copy output to clipboard
49   echo $OUTPUT | xclip -selection clipboard
50 done
51
52 fi