<commit_before>
specs () {
  local +x Color_Off='\e[0m'
  local +x Bold='\e[1m'
  local +x Reset='\e[0m'
  local +x BRed='\e[1;31m'
  local +x Red='\e[0;31m'
  local +x Green='\e[0;32m'
  local +x BGreen='\e[1;32m'
  local +x Orange='\e[0;33m'
  local +x BOrange='\e[1;33m'

  should-match-output "$(echo -e this is ${Bold}bold${Reset})" \
    'mksh_setup COLORIZE "this is BOLD{{bold}}"'

  should-match-output "$(echo -e this is ${BGreen}Bright Green${Reset})" \
    'mksh_setup COLORIZE "this is BRIGHT_GREEN{{Bright Green}}"'

  should-match-output "$(echo -e this is ${Orange}orange${Reset} and ${Orange}this too${Reset})" \
    'mksh_setup COLORIZE "this is ORANGE{{orange}} and {{this too}}"'

  should-match-output "$(echo -e this is ${BOrange}bright orange {curly}${Reset} and ${BOrange}this {too}${Reset})" \
    'mksh_setup COLORIZE "this is BRIGHT_ORANGE{{bright orange {curly}}} and {{this {too}}}"'

  COLOR="RED" should-match-output "$(echo -e this is ${Red}red${Reset})" \
    'mksh_setup COLORIZE "this is {{red}}"'
}

COLOR_TO_CODE () {
  local +x VAL=""
  case "$1" in
    Color_Off)
      VAL='\e[0m'
      ;;
    BOLD|Bold)
      VAL='\e[1m'
      ;;
    RESET|Reset)
      VAL='\e[0m'
      ;;
    BRed)
      VAL='\e[1;31m'
      ;;
    BRIGHT_RED|Bright_Red)
      VAL='\e[1;31m'
      ;;
    RED|Red)
      VAL='\e[0;31m'
      ;;
    BOLD_WHITE)
      VAL="$(tput bold)$(tput setaf 7)"
      ;;
    BOLD_WHITE_ON_GREEN)
      VAL="$(tput bold)$(tput setaf 7)$(tput setab 2)"
      ;;
    GREEN|Green)
      VAL='\e[0;32m'
      ;;
    BRIGHT_GREEN)
      VAL='\e[1;32m'
      ;;
    ORANGE|Orange)
      VAL='\e[0;33m'
      ;;
    BRIGHT_ORANGE|BOrange)
      VAL='\e[1;33m'
      ;;
    *)
      echo "!!! Unknown color: $1" >&2
      exit 1
  esac
  echo -E $VAL
}

