1
0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2026-03-28 18:53:47 +08:00
This commit is contained in:
Oleksandr Molchanov 2026-03-26 18:41:32 +01:00 committed by GitHub
commit e1ec392d4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 1 deletions

View File

@ -127,6 +127,10 @@ The default prompt layout is:
```sh
(<symbol>|<context>:<namespace>)
```
You can enable kube environment context (like dev/staging/test/prod) via `KUBE_PS1_ENV_CTX_ENABLE=true`. In this case, the layout will be:
```sh
[<kube-environment>] (<symbol>|<context>:<namespace>)
```
If the current-context is not set, kube-ps1 will return the following:
@ -204,6 +208,23 @@ the following variables:
| `KUBE_PS1_CTX_COLOR_FUNCTION` | No default, must be user supplied | Function to customize context color based on context name |
| `KUBE_PS1_HIDE_IF_NOCONTEXT` | `false` | Hide the kube-ps1 prompt if no context is set |
For more control over the Kubernetes environment context, you can adjust:
| Variable | Default | Meaning |
| :------- | :-----: | ------- |
| `KUBE_PS1_ENV_CTX_ENABLE` | `false` | Extract environment identifiers from context and display them as a separate block in square brackets, e.g `testing-mycluster` becomes `[test] mycluster` |
| `KUBE_PS1_ENV_PADDING` | one space | Padding (spaces or characters) added around the environment block |
| `KUBE_PS1_ENV_OPEN_SYMBOL` | `[` | Opening symbol used for the environment block |
| `KUBE_PS1_ENV_CLOSE_SYMBOL` | `]` | Closing symbol used for the environment block |
| `KUBE_PS1_ENV_PROD_LABEL` | `prod` | Set default production label |
| `KUBE_PS1_ENV_STG_LABEL` | `stag` | Set default staging label |
| `KUBE_PS1_ENV_TEST_LABEL` | `test` | Set default testing label |
| `KUBE_PS1_ENV_DEV_LABEL` | `dev` | Set default developing label |
| `KUBE_PS1_ENV_PROD_RE` | `(production\|prod)-` | Regex used to detect production in the context name |
| `KUBE_PS1_ENV_STG_RE` | `(staging\|stg)-` | Regex used to detect staging in the context name |
| `KUBE_PS1_ENV_TEST_RE` | `(testing\|test)-` | Regex used to detect test in the context name |
| `KUBE_PS1_ENV_DEV_RE` | `dev(elop(ment)?)?-` | Regex used to detect development in the context name |
To disable a feature, set it to an empty string:
```sh
@ -227,6 +248,15 @@ Blue was used for the default symbol to match the Kubernetes color as closely
as possible. Red was chosen as the context name to stand out, and cyan for the
namespace.
If `KUBE_PS1_ENV_CTX_ENABLE` is set to `true`, you can also modify:
| Variable | Default | Meaning |
| :------- | :-----: | ------- |
| `KUBE_PS1_ENV_PROD_COLOR` | `red` | Set default color of the production environment |
| `KUBE_PS1_ENV_STG_COLOR` | `yellow` | Set default color of the staging environment |
| `KUBE_PS1_ENV_TEST_COLOR` | `green` | Set default color of the testing environment |
| `KUBE_PS1_ENV_DEV_COLOR` | `blue` | Set default color of the development environment |
Set the variable to an empty string if you do not want color for each
prompt section:

View File

@ -36,6 +36,28 @@ KUBE_PS1_SUFFIX="${KUBE_PS1_SUFFIX-)}"
KUBE_PS1_HIDE_IF_NOCONTEXT="${KUBE_PS1_HIDE_IF_NOCONTEXT:-false}"
# Kube environment variables
KUBE_PS1_ENV_CTX_ENABLE="${KUBE_PS1_ENV_CTX_ENABLE:-false}"
KUBE_PS1_ENV_PADDING="${KUBE_PS1_ENV_PADDING:- }"
KUBE_PS1_ENV_OPEN_SYMBOL="${KUBE_PS1_ENV_OPEN_SYMBOL:-[}"
KUBE_PS1_ENV_CLOSE_SYMBOL="${KUBE_PS1_ENV_CLOSE_SYMBOL:-]}"
KUBE_PS1_ENV_PROD_COLOR="${KUBE_PS1_ENV_PROD_COLOR:-red}"
KUBE_PS1_ENV_STG_COLOR="${KUBE_PS1_ENV_STG_COLOR:-yellow}"
KUBE_PS1_ENV_TEST_COLOR="${KUBE_PS1_ENV_TEST_COLOR:-green}"
KUBE_PS1_ENV_DEV_COLOR="${KUBE_PS1_ENV_DEV_COLOR:-blue}"
KUBE_PS1_ENV_PROD_LABEL="${KUBE_PS1_ENV_PROD_LABEL:-prod}"
KUBE_PS1_ENV_STG_LABEL="${KUBE_PS1_ENV_STG_LABEL:-stag}"
KUBE_PS1_ENV_TEST_LABEL="${KUBE_PS1_ENV_TEST_LABEL:-test}"
KUBE_PS1_ENV_DEV_LABEL="${KUBE_PS1_ENV_DEV_LABEL:-dev}"
KUBE_PS1_ENV_PROD_RE="${KUBE_PS1_ENV_PROD_RE:-(production|prod)-}"
KUBE_PS1_ENV_STG_RE="${KUBE_PS1_ENV_STG_RE:-(staging|stg)-}"
KUBE_PS1_ENV_TEST_RE="${KUBE_PS1_ENV_TEST_RE:-(testing|test)-}"
KUBE_PS1_ENV_DEV_RE="${KUBE_PS1_ENV_DEV_RE:-dev(elop(ment)?)?-}"
_KUBE_PS1_KUBECONFIG_CACHE="${KUBECONFIG}"
_KUBE_PS1_DISABLE_PATH="${HOME}/.kube/kube-ps1/disabled"
_KUBE_PS1_LAST_TIME=0
@ -155,7 +177,7 @@ _kube_ps1_symbol() {
local symbol=""
local symbol_default=$'\u2388'
local symbol_img="☸️"
local symbol_img="☸️"
local k8s_glyph=$'\Uf10fe'
local k8s_symbol_color=blue
local oc_glyph=$'\ue7b7'
@ -316,6 +338,35 @@ _kube_ps1_get_context_ns() {
_kube_ps1_get_ns
}
_kube_ps1_cut_context() {
local pattern="$1"
KUBE_PS1_CONTEXT="$(sed -E "s/${pattern}//g" <<< "${KUBE_PS1_CONTEXT}")"
}
_kube_ps1_set_env_ctx() {
local ctx_color env_label
if grep -qE "${KUBE_PS1_ENV_PROD_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_PS1_ENV_PROD_RE}"
ctx_color="$(_kube_ps1_color_fg "${KUBE_PS1_ENV_PROD_COLOR}")"
env_label="${KUBE_PS1_ENV_PROD_LABEL}"
elif grep -qE "${KUBE_PS1_ENV_STG_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_PS1_ENV_STG_RE}"
ctx_color="$(_kube_ps1_color_fg "${KUBE_PS1_ENV_STG_COLOR}")"
env_label="${KUBE_PS1_ENV_STG_LABEL}"
elif grep -qE "${KUBE_PS1_ENV_TEST_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_PS1_ENV_TEST_RE}"
ctx_color="$(_kube_ps1_color_fg "${KUBE_PS1_ENV_TEST_COLOR}")"
env_label="${KUBE_PS1_ENV_TEST_LABEL}"
elif grep -qE "${KUBE_PS1_ENV_DEV_RE}" <<< "${KUBE_PS1_CONTEXT}"; then
_kube_ps1_cut_context "${KUBE_PS1_ENV_DEV_RE}"
ctx_color="$(_kube_ps1_color_fg "${KUBE_PS1_ENV_DEV_COLOR}")"
env_label="${KUBE_PS1_ENV_DEV_LABEL}"
fi
KUBE_PS1+="${KUBE_PS1_ENV_PADDING}${KUBE_PS1_ENV_OPEN_SYMBOL}${ctx_color}${env_label}${KUBE_PS1_RESET_COLOR}${KUBE_PS1_ENV_CLOSE_SYMBOL}${KUBE_PS1_ENV_PADDING}"
}
# Set kube-ps1 shell defaults
_kube_ps1_init
@ -392,6 +443,11 @@ kube_ps1() {
# Background Color
[[ -n "${KUBE_PS1_BG_COLOR}" ]] && KUBE_PS1+="$(_kube_ps1_color_bg "${KUBE_PS1_BG_COLOR}")"
# Context Env
if [[ -n "${KUBE_PS1_ENV_CTX_ENABLE}" ]] && [[ "${KUBE_PS1_ENV_CTX_ENABLE}" == true ]]; then
_kube_ps1_set_env_ctx
fi
# Prefix
if [[ -z "${KUBE_PS1_PREFIX_COLOR:-}" ]] && [[ -n "${KUBE_PS1_PREFIX}" ]]; then
KUBE_PS1+="${KUBE_PS1_PREFIX}"