gui-cs / Terminal.Gui

Cross Platform Terminal UI toolkit for .NET
MIT License
9.57k stars 682 forks source link

`TextFormatter.WordWrapText` with `preserveTrailingSpaces = false` is not working #2520

Open tig opened 1 year ago

tig commented 1 year ago

Looks like none of the existing unit tests actually test if preserveTrailingSpaces is false.

This test proves that it's not working correctly:

        [Fact, AutoInitShutdown]
        public void WordWrap_PreserveTrailingSpaces_Horizontal_With_Simple_Runes ()
        {
            var text = "A sentence has words. ";
            var width = 3;
            var wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: true);
            var breakLines = "";
            foreach (var line in wrappedLines) {
                breakLines += $"{line}{Environment.NewLine}";
            }
            var expected = "A " + Environment.NewLine +
                    "sen" + Environment.NewLine +
                    "ten" + Environment.NewLine +
                    "ce " + Environment.NewLine +
                    "has" + Environment.NewLine +
                    " " + Environment.NewLine +
                    "wor" + Environment.NewLine +
                    "ds." + Environment.NewLine +
                    " " + Environment.NewLine;
            Assert.Equal (expected, breakLines);

            wrappedLines = TextFormatter.WordWrapText (text, width, preserveTrailingSpaces: false);
            foreach (var line in wrappedLines) {
                breakLines += $"{line}{Environment.NewLine}";
            }
            expected = "A s" + Environment.NewLine +
                    "ent" + Environment.NewLine +
                    "enc" + Environment.NewLine +
                    "e h" + Environment.NewLine +
                    "as" + Environment.NewLine +
                    "wor" + Environment.NewLine +
                    "ds." + Environment.NewLine;
            Assert.Equal (expected, breakLines);
        }
tig commented 1 year ago

Nevermind.

BDisp commented 1 year ago

Unfortunately this isn't working again when WordWrap is false. I even had created a unit test before to this doesn't happens again but it seems that the were changed because the tests is passing.

messagebox-wordwrap-false

BDisp commented 1 year ago

https://github.com/gui-cs/Terminal.Gui/blob/f280ded14521268942abc5f0c9b832226d2ff099/Terminal.Gui/Views/MessageBox.cs#L297-L298

@tig I really don't understand what you want doing here. TextFormatter doesn't rely on View, so it's the MessageBox who have to provide the necessary width and height for the Dialog dependent if wrapMessage is true or not. How the TextFormatter will resize the view based on the TextFormatter.WordWrap property? If you have a great idea I'm curious. Otherwise I see that much of the code (#2405) I did before was deleted, so I'll give up on this.

tig commented 1 year ago

I'll look at this later and try to fix this. Sorry about screwing this up.