Merge remote-tracking branch 'mart-e/master' for #127

I merged #127 into master and had to resolve conflicts.
In doing so, I noticed a few things. For one, #127 had an
issue where the name shortening would apply to all except
the last directory. I didn't want to repeat the same if
statement, so I sought to refactor things to make it
easier to deal with. The code as it stood was rather
repetitive and hard to read. So I factored some things
out and tried to add some descriptive comments.

Conflicts:
	segments/cwd.py
This commit is contained in:
Buck Ryan 2015-10-02 22:00:11 -04:00
commit fdf80c1c7e
3 changed files with 44 additions and 11 deletions

View file

@ -53,6 +53,9 @@ There are a few optional arguments which can be seen by running `powerline-shell
How to display the current directory
--cwd-max-depth CWD_MAX_DEPTH
Maximum number of directories to show in path
--cwd-max-dir-size CWD_MAX_DIR_SIZE
Maximum number of letters displayed for each directory
in the path
--colorize-hostname Colorize the hostname based on a hash of itself.
--mode {patched,compatible,flat}
The characters used to make separators between

View file

@ -121,6 +121,8 @@ if __name__ == "__main__":
help='Deprecated. Use --cwd-mode=dironly')
arg_parser.add_argument('--cwd-max-depth', action='store', type=int,
default=5, help='Maximum number of directories to show in path')
arg_parser.add_argument('--cwd-max-dir-size', action='store', type=int,
help='Maximum number of letters displayed for each directory in the path')
arg_parser.add_argument('--colorize-hostname', action='store_true',
help='Colorize the hostname based on a hash of itself.')
arg_parser.add_argument('--mode', action='store', default='patched',

View file

@ -20,6 +20,29 @@ def split_path_into_names(cwd):
return names
def requires_special_home_display(name):
"""Returns true if the given directory name matches the home indicator and
the chosen theme should use a special home indicator display."""
return (name == '~' and Color.HOME_SPECIAL_DISPLAY)
def maybe_shorten_name(name):
"""If the user has asked for each directory name to be shortened, will
return the name up to their specified length. Otherwise returns the full
name."""
if powerline.args.cwd_max_dir_size:
return name[:powerline.args.cwd_max_dir_size]
return name
def get_fg_bg(name):
"""Returns the foreground and background color to use for the given name.
"""
if requires_special_home_display(name):
return (Color.HOME_FG, Color.HOME_BG,)
return (Color.PATH_FG, Color.PATH_BG,)
def add_cwd_segment():
cwd = (powerline.cwd or os.getenv('PWD')).decode('utf-8')
cwd = replace_home_dir(cwd)
@ -32,17 +55,22 @@ def add_cwd_segment():
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):
for n in names[:-1]:
if n == '~' and Color.HOME_SPECIAL_DISPLAY:
powerline.append(' %s ' % n, Color.HOME_FG, Color.HOME_BG)
else:
powerline.append(' %s ' % n, Color.PATH_FG, Color.PATH_BG,
powerline.separator_thin, Color.SEPARATOR_FG)
if (powerline.args.cwd_mode == 'dironly' or powerline.args.cwd_only):
# The user has indicated they only want the current directory to be
# displayed, so chop everything else off
names = names[-1:]
if names[-1] == '~' and Color.HOME_SPECIAL_DISPLAY:
powerline.append(' %s ' % names[-1], Color.HOME_FG, Color.HOME_BG)
else:
powerline.append(' %s ' % names[-1], Color.CWD_FG, Color.PATH_BG)
for i, name in enumerate(names):
fg, bg = get_fg_bg(name)
separator = powerline.separator_thin
separator_fg = Color.SEPARATOR_FG
is_last_dir = (i == len(names) - 1)
if requires_special_home_display(name) or is_last_dir:
separator = None
separator_fg = None
powerline.append(' %s ' % maybe_shorten_name(name), fg, bg,
separator, separator_fg)
add_cwd_segment()