LS_COLORS add 'mh' change 'rs' to 'fi'

This commit is contained in:
Bob Hyman 2020-06-09 21:47:59 -04:00
parent d6ffd678bf
commit 4e230fa5a8
6 changed files with 55 additions and 16 deletions

View file

@ -19,4 +19,5 @@
* <news item>
**Security:**
* <news item>

View file

@ -0,0 +1,24 @@
**Added:**
* $LS_COLORS code 'mh' now recognized for (multi) hard-linked files.
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* $LS_COLORS code 'fi' now used for "regular files", as it should have been all along. (was 'rs')
See (#3608)[https://github.com/xonsh/xonsh/issues/3608].
**Security:**
* <news item>

View file

@ -310,14 +310,14 @@ def test_lscolors_target():
@pytest.mark.parametrize(
"key_in,old_in,new_in,test",
[
("rs", ("NO_COLOR",), ("BLUE",), "existing key, change value"),
("rs", ("NO_COLOR",), ("NO_COLOR",), "existing key, no change in value"),
("fi", ("NO_COLOR",), ("BLUE",), "existing key, change value"),
("fi", ("NO_COLOR",), ("NO_COLOR",), "existing key, no change in value"),
("tw", None, ("NO_COLOR",), "create new key"),
("pi", ("BACKGROUND_BLACK", "YELLOW"), None, "delete existing key"),
],
)
def test_lscolors_events(key_in, old_in, new_in, test, xonsh_builtins):
lsc = LsColors.fromstring("rs=0:di=01;34:pi=40;33")
lsc = LsColors.fromstring("fi=0:di=01;34:pi=40;33")
# corresponding colors: [('NO_COLOR',), ('BOLD_CYAN',), ('BOLD_CYAN',), ('BACKGROUND_BLACK', 'YELLOW')]
event_fired = False

View file

@ -158,10 +158,9 @@ def test_XonshStyle_init_file_color_tokens(xonsh_builtins_LS_COLORS):
_cf = {
"rs": "regular",
"fi": "regular",
"di": "simple_dir",
"ln": "simple_link",
"mh": None,
"pi": "pipe",
"so": None,
"do": None,
@ -179,8 +178,11 @@ _cf = {
"*.emf": "foo.emf",
"*.zip": "foo.zip",
"*.ogg": "foo.ogg",
"mh": "hard_link",
}
# TODO: create equivalent zoo of files for Windows.
@pytest.fixture(scope="module")
def colorizable_files():
@ -203,7 +205,7 @@ def colorizable_files():
os.mkdir(file_path)
else:
open(file_path, "a").close()
if k in ("di", "rg"):
if k in ("di", "fi"):
pass
elif k == "ex":
os.chmod(file_path, stat.S_IXUSR)
@ -232,6 +234,9 @@ def colorizable_files():
)
elif k == "ow":
os.chmod(file_path, stat.S_IWOTH | stat.S_IRUSR | stat.S_IWUSR)
elif k == "mh":
os.rename(file_path, file_path + "_target")
os.link(file_path + "_target", file_path)
else:
pass # cauterize those elseless ifs!
@ -249,7 +254,12 @@ def test_colorize_file(key, file_path, colorizable_files, xonsh_builtins_LS_COLO
XonshStyle()
) # default style
ffp = colorizable_files + "/" + file_path
mode = (os.lstat(ffp)).st_mode
color_token, color_key = color_file(ffp, mode)
stat_result = os.lstat(ffp)
color_token, color_key = color_file(ffp, stat_result)
assert color_key == key, "File classified as expected kind"
assert color_token == file_color_tokens[key], "Color token is as expected"
# TODO: test precedence of some types over others:
# mh + link to ex --> ex
# ln + link to ex --> ex
# ln:target

View file

@ -321,6 +321,7 @@ class LsColors(cabc.MutableMapping):
"di": ("BOLD_BLUE",),
"do": ("BOLD_PURPLE",),
"ex": ("BOLD_GREEN",),
"fi": ("NO_COLOR",),
"ln": ("BOLD_CYAN",),
"mh": ("NO_COLOR",),
"mi": ("NO_COLOR",),

View file

@ -1356,14 +1356,14 @@ def on_lscolors_change(key, oldvalue, newvalue, **kwargs):
events.on_lscolors_change(on_lscolors_change)
def color_file(file_path: str, mode: int) -> (Color, str):
def color_file(file_path: str, path_stat: os.stat_result) -> (Color, str):
"""Determine color to use for file as ls -c would, given stat() results and its name.
Parameters
----------
file_path : string
file_path:
relative path of file (as user typed it).
mode : int
path_stat:
stat() results for file_path.
Returns
@ -1379,7 +1379,8 @@ def color_file(file_path: str, mode: int) -> (Color, str):
"""
lsc = builtins.__xonsh__.env["LS_COLORS"]
color_key = "rs"
color_key = "fi"
mode = path_stat.st_mode
if stat.S_ISLNK(mode): # must test link before S_ISREG (esp execute)
color_key = "ln"
@ -1403,9 +1404,11 @@ def color_file(file_path: str, mode: int) -> (Color, str):
if ext in lsc:
color_key = ext
else:
color_key = "rs"
color_key = "fi"
elif path_stat.st_nlink > 1:
color_key = "mh"
else:
color_key = "rs"
color_key = "fi"
elif stat.S_ISDIR(mode): # ls -c doesn't colorize sticky or ow if not dirs...
color_key = ("di", "ow", "st", "tw")[
(mode & stat.S_ISVTX == stat.S_ISVTX) * 2
@ -1464,8 +1467,8 @@ def subproc_arg_callback(_, match):
yieldVal = Text
try:
path = os.path.expanduser(text)
mode = (os.lstat(path)).st_mode # lstat() will raise FNF if not a real file
yieldVal, _ = color_file(path, mode)
path_stat = os.lstat(path) # lstat() will raise FNF if not a real file
yieldVal, _ = color_file(path, path_stat)
except (FileNotFoundError, OSError):
pass