def abbreviated_working_dir(length=10): """ """ ellipsis = '..' #TermColors.DarkGray+".."+TermColors.Normal try: path = unexpand(os.getcwd()) except OSError: return "directory-disappeared" while len(remove_control_characters(path)) > length: dirs = path.split(os.path.sep); # Find the longest directory in the path. max_index = -1 max_length = 4 for i in range(len(dirs) - 1): if len(dirs[i]) > max_length: max_index = i max_length = len(dirs[i]) # Shorten it by one character. if max_index >= 0: dirs[max_index] = dirs[max_index][:max_length-3] + ellipsis path = "/".join(dirs) # Didn't find anything to shorten. This is as good as it gets. else: break return path
def get_aliases(): """ extract all aliases from the underlying bash shell. """ cmd = 'bash -c "echo alias|bash -i"' p1 = Popen(cmd, shell=True, stdout=PIPE, stdin=PIPE, stderr=PIPE) out, err = p1.communicate() lines = out.split('\n') lines = [x for x in lines if r_alias.match(x)] aliases = [] for line in lines: line = ' '.join(line.split()[1:]) # first word is 'alias' equals_sign = line.find('=') alias = line[:equals_sign] cmd = line[equals_sign + 1:].strip() cmd = remove_control_characters(unicode(cmd)) # cmd may or may not be quoted cmd = cmd[1:-1] if cmd[0] in ['"', "'"] else cmd aliases.append([alias, cmd]) return aliases