mirror of
https://github.com/b-ryan/powerline-shell.git
synced 2026-09-10 07:26:28 -04:00
refactor get_short_path into two functions
use this to replace the home directory in the new plain mode
This commit is contained in:
parent
431601e0a8
commit
dc7b8a3a65
|
|
@ -1,30 +1,38 @@
|
|||
import os
|
||||
|
||||
def get_short_path(cwd):
|
||||
|
||||
def replace_home_dir(cwd):
|
||||
home = os.getenv('HOME')
|
||||
if cwd.startswith(home):
|
||||
return '~' + cwd[len(home):]
|
||||
return cwd
|
||||
|
||||
|
||||
def split_path_into_names(cwd):
|
||||
names = cwd.split(os.sep)
|
||||
if names[0] == '': names = names[1:]
|
||||
path = ''
|
||||
for i in range(len(names)):
|
||||
path += os.sep + names[i]
|
||||
if os.path.samefile(path, home):
|
||||
return ['~'] + names[i+1:]
|
||||
|
||||
if names[0] == '':
|
||||
names = names[1:]
|
||||
|
||||
if not names[0]:
|
||||
return ['/']
|
||||
|
||||
return names
|
||||
|
||||
|
||||
def add_cwd_segment():
|
||||
cwd = powerline.cwd or os.getenv('PWD')
|
||||
names = get_short_path(cwd.decode('utf-8'))
|
||||
cwd = (powerline.cwd or os.getenv('PWD')).decode('utf-8')
|
||||
cwd = replace_home_dir(cwd)
|
||||
names = split_path_into_names(cwd)
|
||||
|
||||
max_depth = powerline.args.cwd_max_depth
|
||||
if len(names) > max_depth:
|
||||
names = names[:2] + [u'\u2026'] + names[2 - max_depth:]
|
||||
|
||||
if powerline.args.cwd_mode=='plain':
|
||||
powerline.append(cwd, Color.CWD_FG, Color.PATH_BG)
|
||||
if powerline.args.cwd_mode == 'plain':
|
||||
powerline.append(' %s ' % (cwd,), Color.CWD_FG, Color.PATH_BG)
|
||||
else:
|
||||
if not (powerline.args.cwd_mode=='dironly' or powerline.args.cwd_only):
|
||||
if not (powerline.args.cwd_mode == 'dironly' or powerline.args.cwd_only):
|
||||
for n in names[:-1]:
|
||||
if n == '~' and Color.HOME_SPECIAL_DISPLAY:
|
||||
powerline.append(' %s ' % n, Color.HOME_FG, Color.HOME_BG)
|
||||
|
|
|
|||
Loading…
Reference in a new issue