def colored(msg, color=None, background=None, bold=None, underline=None):
    """
    Return string formatted using ANSI escape codes. Supports 256 colors.

    https://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg

    :param string: String to format.
    :param color: Color of text.
    :param background: Color of background.
    :param bold: True if text should be bold.
    :param underline: True if text should be underlined.
    """

    prefix = ""

    if color is not None:
        prefix += "\x1b[38;5;{}m".format(color)

    if background is not None:
        prefix += "\x1b[48;5;{}m".format(background)

    if bold:
        prefix += "\x1b[1m"

    if underline:
        prefix += "\x1b[4m"

    suffix = ""
    if prefix:
        suffix = "\x1b[0m"

    return "{}{}{}".format(prefix, msg, suffix)