]> git.armaanb.net Git - charsel.git/blob - charsel
some big changes
[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 $HOME/.local/share/charsel/- $CHARDIR/
13 fi
14
15 # Check for user inputs
16 if [[ $1 == "list" \
17   || $1 == "-l" \
18   || $1 == "--list" ]]
19 then
20   echo "The following charfiles are installed"
21   ls $CHARDIR/charfiles
22   exit 0
23 elif [[ $1 == "help" \
24   || $1 == "-h" \
25   || $1 == "--help" \
26   || $# -ne 1 ]]
27 then
28   cat /usr/share/doc/charsel/README.md
29   exit 0
30 fi
31
32 # Define charfile path
33 CHARFILE=$CHARDIR/charfiles/$1
34
35 if [[ ! -f $CHARFILE ]]
36 then
37   echo "Please enter a valid charfile. You can list installed charfiles with \`charsel list\`"
38   exit 1
39 fi
40
41 # Define length of shortcut
42 LENGTH=$(cat $CHARFILE \
43   | cut -f 1 -d ',' -s \
44   | wc -L \
45   | cut -b 1)
46
47 # Clear screen
48 clear
49
50 # Main program
51 while :
52 do
53   # Format charfile into table, showing only the shortcuts below the divider in the charfile
54   cat $CHARFILE \
55     | grep -A 100 - \
56     | tail -n +2 \
57     | column -t -N input,output --output-separator ' | ' --separator ','
58
59   echo "*-------------------------*"
60   echo "| previous shorcode:" $INPUT
61   echo "| previous output:  " $OUTPUT
62   echo "*-------------------------*"
63   
64   # User input
65   read -p "| input shortcode:   " -N $LENGTH INPUT
66
67   # This can definately be simplifed, but it works fine
68   LINENUMBER=$(cut -f 1 -d ',' -s $CHARFILE \
69     | grep -wn $INPUT \
70     | cut -d : -f 1)
71   OUTPUT=$(cut -f 2 -d ',' -s $CHARFILE \
72     | head -$LINENUMBER \
73     | tail +$LINENUMBER)
74
75   # Copy output to clipboard
76   echo $OUTPUT | xclip -selection clipboard
77
78   # Clear screen
79   clear
80
81 done