From 0166d9af26bfbfe6b51f26cce4e0bd8e2dcf0940 Mon Sep 17 00:00:00 2001 From: Mohammad Ali Amir Date: Thu, 5 Mar 2026 16:25:43 +0100 Subject: [PATCH] feat(prompt): add Kubernetes context and namespace prompt info --- README.md | 2 ++ app_config_sample | 1 + commands-handler.sh | 2 ++ fancygit-completion | 2 ++ help.sh | 2 ++ theme-functions.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ themes/default.sh | 6 ++++-- themes/human.sh | 6 ++++-- themes/simple.sh | 5 ++++- 9 files changed, 63 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d46c9f8..0c7caff 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,8 @@ Type `fancygit -h` to see all available feature switchers on **"FEATURE SWITCHER | fancygit --unset-host-name | Restore the host name to default. | fancygit --enable-git-clear | Clear the terminal as part of some git aliases | fancygit --disable-git-clear | Do not clear the terminal with any git aliases +| fancygit --enable-k8s-info | Show current Kubernetes context and namespace. +| fancygit --disable-k8s-info | Hide Kubernetes context and namespace. | fancygit --separator-default | Change the separator to default style. | fancygit --separator-blocks | Change the separator to blocks style. | fancygit --separator-blocks-tiny | Change the separator to blocks-tiny style. diff --git a/app_config_sample b/app_config_sample index 909dfd5..133f93e 100644 --- a/app_config_sample +++ b/app_config_sample @@ -13,4 +13,5 @@ host_name: bold_prompt:false show_host_prompt:false show_user_symbol_prompt:false +show_k8s_info:false fresh_file diff --git a/commands-handler.sh b/commands-handler.sh index f18eccb..e0c3b63 100755 --- a/commands-handler.sh +++ b/commands-handler.sh @@ -46,6 +46,8 @@ case "$1" in "--disable-user-symbol") fancygit_config_save "show_user_symbol_prompt" "false";; "--enable-git-clear") fancygit_config_save "git_use_clear" "true";; "--disable-git-clear") fancygit_config_save "git_use_clear" "false";; + "--enable-k8s-info") fancygit_config_save "show_k8s_info" "true";; + "--disable-k8s-info") fancygit_config_save "show_k8s_info" "false";; # Set Name and Host. "--set-user-name") fancygit_config_save "user_name" "$2";; diff --git a/fancygit-completion b/fancygit-completion index 10765ae..8725ddf 100644 --- a/fancygit-completion +++ b/fancygit-completion @@ -33,6 +33,8 @@ _fancygit() { --disable-user-symbol \ --enable-git-clear \ --disable-git-clear \ + --enable-k8s-info \ + --disable-k8s-info \ --set-user-name \ --unset-user-name \ --set-host-name \ diff --git a/help.sh b/help.sh index eeab17f..56faaf0 100644 --- a/help.sh +++ b/help.sh @@ -39,6 +39,8 @@ FEATURE SWITCHER COMMANDS: fancygit --disable-host-name Hide host name. (It works for human theme only) fancygit --enable-git-clear Clear the terminal as part of some git aliases fancygit --disable-git-clear Do not clear the terminal with any git aliases + fancygit --enable-k8s-info Show current Kubernetes context and namespace. + fancygit --disable-k8s-info Hide Kubernetes context and namespace. THEME COMMANDS: fancygit --theme-default Change prompt to the default theme. diff --git a/theme-functions.sh b/theme-functions.sh index c9b8e88..fa8480d 100644 --- a/theme-functions.sh +++ b/theme-functions.sh @@ -237,6 +237,48 @@ fancygit_theme_get_double_line() { echo "" } +# ---------------------------------------------------------------------------------------------------------------------- +# Return the prompt time. +# +# return string Formated time. +# ---------------------------------------------------------------------------------------------------------------------- +fancygit_theme_get_k8s_info() { + local show_k8s_info + local context + local namespace + + show_k8s_info=$(fancygit_config_get "show_k8s_info" "false") + + if [ "true" != "$show_k8s_info" ] + then + echo "" + return + fi + + if ! command -v kubectl > /dev/null 2>&1 + then + echo "" + return + fi + + context=$(kubectl config current-context 2> /dev/null) + + if [ "" = "$context" ] + then + echo "" + return + fi + + namespace=$(kubectl config view --minify --output 'jsonpath={..namespace}' 2> /dev/null) + + if [ "" = "$namespace" ] + then + namespace="default" + fi + + echo "[$context:$namespace] " +} + # ---------------------------------------------------------------------------------------------------------------------- # Change color scheme. # Show a warning in case the requested color scheme is not supported by current theme. diff --git a/themes/default.sh b/themes/default.sh index a465ed7..cb019da 100644 --- a/themes/default.sh +++ b/themes/default.sh @@ -79,6 +79,7 @@ fancygit_theme_builder() { local time="${time_color_bg_tag}${time_color_tag}" local time_end="" local prompt_time + local prompt_k8s local prompt_user local prompt_env local prompt_path @@ -107,6 +108,7 @@ fancygit_theme_builder() { prompt_path=$(fancygit_theme_get_path_sign) prompt_symbol="${user_symbol} \$ ${user_symbol_end}" prompt_double_line=$(fancygit_theme_get_double_line) + prompt_k8s=$(fancygit_theme_get_k8s_info) if fancygit_config_is "show_user_at_machine" "true" then @@ -124,7 +126,7 @@ fancygit_theme_builder() { # No branch found, so we're not in a git repo. prompt_env=$(__fancygit_get_venv_icon) prompt_path="${path}${prompt_env} ${prompt_path} ${path_end}${workdir_color_tag}${bg_none}${separator}${none}" - PS1="${clear}${bold_prompt}${prompt_time}${prompt_user}${prompt_symbol}${prompt_path}${clear}${normal_prompt}${prompt_double_line} " + PS1="${clear}${bold_prompt}${prompt_time}${prompt_k8s}${prompt_user}${prompt_symbol}${prompt_path}${clear}${normal_prompt}${prompt_double_line} " return fi @@ -156,7 +158,7 @@ fancygit_theme_builder() { notification_area=$(fancygit_get_notification_area "$is_rich_notification") prompt_path="${path_git}${notification_area} ${prompt_path} ${path_end}" prompt_branch="${branch} $(fancygit_git_get_branch_icon "${branch_name}") ${branch_name} ${branch_end}" - PS1="${clear}${bold_prompt}${prompt_time}${prompt_user}${prompt_symbol}${prompt_path}${prompt_branch}${clear}${normal_prompt}${prompt_double_line} " + PS1="${clear}${bold_prompt}${prompt_time}${prompt_k8s}${prompt_user}${prompt_symbol}${prompt_path}${prompt_branch}${clear}${normal_prompt}${prompt_double_line} " } # Here's where the magic happens! diff --git a/themes/human.sh b/themes/human.sh index 9d74dbe..46dd4cc 100644 --- a/themes/human.sh +++ b/themes/human.sh @@ -55,6 +55,7 @@ fancygit_theme_builder() { local staged_files local is_rich_notification local prompt_time + local prompt_k8s local path_sign local is_double_line local venv_name @@ -81,6 +82,7 @@ fancygit_theme_builder() { path_sign=$(fancygit_theme_get_path_sign) is_double_line=$(fancygit_theme_get_double_line) venv_name=$(fancygit_theme_get_venv_name) + prompt_k8s=$(fancygit_theme_get_k8s_info) prompt_symbol="${user_symbol}\$${user_symbol_end}" prompt_path="${path}${path_sign}${path_end}${color_reset}" @@ -135,12 +137,12 @@ fancygit_theme_builder() { then prompt_path="${path_git}${path_sign}${path_end}" prompt_branch="${branch}${branch_name}${branch_end}" - PS1="${prompt_time}${prompt_user_at_host}${prompt_path}${venv_name}${preposition_color} on ${prompt_branch}$(fancygit_get_notification_area "$is_rich_notification")" + PS1="${prompt_time}${prompt_k8s}${prompt_user_at_host}${prompt_path}${venv_name}${preposition_color} on ${prompt_branch}$(fancygit_get_notification_area "$is_rich_notification")" PS1="${bold_prompt}${PS1}${prompt_symbol}${is_double_line}${normal_prompt} " return fi - PS1="${bold_prompt}${prompt_time}${prompt_user_at_host}${prompt_path}${venv_name}${prompt_symbol}${is_double_line}${normal_prompt} " + PS1="${bold_prompt}${prompt_time}${prompt_k8s}${prompt_user_at_host}${prompt_path}${venv_name}${prompt_symbol}${is_double_line}${normal_prompt} " } # Here's where the magic happens! diff --git a/themes/simple.sh b/themes/simple.sh index 08e80bc..fe52678 100644 --- a/themes/simple.sh +++ b/themes/simple.sh @@ -27,6 +27,7 @@ fancygit_theme_builder() { local host_color_font_tag="\\[\\e[38;5;${FANCYGIT_COLOR_SCHEME_HOST_FOREGROUND}m\\]" local at_color_font_tag="\\[\\e[38;5;${FANCYGIT_COLOR_SCHEME_AT_FOREGROUND}m\\]" local workdir_color_font_tag="\\[\\e[38;5;${FANCYGIT_COLOR_SCHEME_WORKDIR_FOREGROUND}m\\]" + local k8s_color_font_tag local color_reset="\\[\\e[39m\\]" local user_name @@ -41,6 +42,7 @@ fancygit_theme_builder() { local at="${at_color_font_tag}@${color_reset}" local path="${workdir_color_font_tag}" local prompt_time + local prompt_k8s local path_sign local is_double_line local venv_name @@ -62,6 +64,7 @@ fancygit_theme_builder() { venv_name=$(fancygit_theme_get_venv_name) branch_area=$(__fancygit_theme_get_branch_area) where="${path}${path_sign}${color_reset}" + prompt_k8s=$(fancygit_theme_get_k8s_info) if fancygit_config_is "show_user_at_machine" "true" then @@ -78,7 +81,7 @@ fancygit_theme_builder() { venv_name="($venv_name) " fi - PS1="${bold_prompt}${venv_name}${prompt_time}${user_at_host}$where\$${branch_area}${is_double_line}${normal_prompt} " + PS1="${bold_prompt}${venv_name}${prompt_time}${prompt_k8s}${user_at_host}$where\$${branch_area}${is_double_line}${normal_prompt} " } # ----------------------------------------------------------------------------------------------------------------------