Skip to content

Commit

Permalink
Added blocks: docker kube netns openstack
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosmao authored and julien.cosmao committed Mar 10, 2024
1 parent 936389e commit 59b9867
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 18 deletions.
4 changes: 4 additions & 0 deletions blox.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ source "$__BLOX_ROOT/src/blocks/symbol.zsh"
source "$__BLOX_ROOT/src/blocks/time.zsh"
source "$__BLOX_ROOT/src/blocks/virtualenv.zsh"
source "$__BLOX_ROOT/src/blocks/awsprofile.zsh"
source "$__BLOX_ROOT/src/blocks/openstack.zsh"
source "$__BLOX_ROOT/src/blocks/docker.zsh"
source "$__BLOX_ROOT/src/blocks/kube.zsh"
source "$__BLOX_ROOT/src/blocks/netns.zsh"

source "$__BLOX_ROOT/src/title.zsh"
source "$__BLOX_ROOT/src/segments.zsh"
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/bgjobs.zsh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ---------------------------------------------
# Background jobs block configurations

BLOX_BLOCK__BGJOBS_SYMBOL="${BLOX_BLOCK__BGJOBS_SYMBOL:- }"
BLOX_BLOCK__BGJOBS_SYMBOL="${BLOX_BLOCK__BGJOBS_SYMBOL:-  }"
BLOX_BLOCK__BGJOBS_COLOR="${BLOX_BLOCK__BGJOBS_COLOR:-magenta}"

# ---------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions src/blocks/docker.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# --------------------------------------------- #
# | Docker block options
# --------------------------------------------- #
BLOX_BLOCK__DOCKER_SYMBOL="${BLOX_BLOCK__DOCKER_SYMBOL:- }"
BLOX_BLOCK__DOCKER_COLOR="${BLOX_BLOCK__DOCKER_COLOR:-81}"
# --------------------------------------------- #
# | The block itself
# --------------------------------------------- #
function blox_block__docker() {(
[[ ! -f /.dockerenv ]] && return
docker_id=$(cat /proc/self/cgroup | grep -Po '\w{64}' | sort -u | cut -c1-12)

blox_helper__build_block \
"${BLOX_BLOCK__DOCKER_COLOR}" \
"${BLOX_BLOCK__DOCKER_SYMBOL} ${docker_id}"
)}
16 changes: 3 additions & 13 deletions src/blocks/git.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ BLOX_BLOCK__GIT_REPO_SYMBOL="${BLOX_BLOCK__GIT_REPO_SYMBOL:-󰊢}"
# ---------------------------------------------
# Themes

BLOX_BLOCK__GIT_THEME_CLEAN="%F{${BLOX_BLOCK__GIT_CLEAN_COLOR}]%}$BLOX_BLOCK__GIT_CLEAN_SYMBOL%f"
BLOX_BLOCK__GIT_THEME_STASHED="%F{${BLOX_BLOCK__GIT_STASHED_COLOR}]%}$BLOX_BLOCK__GIT_STASHED_SYMBOL%f"
BLOX_BLOCK__GIT_THEME_UNPULLED="%F{${BLOX_BLOCK__GIT_UNPULLED_COLOR}]%}$BLOX_BLOCK__GIT_UNPULLED_SYMBOL%f"
BLOX_BLOCK__GIT_THEME_UNPUSHED="%F{${BLOX_BLOCK__GIT_UNPUSHED_COLOR}]%}$BLOX_BLOCK__GIT_UNPUSHED_SYMBOL%f"

Expand Down Expand Up @@ -75,17 +73,11 @@ function blox_block__git_helper__remote_branch() {
fi
}

# Echo the appropriate symbol for branch's status
function blox_block__git_helper__status() {
if [[ -z "$(git status --porcelain --ignore-submodules 2> /dev/null)" ]]; then
echo " $BLOX_BLOCK__GIT_THEME_CLEAN"
fi
}

# Echo the appropriate symbol if there are stashed files
function blox_block__git_helper__stashed_status() {
if $(command git rev-parse --verify refs/stash &> /dev/null); then
echo " $BLOX_BLOCK__GIT_THEME_STASHED"
stash=$(git stash list 2> /dev/null | wc -l)
if [[ $stash > 0 ]]; then
echo " %F{${BLOX_BLOCK__GIT_STASHED_COLOR}]%}${BLOX_BLOCK__GIT_STASHED_SYMBOL} ${stash}%f"
fi
}

