spectre.console icon indicating copy to clipboard operation
spectre.console copied to clipboard

MarkupLine does not escape given parameters.

Open FreeApophis opened this issue 3 years ago • 4 comments

Information

  • OS: Windows (but probably everywhere)
  • Version: 0.41.0
  • Terminal: Windows Terminal (but probably everywhere)

Describe the bug

I encountared this in a situation like this:

var list = new List<int>() { 1337, 42 };

_ansiConsole.MarkupLine("[gray]Log[/]: {0}", list);

which threw this exception: System.InvalidOperationException: Could not find color or style 'System.Int32'.

The problem seems to be that the parameters are applied before parsing the format. How to format the string should be defined in the format string and not applied to the parameters.

_ansiConsole.MarkupLine("[gray]Log[/]: {0}", "[not a style]");

FreeApophis avatar Aug 24 '21 12:08 FreeApophis

@FreeApophis Thanks for reporting this, but this is by design. The Markup API expects all format arguments that are strings to be properly escaped.

It would break too many consumers if we would change this behavior now. We're, however, considering adding new functionality in a later release that takes care of this problem.

patriksvensson avatar Aug 30 '21 21:08 patriksvensson

Hello, I run into the same issue today. What about a simply static method, which escapes all relevant sequences from a string (and a method to reverse the escaping). Then it is easy to embedded strings into MarkupLine and escape it when necessary?

Netzeband avatar Jun 18 '22 08:06 Netzeband

There is one: EscapeMarkup

patriksvensson avatar Jun 18 '22 11:06 patriksvensson

Wow, I did not notice this. In this case, everything is in place I would say. Thanks 👍

Netzeband avatar Jun 19 '22 07:06 Netzeband