1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-26 05:50:07 +00:00

Added async docker segment to display number of running, paused and exited containers.

This commit is contained in:
Richard Ziegler 2021-03-08 00:14:49 -05:00
parent 3920940ea8
commit 1878d67bfa
7 changed files with 142 additions and 0 deletions

View file

@ -297,6 +297,7 @@ enable as many segments as you like. It won't slow down your prompt or Zsh start
| `laravel_version` | [laravel php framework](https://laravel.com/) version | | `laravel_version` | [laravel php framework](https://laravel.com/) version |
| `java_version` | [java](https://www.java.com/) version | | `java_version` | [java](https://www.java.com/) version |
| `package` | `name@version` from [package.json](https://docs.npmjs.com/files/package.json) | | `package` | `name@version` from [package.json](https://docs.npmjs.com/files/package.json) |
| `docker` | current number of running and exited [docker](https://docker.io/) containers |
| `kubecontext` | current [kubernetes](https://kubernetes.io/) context | | `kubecontext` | current [kubernetes](https://kubernetes.io/) context |
| `terraform` | [terraform](https://www.terraform.io) workspace | | `terraform` | [terraform](https://www.terraform.io) workspace |
| `aws` | [aws profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) | | `aws` | [aws profile](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html) |

View file

@ -100,6 +100,7 @@
# battery # internal battery # battery # internal battery
# wifi # wifi speed # wifi # wifi speed
# example # example user-defined segment (see prompt_example function below) # example # example user-defined segment (see prompt_example function below)
# docker # Docker containers running and exited (https://www.docker.io)
) )
# Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you. # Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you.
@ -1192,6 +1193,12 @@
typeset -g POWERLEVEL9K_TERRAFORM_OTHER_FOREGROUND=38 typeset -g POWERLEVEL9K_TERRAFORM_OTHER_FOREGROUND=38
# typeset -g POWERLEVEL9K_TERRAFORM_OTHER_VISUAL_IDENTIFIER_EXPANSION='⭐' # typeset -g POWERLEVEL9K_TERRAFORM_OTHER_VISUAL_IDENTIFIER_EXPANSION='⭐'
#############[ docker: shows the whale when online, and the # running and exited containers (https://www.docker.io/) ]#############
# typeset -g POWERLEVEL9K_DOCKER_ICON='🐳'
# typeset -g POWERLEVEL9K_DOCKER_RUNNING_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_PAUSED_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_EXITED_ICON=''
#############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
# Show kubecontext only when the the command you are typing invokes one of these tools. # Show kubecontext only when the the command you are typing invokes one of these tools.
# Tip: Remove the next line to always show kubecontext. # Tip: Remove the next line to always show kubecontext.

View file

@ -99,6 +99,7 @@
# battery # internal battery # battery # internal battery
# wifi # wifi speed # wifi # wifi speed
# example # example user-defined segment (see prompt_example function below) # example # example user-defined segment (see prompt_example function below)
# docker # Docker containers running and exited (https://www.docker.io)
) )
# Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you. # Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you.
@ -1140,6 +1141,12 @@
# Custom icon. # Custom icon.
# typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐' # typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐'
#############[ docker: shows the whale when online, and the # running and exited containers (https://www.docker.io/) ]#############
# typeset -g POWERLEVEL9K_DOCKER_ICON='🐳'
# typeset -g POWERLEVEL9K_DOCKER_RUNNING_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_PAUSED_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_EXITED_ICON=''
#############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
# Show kubecontext only when the the command you are typing invokes one of these tools. # Show kubecontext only when the the command you are typing invokes one of these tools.
# Tip: Remove the next line to always show kubecontext. # Tip: Remove the next line to always show kubecontext.

View file

@ -99,6 +99,7 @@
# battery # internal battery # battery # internal battery
# wifi # wifi speed # wifi # wifi speed
# example # example user-defined segment (see prompt_example function below) # example # example user-defined segment (see prompt_example function below)
# docker # Docker containers running and exited (https://www.docker.io)
) )
# Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you. # Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you.
@ -1136,6 +1137,12 @@
# Custom icon. # Custom icon.
# typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐' # typeset -g POWERLEVEL9K_HASKELL_STACK_VISUAL_IDENTIFIER_EXPANSION='⭐'
#############[ docker: shows the whale when online, and the # running and exited containers (https://www.docker.io/) ]#############
# typeset -g POWERLEVEL9K_DOCKER_ICON='🐳'
# typeset -g POWERLEVEL9K_DOCKER_RUNNING_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_PAUSED_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_EXITED_ICON=''
#############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
# Show kubecontext only when the the command you are typing invokes one of these tools. # Show kubecontext only when the the command you are typing invokes one of these tools.
# Tip: Remove the next line to always show kubecontext. # Tip: Remove the next line to always show kubecontext.

