From e4ee382336a6a633c5b1c27a74c662d53d1d7940 Mon Sep 17 00:00:00 2001 From: Brian G Date: Fri, 18 Jun 2021 17:04:26 -0700 Subject: [PATCH] Add ANSI Terminal support to highligh functions [GB.EVAL.HIGHLIGHT] * NEW: Add ToConsole which generates ansii color text which can be printed to most consoles * NEW: Unit test for Ansi color Console text * NEW: Unit text for HTML color page * NEW: Unit text for RTF Color text --- comp/src/gb.eval.highlight/.src/Main.module | 6 +- .../.src/TestSuite/AnsiConsoleColor.test | 13 +++ .../.src/TestSuite/HtmlTextFormatColor.test | 14 +++ .../.src/TestSuite/RichTextFormatColor.test | 13 +++ .../.src/TextHighlighter.class | 98 +++++++++++++++++++ comp/src/gb.eval.highlight/.test | 5 + .../UnitTest/ConsoleTestResults | 18 ++++ .../UnitTest/HTMLTestResults | 18 ++++ .../gb.eval.highlight/UnitTest/RTFTestResults | 18 ++++ .../gb.eval.highlight/UnitTest/TestProgram | 17 ++++ 10 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 comp/src/gb.eval.highlight/.src/TestSuite/AnsiConsoleColor.test create mode 100644 comp/src/gb.eval.highlight/.src/TestSuite/HtmlTextFormatColor.test create mode 100644 comp/src/gb.eval.highlight/.src/TestSuite/RichTextFormatColor.test create mode 100644 comp/src/gb.eval.highlight/.test create mode 100644 comp/src/gb.eval.highlight/UnitTest/ConsoleTestResults create mode 100644 comp/src/gb.eval.highlight/UnitTest/HTMLTestResults create mode 100644 comp/src/gb.eval.highlight/UnitTest/RTFTestResults create mode 100644 comp/src/gb.eval.highlight/UnitTest/TestProgram diff --git a/comp/src/gb.eval.highlight/.src/Main.module b/comp/src/gb.eval.highlight/.src/Main.module index f023c5987..1eef248dc 100644 --- a/comp/src/gb.eval.highlight/.src/Main.module +++ b/comp/src/gb.eval.highlight/.src/Main.module @@ -46,9 +46,9 @@ End Public Sub Main() - git stashDim hHighlighter As TextHighlighter - Dim sLine As String - Dim L As Integer + Dim hHighlighter As TextHighlighter + 'Dim sLine As String + ' Dim L As Integer hHighlighter = TextHighlighter["webpage"] diff --git a/comp/src/gb.eval.highlight/.src/TestSuite/AnsiConsoleColor.test b/comp/src/gb.eval.highlight/.src/TestSuite/AnsiConsoleColor.test new file mode 100644 index 000000000..698c3519f --- /dev/null +++ b/comp/src/gb.eval.highlight/.src/TestSuite/AnsiConsoleColor.test @@ -0,0 +1,13 @@ +' Gambas test file + +Public Sub ColorConsoleAnsiText() + + Dim hText As TextHighlighter + hText = TextHighlighter["gambas"] + Dim program As String = File.Load("UnitTest/TestProgram") + Dim hProgram As String = hText.ToConsole(program) +'file.save("~/ConsoleTestResults",hProgram) 'enable this and inport result id changes to toconsole +Assert.Equals(hProgram, File.Load("UnitTest/ConsoleTestResults")) + +End + diff --git a/comp/src/gb.eval.highlight/.src/TestSuite/HtmlTextFormatColor.test b/comp/src/gb.eval.highlight/.src/TestSuite/HtmlTextFormatColor.test new file mode 100644 index 000000000..be94c451b --- /dev/null +++ b/comp/src/gb.eval.highlight/.src/TestSuite/HtmlTextFormatColor.test @@ -0,0 +1,14 @@ +' Gambas test file + +Public Sub ColorHTMLPage() + + + Dim hText As TextHighlighter + hText = TextHighlighter["gambas"] + Dim program As String = File.Load("UnitTest/TestProgram") + Dim hProgram As String = hText.ToHTML(program) +'file.save("~/HTMLTestResults", hProgram) 'enable this and import result, when changes to toHTML +Assert.Equals(hProgram, File.Load("UnitTest/HTMLTestResults")) + +End ' Gambas test file + diff --git a/comp/src/gb.eval.highlight/.src/TestSuite/RichTextFormatColor.test b/comp/src/gb.eval.highlight/.src/TestSuite/RichTextFormatColor.test new file mode 100644 index 000000000..380bff5ce --- /dev/null +++ b/comp/src/gb.eval.highlight/.src/TestSuite/RichTextFormatColor.test @@ -0,0 +1,13 @@ +' Gambas test file + +Public Sub ColorRTFText() + + + Dim hText As TextHighlighter + hText = TextHighlighter["gambas"] + Dim program As String = File.Load("UnitTest/TestProgram") + Dim hProgram As String = hText.ToRichText(program) +'file.save("~/RTFTestResults", hProgram) 'enable this and import result, when changes to toRichText +Assert.Equals(hProgram, File.Load("UnitTest/RTFTestResults")) + +End \ No newline at end of file diff --git a/comp/src/gb.eval.highlight/.src/TextHighlighter.class b/comp/src/gb.eval.highlight/.src/TextHighlighter.class index 5103613cc..d0c6e0729 100644 --- a/comp/src/gb.eval.highlight/.src/TextHighlighter.class +++ b/comp/src/gb.eval.highlight/.src/TextHighlighter.class @@ -311,6 +311,104 @@ Public Sub ToRichText(Text As String, Optional Styles As TextHighlighterStyle[]) End +Private Sub ColorToConsole(TxtColor As Integer) As String + + Dim Red As Integer + Dim Green As Integer + Dim Blue As Integer + + red = Shr((TxtColor And &hff0000), 16) + green = Shr((TxtColor And &h00ff00), 8) + Blue = TxtColor And &h0000FF + Return "" & red & ";" & green & ";" & blue +End + +Public Sub ToConsole(Text As String, Optional Styles As TextHighlighterStyle[]) As String + + Dim aResult As New String[] + Dim sLine As String + Dim aHighlight As Byte[] + Dim Y As Integer + Dim I As Integer + Dim sHTML As String + Dim aText As String[] + Dim iState As Integer + Dim hStyle As TextHighlighterStyle + Dim bAlt As Boolean + Dim iLen As Integer + Dim P As Integer + Dim sLineHtml As String + Dim iColor As Integer + Dim sOldStyle As String + Dim sStyle As String + + If Not Styles Then Styles = TextHighlighter.DefaultStyle + + TextHighlighter.State = Highlight.Normal + TextHighlighter.Tag = 0 + TextHighlighter.Alternate = False + TextHighlighter.Limit = False + + aText = Split(Text, "\n") + + iColor = 0 ' this is black but 0 in terminal means default settings for terminal + aResult.Add("\x1b[0m") + sOldStyle = "color:#000000;" + + For Y = 0 To aText.Max + + aHighlight = New Byte[] + + TextHighlighter._Highlight = aHighlight + TextHighlighter.TextAfter = "" + TextHighlighter.Line = Y + + sLine = aText[Y] + Me.Run(sLine) + If Y = 0 Then TextHighlighter.Limit = False + + If TextHighlighter.TextAfter Then sLine = TextHighlighter.TextAfter + + sLineHtml = "" + P = 1 + + For I = 0 To aHighlight.Max Step 2 + + iState = aHighlight[I] And 31 + Try hStyle = Styles[iState] + If Error Then hStyle = Styles[0] + bAlt = aHighlight[I] >= 128 + iLen = aHighlight[I + 1] + + sHtml = String.Mid$(sLine, P, iLen) + P += iLen + + If hStyle.Bold Then sHtml = "\x1b[1m" & sHtml & "\x1b[22m" ' "\x1b[21m" some terminal don't regognize this + If hStyle.Underline Then sHtml = "\x1b[4m" & sHtml & "\x1b[24m" + + sStyle = "" + If bAlt Then sStyle &= "\x1b[48;2;" & ColorToConsole(Styles[Highlight.Alternate]) & "m" ' background color + If hStyle.Color Then sStyle &= "\x1b[38;2;" & ColorToConsole(hStyle.Color) & "m" ' forground color + + If sStyle <> sOldStyle Then + sOldStyle = sStyle + sHtml = "\x1b[39m\x1b[49m" & sStyle & sHtml + Endif + + sLineHtml &= sHtml + + Next + + aResult.Add(sLineHtml & "\n") + + Next + + aResult.Add("\x1b[0m") + Return aResult.Join("") + + End + + ' Public Sub Paint(Text As String, X As Integer, Y As Integer, Optional Styles As TextHighlighterStyle[]) As String ' ' Dim sLine As String diff --git a/comp/src/gb.eval.highlight/.test b/comp/src/gb.eval.highlight/.test new file mode 100644 index 000000000..7c7b02c27 --- /dev/null +++ b/comp/src/gb.eval.highlight/.test @@ -0,0 +1,5 @@ +[TestSuites] +Count=0 +TestWith=0 +Default="AnsiConsoleColor.ColorConsoleAnsiText,HtmlTextFormatColor.ColorHTMLPage,RichTextFormatColor.ColorRTFText" + diff --git a/comp/src/gb.eval.highlight/UnitTest/ConsoleTestResults b/comp/src/gb.eval.highlight/UnitTest/ConsoleTestResults new file mode 100644 index 000000000..695d62abb --- /dev/null +++ b/comp/src/gb.eval.highlight/UnitTest/ConsoleTestResults @@ -0,0 +1,18 @@ +Sub Test() As Integer + Static N As Integer = 1 + N *= 2 + Return N +End + +Sub Test1() As Integer + Static N As Integer = 1 + N *= 3 + Return N +End + +Public BigString as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +Dim I As Integer +For I = 1 to 10 + Print Test();;":";;test1() +Next + \ No newline at end of file diff --git a/comp/src/gb.eval.highlight/UnitTest/HTMLTestResults b/comp/src/gb.eval.highlight/UnitTest/HTMLTestResults new file mode 100644 index 000000000..5fb25da6c --- /dev/null +++ b/comp/src/gb.eval.highlight/UnitTest/HTMLTestResults @@ -0,0 +1,18 @@ +
Sub Test() As Integer
+  Static N As Integer = 1
+  N *= 2
+  Return N
+End
+
+Sub Test1() As Integer
+  Static N As Integer = 1
+  N *= 3
+  Return N
+End
+
+Public BigString as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+Dim I As Integer
+For I = 1 to 10
+  Print Test();;":";;test1()
+Next
+
\ No newline at end of file diff --git a/comp/src/gb.eval.highlight/UnitTest/RTFTestResults b/comp/src/gb.eval.highlight/UnitTest/RTFTestResults new file mode 100644 index 000000000..3c154414f --- /dev/null +++ b/comp/src/gb.eval.highlight/UnitTest/RTFTestResults @@ -0,0 +1,18 @@ +Sub Test() As Integer +  Static N As Integer = 1 +  N *= 2 +  Return N +End + +Sub Test1() As Integer +  Static N As Integer = 1 +  N *= 3 +  Return N +End + +Public BigString as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +Dim I As Integer +For I = 1 to 10 +  Print Test();;":";;test1() +Next + \ No newline at end of file diff --git a/comp/src/gb.eval.highlight/UnitTest/TestProgram b/comp/src/gb.eval.highlight/UnitTest/TestProgram new file mode 100644 index 000000000..d2b92ce25 --- /dev/null +++ b/comp/src/gb.eval.highlight/UnitTest/TestProgram @@ -0,0 +1,17 @@ +Sub Test() As Integer + Static N As Integer = 1 + N *= 2 + Return N +End + +Sub Test1() As Integer + Static N As Integer = 1 + N *= 3 + Return N +End + +Public BigString as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +Dim I As Integer +For I = 1 to 10 + Print Test();;":";;test1() +Next \ No newline at end of file