From 94591b88f34e6a8b591821c5cd281d138c0ac7ca Mon Sep 17 00:00:00 2001 From: Guillaume Leclerc Date: Thu, 19 May 2016 16:44:56 +0200 Subject: [PATCH] add ensurers --- xonsh/environ.py | 4 ++-- xonsh/tools.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/xonsh/environ.py b/xonsh/environ.py index 5a14fb342..6e2c395dc 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -26,7 +26,7 @@ from xonsh.tools import ( is_completions_display_value, to_completions_display_value, is_string_set, csv_to_set, set_to_csv, get_sep, is_int, is_bool_seq, csv_to_bool_seq, bool_seq_to_csv, DefaultNotGiven, setup_win_unicode_console, - intensify_colors_on_win_setter + intensify_colors_on_win_setter, is_dynamic_cwd_width, to_dynamic_cwd_tuple, dynamic_cwd_tuple_to_str ) from xonsh.codecache import run_script_with_cache from xonsh.dirstack import _get_cwd @@ -85,7 +85,7 @@ DEFAULT_ENSURERS = { 'PATHEXT': (is_env_path, str_to_env_path, env_path_to_str), 'RAISE_SUBPROC_ERROR': (is_bool, to_bool, bool_to_str), 'RIGHT_PROMPT': (is_string, ensure_string, ensure_string), - 'DYNAMIC_CWD_WIDTH': (is_string, ensure_string, ensure_string), + 'DYNAMIC_CWD_WIDTH': (is_dynamic_cwd_width, to_dynamic_cwd_tuple, dynamic_cwd_tuple_to_str), 'TEEPTY_PIPE_DELAY': (is_float, float, str), 'UPDATE_OS_ENVIRON': (is_bool, to_bool, bool_to_str), 'XONSHRC': (is_env_path, str_to_env_path, env_path_to_str), diff --git a/xonsh/tools.py b/xonsh/tools.py index fdeee767d..97fe2c54b 100644 --- a/xonsh/tools.py +++ b/xonsh/tools.py @@ -736,6 +736,27 @@ def is_history_tuple(x): return False +def is_dynamic_cwd_width(x): + if x[-1] == '%': + x = x[:-1] + return x.isdigit() + + +def to_dynamic_cwd_tuple(x): + unit = 'c' + if x[-1] == '%': + x = x[:-1] + unit = '%' + return (float(x), unit) + + +def dynamic_cwd_tuple_to_str(x): + if x[1] == '%': + return str(x[0]) + '%' + else: + return str(x[0]) + + RE_HISTORY_TUPLE = re.compile('([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\s*([A-Za-z]*)') def to_history_tuple(x):