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:
A -- somewhat crude -- fix is to set `$LS_COLORS` to empty:

    $LS_COLORS=''

Still it would be nice if xonsh would not completely fail if theer's a problem with some terminal colors...

Thanks @halloleo - the ValueError should probably be a warning instead.  PRs welcome.  I am curious as to why that color fails. It looks like it actually might be malformed.
The problem still occurs when I do _not_ set `$LS_COLOR = ''` in my startup file.

I checked  `$LS_COLOR` in the environment before issuing `xonsh SCRIPT` - and the variable is  weirdly empty:

    $ echo $LS_COLOR
    $LS_COLOR

So nothing malformed in there. However the traceback reports:

    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.
Isn't the variable called LS_COLORS, not LS_COLOR
Ups, that's a good one! 

I looked at the startup file and I definitely set `LS_COLORS` (*with* an 's') to empty to prevent the problem, but I have to check on Tuesday when back at the troubling CentOS machine what `$LS_COLORS` echos there.

(Side note: On Mac OS I have no problems:

```
$$$ echo $LS_COLORS
*.7z=1;38;5;124:*.Z=1;38;5;124:*.aac=38;5;26:*.ace=1;38;5;124:*.alz=1;38;5;124:*.arc=1;38;5;124:*.arj=1;38;5;124:*.asf=1;38;5;90:*.au=38;5;26:*.avi=1;38;5;90:*.bmp=1;38;5;90:*.bz=1;38;5;124:*.bz2=1;38;5;124:*.cab=1;38;5;124:*.cgm=1;38;5;90:*.cpio=1;38;5;124:*.deb=1;38;5;124:*.dl=1;38;5;90:*.dwm=1;38;5;124:*.dz=1;38;5;124:*.ear=1;38;5;124:*.emf=1;38;5;90:*.esd=1;38;5;124:*.flac=38;5;26:*.flc=1;38;5;90:*.fli=1;38;5;90:*.flv=1;38;5;90:*.gif=1;38;5;90:*.gl=1;38;5;90:*.gz=1;38;5;124:*.jar=1;38;5;124:*.jpeg=1;38;5;90:*.jpg=1;38;5;90:*.lha=1;38;5;124:*.lrz=1;38;5;124:*.lz=1;38;5;124:*.lz4=1;38;5;124:*.lzh=1;38;5;124:*.lzma=1;38;5;124:*.lzo=1;38;5;124:*.m2v=1;38;5;90:*.m4a=38;5;26:*.m4v=1;38;5;90:*.mid=38;5;26:*.midi=38;5;26:*.mjpeg=1;38;5;90:*.mjpg=1;38;5;90:*.mka=38;5;26:*.mkv=1;38;5;90:*.mng=1;38;5;90:*.mov=1;38;5;90:*.mp3=38;5;26:*.mp4=1;38;5;90:*.mp4v=1;38;5;90:*.mpc=38;5;26:*.mpeg=1;38;5;90:*.mpg=1;38;5;90:*.nuv=1;38;5;90:*.oga=38;5;26:*.ogg=38;5;26:*.ogm=1;38;5;90:*.ogv=1;38;5;90:*.ogx=1;38;5;90:*.opus=38;5;26:*.pbm=1;38;5;90:*.pcx=1;38;5;90:*.pgm=1;38;5;90:*.png=1;38;5;90:*.ppm=1;38;5;90:*.qt=1;38;5;90:*.ra=38;5;26:*.rar=1;38;5;124:*.rm=1;38;5;90:*.rmvb=1;38;5;90:*.rpm=1;38;5;124:*.rz=1;38;5;124:*.sar=1;38;5;124:*.spx=38;5;26:*.svg=1;38;5;90:*.svgz=1;38;5;90:*.swm=1;38;5;124:*.t7z=1;38;5;124:*.tar=1;38;5;124:*.taz=1;38;5;124:*.tbz=1;38;5;124:*.tbz2=1;38;5;124:*.tga=1;38;5;90:*.tgz=1;38;5;124:*.tif=1;38;5;90:*.tiff=1;38;5;90:*.tlz=1;38;5;124:*.txz=1;38;5;124:*.tz=1;38;5;124:*.tzo=1;38;5;124:*.tzst=1;38;5;124:*.vob=1;38;5;90:*.war=1;38;5;124:*.wav=38;5;26:*.webm=1;38;5;90:*.wim=1;38;5;124:*.wmv=1;38;5;90:*.xbm=1;38;5;90:*.xcf=1;38;5;90:*.xpm=1;38;5;90:*.xspf=38;5;26:*.xwd=1;38;5;90:*.xz=1;38;5;124:*.yuv=1;38;5;90:*.z=1;38;5;124:*.zip=1;38;5;124:*.zoo=1;38;5;124:*.zst=1;38;5;124:bd=48;5;28;38;5;130:ca=38;5;28;48;5;124:cd=48;5;28;38;5;130:di=1;38;5;18:do=1;38;5;90:ex=1;38;5;34:ln=1;38;5;26:mh=0:mi=0:or=48;5;28;38;5;124:ow=38;5;18;48;5;34:pi=48;5;28;38;5;130:rs=0:sg=38;5;28;48;5;130:so=1;38;5;90:st=38;5;145;48;5;18:su=38;5;145;48;5;124:tw=38;5;28;48;5;34
```

And I can call scripts perfectly via `xonsh SCRIPT`.)

Resolving patch:
diff --git a/xonsh/ansi_colors.py b/xonsh/ansi_colors.py
--- 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
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -418,9 +418,13 @@ def fromstring(cls, s):
             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
--- 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):