Expand Down Expand Up @@ -152,7 +144,6 @@ function blox_block__git() {
branch_name="$(blox_block__git_helper__branch)"
branch_remote="$(blox_block__git_helper__remote_branch $branch_name)"
tag_name="$(blox_block__git_helper__tag)"
branch_status="$(blox_block__git_helper__status)"
stashed_status="$(blox_block__git_helper__stashed_status)"
remote_status="$(blox_block__git_helper__remote_status)"
short_status="$(blox_block__git_helper__short_status)"
Expand All @@ -167,7 +158,6 @@ function blox_block__git() {
&& commit_hash="$(blox_block__git_helper__commit)" \
&& result+="%F{${BLOX_BLOCK__GIT_COMMIT_COLOR}}${BLOX_CONF__BLOCK_PREFIX}${commit_hash}${BLOX_CONF__BLOCK_SUFFIX}%f"

result+="${branch_status}"
result+="${stashed_status}"
result+="${remote_status}"
result+="${short_status}"
Expand Down
25 changes: 25 additions & 0 deletions src/blocks/kube.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# --------------------------------------------- #
# | Docker block options
# --------------------------------------------- #
BLOX_BLOCK__KUBE_SYMBOL="${BLOX_BLOCK__KUBE_SYMBOL:-󱃾}"
BLOX_BLOCK__KUBE_COLOR="${BLOX_BLOCK__KUBE_COLOR:-111}"
# --------------------------------------------- #
# | The block itself
# --------------------------------------------- #
function blox_block__kube() {(
[[ -z $KUBECONFIG ]] && return

kube_context=$(cat $KUBECONFIG | tr -d '"' | grep -oPm 1 'current-context: \K.+' 2> /dev/null)

if [[ -n $KUBENS ]]; then
kubens=$KUBENS
namespace_color=108
else
kubens=$(cat $KUBECONFIG | tr -d '"' | sed -n "/- context:/,/\s*name: $context/p" | grep -oPm 1 '\s*namespace: \K.+')
namespace_color=244
fi

blox_helper__build_block \
"${BLOX_BLOCK__KUBE_COLOR}" \
"${BLOX_BLOCK__KUBE_SYMBOL} ${kube_context} %F{$namespace_color}${kubens:-default}%f"
)}
16 changes: 16 additions & 0 deletions src/blocks/netns.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# --------------------------------------------- #
# | NETNS block options
# --------------------------------------------- #
BLOX_BLOCK__NETNS_SYMBOL="${BLOX_BLOCK__NETNS_SYMBOL:- }"
BLOX_BLOCK__NETNS_COLOR="${BLOX_BLOCK__NETNS_COLOR:-198}"
# --------------------------------------------- #
# | The block itself
# --------------------------------------------- #
function blox_block__netns() {(
netns=$(ip netns identify $$)
[[ -z $netns ]] && return

blox_helper__build_block \
"${BLOX_BLOCK__NETNS_COLOR}" \
"${BLOX_BLOCK__NETNS_SYMBOL} $netns"
)}
27 changes: 27 additions & 0 deletions src/blocks/openstack.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# --------------------------------------------- #
# | Openstack block options
# --------------------------------------------- #
BLOX_BLOCK__OPENSTACK_SYMBOL="${BLOX_BLOCK__OPENSTACK_SYMBOL:-󰅡}"
BLOX_BLOCK__OPENSTACK_COLOR="${BLOX_BLOCK__OPENSTACK_COLOR:-9}"

# --------------------------------------------- #
# | The block itself
# --------------------------------------------- #
function blox_block__openstack() {
[[ -z $OS_PROJECT_NAME && -z $OS_TENANT_NAME ]] && return
region_name=${OS_REGION_NAME:-..}

display_name=''
project_name=${OS_PROJECT_NAME:-$OS_TENANT_NAME}
project_id=${OS_PROJECT_ID:-$OS_TENANT_ID}

if [[ $project_name =~ '[0-9]+' && -n $project_id ]]; then
display_name="id: ${project_id}"
else
display_name=$project_name
fi

blox_helper__build_block \
"${BLOX_BLOCK__OPENSTACK_COLOR}" \
"${BLOX_BLOCK__OPENSTACK_SYMBOL} ${region_name} %F{244}${display_name}%f"
}
2 changes: 1 addition & 1 deletion src/render.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function blox_hook__render() {
# Otherwise, we'll first render the upper segments separately, then the lower segments. Doing
# this may solve some resizing issue (#2).
print -rP " %{${upper_left}%}${spacing}%{${upper_right}%} "
PROMPT=" ${lower_left} "
PROMPT="${lower_left} "
fi

# Lower right prompt
Expand Down
6 changes: 3 additions & 3 deletions src/utils.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function blox_helper__build_block() {

local result=""

result+="%F{${color}}"
result+="${prefix}${contents}${suffix}";
result+="%f"
result+="%F{${color}}${prefix}"
result+="${contents}%f";
result+="%F{${color}}${suffix}%f"

echo -n "$result"
}

0 comments on commit 59b9867

Please sign in to comment.