FRACerqueira / PromptPlus

Interactive command-line toolkit for .Net core with powerful controls and commands to create professional console applications.
https://fracerqueira.github.io/PromptPlus/
MIT License
50 stars 5 forks source link

Escaping format characters (by default) #65

Closed ividyon closed 1 year ago

ividyon commented 1 year ago

I'm having some trouble with how PromptPlus automatically formats characters in strings.

My program is processing files such as C[c4201]_break.matbin. When processing them, it prints them to the console:

                                PromptPlus.WriteLine($"Unpacking {parser.Name}: {fileName}...");

So, when [] is contained in the file name, PromptPlus stumbles and throws an exception due to applying internal formatting to this somehow, which I can't seem to find in the docs.

It would be quite nice if it was possible to disable this formatting; on a per-function-call basis, as well as as a global configuration that will automatically escape any formatting in strings entered in any WriteLine (and similar functions).

For starters, I'd like to know how to manually escape such characters, for now.

FRACerqueira commented 1 year ago

@ividyon , hi!

PromptPlus also has commands for coloring parts of text. To know the colored parts of the text, the characters '[' and ']' are used to delimit the color commands. (https://fracerqueira.github.io/PromptPlus/#colors)

I will investigate a way to have a global functionality to define these delimiters so as not to have an overhead for the end user.

To get around this problem you must use '[[' and ']]'

Maybe an extension like: PromptPlus.RawWriteLine / PromptPlus.RawWrite to skip color engine parse

ividyon commented 1 year ago

Being able to provide an optional parameter to output methods like WriteLine, DoubleDash etc. which runs an inbuilt replacer for [ and ] (and other control characters PromptPlus may have) would be ideal, as well as public API access to such a filter function.

For now I wrote my own string extension for it:

public static class StringExtensions
{
    public static string PromptPlusEscape(this string str)
    {
        return str.Replace("[", "[[").Replace("]", "]]");
    }
}

But ideally such a thing should not be "polluting" the string function space, but rather be self-contained within PromptPlus.

FRACerqueira commented 1 year ago

@ividyon , 'm working on this implementation.

The implementation so far looks like this:

//local change AcceptMalformedColorToken
using (PromptPlus.IgnoreMalformedColorToken())
{
    PromptPlus.WriteLine("MalformedColorToken_AnyText[c4201]_Othertext");
    PromptPlus.WriteLine("ValidformedColorToken_Any[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
}

//global change AcceptMalformedColorToken
PromptPlus.AcceptMalformedColorToken = true;
.
.
.
PromptPlus.WriteLine("MalformedColorToken_AnyText[c4201]_Othertext[/]");
PromptPlus.WriteLine("ValidformedColorToken_Any[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
.
.
.
PromptPlus.AcceptMalformedColorToken = false;
ividyon commented 1 year ago

Looks like a great solution! I guess I'd call it something simpler like EscapeColorTokens or EscapeStyleCharacters, but yeah.

FRACerqueira commented 1 year ago

Good name for this feature!

FRACerqueira commented 1 year ago

Fixed scheduled for publication 4.0.6:

            //global change IgnoreErrorColorTokens
            PromptPlus.IgnoreErrorColorTokens = true;
            //write text with char token
            PromptPlus.WriteLine("MalformedColorToken_AnyText[c4201]_Othertext[/]");
            //show text with color
            PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
            PromptPlus.IgnoreErrorColorTokens = false;

            //change local IgnoreErrorColorTokens 
            using (PromptPlus.EscapeColorTokens())
            {
                //write text with char token
                PromptPlus.WriteLine("MalformedColor_TokenAnyText[c4201]_Othertext");
                //show text with color
                PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
            }
            //restore original value IgnoreErrorColorTokens
ividyon commented 1 year ago

Is it possible to have this in reverse? So, if color tokens are set to be ignored globally, have a "using" clause that enables them within it.

Also I realized that, if you're actually ignoring the tokens instead of escaping them with [[, then "escape" isn't really the right term, Ignore would be.

So "IgnoreColorTokens" and "AcceptColorTokens".

FRACerqueira commented 1 year ago

@ividyon , See the changes, what do you think??

            //standard 
            PromptPlus.DoubleDash($"PromptPlus IgnoreColorTokens = false - Default value and usage");
            PromptPlus.WriteLine("Valid[[]]formedColor_TokenAny[[RED ON WHITE]]Text[[/]]_[[YELLOW]]Othertext[[/]]");
            PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");

            PromptPlus.DoubleDash($"PromptPlus IgnoreColorTokens = true");
            //global change IgnoreColorTokens
            PromptPlus.IgnoreColorTokens = true;
            //show text with no color!
            PromptPlus.WriteLine("Valid[]formedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
            //create context to IgnoreColorTokens  = false
            using (PromptPlus.AcceptColorTokens())
            {
                PromptPlus.DoubleDash($"PromptPlus with context IgnoreColorTokens = false");
                //show text with color!
                PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
            }
            PromptPlus.IgnoreColorTokens = false;

            //create context to IgnoreColorTokens  = true
            using (PromptPlus.EscapeColorTokens())
            {
                PromptPlus.DoubleDash($"PromptPlus with context IgnoreColorTokens = true");
                //show text with color
                PromptPlus.WriteLine("ValidformedColor_TokenAny[RED ON WHITE]Text[/]_[YELLOW]Othertext[/]");
            }
            //restore original value IgnoreColorTokens 

output:

Captura de tela 2023-09-19 171806

ividyon commented 1 year ago

Looks great!