Skip to content

Commit

Permalink
refactor get_short_path into two functions
Browse files Browse the repository at this point in the history
use this to replace the home directory in the new plain mode
  • Loading branch information
b-ryan committed Aug 26, 2015
1 parent 431601e commit dc7b8a3
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions segments/cwd.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit dc7b8a3

Please sign in to comment.