Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.InvalidOperationException may be thrown when using AnsiConsole.MarkupLineInterpolated #1348

Open
Zaitonn opened this issue Oct 28, 2023 · 1 comment
Labels
bug Something isn't working needs triage

Comments

@Zaitonn
Copy link

Zaitonn commented Oct 28, 2023

Information

  • OS: Windows 10.0.19045.3570
  • Version: 0.47.0
  • Terminal: Windows Terminal

Describe the bug

System.InvalidOperationException may be thrown when using AnsiConsole.MarkupLineInterpolated to output a FormattableString with an object interpolated which overloads .ToString() method. What's worse, it may bring some unexpected strings without being escaped into the output.

To Reproduce

using Spectre.Console;

AnsiConsole.MarkupInterpolated($"{new Example()}");

class Example
{
    public override string ToString()
    {
        return "[123]";
    }
}

here is the output

PS C:\GitHub\********\test> dotnet run
Unhandled exception. System.InvalidOperationException: Encountered malformed markup tag at position 5.
   at Spectre.Console.MarkupTokenizer.ReadMarkup() in /_/src/Spectre.Console/Internal/Text/Markup/MarkupTokenizer.cs:line 192
   at Spectre.Console.MarkupTokenizer.MoveNext() in /_/src/Spectre.Console/Internal/Text/Markup/MarkupTokenizer.cs:line 27
   at Spectre.Console.MarkupParser.Parse(String text, Style style) in /_/src/Spectre.Console/Internal/Text/Markup/MarkupParser.cs:line 17
   at Spectre.Console.AnsiConsoleExtensions.Markup(IAnsiConsole console, String value) in /_/src/Spectre.Console/Extensions/AnsiConsoleExtensions.Markup.cs:line 77
   at Spectre.Console.AnsiConsoleExtensions.MarkupInterpolated(IAnsiConsole console, IFormatProvider provider, FormattableString value) in /_/src/Spectre.Console/Extensions/AnsiConsoleExtensions.Markup.cs:line 67
   at Spectre.Console.AnsiConsoleExtensions.MarkupInterpolated(IAnsiConsole console, FormattableString value) in /_/src/Spectre.Console/Extensions/AnsiConsoleExtensions.Markup.cs:line 35
   at Spectre.Console.AnsiConsole.MarkupInterpolated(FormattableString value) in /_/src/Spectre.Console/AnsiConsole.Markup.cs:line 42
   at Program.<Main>$(String[] args) in C:\GitHub\********\test\Program.cs:line 3

Expected behavior

just output lines normally

[123] // without any styles

Screenshots

none

Additional context

none


Please upvote 👍 this issue if you are interested in it.

@Zaitonn Zaitonn added bug Something isn't working needs triage labels Oct 28, 2023
@github-project-automation github-project-automation bot moved this to Todo 🕑 in Spectre Console Oct 28, 2023
@Zaitonn
Copy link
Author

Zaitonn commented Oct 28, 2023

internal static string EscapeInterpolated(IFormatProvider provider, FormattableString value)
{
object?[] args = value.GetArguments().Select(arg => arg is string s ? s.EscapeMarkup() : arg).ToArray();
return string.Format(provider, value.Format, args);
}
}

maybe somesthing here should be edited?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
Status: Todo 🕑
Development

No branches or pull requests

1 participant