Repository: ErikBjare/gptme
File: gptme/message.py (Python)

Code before:
        return tok * price


def format_msgs(
    msgs: list[Message],
    oneline: bool = False,
    highlight: bool = False,
    indent: int = 0,
) -> list[str]:
    """Formats messages for printing to the console."""
    outputs = []
    for msg in msgs:
        userprefix = msg.role.capitalize()
        if highlight:
            color = ROLE_COLOR[msg.role]
            userprefix = f"[bold {color}]{userprefix}[/bold {color}]"
            
        # Add status emoji for system messages
        if msg.role == "system" and msg.status:
            status_emoji = {
                "success": "✅",
                "error": "❌",
                "info": "ℹ️",
            }
            if emoji := status_emoji.get(msg.status):
                userprefix = f"{userprefix} {emoji}"
                
        # get terminal width
        max_len = shutil.get_terminal_size().columns - len(userprefix)
        output = ""
        if oneline:
            output += textwrap.shorten(
                msg.content.replace("\n", "\\n"), width=max_len, placeholder="..."
            )
            if len(output) < 20:
                output = msg.content.replace("\n", "\\n")[:max_len] + "..."
        else:
            multiline = len(msg.content.split("\n")) > 1
            output += "\n" + indent * " " if multiline else ""
            for i, block in enumerate(msg.content.split("```")):
                if i % 2 == 0:
                    output += textwrap.indent(block, prefix=indent * " ")
                    continue
                elif highlight:
                    lang = block.split("\n", 1)[0]
                    content = block.split("\n", 1)[-1]
                    fmt = "underline blue"
                    block = f"[{fmt}]{lang}\n[/{fmt}]" + rich_to_str(
                        Syntax(
                            content.rstrip().replace("[", r"\["),
                            lang,

Review comment (bug):
In gptme-webui I simply check the first line with:

```py
first_line = msg.content.split("\n", 1)[0].lower()
isSuccess = first_line.startswith(["saved", "success", "patch successfully"])
isError = first_line.startswith(["error", "failed"])
```

Since it's just a design thing, I think such a lightweight approach makes more sense.
The "info" emoji also doesn't make much sense to me (how is it different from no status?)

Code after:
    msgs: list[Message],
    oneline: bool = False,
    highlight: bool = False,
    indent: int = 0,
) -> list[str]:
    """Formats messages for printing to the console."""
    outputs = []
    for msg in msgs:
        userprefix = msg.role.capitalize()
        if highlight:
            color = ROLE_COLOR[msg.role]
            userprefix = f"[bold {color}]{userprefix}[/bold {color}]"
        if msg.role == "system":
            first_line = msg.content.split("\n", 1)[0].lower()
            isSuccess = first_line.startswith(("saved", "appended", "patch successfully", "ran"))
            isError = first_line.startswith(("error", "failed"))
            if isSuccess:
                userprefix = f"{userprefix} ✅"
            elif isError:
                userprefix = f"{userprefix} ❌"
                
        # get terminal width
        max_len = shutil.get_terminal_size().columns - len(userprefix)
        output = ""
        if oneline:
            output += textwrap.shorten(
                msg.content.replace("\n", "\\n"), width=max_len, placeholder="..."
            )
            if len(output) < 20:
                output = msg.content.replace("\n", "\\n")[:max_len] + "..."
        else:
            multiline = len(msg.content.split("\n")) > 1
            output += "\n" + indent * " " if multiline else ""
            for i, block in enumerate(msg.content.split("```")):
                if i % 2 == 0:
                    output += textwrap.indent(block, prefix=indent * " ")
                    continue
                elif highlight:
                    lang = block.split("\n", 1)[0]
                    content = block.split("\n", 1)[-1]
                    fmt = "underline blue"
                    block = f"[{fmt}]{lang}\n[/{fmt}]" + rich_to_str(
                        Syntax(
                            content.rstrip().replace("[", r"\["),
                            lang,
                        )
                    )
                output += f"```{block.rstrip()}\n```"
        outputs.append(f"{userprefix}: {output.rstrip()}")
    return outputs