diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 71756d607..5dd2d11a8 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -838,6 +838,8 @@ By default, the following variables are available for use: * ``user``: The username of the current user * ``hostname``: The name of the host computer * ``cwd``: The current working directory + * ``short_cwd``: A shortened form of the current working directory; e.g., + ``/path/to/xonsh`` becomes ``/p/t/xonsh`` * ``cwd_dir``: The dirname of the current working directory, e.g. ``/path/to`` in ``/path/to/xonsh``. * ``cwd_base``: The basename of the current working directory, e.g. ``xonsh`` in diff --git a/xonsh/environ.py b/xonsh/environ.py index 185c1df7a..5300b98a1 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -58,6 +58,7 @@ DEFAULT_ENSURERS = { 'CASE_SENSITIVE_COMPLETIONS': (is_bool, to_bool, bool_to_str), re.compile('\w*DIRS'): (is_env_path, str_to_env_path, env_path_to_str), 'COMPLETIONS_DISPLAY': (is_completions_display_value, to_completions_display_value, str), + 'FORCE_POSIX_PATHS': (is_bool, to_bool, bool_to_str), 'HISTCONTROL': (is_string_set, csv_to_set, set_to_csv), 'IGNOREEOF': (is_bool, to_bool, bool_to_str), 'LC_COLLATE': (always_false, locale_convert('LC_COLLATE'), ensure_string), @@ -547,6 +548,16 @@ def _replace_home(x): _replace_home_cwd = lambda: _replace_home(builtins.__xonsh_env__['PWD']) +def _collapsed_pwd(): + sep = os.sep + if ON_WINDOWS and builtins.__xonsh_env__.get('FORCE_POSIX_PATHS'): + sep = os.altsep + pwd = _replace_home_cwd().split(sep) + l = len(pwd) + leader = sep if l>0 and len(pwd[0])==0 else '' + base = [i[0] if ix != l-1 else i for ix,i in enumerate(pwd) if len(i) > 0] + return leader + sep.join(base) + if ON_WINDOWS: USER = 'USERNAME' @@ -561,6 +572,7 @@ FORMATTER_DICT = dict( cwd=_replace_home_cwd, cwd_dir=lambda: os.path.dirname(_replace_home_cwd()), cwd_base=lambda: os.path.basename(_replace_home_cwd()), + short_cwd=_collapsed_pwd, curr_branch=current_branch, branch_color=branch_color, **TERM_COLORS)