Cleanup expressions

This commit is contained in:
3coma3 2023-07-15 19:10:58 +01:00
parent fd6305a5f7
commit 27faf9e0c3
No known key found for this signature in database
GPG key ID: 97B4175B5710A5A6

View file

@ -4,6 +4,7 @@ from ..utils import BasicSegment
from ..colortrans import rgb2short
from ..color_compliment import stringToHashToColorAndOpposite
EMPTY = ''
def kube_info(cmd):
try:
@ -12,53 +13,53 @@ def kube_info(cmd):
stderr=subprocess.PIPE,
env=os.environ.copy())
except OSError:
return ''
return EMPTY
pdata = p.communicate()
if p.returncode != 0:
return ''
data = p.communicate()
if p.returncode:
return EMPTY
return ''.join(pdata[0].decode("utf-8").splitlines())
return data[0].decode('utf-8').splitlines()[0]
def ellipse(string, length):
if length > 0:
return (string[:length] + '~') if len(string) > length else string
else:
return ''
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
kctx = kube_info("kubectl-ctx -c")
if not kctx: # without context there's nothing to do
return
kctx = ellipse(kctx,
powerline.segment_conf("k8s", "max_context"))
powerline.segment_conf('k8s', 'max_context'))
kns = ellipse(kube_info("kubectl-ns -c"),
powerline.segment_conf("k8s", "max_namespace"))
powerline.segment_conf('k8s', 'max_namespace'))
status = kctx + ('|' if kctx and kns else '') + kns
status = kctx + ('|' if kctx and kns else EMPTY) + kns
bg = powerline.theme.K8S_BG
fg = powerline.theme.K8S_FG
sym_fg = powerline.theme.K8S_SYMBOL_FG
if powerline.segment_conf("k8s", "colorize"):
if powerline.segment_conf('k8s', 'colorize'):
bg, fg = stringToHashToColorAndOpposite(status)
fg, bg = (rgb2short(*color) for color in [fg, bg])
if powerline.segment_conf("k8s", "colorize_symbol"):
if powerline.segment_conf('k8s', 'colorize_symbol'):
sym_fg = fg
lspace = '' if powerline.segment_conf("k8s", "ltrim") else ' '
rspace = '' if powerline.segment_conf("k8s", "rtrim") else ' '
lspace = EMPTY if powerline.segment_conf('k8s', 'ltrim') else ' '
rspace = EMPTY if powerline.segment_conf('k8s', 'rtrim') else ' '
if powerline.segment_conf("k8s", "symbol"):
if powerline.segment_conf('k8s', 'symbol'):
powerline.append(lspace + U'\U0001F578' + ' ', sym_fg, bg)
powerline.append(status + rspace, fg, bg)
else: