2015-10-19 15:37:18 +00:00
|
|
|
# vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
|
|
|
|
################################################################
|
2015-10-23 21:42:59 +00:00
|
|
|
# Utility functions
|
2015-10-19 15:37:18 +00:00
|
|
|
# This file holds some utility-functions for
|
|
|
|
# the powerlevel9k-ZSH-theme
|
|
|
|
# https://github.com/bhilburn/powerlevel9k
|
|
|
|
################################################################
|
|
|
|
|
2019-03-13 16:04:27 +00:00
|
|
|
# Usage: set_default [OPTION]... NAME [VALUE]...
|
2015-10-19 15:37:18 +00:00
|
|
|
#
|
2019-03-13 16:04:27 +00:00
|
|
|
# Options are the same as in `typeset`.
|
2015-10-19 15:37:18 +00:00
|
|
|
function set_default() {
|
2019-04-22 15:06:41 +00:00
|
|
|
emulate -L zsh
|
2019-03-13 16:04:27 +00:00
|
|
|
local -a flags=(-g)
|
|
|
|
while true; do
|
|
|
|
case $1 in
|
|
|
|
--) shift; break;;
|
|
|
|
-*) flags+=$1; shift;;
|
|
|
|
*) break;
|
|
|
|
esac
|
|
|
|
done
|
2015-10-19 15:37:18 +00:00
|
|
|
|
2019-03-13 16:04:27 +00:00
|
|
|
local varname=$1
|
|
|
|
shift
|
2019-04-30 10:51:34 +00:00
|
|
|
if [[ -n ${(tP)varname} ]]; then
|
2019-03-13 16:04:27 +00:00
|
|
|
typeset $flags $varname
|
|
|
|
elif [[ "$flags" == *[aA]* ]]; then
|
|
|
|
eval "typeset ${(@q)flags} ${(q)varname}=(${(qq)@})"
|
|
|
|
else
|
|
|
|
typeset $flags $varname="$*"
|
|
|
|
fi
|
2015-10-19 15:37:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 16:24:56 +00:00
|
|
|
function _p9k_g_expand() {
|
2019-05-19 10:46:28 +00:00
|
|
|
(( $+parameters[$1] )) || return
|
2019-03-18 16:24:56 +00:00
|
|
|
local -a ts=("${=$(typeset -p $1)}")
|
|
|
|
shift ts
|
2019-05-19 10:46:28 +00:00
|
|
|
local x
|
2019-03-18 16:24:56 +00:00
|
|
|
for x in "${ts[@]}"; do
|
|
|
|
[[ $x == -* ]] || break
|
|
|
|
# Don't change readonly variables. Ideally, we shouldn't modify any variables at all,
|
|
|
|
# but for now this will do.
|
|
|
|
[[ $x == -*r* ]] && return
|
|
|
|
done
|
|
|
|
typeset -g $1=${(g::)${(P)1}}
|
|
|
|
}
|
|
|
|
|
2015-10-19 15:37:18 +00:00
|
|
|
# Converts large memory values into a human-readable unit (e.g., bytes --> GB)
|
2016-02-09 20:44:45 +00:00
|
|
|
# Takes two arguments:
|
|
|
|
# * $size - The number which should be prettified
|
|
|
|
# * $base - The base of the number (default Bytes)
|
2015-10-19 15:37:18 +00:00
|
|
|
printSizeHumanReadable() {
|
2015-10-22 08:10:15 +00:00
|
|
|
typeset -F 2 size
|
|
|
|
size="$1"+0.00001
|
2015-10-19 15:37:18 +00:00
|
|
|
local extension
|
2015-10-22 08:10:15 +00:00
|
|
|
extension=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
|
2015-10-19 15:37:18 +00:00
|
|
|
local index=1
|
|
|
|
|
|
|
|
# if the base is not Bytes
|
|
|
|
if [[ -n $2 ]]; then
|
2018-03-26 23:09:41 +00:00
|
|
|
local idx
|
2015-10-19 15:37:18 +00:00
|
|
|
for idx in "${extension[@]}"; do
|
|
|
|
if [[ "$2" == "$idx" ]]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
index=$(( index + 1 ))
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2015-10-22 08:10:15 +00:00
|
|
|
while (( (size / 1024) > 0.1 )); do
|
2015-10-19 15:37:18 +00:00
|
|
|
size=$(( size / 1024 ))
|
|
|
|
index=$(( index + 1 ))
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "$size${extension[$index]}"
|
|
|
|
}
|
|
|
|
|
2016-08-11 19:40:05 +00:00
|
|
|
# Determine if the passed segment is used in the prompt
|
|
|
|
#
|
|
|
|
# Pass the name of the segment to this function to test for its presence in
|
|
|
|
# either the LEFT or RIGHT prompt arrays.
|
|
|
|
# * $1: The segment to be tested.
|
|
|
|
segment_in_use() {
|
2019-03-13 16:04:27 +00:00
|
|
|
local key=$1
|
2019-03-26 14:29:31 +00:00
|
|
|
[[ -n "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(r)${key}]}" ||
|
|
|
|
-n "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(r)${key}_joined]}" ||
|
|
|
|
-n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)${key}]}" ||
|
|
|
|
-n "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(r)${key}_joined]}" ]]
|
2016-01-22 18:13:07 +00:00
|
|
|
}
|
2016-03-17 22:00:20 +00:00
|
|
|
|
2019-02-03 18:20:14 +00:00
|
|
|
# Parse IP address from ifconfig on OSX and from IP on Linux
|
|
|
|
# Parameters:
|
|
|
|
# $1 - string The desired Interface
|
|
|
|
# $2 - string A root prefix for testing purposes
|
|
|
|
function p9k::parseIp() {
|
|
|
|
local desiredInterface="${1}"
|
|
|
|
|
|
|
|
if [[ -z "${desiredInterface}" ]]; then
|
|
|
|
desiredInterface="^[^ ]+"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local ROOT_PREFIX="${2}"
|
|
|
|
if [[ "$OS" == "OSX" ]]; then
|
|
|
|
# Get a plain list of all interfaces
|
|
|
|
local rawInterfaces="$(${ROOT_PREFIX}/sbin/ifconfig -l 2>/dev/null)"
|
|
|
|
# Parse into array (split by whitespace)
|
|
|
|
local -a interfaces
|
|
|
|
interfaces=(${=rawInterfaces})
|
|
|
|
# Parse only relevant interface names
|
|
|
|
local pattern="${desiredInterface}[^ ]?"
|
|
|
|
local -a relevantInterfaces
|
|
|
|
for rawInterface in $interfaces; do
|
|
|
|
[[ "$rawInterface" =~ $pattern ]] && relevantInterfaces+=( $MATCH )
|
|
|
|
done
|
|
|
|
local newline=$'\n'
|
|
|
|
for interfaceName in $relevantInterfaces; do
|
|
|
|
local interface="$(${ROOT_PREFIX}/sbin/ifconfig $interfaceName 2>/dev/null)"
|
2019-02-06 07:53:46 +00:00
|
|
|
if [[ "${interface}" =~ "lo[0-9]*" ]]; then
|
|
|
|
continue
|
|
|
|
fi
|
2019-02-03 18:20:14 +00:00
|
|
|
# Check if interface is UP.
|
2019-02-22 17:16:33 +00:00
|
|
|
if [[ "${interface//${newline}/}" =~ "<([^>]*)>(.*)inet[ ]+([^ ]*)" ]]; then
|
2019-02-21 17:32:17 +00:00
|
|
|
local ipFound="${match[3]}"
|
|
|
|
local -a interfaceStates=(${(s:,:)match[1]})
|
|
|
|
if [[ "${interfaceStates[(r)UP]}" == "UP" ]]; then
|
|
|
|
echo "${ipFound}"
|
|
|
|
return 0
|
|
|
|
fi
|
2019-02-03 18:20:14 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
local -a interfaces
|
|
|
|
interfaces=( "${(f)$(${ROOT_PREFIX}/sbin/ip -brief -4 a show 2>/dev/null)}" )
|
|
|
|
local pattern="^${desiredInterface}[ ]+UP[ ]+([^/ ]+)"
|
|
|
|
for interface in "${(@)interfaces}"; do
|
|
|
|
if [[ "$interface" =~ $pattern ]]; then
|
|
|
|
echo "${match[1]}"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|