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
This commit is contained in:
Brian G 2021-06-18 17:04:26 -07:00
parent 16dd6690ce
commit e4ee382336
10 changed files with 217 additions and 3 deletions

View file

@ -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"]

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
[TestSuites]
Count=0
TestWith=0
Default="AnsiConsoleColor.ColorConsoleAnsiText,HtmlTextFormatColor.ColorHTMLPage,RichTextFormatColor.ColorRTFText"

View file

@ -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


View file

@ -0,0 +1,18 @@
<pre><span style="color:black;"></span><span style=""><b>Sub</b>&nbsp;Test<b>()</b>&nbsp;<b>As</b>&nbsp;</span><span style="color:#DF6B00;"><b>Integer</b>
</span><span style="">&nbsp;&nbsp;<b>Static</b>&nbsp;N&nbsp;<b>As</b>&nbsp;</span><span style="color:#DF6B00;"><b>Integer</b></span><span style="">&nbsp;<b>=</b>&nbsp;</span><span style="color:#FF0000;"><b>1</b>
</span><span style="">&nbsp;&nbsp;N&nbsp;<b>*=</b>&nbsp;</span><span style="color:#FF0000;"><b>2</b>
</span><span style="">&nbsp;&nbsp;<b>Return</b>&nbsp;N
<b>End</b>
<b>Sub</b>&nbsp;Test1<b>()</b>&nbsp;<b>As</b>&nbsp;</span><span style="color:#DF6B00;"><b>Integer</b>
</span><span style="">&nbsp;&nbsp;<b>Static</b>&nbsp;N&nbsp;<b>As</b>&nbsp;</span><span style="color:#DF6B00;"><b>Integer</b></span><span style="">&nbsp;<b>=</b>&nbsp;</span><span style="color:#FF0000;"><b>1</b>
</span><span style="">&nbsp;&nbsp;N&nbsp;<b>*=</b>&nbsp;</span><span style="color:#FF0000;"><b>3</b>
</span><span style="">&nbsp;&nbsp;<b>Return</b>&nbsp;N
<b>End</b>
<b>Public</b>&nbsp;BigString&nbsp;<b>as</b>&nbsp;</span><span style="color:#DF6B00;"><b>string</b></span><span style="">&nbsp;<b>=</b>&nbsp;</span><span style="color:#7F0000;">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
</span><span style=""><b>Dim</b>&nbsp;I&nbsp;<b>As</b>&nbsp;</span><span style="color:#DF6B00;"><b>Integer</b>
</span><span style=""><b>For</b>&nbsp;I&nbsp;<b>=</b>&nbsp;</span><span style="color:#FF0000;"><b>1</b></span><span style="">&nbsp;<b>to</b>&nbsp;</span><span style="color:#FF0000;"><b>10</b>
</span><span style="">&nbsp;&nbsp;<b>Print</b>&nbsp;Test<b>();;</b></span><span style="color:#7F0000;">&quot;:&quot;</span><span style=""><b>;;</b>test1<b>()</b>
<b>Next</b>
</span></pre>

View file

@ -0,0 +1,18 @@
<font color="#000000"></font><font><b>Sub</b>&nbsp;Test<b>()</b>&nbsp;<b>As</b>&nbsp;</font><font color="#DF6B00"><b>Integer</b>
</font><font>&nbsp;&nbsp;<b>Static</b>&nbsp;N&nbsp;<b>As</b>&nbsp;</font><font color="#DF6B00"><b>Integer</b></font><font>&nbsp;<b>=</b>&nbsp;</font><font color="#FF0000"><b>1</b>
</font><font>&nbsp;&nbsp;N&nbsp;<b>*=</b>&nbsp;</font><font color="#FF0000"><b>2</b>
</font><font>&nbsp;&nbsp;<b>Return</b>&nbsp;N
<b>End</b>
<b>Sub</b>&nbsp;Test1<b>()</b>&nbsp;<b>As</b>&nbsp;</font><font color="#DF6B00"><b>Integer</b>
</font><font>&nbsp;&nbsp;<b>Static</b>&nbsp;N&nbsp;<b>As</b>&nbsp;</font><font color="#DF6B00"><b>Integer</b></font><font>&nbsp;<b>=</b>&nbsp;</font><font color="#FF0000"><b>1</b>
</font><font>&nbsp;&nbsp;N&nbsp;<b>*=</b>&nbsp;</font><font color="#FF0000"><b>3</b>
</font><font>&nbsp;&nbsp;<b>Return</b>&nbsp;N
<b>End</b>
<b>Public</b>&nbsp;BigString&nbsp;<b>as</b>&nbsp;</font><font color="#DF6B00"><b>string</b></font><font>&nbsp;<b>=</b>&nbsp;</font><font color="#7F0000">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
</font><font><b>Dim</b>&nbsp;I&nbsp;<b>As</b>&nbsp;</font><font color="#DF6B00"><b>Integer</b>
</font><font><b>For</b>&nbsp;I&nbsp;<b>=</b>&nbsp;</font><font color="#FF0000"><b>1</b></font><font>&nbsp;<b>to</b>&nbsp;</font><font color="#FF0000"><b>10</b>
</font><font>&nbsp;&nbsp;<b>Print</b>&nbsp;Test<b>();;</b></font><font color="#7F0000">&quot;:&quot;</font><font><b>;;</b>test1<b>()</b>
<b>Next</b>
</font>

View file

@ -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