#!/usr/bin/env sh
#
# suppress - generates a function named $1 which:
#     - executes $(which $1) [with args]
#     - suppresses output lines which match $2
#
# from now on, using echo will work normally except that lines with hello or
# world will not show at the output. to see the generated functions, replace
# eval with echo below the 'tr' filter makes sure no spurious empty lines

# pass from some commands
info() { printf "%b[info]%b %s\n" '\e[0;34m\033[1m' '\e[0m' "$*" >&2; }
warn() { printf "%b[warn]%b %s\n" '\e[0;33m\033[1m' '\e[0m' "$*" >&2; }

suppress() {
  emulate -L zsh

  [ "$#" -eq 2 ] || {
    warn "2 arguments are expected"
    info "suppress gedit          'Gtk-WARNING\|connect to accessibility bus'"
    info "suppress gnome-terminal 'accessibility bus\|stop working with a future version'"
    info "suppress firefox        'g_slice_set_config'"
    return 1
  }

  if [ "$1" = "gen" ]; then
    {
      eval \
        "${1}() {
      \$(whence -c $1) \"\$@\" 2>&1 \\
      | rg -v \"$2\"
    }"
      whence -f "$1"
    }
  else
    {
      eval "$1 2>&1 | rg -v \"$2\""
    }
  fi
}

suppress "$@"