View file

@ -100,6 +100,7 @@
# battery # internal battery # battery # internal battery
# wifi # wifi speed # wifi # wifi speed
# example # example user-defined segment (see prompt_example function below) # example # example user-defined segment (see prompt_example function below)
# docker # Docker containers running and exited (https://www.docker.io)
) )
# Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you. # Defines character set used by powerlevel10k. It's best to let `p10k configure` set it for you.
@ -1258,6 +1259,12 @@
typeset -g POWERLEVEL9K_TERRAFORM_OTHER_BACKGROUND=0 typeset -g POWERLEVEL9K_TERRAFORM_OTHER_BACKGROUND=0
# typeset -g POWERLEVEL9K_TERRAFORM_OTHER_VISUAL_IDENTIFIER_EXPANSION='⭐' # typeset -g POWERLEVEL9K_TERRAFORM_OTHER_VISUAL_IDENTIFIER_EXPANSION='⭐'
#############[ docker: shows the whale when online, and the # running and exited containers (https://www.docker.io/) ]#############
# typeset -g POWERLEVEL9K_DOCKER_ICON='🐳'
# typeset -g POWERLEVEL9K_DOCKER_RUNNING_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_PAUSED_ICON=''
# typeset -g POWERLEVEL9K_DOCKER_EXITED_ICON=''
#############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]############# #############[ kubecontext: current kubernetes context (https://kubernetes.io/) ]#############
# Show kubecontext only when the the command you are typing invokes one of these tools. # Show kubecontext only when the the command you are typing invokes one of these tools.
# Tip: Remove the next line to always show kubecontext. # Tip: Remove the next line to always show kubecontext.

View file

@ -144,6 +144,9 @@ function _p9k_init_icons() {
PACKAGE_ICON 'pkg' PACKAGE_ICON 'pkg'
JULIA_ICON 'jl' JULIA_ICON 'jl'
SCALA_ICON 'scala' SCALA_ICON 'scala'
DOCKER_ICON '\UE7B0'
ONLINE_ICON '\UF111'
PAUSE_ICON '\UF04C' # 
) )
;; ;;
'awesome-fontconfig') 'awesome-fontconfig')
@ -272,6 +275,9 @@ function _p9k_init_icons() {
PACKAGE_ICON 'pkg' PACKAGE_ICON 'pkg'
JULIA_ICON 'jl' JULIA_ICON 'jl'
SCALA_ICON 'scala' SCALA_ICON 'scala'
DOCKER_ICON '\UE7B0'
ONLINE_ICON '\UF111'
PAUSE_ICON '\UF04C' # 
) )
;; ;;
'awesome-mapped-fontconfig') 'awesome-mapped-fontconfig')
@ -403,6 +409,9 @@ function _p9k_init_icons() {
PACKAGE_ICON 'pkg' PACKAGE_ICON 'pkg'
JULIA_ICON 'jl' JULIA_ICON 'jl'
SCALA_ICON 'scala' SCALA_ICON 'scala'
DOCKER_ICON '\UE7B0'
ONLINE_ICON '\UF111'
PAUSE_ICON '\UF04C' # 
) )
;; ;;
'nerdfont-complete'|'nerdfont-fontconfig') 'nerdfont-complete'|'nerdfont-fontconfig')
@ -532,6 +541,9 @@ function _p9k_init_icons() {
PACKAGE_ICON '\uF8D6' #  PACKAGE_ICON '\uF8D6' # 
JULIA_ICON '\uE624' #  JULIA_ICON '\uE624' # 
SCALA_ICON '\uE737' #  SCALA_ICON '\uE737' # 
DOCKER_ICON '\UE7B0' # 
ONLINE_ICON '\UF111' # 
PAUSE_ICON '\UF04C' # 
) )
;; ;;
ascii) ascii)
@ -658,6 +670,9 @@ function _p9k_init_icons() {
PACKAGE_ICON 'pkg' PACKAGE_ICON 'pkg'
JULIA_ICON 'jl' JULIA_ICON 'jl'
SCALA_ICON 'scala' SCALA_ICON 'scala'
DOCKER_ICON 'docker'
ONLINE_ICON 'o'
PAUSE_ICON '"'
) )
;; ;;
*) *)
@ -786,6 +801,9 @@ function _p9k_init_icons() {
PACKAGE_ICON 'pkg' PACKAGE_ICON 'pkg'
JULIA_ICON 'jl' JULIA_ICON 'jl'
SCALA_ICON 'scala' SCALA_ICON 'scala'
DOCKER_ICON '\UE7B0'
ONLINE_ICON '\UF111'
PAUSE_ICON '\UF04C' # 
) )
;; ;;
esac esac

