Repository: xonsh/xonsh

Issue:
After upgrade xonsh crashes with "Wrong color format" error
## What I did to trigger the issue
I've upgraded with `pip install --upgrade xonsh` to see if another issue was resolved.

## The trace I get:
```
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21008, in main
    return main_xonsh(args)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21034, in main_xonsh
    shell.shell.cmdloop()
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 168, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 133, in singleline
    prompt_args['style'] = style_from_dict(DEFAULT_STYLE_DICT)
  File "/usr/local/lib/python3.5/dist-packages/prompt_toolkit/styles/from_dict.py", line 122, in style_from_dict
    attrs = attrs._replace(bgcolor=_colorformat(part[3:]))
  File "/usr/local/lib/python3.5/dist-packages/prompt_toolkit/styles/from_dict.py", line 39, in _colorformat
    raise ValueError('Wrong color format %r' % text)
ValueError: Wrong color format '##ansidarkblue'
Xonsh encountered an issue during launch
Failback to /bin/sh
```
## What I've tried with no success
I've tried un-installing pygments and setting $XONSH_COLOR_STYLE='bw'.

## My current workaround 
I'm starting xonsh with the readline backend (`xonsh --shell-type=readline`).

## xonfig 
```
+------------------+----------------------+
| xonsh            | 0.7.5                |
| Git SHA          | 8ab50b7e             |
| Commit Date      | Aug 16 15:26:52 2018 |
| Python           | 3.5.2                |
| PLY              | 3.9                  |
| have readline    | True                 |
| prompt toolkit   | 1.0.15               |
| shell type       | readline             |
| pygments         | None                 |
| on posix         | True                 |
| on linux         | True                 |
| distro           | Ubuntu               |
| 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      |
+------------------+----------------------+
```



Discussion:
Sorry about this. You do a small workaround by installing pygments for now.
Thanks for the suggestion melund but installing pygments doesn't solve the issue (I had it installed to start with). The trace changes a bit:
```
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21008, in main
    return main_xonsh(args)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21034, in main_xonsh
    shell.shell.cmdloop()
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 168, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 131, in singleline
    prompt_args['style'] = PygmentsStyle(pyghooks.xonsh_style_proxy(self.styler))
  File "/usr/local/lib/python3.5/dist-packages/xonsh/pyghooks.py", line 482, in xonsh_style_proxy
    class XonshStyleProxy(Style):
  File "/usr/local/lib/python3.5/dist-packages/pygments/style.py", line 101, in __new__
    ndef[0] = colorformat(styledef)
  File "/usr/local/lib/python3.5/dist-packages/pygments/style.py", line 58, in colorformat
    assert False, "wrong color format %r" % text
AssertionError: wrong color format '##ansidarkblue'
Xonsh encountered an issue during launch
Failback to /bin/sh
```

Weird. I can't reproduce this. Can you start xonsh with `xonsh --no-rc`
Here's the output
```
$ xonsh --no-rc
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21008, in main
    return main_xonsh(args)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21031, in main_xonsh
    print_welcome_screen()
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 14847, in print_welcome_screen
    print_color(line)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 5103, in print_color
    builtins.__xonsh_shell__.shell.print_color(string, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 288, in print_color
    proxy_style = PygmentsStyle(pyghooks.xonsh_style_proxy(self.styler))
  File "/usr/local/lib/python3.5/dist-packages/xonsh/pyghooks.py", line 482, in xonsh_style_proxy
    class XonshStyleProxy(Style):
  File "/usr/local/lib/python3.5/dist-packages/pygments/style.py", line 101, in __new__
    ndef[0] = colorformat(styledef)
  File "/usr/local/lib/python3.5/dist-packages/pygments/style.py", line 58, in colorformat
    assert False, "wrong color format %r" % text
AssertionError: wrong color format '##ansibrown'
Xonsh encountered an issue during launch
```
If you're on Ubuntu 16.04 and use the python3-pygments distro package, the #ansi colors wan't work for you. They were introduced in pygments 2.2.0. If that's the case, try to remove the python3-pygments package and `pip3 install pygments`.
Edit: ~~...or perhaps the problem might be that you only have pygments for Python 2...~~
Nonsense. It's clearly Python 3.5.
Ahh. Yes. Xonsh no longer support pygments<2.2... That change may not have propagated to all package managers. 

But off course it shouldn't come with the first error @ndemou posted
But looking at the traceback I am not sure that is the case. It looks like it has tried to converted the colors to the old format twice...
Thanks again for all the help to everyone. @laloch: I'm indeed in Ubuntu 16.04 but my version of pygments is 2.2.0 and was apparently installed from pip3:

```
# ndemou  @ ndhp  ~  
cat /etc/os-release |grep PRETTY
PRETTY_NAME="Ubuntu 16.04.5 LTS"

# ndemou  @ ndhp  ~  
pip3 install --upgrade pygments
Requirement already up-to-date: pygments in /usr/local/lib/python3.5/dist-packages (2.2.0)

# ndemou  @ ndhp  ~  
xonsh
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21008, in main
    return main_xonsh(args)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/__amalgam__.py", line 21034, in main_xonsh
    shell.shell.cmdloop()
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 168, in cmdloop
    line = self.singleline(auto_suggest=auto_suggest)
  File "/usr/local/lib/python3.5/dist-packages/xonsh/ptk/shell.py", line 131, in singleline
    prompt_args['style'] = PygmentsStyle(pyghooks.xonsh_style_proxy(self.styler))
  File "/usr/local/lib/python3.5/dist-packages/xonsh/pyghooks.py", line 482, in xonsh_style_proxy
    class XonshStyleProxy(Style):
  File "/usr/local/lib/python3.5/dist-packages/pygments/style.py", line 101, in __new__
    ndef[0] = colorformat(styledef)
  File "/usr/local/lib/python3.5/dist-packages/pygments/style.py", line 58, in colorformat
    assert False, "wrong color format %r" % text
AssertionError: wrong color format '##ansidarkred'
```
This is a python 3.5. I can reproduce it in a 3.5 environment... Hmm...
I think it has to do with the guaranteed order of dicts in 3.6. The bug just doesn't manifest itself in 3.6 due to a lucky order of colors in https://github.com/xonsh/xonsh/blob/master/xonsh/tools.py#L1642-L1659
Hi @ndemou, @melund!  I am on Ubuntu, and in a Python 3.5 environment (with readline, ptk1, and ptk2, with and without pygments) I cannot reproduce this. How did you get this to fail @melund? Maybe, rather than doing an upgrade, you can do an uninstall and a reinstall?
> I cannot reproduce this. How did you get this to fail @melund? 

I didn't do anything special. Just a Python 3.5 environment on conda with ptk1 and pygments 2.2.

I don't think you can be sure that dicts are laid out identical across platforms in Python 3.5. So this may be why you don't see it. I have a fix incoming. 

Resolving patch:
diff --git a/xonsh/tools.py b/xonsh/tools.py
--- a/xonsh/tools.py
+++ b/xonsh/tools.py
@@ -1734,7 +1734,7 @@ def ansicolors_to_ptk1_names(stylemap):
     modified_stylemap = {}
     for token, style_str in stylemap.items():
         for color, ptk1_color in ANSICOLOR_NAMES_MAP.items():
-            if ptk1_color not in style_str:
+            if '#' + color not in style_str:
                 style_str = style_str.replace(color, ptk1_color)
         modified_stylemap[token] = style_str
     return modified_stylemap


Test patch: