Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _dynamically_collapsed_pwd():
"""Return the compact current working directory. It respects the
environment variable DYNAMIC_CWD_WIDTH.
"""
original_path = _replace_home_cwd()
target_width, units = builtins.__xonsh_env__["DYNAMIC_CWD_WIDTH"]
elision_char = builtins.__xonsh_env__["DYNAMIC_CWD_ELISION_CHAR"]
if target_width == float("inf"):
return original_path
if units == "%":
cols, _ = shutil.get_terminal_size()
target_width = (cols * target_width) // 100
sep = xt.get_sep()
pwd = original_path.split(sep)
last = pwd.pop()
remaining_space = target_width - len(last)
# Reserve space for separators
remaining_space_for_text = remaining_space - len(pwd)
parts = []
for i in range(len(pwd)):
part = pwd[i]
part_len = int(
min(len(part), max(1, remaining_space_for_text // (len(pwd) - i)))
)
remaining_space_for_text -= part_len
if len(part) > part_len:
reduced_part = part[0 : part_len - len(elision_char)] + elision_char
parts.append(reduced_part)
else:
toks = basetoks * (headlen // baselen)
n = headlen % baselen
count = 0
for tok in basetoks:
slen = len(tok[1])
newcount = slen + count
if slen == 0:
continue
elif newcount <= n:
toks.append(tok)
else:
toks.append((tok[0], tok[1][: n - count]))
count = newcount
if n <= count:
break
toks.append((xt.format_color("{NO_COLOR}", hide=True), tail))
rtn = "".join(itertools.chain.from_iterable(toks))
return rtn
def test_subproc_toks_ls_l_semi_ls_second():
lsdl = "ls -l"
ls = "ls"
s = "{0}; {1}".format(lsdl, ls)
exp = "{0}; ![{1}]".format(lsdl, ls)
obs = subproc_toks(s, lexer=LEXER, mincol=7, returnline=True)
assert exp == obs
def test_subproc_toks_ls_l_semi_ls_first():
lsdl = "ls -l"
ls = "ls"
s = "{0}; {1}".format(lsdl, ls)
exp = "![{0}]; {1}".format(lsdl, ls)
obs = subproc_toks(s, lexer=LEXER, maxcol=6, returnline=True)
assert exp == obs
def test_subproc_toks_hello_mom_second():
fst = "echo 'hello'"
sec = "echo 'mom'"
s = "{0}; {1}".format(fst, sec)
exp = "{0}; ![{1}]".format(fst, sec)
obs = subproc_toks(s, lexer=LEXER, mincol=len(fst), returnline=True)
assert exp == obs
def test_subproc_toks_ls_l_semi_ls_second():
lsdl = 'ls -l'
ls = 'ls'
s = '{0}; {1}'.format(lsdl, ls)
exp = '{0}; $[{1}]'.format(lsdl, ls)
obs = subproc_toks(s, lexer=LEXER, mincol=7, returnline=True)
assert_equal(exp, obs)
def test_subproc_toks_indent_ls_comment():
ind = " "
s = "ls -l"
com = " # lets list"
exp = "{0}![{1}]{2}".format(ind, s, com)
obs = subproc_toks(ind + s + com, lexer=LEXER, returnline=True)
assert exp == obs
def test_subproc_toks_indent_ls_no_min():
s = 'ls -l'
exp = INDENT + '$[{0}]'.format(s)
obs = subproc_toks(INDENT + s, lexer=LEXER, returnline=True)
assert_equal(exp, obs)
def test_subproc_toks_ls_l():
exp = '$[ls -l]'
obs = subproc_toks('ls -l', lexer=LEXER, returnline=True)
assert_equal(exp, obs)
def test_cdpath_expansion(xonsh_builtins):
xonsh_builtins.__xonsh__.env = Env(HERE=HERE, CDPATH=("~", "$HERE"))
test_dirs = (
os.path.join(HERE, "xonsh-test-cdpath-here"),
os.path.expanduser("~/xonsh-test-cdpath-home"),
)
try:
for d in test_dirs:
if not os.path.exists(d):
os.mkdir(d)
assert os.path.exists(
dirstack._try_cdpath(d)
), "dirstack._try_cdpath: could not resolve {0}".format(d)
finally:
for d in test_dirs:
if os.path.exists(d):
os.rmdir(d)