# Buggy code
def _partial_color_tokenize_main(template):
    formatter = string.Formatter()
    bopen = "{"
    bclose = "}"
    colon = ":"
    expl = "!"
    color = Color.NO_COLOR
    fg = bg = None
    value = ""
    toks = []
    for literal, field, spec, conv in formatter.parse(template):
        if field is None:
            value += literal
        elif field in KNOWN_COLORS or "#" in field:
            value += literal
            next_color, fg, bg = color_by_name(field, fg, bg)
            if next_color is not color:
                if len(value) > 0:
                    toks.append((color, value))
                    if styles is not None:
                        styles[color]  # ensure color is available
                color = next_color
                value = ""
        elif field is not None:
            parts = [literal, bopen, field]
            if conv is not None and len(conv) > 0:
                parts.append(expl)
                parts.append(conv)
            if spec is not None and len(spec) > 0:
                parts.append(colon)
                parts.append(spec)
            parts.append(bclose)
            value += "".join(parts)
        else:
            value += literal
    toks.append((color, value))
    return toks, color


# Traceback (ValueError)
hartz@tabby-3 ~ $ $PROMPT = "{{hello"
Traceback (most recent call last):
File "/usr/local/bin/xonsh", line 9, in <module>
load_entry_point('xonsh==0.3.2', 'console_scripts', 'xonsh')()
File "/usr/local/lib/python3.5/site-packages/xonsh/main.py", line 260, in main
shell.cmdloop()
File "/usr/local/lib/python3.5/site-packages/xonsh/ptk/shell.py", line 116, in cmdloop
line = self.singleline(auto_suggest=auto_suggest)
File "/usr/local/lib/python3.5/site-packages/xonsh/ptk/shell.py", line 63, in singleline
prompt_tokens = self.prompt_tokens(None)
File "/usr/local/lib/python3.5/site-packages/xonsh/ptk/shell.py", line 137, in prompt_tokens
toks = partial_color_tokenize(p)
File "/usr/local/lib/python3.5/site-packages/xonsh/pyghooks.py", line 206, in partial_color_tokenize
for literal, field, spec, conv in formatter.parse(template):
ValueError: expected '}' before end of string

# Fixed code
def _partial_color_tokenize_main(template, styles):
    formatter = string.Formatter()
    bopen = "{"
    bclose = "}"
    colon = ":"
    expl = "!"
    color = Color.NO_COLOR
    fg = bg = None
    value = ""
    toks = []
    for literal, field, spec, conv in formatter.parse(template):
        if field is None:
            value += literal
        elif field in KNOWN_COLORS or "#" in field:
            value += literal
            next_color, fg, bg = color_by_name(field, fg, bg)
            if next_color is not color:
                if len(value) > 0:
                    toks.append((color, value))
                    if styles is not None:
                        styles[color]  # ensure color is available
                color = next_color
                value = ""
        elif field is not None:
            parts = [literal, bopen, field]
            if conv is not None and len(conv) > 0:
                parts.append(expl)
                parts.append(conv)
            if spec is not None and len(spec) > 0:
                parts.append(colon)
                parts.append(spec)
            parts.append(bclose)
            value += "".join(parts)
        else:
            value += literal
    toks.append((color, value))
    return toks, color