summaryrefslogtreecommitdiff
path: root/bash/colorize.sh
blob: 519fa199a97bae3d7d7540ff22fbb42f49b714c9 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash

NAME=bungmonkey

IFS=$'\n'

set -f # disable pathname expansion out of an abundance of caution
declare -A assArray

colorword () {
  WORD="$1"
  if [ ${assArray["$WORD"]+_} ]; then
    I="${assArray["$WORD"]}"
  else
    #I="$( crc32 <( echo "$WORD" ) | cut -f 1 -d ' ' )" # WHY IS THIS FUCKING PERL
    I="$( echo "ff$WORD" | md5sum )"
    I="${I:0:4}"

    I="$(printf '%d' 0x"$I")"

    I=$(( ( "$I" & 226 ) + 1 ))

#    I=$(( ( $RANDOM & 226 ) + 1 ))
    #I=7
    # Grayscales to exclude:
    # 007, 008, 015, 016, 231-255

    ( [ $I -ge 7  ] && [ $I -le 8  ] ) && ((I+=2))
    ( [ $I -ge 15 ] && [ $I -le 16 ] ) && ((I+=2))
    assArray["$WORD"]="$I"
  fi

  echo -ne "\033[38;5;${assArray["$WORD"]}m${WORD}\033[0m"
# How to GC?
#  if [ ${#assArray[@]} -gt 10 ]; then
#    echo purging assArray
#    unset assArray
#  fi
  return "$I"
}

while read -r line; do
  # 11:39 < bungmonkey> test test
  if [[ "$line" =~ ^([0-9]{2}:[0-9]{2})\ \<(.)([0-9a-zA-Z_]+)\>\ (.*+)$ ]] ; then
    TIME="${BASH_REMATCH[1]}"
    MOD="${BASH_REMATCH[2]}"
    NAME="${BASH_REMATCH[3]}"
    MSG="${BASH_REMATCH[4]}"
    #echo "$TIME $NAME $MSG"
    echo -n "$TIME "
    echo -ne "\033[38;5;8m<\033[0m"
    echo -ne "$MOD"
    colorword "$NAME"
    COLOR="$?"
    echo -ne "\033[38;5;8m>\033[0m\033[38;5;${COLOR}m"
    IFS=" "
    while read -r word; do
      if [[ "$word" =~ ^[a-z0-9]+[A-Z][a-zA-Z0-9]+$ ]] ; then
        echo -ne " \033[0m"
        echo -n "$word"
        echo -ne "\033[38;5;${COLOR}m"
        # FIXME: why does @ break here?
      #elif [[ "$word" =~ ^@([a-zA-Z0-9_]+) ]]; then
      #  echo -ne " \033[0m@"
      #  colorword "${BASH_REMATCH[1]}"
      #  echo -ne "\033[38;5;${COLOR}m"
      else
        echo -n " $word"
      fi
    done < <( echo "$MSG" )
    IFS=$'\n'
    echo -e "\033[0m"
  # 11:28 -!- cokedrips69 [cokedrips69@cokedrips69.tmi.twitch.tv] has left #bungmonkey []
  elif [[ "$line" =~ ^([0-9]{2}:[0-9]{2})\ -!-\ ([0-9a-zA-Z_]+)\ \[([0-9a-zA-Z_]+)@([0-9a-zA-Z_.]+)\]\ has\ ([a-zA-Z]+)\ ([#a-zA-Z0-9_-]+)(\ |)(\[(.*)\]|) ]]; then
    TIME="${BASH_REMATCH[1]}"
    NAME="${BASH_REMATCH[2]}"
    LOCAL="${BASH_REMATCH[3]}"
    HOST="${BASH_REMATCH[4]}"
    STATE="${BASH_REMATCH[5]}"
    ROOM="${BASH_REMATCH[6]}"
    REASON="${BASH_REMATCH[9]}"
    # remove some noise
    if [ "$NAME" == "$LOCAL" ] && [ "$NAME".tmi.twitch.tv == "$HOST" ] ; then
      LOCAL=""
      HOST="irc.chat.twitch.tv"
    fi
    echo -n "$TIME "
    echo -ne "\033[38;5;12m-\033[0m!\033[38;5;12m-\033[0m "
    colorword "$NAME"
    echo -ne " \033[38;5;8"
    echo -n  "[$LOCAL@$HOST]"
    echo -ne "\033[0m has "
    colorword "$STATE"
    echo -n " "
    colorword "$ROOM"
    if [ "x$REASON" != "x" ]; then
      echo -ne "\033[38;5;8m[\033[0m"
      echo -n "$REASON"
      echo -ne "\033[38;5;8m]\033[0m"
    fi
    echo
  else
    echo "$line"
  fi
done