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:
parent
16dd6690ce
commit
e4ee382336
10 changed files with 217 additions and 3 deletions
|
@ -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"]
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
|
@ -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
|
||||
|
|
5
comp/src/gb.eval.highlight/.test
Normal file
5
comp/src/gb.eval.highlight/.test
Normal file
|
@ -0,0 +1,5 @@
|
|||
[TestSuites]
|
||||
Count=0
|
||||
TestWith=0
|
||||
Default="AnsiConsoleColor.ColorConsoleAnsiText,HtmlTextFormatColor.ColorHTMLPage,RichTextFormatColor.ColorRTFText"
|
||||
|
18
comp/src/gb.eval.highlight/UnitTest/ConsoleTestResults
Normal file
18
comp/src/gb.eval.highlight/UnitTest/ConsoleTestResults
Normal file
|
@ -0,0 +1,18 @@
|
|||
[0m[39m[49m[1mSub[22m Test[1m()[22m [1mAs[22m [39m[49m[38;2;223;107;0m[1mInteger[22m
|
||||
[39m[49m [1mStatic[22m N [1mAs[22m [39m[49m[38;2;223;107;0m[1mInteger[22m[39m[49m [1m=[22m [39m[49m[38;2;255;0;0m[1m1[22m
|
||||
[39m[49m N [1m*=[22m [39m[49m[38;2;255;0;0m[1m2[22m
|
||||
[39m[49m [1mReturn[22m N
|
||||
[1mEnd[22m
|
||||
|
||||
[1mSub[22m Test1[1m()[22m [1mAs[22m [39m[49m[38;2;223;107;0m[1mInteger[22m
|
||||
[39m[49m [1mStatic[22m N [1mAs[22m [39m[49m[38;2;223;107;0m[1mInteger[22m[39m[49m [1m=[22m [39m[49m[38;2;255;0;0m[1m1[22m
|
||||
[39m[49m N [1m*=[22m [39m[49m[38;2;255;0;0m[1m3[22m
|
||||
[39m[49m [1mReturn[22m N
|
||||
[1mEnd[22m
|
||||
|
||||
[1mPublic[22m BigString [1mas[22m [39m[49m[38;2;223;107;0m[1mstring[22m[39m[49m [1m=[22m [39m[49m[38;2;127;0;0m"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
[39m[49m[1mDim[22m I [1mAs[22m [39m[49m[38;2;223;107;0m[1mInteger[22m
|
||||
[39m[49m[1mFor[22m I [1m=[22m [39m[49m[38;2;255;0;0m[1m1[22m[39m[49m [1mto[22m [39m[49m[38;2;255;0;0m[1m10[22m
|
||||
[39m[49m [1mPrint[22m Test[1m();;[22m[39m[49m[38;2;127;0;0m":"[39m[49m[1m;;[22mtest1[1m()[22m
|
||||
[1mNext[22m
|
||||
[0m
|
18
comp/src/gb.eval.highlight/UnitTest/HTMLTestResults
Normal file
18
comp/src/gb.eval.highlight/UnitTest/HTMLTestResults
Normal file
|
@ -0,0 +1,18 @@
|
|||
<pre><span style="color:black;"></span><span style=""><b>Sub</b> Test<b>()</b> <b>As</b> </span><span style="color:#DF6B00;"><b>Integer</b>
|
||||
</span><span style=""> <b>Static</b> N <b>As</b> </span><span style="color:#DF6B00;"><b>Integer</b></span><span style=""> <b>=</b> </span><span style="color:#FF0000;"><b>1</b>
|
||||
</span><span style=""> N <b>*=</b> </span><span style="color:#FF0000;"><b>2</b>
|
||||
</span><span style=""> <b>Return</b> N
|
||||
<b>End</b>
|
||||
|
||||
<b>Sub</b> Test1<b>()</b> <b>As</b> </span><span style="color:#DF6B00;"><b>Integer</b>
|
||||
</span><span style=""> <b>Static</b> N <b>As</b> </span><span style="color:#DF6B00;"><b>Integer</b></span><span style=""> <b>=</b> </span><span style="color:#FF0000;"><b>1</b>
|
||||
</span><span style=""> N <b>*=</b> </span><span style="color:#FF0000;"><b>3</b>
|
||||
</span><span style=""> <b>Return</b> N
|
||||
<b>End</b>
|
||||
|
||||
<b>Public</b> BigString <b>as</b> </span><span style="color:#DF6B00;"><b>string</b></span><span style=""> <b>=</b> </span><span style="color:#7F0000;">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
</span><span style=""><b>Dim</b> I <b>As</b> </span><span style="color:#DF6B00;"><b>Integer</b>
|
||||
</span><span style=""><b>For</b> I <b>=</b> </span><span style="color:#FF0000;"><b>1</b></span><span style=""> <b>to</b> </span><span style="color:#FF0000;"><b>10</b>
|
||||
</span><span style=""> <b>Print</b> Test<b>();;</b></span><span style="color:#7F0000;">":"</span><span style=""><b>;;</b>test1<b>()</b>
|
||||
<b>Next</b>
|
||||
</span></pre>
|
18
comp/src/gb.eval.highlight/UnitTest/RTFTestResults
Normal file
18
comp/src/gb.eval.highlight/UnitTest/RTFTestResults
Normal file
|
@ -0,0 +1,18 @@
|
|||
<font color="#000000"></font><font><b>Sub</b> Test<b>()</b> <b>As</b> </font><font color="#DF6B00"><b>Integer</b>
|
||||
</font><font> <b>Static</b> N <b>As</b> </font><font color="#DF6B00"><b>Integer</b></font><font> <b>=</b> </font><font color="#FF0000"><b>1</b>
|
||||
</font><font> N <b>*=</b> </font><font color="#FF0000"><b>2</b>
|
||||
</font><font> <b>Return</b> N
|
||||
<b>End</b>
|
||||
|
||||
<b>Sub</b> Test1<b>()</b> <b>As</b> </font><font color="#DF6B00"><b>Integer</b>
|
||||
</font><font> <b>Static</b> N <b>As</b> </font><font color="#DF6B00"><b>Integer</b></font><font> <b>=</b> </font><font color="#FF0000"><b>1</b>
|
||||
</font><font> N <b>*=</b> </font><font color="#FF0000"><b>3</b>
|
||||
</font><font> <b>Return</b> N
|
||||
<b>End</b>
|
||||
|
||||
<b>Public</b> BigString <b>as</b> </font><font color="#DF6B00"><b>string</b></font><font> <b>=</b> </font><font color="#7F0000">"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
</font><font><b>Dim</b> I <b>As</b> </font><font color="#DF6B00"><b>Integer</b>
|
||||
</font><font><b>For</b> I <b>=</b> </font><font color="#FF0000"><b>1</b></font><font> <b>to</b> </font><font color="#FF0000"><b>10</b>
|
||||
</font><font> <b>Print</b> Test<b>();;</b></font><font color="#7F0000">":"</font><font><b>;;</b>test1<b>()</b>
|
||||
<b>Next</b>
|
||||
</font>
|
17
comp/src/gb.eval.highlight/UnitTest/TestProgram
Normal file
17
comp/src/gb.eval.highlight/UnitTest/TestProgram
Normal 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
|
Loading…
Reference in a new issue