summaryrefslogtreecommitdiffstats
path: root/src/cheat.sh
blob: 203b8ddc1acf9f1be6289a2fda7f6d26bced9ce8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env bash

cheat::find() {
  for path in $(echo "$NAVI_PATH" | tr ':' '\n'); do
    find "$path" -iname '*.cheat'
  done
}

cheat::read_many() {
   for cheat in $(cat); do
      awk '
        function color(c,s) {
           printf("\033[%dm%s\033[0m",30+c,s)
        }

        /^%/ { tags=" ["substr($0, 3)"]"; next }
        /^#/ { print color(4, $0) color(60, tags); next }
        /^\$/ { next }
      NF { print color(7, $0) color(60, tags); next }' "$cheat"
   done
}

cheat::from_selection() {
   local readonly cheats="$1"
   local readonly selection="$2"

   local readonly tags="$(echo "$selection" | selection::tags)"

   for cheat in $cheats; do
      if grep -q "% $tags" "$cheat"; then
         echo "$cheat"
         exit 0
      fi
   done

   echoerr "No valid cheatsheet!"
   exit 67
}