1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-22 20:30:07 +00:00

Merge pull request #153 from Tritlo/custom_string

Adding a segment that can execute a custom command.
This commit is contained in:
Ben Hilburn 2015-11-24 15:27:07 -08:00
commit 3fea92369f
3 changed files with 81 additions and 2 deletions

View file

@ -1,5 +1,9 @@
## v0.3.0 (next) ## v0.3.0 (next)
### New segment `custom_command` added
A new segment that allows users to define a custom command was added.
### `virtualenv` changes ### `virtualenv` changes
This segment now respects `VIRTUAL_ENV_DISABLE_PROMPT`. If this variable is set This segment now respects `VIRTUAL_ENV_DISABLE_PROMPT`. If this variable is set

View file

@ -73,6 +73,7 @@ The segments that are currently available are:
* [aws](#aws) - The current AWS profile, if active. * [aws](#aws) - The current AWS profile, if active.
* [battery](#battery) - Current battery status. * [battery](#battery) - Current battery status.
* [context](#context) - Your username and host. * [context](#context) - Your username and host.
* [custom_command](#custom_command) - A custom command to display the output of.
* [dir](#dir) - Your current working directory. * [dir](#dir) - Your current working directory.
* **go_version** - Show the current GO version. * **go_version** - Show the current GO version.
* **history** - The command number for the current line. * **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) 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 ##### context

View file

@ -208,6 +208,15 @@ prompt_aws() {
fi 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() { prompt_battery() {
icons[BATTERY_ICON]=$'\UE894' icons[BATTERY_ICON]=$'\UE894'
# set default values of not specified in shell # set default values of not specified in shell
@ -680,7 +689,13 @@ build_left_prompt() {
defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs) defined POWERLEVEL9K_LEFT_PROMPT_ELEMENTS || POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(context dir rbenv vcs)
for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do for element in "${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[@]}"; do
# 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" "prompt_$element" "left"
fi
done done
left_prompt_end left_prompt_end
@ -691,7 +706,13 @@ build_right_prompt() {
defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status history time) defined POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS || POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status history time)
for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do for element in "${POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[@]}"; do
# 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" "prompt_$element" "right"
fi
done done
} }