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