summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md4
-rw-r--r--README.md54
-rw-r--r--functions/icons.zsh3
-rwxr-xr-xpowerlevel9k.zsh-theme127
4 files changed, 149 insertions, 39 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 933eceaa..8c21ea0e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
## v0.3.0 (next)
+### New segment `custom_command` added
+
+A new segment that allows users to define a custom command was added.
+
### `virtualenv` changes
This segment now respects `VIRTUAL_ENV_DISABLE_PROMPT`. If this variable is set
diff --git a/README.md b/README.md
index 4d225d14..6a690bfd 100644
--- a/README.md
+++ b/README.md
@@ -73,6 +73,7 @@ The segments that are currently available are:
* [aws](#aws) - The current AWS profile, if active.
* [battery](#battery) - Current battery status.
* [context](#context) - Your username and host.
+* [custom_command](#custom_command) - A custom command to display the output of.
* [dir](#dir) - Your current working directory.
* **go_version** - Show the current GO version.
* **history** - The command number for the current line.
@@ -121,6 +122,59 @@ In addition to the above it supports standard _FOREGROUND value without affectin
Supports both OS X and Linux(time remaining requires the acpi program on Linux)
+##### custom_command
+
+The `custom_...` segment lets you add a custom command to your prompt, to e.g. display the wifi signal. You choose a name for the segment yourself, (here signal), and then set the appropriate variables, as so (based on the name you chose)
+
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context time battery dir vcs virtualenv custom_signal)
+ POWERLEVEL9K_CUSTOM_SIGNAL="echo signal: \$(nmcli device wifi | grep yes | awk '{print \$8}')"
+ POWERLEVEL9K_CUSTOM_SIGNAL_BACKGROUND="blue"
+ POWERLEVEL9K_CUSTOM_SIGNAL_FOREGROUND="yellow"
+ POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(aws status load ram)
+
+gives
+
+![simplesignal](http://i.imgur.com/SQmYVFL.png)
+
+Instead of defining the command inline (if it is kinda long or unreadable), one can also add a function to the .zshrc like:
+
+ zsh_signal(){
+ local signal=$(nmcli device wifi | grep yes | awk '{print $8}')
+ local color='%F{yellow}'
+ [[ $signal -gt 75 ]] && color='%F{green}'
+ [[ $signal -lt 50 ]] && color='%F{red}'
+ echo -n "%{$color%}\uf230 $signal%{%f%}" # \uf230 is 
+ }
+
+And then by changing the custom commands array (and rearranging a bit the prompt elements) to read:
+
+ POWERLEVEL9K_CUSTOM_SIGNAL="zsh_signal"
+
+Then this updated command looks like:
+
+![signal](http://i.imgur.com/hviMATC.png)
+
+You can also have multiple custom commands. Say you have
+
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context time battery custom_signal dir vcs virtualenv custom_time )
+ POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(aws status load ram custom_docker)
+
+ POWERLEVEL9K_CUSTOM_SIGNAL="zsh_signal"
+ POWERLEVEL9K_CUSTOM_SIGNAL_FOREGROUND="white"
+ POWERLEVEL9K_CUSTOM_SIGNAL_BACKGROUND="black"
+
+ POWERLEVEL9K_CUSTOM_DOCKER='echo "\uf299 $(docker ps -a | grep Up | wc -l)"' # \uf299 is 
+ POWERLEVEL9K_CUSTOM_DOCKER_FOREGROUND="white"
+ POWERLEVEL9K_CUSTOM_DOCKER_BACKGROUND="blue"
+
+ POWERLEVEL9K_CUSTOM_TIME='echo "$(date +%s)"'
+ POWERLEVEL9K_CUSTOM_TIME_FOREGROUND="black"
+ POWERLEVEL9K_CUSTOM_TIME_BACKGROUND="yellow"
+
+
+Then you get:
+
+![](http://i.imgur.com/QGGBTqY.png)
##### context
diff --git a/functions/icons.zsh b/functions/icons.zsh
index dd256b06..fcf08163 100644
--- a/functions/icons.zsh
+++ b/functions/icons.zsh
@@ -44,6 +44,7 @@ case $POWERLEVEL9K_MODE in
LINUX_ICON $'\UE271' # 
SUNOS_ICON $'\U1F31E ' # 🌞
HOME_ICON $'\UE12C ' # 
+ FOLDER_ICON $'\UE818 ' # 
NETWORK_ICON $'\UE1AD ' # 
LOAD_ICON $'\UE190 ' # 
#RAM_ICON $'\UE87D' # 
@@ -95,6 +96,7 @@ case $POWERLEVEL9K_MODE in
LINUX_ICON $'\UF17C' # 
SUNOS_ICON $'\UF185 ' # 
HOME_ICON $'\UF015 ' # 
+ FOLDER_ICON $'\UF115 ' # 
NETWORK_ICON $'\UF09E ' # 
LOAD_ICON $'\UF080 ' # 
RAM_ICON $'\UF0E4' # 
@@ -141,6 +143,7 @@ case $POWERLEVEL9K_MODE in
LINUX_ICON 'Lx'
SUNOS_ICON 'Sun'
HOME_ICON ''
+ FOLDER_ICON ''
NETWORK_ICON 'IP'
LOAD_ICON 'L'
RAM_ICON 'RAM'
diff --git a/powerlevel9k.zsh-theme b/powerlevel9k.zsh-theme
index bcdacd0e..3d2d5d2c 100755
--- a/powerlevel9k.zsh-theme
+++ b/powerlevel9k.zsh-theme
@@ -229,44 +229,66 @@ prompt_aws() {
fi
}
+# Custom: a way for the user to specify custom commands to run,
+# and display the output of.
+#
+prompt_custom() {
+ local command=POWERLEVEL9K_CUSTOM_$2:u
+
+ "$1_prompt_segment" "${0}_${2:u}" $DEFAULT_COLOR_INVERTED $DEFAULT_COLOR "$(eval ${(P)command})"
+}
+
prompt_battery() {
- icons[BATTERY_ICON]=$'\UE894'
+ # The battery can have different states.
+ # Default is "unknown"
+ local current_state="unknown"
+ typeset -AH battery_states
+ battery_states=(
+ 'low' 'red'
+ 'charging' 'yellow'
+ 'charged' 'green'
+ 'disconnected' "$DEFAULT_COLOR_INVERTED"
+ )
# set default values of not specified in shell
- [[ -z $POWERLEVEL9K_BATTERY_CHARGING ]] && POWERLEVEL9K_BATTERY_CHARGING="yellow"
- [[ -z $POWERLEVEL9K_BATTERY_CHARGED ]] && POWERLEVEL9K_BATTERY_CHARGED="green"
- [[ -z $POWERLEVEL9K_BATTERY_DISCONNECTED ]] && POWERLEVEL9K_BATTERY_DISCONNECTED="$DEFAULT_COLOR_INVERTED"
- [[ -z $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && POWERLEVEL9K_BATTERY_LOW_THRESHOLD=10
- [[ -z $POWERLEVEL9K_BATTERY_LOW_COLOR ]] && POWERLEVEL9K_BATTERY_LOW_COLOR="red"
- [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local fg_color="" || local fg_color="%F{$POWERLEVEL9K_BATTERY_FOREGROUND}"
- [[ -z $POWERLEVEL9K_BATTERY_FOREGROUND ]] && local conn="$DEFAULT_COLOR_INVERTED" || local conn="$POWERLEVEL9K_BATTERY_FOREGROUND"
+ set_default POWERLEVEL9K_BATTERY_LOW_THRESHOLD 10
if [[ $OS =~ OSX && -f /usr/sbin/ioreg && -x /usr/sbin/ioreg ]]; then
+ # Pre-Grep all needed informations to save some memory and
+ # as little pollution of the xtrace output as possible.
+ local raw_data=$(ioreg -n AppleSmartBattery | grep -E "MaxCapacity|TimeRemaining|CurrentCapacity|ExternalConnected|IsCharging")
# return if there is no battery on system
- [[ -z $(ioreg -n AppleSmartBattery | grep MaxCapacity) ]] && return
-
- # get charge status
- [[ $(ioreg -n AppleSmartBattery | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]] && local connected=true
- [[ $(ioreg -n AppleSmartBattery | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]] && local charging=true
+ [[ -z $(echo $raw_data | grep MaxCapacity) ]] && return
# convert time remaining from minutes to hours:minutes date string
- local time_remaining=$(ioreg -n AppleSmartBattery | grep TimeRemaining | awk '{ print $5 }')
- if [[ ! -z $time_remaining ]]; then
+ local time_remaining=$(echo $raw_data | grep TimeRemaining | awk '{ print $5 }')
+ if [[ -n $time_remaining ]]; then
# this value is set to a very high number when the system is calculating
[[ $time_remaining -gt 10000 ]] && local tstring="..." || local tstring=${(f)$(date -u -r $(($time_remaining * 60)) +%k:%M)}
fi
# get charge values
- local max_capacity=$(ioreg -n AppleSmartBattery | grep MaxCapacity | awk '{ print $5 }')
- local current_capacity=$(ioreg -n AppleSmartBattery | grep CurrentCapacity | awk '{ print $5 }')
+ local max_capacity=$(echo $raw_data | grep MaxCapacity | awk '{ print $5 }')
+ local current_capacity=$(echo $raw_data | grep CurrentCapacity | awk '{ print $5 }')
- [[ ! -z $max_capacity && ! -z $current_capacity ]] && local bat_percent=$(ruby -e "puts ($current_capacity.to_f / $max_capacity.to_f * 100).round.to_i")
+ if [[ -n "$max_capacity" && -n "$current_capacity" ]]; then
+ typeset -i 10 bat_percent
+ bat_percent=$(( (current_capacity * 100) / max_capacity ))
+ fi
- # logic for string output
- [[ $charging =~ true && $connected =~ true ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGING" && local remain=" ($tstring)"
- [[ ! $charging =~ true && $connected =~ true ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGED" && local remain=""
- if [[ ! $connected =~ true ]]; then
- [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local conn="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local conn="$POWERLEVEL9K_BATTERY_DISCONNECTED"
- local remain=" ($tstring)"
+ local remain=""
+ ## logic for string output
+ # Powerplug connected
+ if [[ $(echo $raw_data | grep ExternalConnected | awk '{ print $5 }') =~ "Yes" ]]; then
+ # Battery is charging
+ if [[ $(echo $raw_data | grep IsCharging | awk '{ print $5 }') =~ "Yes" ]]; then
+ current_state="charging"
+ remain=" ($tstring)"
+ else
+ current_state="charged"
+ fi
+ else
+ [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected"
+ remain=" ($tstring)"
fi
fi
@@ -278,12 +300,13 @@ prompt_battery() {
# return if no battery found
[[ -z $bat ]] && return
+
[[ $(cat $bat/capacity) -gt 100 ]] && local bat_percent=100 || local bat_percent=$(cat $bat/capacity)
[[ $(cat $bat/status) =~ Charging ]] && local connected=true
- [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGED"
- [[ $(cat $bat/status) =~ Charging && $bat_percent -lt 100 ]] && local conn="$POWERLEVEL9K_BATTERY_CHARGING"
+ [[ $(cat $bat/status) =~ Charging && $bat_percent =~ 100 ]] && current_state="charged"
+ [[ $(cat $bat/status) =~ Charging && $bat_percent -lt 100 ]] && current_state="charging"
if [[ -z $connected ]]; then
- [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && local conn="$POWERLEVEL9K_BATTERY_LOW_COLOR" || local conn="$POWERLEVEL9K_BATTERY_DISCONNECTED"
+ [[ $bat_percent -lt $POWERLEVEL9K_BATTERY_LOW_THRESHOLD ]] && current_state="low" || current_state="disconnected"
fi
if [[ -f /usr/bin/acpi ]]; then
local time_remaining=$(acpi | awk '{ print $5 }')
@@ -293,11 +316,19 @@ prompt_battery() {
local tstring=${(f)$(date -u -d "$(echo $time_remaining)" +%k:%M)}
fi
fi
- [[ ! -z $tstring ]] && local remain=" ($tstring)"
+ [[ -n $tstring ]] && local remain=" ($tstring)"
+ fi
+
+ # prepare string
+ local message="$(print_icon 'BATTERY_ICON')"
+ # Default behavior: Be verbose!
+ set_default POWERLEVEL9K_BATTERY_VERBOSE true
+ if [[ "$POWERLEVEL9K_BATTERY_VERBOSE" == true ]]; then
+ message="$message $bat_percent%%$remain"
fi
# display prompt_segment
- [[ ! -z $bat_percent ]] && "$1_prompt_segment" "$0" "$DEFAULT_COLOR" "$DEFAULT_COLOR_INVERTED" "%F{$conn}$(print_icon 'BATTERY_ICON')$fg_color $bat_percent%%$remain%f"
+ [[ -n $bat_percent ]] && "$1_prompt_segment" "${0}_${current_state}" "$DEFAULT_COLOR" "${battery_states[$current_state]}" "$message"
}
# Context: user@hostname (who am I and where am I)
@@ -332,13 +363,20 @@ prompt_dir() {
fi
- "$1_prompt_segment" "$0" "blue" "$DEFAULT_COLOR" "$(print_icon 'HOME_ICON')$current_path"
+ local current_icon=''
+ if [[ $(print -P "%~") == '~'* ]]; then
+ current_icon=$(print_icon 'HOME_ICON')
+ else
+ current_icon=$(print_icon 'FOLDER_ICON')
+ fi
+
+ "$1_prompt_segment" "$0" "blue" "$DEFAULT_COLOR" "$current_icon$current_path"
}
# GO-prompt
prompt_go_version() {
local go_version
- go_version=$(go version 2>&1 | grep -oe "^go[0-9.]*")
+ go_version=$(go version 2>&1 | sed -E "s/.*(go[0-9.]*).*/\1/")
if [[ -n "$go_version" ]]; then
"$1_prompt_segment" "$0" "green" "255" "$go_version"
@@ -416,11 +454,10 @@ prompt_load() {
# Node version
prompt_node_version() {
- local nvm_prompt
- nvm_prompt=$(node -v 2>/dev/null)
- [[ -z "${nvm_prompt}" ]] && return
+ local node_version=$(node -v 2>/dev/null)
+ [[ -z "${node_version}" ]] && return
- "$1_prompt_segment" "$0" "green" "white" "${nvm_prompt:1} $(print_icon 'NODE_ICON')"
+ "$1_prompt_segment" "$0" "green" "white" "${node_version:1} $(print_icon 'NODE_ICON')"
}
# print a little OS icon
@@ -489,8 +526,8 @@ prompt_nvm() {
local nvm_default=$(cat $NVM_DIR/alias/default)
[[ -z "${node_version}" ]] && return
[[ "$node_version" =~ "$nvm_default" ]] && return
- NODE_ICON=$'\u2B22' # ⬢
- $1_prompt_segment "$0" "green" "011" "${node_version:1} $NODE_ICON"
+
+ $1_prompt_segment "$0" "green" "011" "${node_version:1} $(print_icon 'NODE_ICON')"
}
# rbenv information
@@ -702,7 +739,13 @@ build_left_prompt() {
defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do
- "prompt_$element" "left"
+ # Check if it is a custom command, otherwise interpet it as
+ # a prompt.
+ if [[ $element[0,7] =~ "custom_" ]]; then
+ "prompt_custom" "left" $element[8,-1]
+ else
+ "prompt_$element" "left"
+ fi
done
left_prompt_end
@@ -713,7 +756,13 @@ build_right_prompt() {
defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status history time)
for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do
- "prompt_$element" "right"
+ # Check if it is a custom command, otherwise interpet it as
+ # a prompt.
+ if [[ $element[0,7] =~ "custom_" ]]; then
+ "prompt_custom" "right" $element[8,-1]
+ else
+ "prompt_$element" "right"
+ fi
done
}