<commit_before>#!/usr/bin/env zsh
# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
################################################################
# @title powerlevel9k Utility Functions
# @source [powerlevel9k](https://github.com/bhilburn/powerlevel9k)
##
# @info
#   This file contains some utility-functions for
#   the powerlevel9k ZSH theme.
##

################################################################
# @description
#   Source all autoload functions.
##
# @noargs
##
function __p9k_source_autoloads() {
  local autoload_path="$__P9K_DIRECTORY/functions/autoload"
  # test if we already autoloaded the functions
  if [[ ${fpath[(ie)$autoload_path]} -gt ${#fpath} ]]; then
    fpath=( ${autoload_path} "${fpath[@]}" )
    autoload -Uz __p9k_get_unique_path
    autoload -Uz __p9k_segment_should_be_joined
    autoload -Uz __p9k_segment_should_be_printed
    autoload -Uz __p9k_truncate_path
    autoload -Uz __p9k_update_var_name
    autoload -Uz __p9k_upsearch
  fi
}
__p9k_source_autoloads

###############################################################
# @description
#   Determine the OS and version (if applicable).
##
# @noargs
##
# @result
#   __P9K_OS string Name of OS
#   __P9K_OS_ID string Version of OS
##
function __p9k_detect_os() {
  typeset -g __P9K_OS __P9K_OS_ID
  case $(uname) in
    Darwin)
      __P9K_OS='OSX'
      __P9K_OS_ID="$(uname -r)"
      [[ "$(which stat)" != "/usr/bin/stat" ]] && __P9K_OSX_COREUTILS=true || __P9K_OSX_COREUTILS=false
    ;;
    CYGWIN_NT-* | MSYS_NT-*) __P9K_OS='Windows' ;;
    FreeBSD | OpenBSD | DragonFly) __P9K_OS='BSD' ;;
    Linux)
      __P9K_OS='Linux'
      [[ ${(f)"$((</etc/os-release) 2>/dev/null)"} =~ "ID=([A-Za-z]+)" ]] && __P9K_OS_ID="${match[1]}"
      case $(uname -o 2>/dev/null) in
        Android) __P9K_OS='Android' ;;
      esac
    ;;
    SunOS) __P9K_OS='Solaris' ;;
    *) __P9K_OS='' ;;
  esac
  readonly __P9K_OS
  readonly __P9K_OS_ID
}
__p9k_detect_os

################################################################
# @description
#   Identify Terminal Emulator.
#
#   Find out which emulator is being used for terminal specific options
#   The testing order is important, since some override others.
##
# @noargs
##
# @result
#  __P9K_TERMINAL string Readonly global with the terminal ID
##
function __p9k_detect_terminal() {
  typeset -g __P9K_TERMINAL
  if [[ "$TMUX" =~ "tmux" ]]; then
    __P9K_TERMINAL="tmux"
  elif [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
    __P9K_TERMINAL="iterm"
  elif [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
    __P9K_TERMINAL="appleterm"
  else
    if [[ "${__P9K_OS}" == "OSX" ]]; then
      #local termtest=$(ps -o 'command=' -p $(ps -o 'ppid=' -p $$) | tail -1 | awk '{print $NF}')
      local termtest=${$(ps -o 'command=' -p $(ps -o 'ppid='$$))[1]:t}
      # test if we are in a sudo su -
      #if [[ ${termtest} == "-" || ${termtest} == "root" ]]; then
      if [[ ${termtest} == "zsh" ]]; then
        #termtest=${($(ps -o 'command=' -p $(ps -o 'ppid=' -p $(ps -o 'ppid='$$))))[1]:t}
        #termtest=${termtest[1]:t}
        termtest=${$(ps -o 'command=' -p $(ps -o 'ppid=' -p $(ps -o 'ppid='$$)))[1]:t}
      fi
    else
      local termtest=$(ps -o 'cmd=' -p $(ps -o 'ppid=' -p $$) | tail -1 | awk '{print $NF}')
      # test if we are in a sudo su -
      if [[ ${termtest} == "-" || ${termtest} == "root" ]]; then
        termtest=($(ps -o 'cmd=' -p $(ps -o 'ppid=' $(ps -o 'ppid='$$))))
        if [[ $termtest[1] == "zsh" ]]; then  # gnome terminal works differently than the rest... sigh
          termtest=$termtest[-1]
        elif [[ $termtest[1] =~ "python" ]]; then   # as does guake
          termtest=$termtest[3]
        else
          termtest=$termtest[1]
        fi
      fi
    fi
    case "${termtest##*/}" in
      gnome-terminal-server)    __P9K_TERMINAL="gnometerm";;
      guake.main)               __P9K_TERMINAL="guake";;
      iTerm2)                   __P9K_TERMINAL="iterm";;
      konsole)                  __P9K_TERMINAL="konsole";;
      termite)                  __P9K_TERMINAL="termite";;
      urxvt)                    __P9K_TERMINAL="rxvt";;
      yakuake)                  __P9K_TERMINAL="yakuake";;
      xterm | xterm-256color)   __P9K_TERMINAL="xterm";;
      *tty*)                    __P9K_TERMINAL="tty";;
      *)                        __P9K_TERMINAL=${termtest##*/};;
    esac

    unset termtest
    unset uname
  fi
  readonly __P9K_TERMINAL
}
__p9k_detect_terminal

