Repository: xonsh/xonsh

Issue:
Starting up a xonsh in xonsh fails again with "ValueError: Could not translate ANSI color code"
<!--- Provide a general summary of the issue in the Title above -->

When having a xonsh shell running in readline mode and I execute on the xonsh command line a script via

    xonsh script

I get the error: `ValueError: Could not translate ANSI color code`.

## xonfig

<details>

```
+------------------+---------------------+
| xonsh            | 0.9.8               |
| Git SHA          | cb485817            |
| Commit Date      | Jul 1 20:35:24 2019 |
| Python           | 3.7.4               |
| PLY              | 3.11                |
| have readline    | True                |
| prompt toolkit   | None                |
| shell type       | dumb                |
| pygments         | None                |
| on posix         | True                |
| on linux         | True                |
| distro           | unknown             |
| on darwin        | False               |
| on windows       | False               |
| on cygwin        | False               |
| on msys2         | False               |
| is superuser     | False               |
| default encoding | utf-8               |
| xonsh encoding   | utf-8               |
| encoding errors  | surrogateescape     |
+------------------+---------------------+
```

</details>

## Expected Behavior

When I type on the xonsh command line 

    xonsh SCRIPT
the script SCRIPT should run.

## Current Behavior

Instead I get

```
ValueError: Could not translate ANSI color code '1;5;38;5;145;48;5;124' into a known color in the palette. Specifically, the 'BOLD_SLOWBLINK_SET_FOREGROUND_5' portion of '5' in ['BOLD_', 'SLOWBLINK_', 'SET_FOREGROUND_', '5', '145', 'SET_BACKGROUND_', 'SLOWBLINK_', '124'] seems to missing.
```

### Traceback (if applicable)

<details>

```
Traceback (most recent call last):
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/main.py", line 401, in main
    args = premain(argv)
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/main.py", line 352, in premain
    env = start_services(shell_kwargs, args)
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/main.py", line 287, in start_services
    cacheall=shell_kwargs.get("cacheall", False),
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/execer.py", line 62, in __init__
    load_builtins(execer=self, ctx=xonsh_ctx)
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/built_ins.py", line 1283, in load_builtins
    builtins.__xonsh__.load(execer=execer, ctx=ctx)
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/built_ins.py", line 1359, in load
    self.env = Env(default_env())
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/environ.py", line 1383, in __init__
    self[key] = val
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/environ.py", line 1545, in __setitem__
    val = ensurer.convert(val)
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/environ.py", line 450, in convert
    return cls.fromstring(x)
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/environ.py", line 413, in fromstring
    esc, "default", reversed_style=reversed_default
  File "/home/broleo/.local/pipx/venvs/xonsh/lib/python3.7/site-packages/xonsh/ansi_colors.py", line 323, in ansi_color_escape_code_to_name
    msg.format(escape_code=escape_code, names=names, name=name, n=n)
ValueError: Could not translate ANSI color code '1;5;38;5;145;48;5;124' into a known color in the palette. Specifically, the 'BOLD_SLOWBLINK_SET_FOREGROUND_5' portion of '5' in ['BOLD_', 'SLOWBLINK_', 'SET_FOREGROUND_', '5', '145', 'SET_BACKGROUND_', 'SLOWBLINK_', '124'] seems to missing.
Xonsh encountered an issue during launch
Failback to /bin/sh
```

</details>


Discussion:

Patch:
diff --git a/news/badcol.rst b/news/badcol.rst
new file mode 100644
index 00000000..34e3f393
--- /dev/null
+++ b/news/badcol.rst
@@ -0,0 +1,25 @@
+**Added:**
+
+* <news item>
+
+**Changed:**
+
+* The ``$LS_COLORS`` environment variable will no longer raise exceptions when trying
+  to convert ANSI color sequences to xonsh color names.
+
+**Deprecated:**
+
+* <news item>
+
+**Removed:**
+
+* <news item>
+
+**Fixed:**
+
+* Fixed issue converting ANSI color codes that contained both slow blink and set foreground
+  or set background sequences.
+
+**Security:**
+
+* <news item>
diff --git a/xonsh/ansi_colors.py b/xonsh/ansi_colors.py
index e7fb2209..81b6d562 100644
--- a/xonsh/ansi_colors.py
+++ b/xonsh/ansi_colors.py
@@ -296,9 +296,9 @@ def ansi_color_escape_code_to_name(escape_code, style, reversed_style=None):
         # set the flags for next time
         if "38" == e or "48" == e:
             seen_set_foreback = True
-        elif "2" == e:
+        elif seen_set_foreback and "2" == e:
             n_ints = 3
-        elif "5" == e:
+        elif seen_set_foreback and "5" == e:
             n_ints = 1
     # normalize names
     n = ""
diff --git a/xonsh/environ.py b/xonsh/environ.py
index 500ce14e..08ec3149 100644
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -418,9 +418,13 @@ class LsColors(cabc.MutableMapping):
             elif esc == "target":
                 data[key] = ("TARGET",)
             else:
-                data[key] = ansi_color_escape_code_to_name(
-                    esc, "default", reversed_style=reversed_default
-                )
+                try:
+                    data[key] = ansi_color_escape_code_to_name(
+                        esc, "default", reversed_style=reversed_default
+                    )
+                except Exception as e:
+                    print("xonsh:warning:" + str(e), file=sys.stderr)
+                    data[key] = ("NO_COLOR",)
         obj._d = data
         return obj
 


Test patch:
diff --git a/tests/test_ansi_colors.py b/tests/test_ansi_colors.py
index d8c5d649..2973de84 100644
--- a/tests/test_ansi_colors.py
+++ b/tests/test_ansi_colors.py
@@ -117,6 +117,9 @@ def test_ansi_reverse_style(key, value):
         ),
         ("48;5;235;38;5;139;3", ("BACKGROUND_BLACK", "ITALIC_WHITE")),
         ("38;5;111;4", ("UNDERLINE_WHITE",)),
+        ("1;48;5;124", ("BACKGROUND_RED", "BOLD_WHITE")),
+        ("5;48;5;124", ("BACKGROUND_RED", "SLOWBLINK_WHITE")),
+        ("1;5;38;5;145;48;5;124", ("BOLD_SLOWBLINK_WHITE", "BACKGROUND_RED")),
     ],
 )
 def test_ansi_color_escape_code_to_name(inp, exp):