#{- ~/.functions -}#

# Enables or disables kwm hotkeys
hotkeys()
{
  case "$1" in
    "on") kwmc config hotkeys on; echo "Hotkeys enabled." ;;
    "off") kwmc config hotkeys off; echo "Hotkeys disabled." ;;
    *) printf "hotkeys: usage: hotkeys {on|off}\n" ;;
  esac
}

# Passes basic arithmetic to bc
maths()
{
  bc -l <<< "scale=2; $*"
}

cat_with_newline()
{
  \cat "$@" && echo
}
alias cat="cat_with_newline"

k_prompt()
{
  set -x
  if [ -z ${KUBE_PROMPT+x} ]; then
    export KUBE_PROMPT=1
  else
    unset KUBE_PROMPT
  fi
  set +x
}

kube_prompt()
{
  set -x
  if ! [ -z ${KUBE_PROMPT+x} ]; then
    context=$(\cat ${KUBECONFIG} \
              | grep "current-context: " \
              | sed "s/current-context: //")
    echo {$context}
  fi
  set +x
}

g_prompt()
{
  if ! [ -z ${GIT_PROMPT+x} ]; then
    export GIT_PROMPT=1
  else
    unset GIT_PROMPT
  fi
}

git_prompt()
{
  if ! [ -z ${GIT_PROMPT+x} ]; then
    # Determine if we are in a git repository
    if git rev-parse --is-inside-work-tree &> /dev/null; then
      branch=$(git rev-parse --abbrev-ref HEAD)
      echo [$branch]
    fi
  fi
}

prompt_command_append() {
  arg=${1}
  PROMPT_COMMAND=${PROMPT_COMMAND:+"$PROMPT_COMMAND; "}"${arg}"
}

prompt_command_prepend() {
  arg=${1}
  PROMPT_COMMAND="${arg}"${PROMPT_COMMAND:+"; $PROMPT_COMMAND"}
}
