diff --git a/powerline_shell/segments/k8s.py b/powerline_shell/segments/k8s.py new file mode 100644 index 00000000..88257ea0 --- /dev/null +++ b/powerline_shell/segments/k8s.py @@ -0,0 +1,66 @@ +import os +import subprocess +from ..utils import BasicSegment +from ..colortrans import rgb2short +from ..color_compliment import stringToHashToColorAndOpposite + + +EMPTY = '' + + +def kube_info(cmd): + try: + p = subprocess.Popen(cmd.split(), + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + env=os.environ.copy()) + except OSError: + return EMPTY + + data = p.communicate() + if p.returncode: + return EMPTY + + return data[0].decode('utf-8').splitlines()[0] + + +def ellipse(s, length): + if length > 0: + return (s[:length] + '~') if len(s) > length else s + else: + return EMPTY + + +class Segment(BasicSegment): + def add_to_powerline(self): + kctx = kube_info('kubectl-ctx -c') + if not kctx: + return # no context, nothing to do + + powerline = self.powerline + conf = lambda key: powerline.segment_conf('k8s', key) + + kctx = ellipse(kctx, conf('max_context')) + kns = ellipse(kube_info("kubectl-ns -c"), conf('max_namespace')) + status = f"{kctx}{'|' if kctx and kns else EMPTY}{kns}" + + # the symbol and separator can be themed too + bg = powerline.theme.K8S_BG + fg = powerline.theme.K8S_FG + sym_fg = powerline.theme.K8S_SYMBOL_FG + + if conf('colorize'): + bg, fg = stringToHashToColorAndOpposite(status) + fg, bg = (rgb2short(*color) for color in [fg, bg]) + + if conf('colorize_symbol'): + sym_fg = fg + + lspace = EMPTY if conf('ltrim') else ' ' + rspace = EMPTY if conf('rtrim') else ' ' + + if conf('symbol'): + powerline.append(f"{lspace}\U0001F578 ", sym_fg, bg) + powerline.append(f"{status}{rspace}", fg, bg) + else: + powerline.append(f"{lspace}{status}{rspace}", fg, bg) diff --git a/powerline_shell/themes/default.py b/powerline_shell/themes/default.py index da0c0034..350e1ba0 100644 --- a/powerline_shell/themes/default.py +++ b/powerline_shell/themes/default.py @@ -72,6 +72,10 @@ class DefaultColor(object): AWS_PROFILE_FG = 39 AWS_PROFILE_BG = 238 + K8S_FG = 57 + K8S_BG = 7 + K8S_SYMBOL_FG = 57 + TIME_FG = 250 TIME_BG = 238