View file

@ -1247,6 +1247,101 @@ function _p9k_fvm_new() {
return 1 return 1
} }
#####################################################################
# Docker support - Appears if docker cli can talk to server.
# Shows # running and # stopped containers.
#
# Also commented with instructions on how to write an async segment.
#####################################################################
# The main prompt_ function adds the prompt segment definitions, but
# the data will be computed elsewhere.
function prompt_docker() {
# Save the length for the last line
local -i len=$#_p9k__prompt _p9k__has_upglob
# Register the segments. Search for _p9k_left_prompt_segment for argument docs
_p9k_prompt_segment $0 NONE green DOCKER_ICON 1 '$_p9k__docker_segment' '$_p9k__docker_segment'
# Copy and paste this line; no changes needed.
(( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1]
}
# The _init function must be named according to the pattern seen here.
function _p9k_prompt_docker_init() {
# Abort if the command isn't available.
(( $+commands[docker] )) || return
# Declare global variable to hold the segment content
typeset -g _p9k__docker_segment=
# Register this segment for async computation. Again note the pattern
_p9k__async_segments_compute+='_p9k_worker_invoke docker _p9k_prompt_docker_compute'
}
# Runs the workers
function _p9k_prompt_docker_compute() {
(( $+commands[docker] )) || return
# Copy and paste this line. Follow the pattern and adapt for your segment name.
_p9k_worker_async _p9k_prompt_docker_async _p9k_prompt_docker_sync
# See the other invocations of _p9k_worker_async for examples of argument
# passing to the async impl. The sync impl should not take args.
}
# `_async` runs the slow processes and converts the output into variables.
#
# The content will then be rendered by the segment template that was previously
# registered by the _p9k_prompt_segment in the `prompt_` function.
#
function _p9k_prompt_docker_async() {
(( $+commands[docker] )) || return
local -A container_status_counts
container_status_counts=(E 0 U 0 P 0)
# Minimize all use of subshells and command invocations in general.
# Async is not a license to be needlessly slow.
for line in ${(f)"$(command docker ps -a --format '{{ .Status }}' 2>/dev/null)"}; do
if [[ "$line" == *(Paused)* ]]; then
(( container_status_counts[P]++ ))
else
(( container_status_counts[${line:0:1}]++ ))
fi
_p9k__docker_up=1
done
_p9k__docker_segment=
_p9k_get_icon '' ONLINE_ICON
(( ${container_status_counts[U]} )) && \
_p9k__docker_segment="%F{green}${POWERLEVEL9K_DOCKER_RUNNING_ICON:-$_p9k__ret} ${container_status_counts[U]} %f"
_p9k_get_icon '' PAUSE_ICON
(( ${container_status_counts[P]} )) && \
_p9k__docker_segment+="%F{yellow}${POWERLEVEL9K_DOCKER_PAUSED_ICON:-$_p9k__ret} ${container_status_counts[P]} %f"
_p9k_get_icon '' FAIL_ICON
(( ${container_status_counts[E]} )) && \
_p9k__docker_segment+="%F{red}${POWERLEVEL9K_DOCKER_EXITED_ICON:-$_p9k__ret} ${container_status_counts[E]} %f"
# All vars that may have changed state must be sent to _p9k_print_params
_p9k_print_params \
_p9k__docker_segment
# The function must end with a reset=value
echo -E - 'reset=1'
}
# Copy, paste, and rename this function.
function _p9k_prompt_docker_sync() {
if [[ -n $REPLY ]]; then
eval $REPLY
_p9k_worker_reply $REPLY
fi
}
##########################################################################
prompt_fvm() { prompt_fvm() {
_p9k_fvm_new || _p9k_fvm_old _p9k_fvm_new || _p9k_fvm_old
} }