# === {{CMD}}  "my {{text}}"
# === {{CMD}}  "my {{text}}"   "-n -E"
# === {{CMD}}  "my {{text}}"   "-n -E"
COLORIZE () {

  if [[ "$#" -eq 2 ]]; then
    local +x ECHO_ARGS="$1"; shift
    local +x TEXT="$1"; shift
  else
    local +x TEXT="$@"
    local +x ECHO_ARGS="-e"
  fi

  local +x REPLACE=""
  local +x COLOR="${COLOR:-BOLD}"
  local +x DEFAULT_COLOR="$COLOR"
  local +x MATCH=""
  local +x NOT_A_COLOR=""

  local +x IFS=$'\n'
  for PARTIAL in $(echo "$TEXT" | grep -Po "([A-Za-z\_\-]+)?\{\{(.+?)\}\}(?!\})"); do
    NOT_A_COLOR=""
    COLOR=$(echo "$PARTIAL" | cut -d'{' -f1)
    if [[ -z "$COLOR" ]]; then
      COLOR="$DEFAULT_COLOR"
    else
      if [[ -z "$(COLOR_TO_CODE $COLOR 2>/dev/null)" ]]; then
        NOT_A_COLOR="$COLOR"
        COLOR="$DEFAULT_COLOR"
      else
        DEFAULT_COLOR="$COLOR"
      fi
    fi
    MATCH=$(echo "$PARTIAL" | cut -d'{' -f3- | rev | cut -d'}' -f3- | rev)
    REPLACE="$NOT_A_COLOR$(COLOR_TO_CODE "$COLOR" 2>/dev/null || "${DEFAULT_COLOR}")$MATCH$(COLOR_TO_CODE "RESET")"
    TEXT="${TEXT//$PARTIAL/$REPLACE}"
  done<commit_msg>Fixed: square brackets in colorizing.<commit_after>
specs () {
  local +x Color_Off='\e[0m'
  local +x Bold='\e[1m'
  local +x Reset='\e[0m'
  local +x BRed='\e[1;31m'
  local +x Red='\e[0;31m'
  local +x Green='\e[0;32m'
  local +x BGreen='\e[1;32m'
  local +x Orange='\e[0;33m'
  local +x BOrange='\e[1;33m'

  should-match-output "$(echo -e this is ${Bold}bold${Reset})" \
    'mksh_setup COLORIZE "this is BOLD{{bold}}"'

  should-match-output "$(echo -e this is ${BGreen}Bright Green${Reset})" \
    'mksh_setup COLORIZE "this is BRIGHT_GREEN{{Bright Green}}"'

  should-match-output "$(echo -e this is ${Orange}orange${Reset} and ${Orange}this too${Reset})" \
    'mksh_setup COLORIZE "this is ORANGE{{orange}} and {{this too}}"'

  should-match-output "$(echo -e this is ${BOrange}bright orange {curly}${Reset} and ${BOrange}this {too}${Reset})" \
    'mksh_setup COLORIZE "this is BRIGHT_ORANGE{{bright orange {curly}}} and {{this {too}}}"'

  COLOR="RED" should-match-output "$(echo -e this is ${Red}red${Reset})" \
    'mksh_setup COLORIZE "this is {{red}}"'


  # NOTE: Different shells handle '[' differently in subsitutions like ${VAR/$FIND/$REPLACE},
  # like MKSH.
  # Therefore, we have to check if {{[color]}} works.
  COLOR="BOLD" should-match-output "$(echo -e this is ${Bold}[bold]${Reset})" \
    'mksh_setup COLORIZE "this is {{[bold]}}"'
}

COLOR_TO_CODE () {
  local +x VAL=""
  case "$1" in
    Color_Off)
      VAL='\e[0m'
      ;;
    BOLD|Bold)
      VAL='\e[1m'
      ;;
    RESET|Reset)
      VAL='\e[0m'
      ;;
    BRed)
      VAL='\e[1;31m'
      ;;
    BRIGHT_RED|Bright_Red)
      VAL='\e[1;31m'
      ;;
    RED|Red)
      VAL='\e[0;31m'
      ;;
    BOLD_WHITE)
      VAL="$(tput bold)$(tput setaf 7)"
      ;;
    BOLD_WHITE_ON_GREEN)
      VAL="$(tput bold)$(tput setaf 7)$(tput setab 2)"
      ;;
    GREEN|Green)
      VAL='\e[0;32m'
      ;;
    BRIGHT_GREEN)
      VAL='\e[1;32m'
      ;;
    ORANGE|Orange)
      VAL='\e[0;33m'
      ;;
    BRIGHT_ORANGE|BOrange)
      VAL='\e[1;33m'
      ;;
    *)
      echo "!!! Unknown color: $1" >&2
      exit 1
  esac
  echo -E $VAL
}

# === {{CMD}}           "my {{text}}"
# === {{CMD}}  "-n -E"  "my {{text}}"
COLORIZE () {

  if [[ "$#" -eq 2 ]]; then
    local +x ECHO_ARGS="$1"; shift
    local +x TEXT="$1"; shift
  else
    local +x TEXT="$@"
    local +x ECHO_ARGS="-e"
  fi

  local +x REPLACE=""
  local +x COLOR="${COLOR:-BOLD}"
  local +x DEFAULT_COLOR="$COLOR"
  local +x MATCH=""
  local +x NOT_A_COLOR=""

  local +x IFS=$'\n'
  for PARTIAL in $(echo "$TEXT" | grep -Po "([A-Za-z\_\-]+)?\{\{(.+?)\}\}(?!\})"); do
    NOT_A_COLOR=""
    COLOR=$(echo "$PARTIAL" | cut -d'{' -f1)
    if [[ -z "$COLOR" ]]; then
      COLOR="$DEFAULT_COLOR"
    else
      if [[ -z "$(COLOR_TO_CODE $COLOR 2>/dev/null)" ]]; then
        NOT_A_COLOR="$COLOR"
        COLOR="$DEFAULT_COLOR"
      else
        DEFAULT_COLOR="$COLOR"
      fi
    fi

    MATCH=$(echo "$PARTIAL" | cut -d'{' -f3- | rev | cut -d'}' -f3- | rev)
    REPLACE="$NOT_A_COLOR$(COLOR_TO_CODE "$COLOR" 2>/dev/null || "${DEFAULT_COLOR}")$MATCH$(COLOR_TO_CODE "RESET")"

    # NOTE: MKSH handles '[' differently in vars when using
    # subsitutions like ${VAR/$PARTIAL/$REPLACE}.
    # So we have to escape it by quoting the variables: ${VAR/"$PARTIAL"/"$REPLACE"}
    TEXT="${TEXT//"$PARTIAL"/"$REPLACE"}"
  done
{'fraction_non_alphanumeric': 0.21387375841076578, 'fraction_numerical': 0.019224607497596923, 'mean_word_length': 2.763110307414105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5173743', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2510, 'n_words': 561}