###############################################################
# @description
#   This function determines if POWERLEVEL9K_ variables have
#   been previously defined and changes them to P9K_ variables.
##
# @noargs
##
function __p9k_update_environment_vars() {
  local envVar varType varName origVar newVar newVal
  local oldVarsFound=false
  for envVar in $(declare); do
    if [[ ${envVar} =~ "POWERLEVEL9K_" ]]; then<commit_msg>Fixed Linux terminal emulator detection (pure ZSH)<commit_after>#!/usr/bin/env zsh
# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
################################################################
# @title powerlevel9k Utility Functions
# @source [powerlevel9k](https://github.com/bhilburn/powerlevel9k)
##
# @info
#   This file contains some utility-functions for
#   the powerlevel9k ZSH theme.
##

################################################################
# @description
#   Source all autoload functions.
##
# @noargs
##
function __p9k_source_autoloads() {
  local autoload_path="$__P9K_DIRECTORY/functions/autoload"
  # test if we already autoloaded the functions
  if [[ ${fpath[(ie)$autoload_path]} -gt ${#fpath} ]]; then
    fpath=( ${autoload_path} "${fpath[@]}" )
    autoload -Uz __p9k_get_unique_path
    autoload -Uz __p9k_segment_should_be_joined
    autoload -Uz __p9k_segment_should_be_printed
    autoload -Uz __p9k_truncate_path
    autoload -Uz __p9k_update_var_name
    autoload -Uz __p9k_upsearch
  fi
}
__p9k_source_autoloads

###############################################################
# @description
#   Determine the OS and version (if applicable).
##
# @noargs
##
# @result
#   __P9K_OS string Name of OS
#   __P9K_OS_ID string Version of OS
##
function __p9k_detect_os() {
  typeset -g __P9K_OS __P9K_OS_ID
  case $(uname) in
    Darwin)
      __P9K_OS='OSX'
      __P9K_OS_ID="$(uname -r)"
      [[ "$(which stat)" != "/usr/bin/stat" ]] && __P9K_OSX_COREUTILS=true || __P9K_OSX_COREUTILS=false
    ;;
    CYGWIN_NT-* | MSYS_NT-*) __P9K_OS='Windows' ;;
    FreeBSD | OpenBSD | DragonFly) __P9K_OS='BSD' ;;
    Linux)
      __P9K_OS='Linux'
      [[ ${(f)"$((</etc/os-release) 2>/dev/null)"} =~ "ID=([A-Za-z]+)" ]] && __P9K_OS_ID="${match[1]}"
      case $(uname -o 2>/dev/null) in
        Android) __P9K_OS='Android' ;;
      esac
    ;;
    SunOS) __P9K_OS='Solaris' ;;
    *) __P9K_OS='' ;;
  esac
  readonly __P9K_OS
  readonly __P9K_OS_ID
}
__p9k_detect_os

################################################################
# @description
#   Identify Terminal Emulator.
#
#   Find out which emulator is being used for terminal specific options
#   The testing order is important, since some override others.
##
# @noargs
##
# @result
#  __P9K_TERMINAL string Readonly global with the terminal ID
##
function __p9k_detect_terminal() {
  typeset -g __P9K_TERMINAL
  if [[ "$TMUX" =~ "tmux" ]]; then
    __P9K_TERMINAL="tmux"
  elif [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
    __P9K_TERMINAL="iterm"
  elif [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
    __P9K_TERMINAL="appleterm"
  else
    if [[ "${__P9K_OS}" == "OSX" ]]; then
      local termtest=${$(ps -o 'command=' -p $(ps -o 'ppid='$$))[1]:t}
      # test if we are in a sudo su -
      if [[ ${termtest} == "zsh" ]]; then
        termtest=${$(ps -o 'command=' -p $(ps -o 'ppid=' -p $(ps -o 'ppid='$$)))[1]:t}
      fi
    else # Linux
      # see: https://askubuntu.com/a/966934
      if [[ $TTY = "/dev/tty"* ]]; then
        __P9K_TERMINAL="linux-console"
        return
      fi
      local pid=$$ termtest=''
      while true; do
        proc_stat=(${(@f)$(</proc/${pid}/stat)})
        termtest=${proc_stat[2]//[()]/}
        echo "$termtest" # debug line
        case "${termtest}" in
          gnome-terminal|guake|konsole|rxvt|termite|urxvt|xterm|yakuake)
            __P9K_TERMINAL="${termtest}"
            return
          ;;
          python*)
            local cmdline=(${(@f)$(</proc/${pid}/cmdline)})
            if [[ "$cmdline" =~ "\\bguake.main\\b" ]]; then
              __P9K_TERMINAL="guake"
              return
            fi
          ;;
        esac
        if test "$pid" = "1" -o "$pid" = ""; then
          __P9K_TERMINAL="unknown"
          return
        fi
        pid=${proc_stat[4]}
      done
    fi
    case "${termtest##*/}" in
      gnome-terminal*)          __P9K_TERMINAL="gnometerm";;
      iTerm2)                   __P9K_TERMINAL="iterm";;
      urxvt)                    __P9K_TERMINAL="rxvt";;
      xterm | xterm-256color)   __P9K_TERMINAL="xterm";;
      *tty*)                    __P9K_TERMINAL="tty";;
      *)                        __P9K_TERMINAL=${termtest##*/};;
    esac
  fi
  readonly __P9K_TERMINAL
}
__p9k_detect_terminal

###############################################################
# @description
#   This function determines if POWERLEVEL9K_ variables have
#   been previously defined and changes them to P9K_ variables.
##
# @noargs
##
function __p9k_update_environment_vars() {
  local envVar varType varName origVar newVar newVal
  local oldVarsFound=false
  for envVar in $(declare); do
    if [[ ${envVar} =~ "POWERLEVEL9K_" ]]; then
{'fraction_non_alphanumeric': 0.21167274622199062, 'fraction_numerical': 0.0160500260552371, 'mean_word_length': 3.0661016949152544, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 3, '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': '1515113', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3485, 'n_words': 739}