def add_username_segment(powerline): import os if powerline.args.shell == 'bash': user_prompt = ' \\u ' elif powerline.args.shell == 'zsh': user_prompt = ' %n ' else: user_prompt = ' ' + os.environ[_as_bytes('USER')] + ' ' if os.environ[_as_bytes('USER')] == 'root': bgcolor = Color.USERNAME_ROOT_BG else: bgcolor = Color.USERNAME_BG powerline.append(user_prompt, Color.USERNAME_FG, bgcolor)
def add_cwd_segment(powerline): cwd = powerline.cwd or os.environ[_as_bytes('PWD')] names = get_short_path(cwd.decode('utf-8')) max_depth = powerline.args.cwd_max_depth if len(names) > max_depth: i = 2 - max_depth assert i > 0 names = names[:2] + [u'\u2026'] + names[i:] if not powerline.args.cwd_only: for n in names[:-1]: if n == u'~' and Color.HOME_SPECIAL_DISPLAY: powerline.append(' ' + n.encode('utf-8') + ' ', Color.HOME_FG, Color.HOME_BG) else: powerline.append(' ' + n.encode('utf-8') + ' ', Color.PATH_FG, Color.PATH_BG, powerline.separator_thin.encode('utf-8'), str(Color.SEPARATOR_FG)) if names[-1] == u'~' and Color.HOME_SPECIAL_DISPLAY: powerline.append(' ' + names[-1].encode('utf-8') + ' ', Color.HOME_FG, Color.HOME_BG) else: powerline.append(' ' + names[-1].encode('utf-8') + ' ', Color.CWD_FG, Color.PATH_BG)
def get_short_path(cwd): home = os.environ[_as_bytes('HOME')] names = cwd.split(sep) if names[0] == u'': names = names[1:] path = u'' for i in range(len(names)): path += sep + names[i] if os.path.samefile(path.encode('utf-8'), home): return [u'~'] + names[i + 1:] if not names[0]: return [u'/'] return names
def get_short_path(cwd): home = os.environ[_as_bytes('HOME')] names = cwd.split(sep) if names[0] == u'': names = names[1:] path = u'' for i in range(len(names)): path += sep + names[i] if os.path.samefile(path.encode('utf-8'), home): return [u'~'] + names[i+1:] if not names[0]: return [u'/'] return names
def get_valid_cwd(): """ We check if the current working directory is valid or not. Typically happens when you checkout a different branch on git that doesn't have this directory. We return the original cwd because the shell still considers that to be the working directory, so returning our guess will confuse people """ cwd = os.environ[_as_bytes('PWD')] parts = cwd.split(os.sep) up = cwd while parts and not os.path.exists(up): parts.pop() up = os.sep.join(parts) chdir(up) return cwd