Wish Simple CharSequence

Antoine EGO

Member
To Mr Erel,
I wish you add a new command to B4A to get a simple CharSequence, instead of typing a long code using Append & Pop
such as this function:

A simple CharSequence:
'To Make a simple CharSequence:
'Message="Color1/txt1/Color2/txt2/Color3/txt3...."
'  Example : <code>
'Dim msg As string
'msg="black/Hello Mr. /red/Antoine /green/*EGO*/gray/ How are you?"
'Msgbox(CS(msg),"Title")
'  Equals to :
'Dim msg As CSBuilder
'msg.Initialize.Append("Hello Mr. ").Color(Colors.Red).Append("Antoine ").Pop. _
'Color(Colors.Green).Bold.Append("EGO").Pop.Pop.color(Colors.gray).Append _
'(" How are you?").PopAll
'Msgbox(msg,"Title")
'</code>
'If Color is ommitted so it is Black
'If text is written like *txt* so it is Bold
'If text starts with {} so a new line is add, Example:<code>
'Message="red/Dear sir/blue/{}Hope we meet tomorrow./green/{}*Thanks*"
'Message="/Dear sir//{}Hope we meet tomorrow.//*{}Thanks*"
'</code>
Sub CS (Message As String) As CSBuilder
    Dim msg As CSBuilder
    msg.Initialize
    '-------------------
    If Not (Message.Contains("/")) Then
        msg.Append(Message).PopAll
        Return  msg
    End If
    '--------------------
    Dim L,i As Int
    Dim P() As String' Parts of string
    Dim A As String
    
    P=Regex.Split("/",Message)
    L=P.Length 'How many parts
    Log("L="&L)
    Dim C(L) As Int 'Color of the part
    If L Mod 2 <>0 Then L=L-1' If L is odd we ommit the last Part
    
    For i=0 To L-1
        If i Mod 2=0 Then '--------------------
            'even number
            Select P(i).ToLowerCase'same color numbers as DOS
                'Case "black","0","":C(i)=Colors.DarkGray
                Case "blue","1":C(i)=Colors.blue
                Case "green","2":C(i)=Colors.green
                Case "cyan","3":C(i)=Colors.Cyan
                Case "red","4":C(i)=Colors.red
                Case "magenta","5":C(i)=Colors.Magenta
                Case "yellow","6":C(i)=Colors.yellow
                Case "white","7":C(i)=Colors.white
                Case "gray","8":C(i)=Colors.gray
                Case Else
                    C(i)=Colors.DarkGray
            End Select
        Else '--------------------
            'Odd Number
            If p(i).StartsWith("{}") Then 'add a new line
                msg.Append(CRLF)
                P(i)=P(i).SubString(2)
            End If
            
            If p(i).StartsWith("*") And P(i).EndsWith("*") Then ' Bold text must be like: *txt*
                P(i)=P(i).Replace("*","")
                If p(i).StartsWith("{}") Then 'add a new line when text is *{}txt* instead of {}*txt*
                    msg.Append(CRLF)
                    P(i)=P(i).SubString(2)
                End If
                msg.Color(C(i-1)).Bold.Append(P(i)).Pop.Pop
            Else
                msg.Color(C(i-1)).Append(P(i)).Pop
            End If
        End If '--------------------
    Next
    msg.append("").PopAll
    Return msg
End Sub
 

Attachments

  • CS.bas
    2.4 KB · Views: 480
Last edited:

Antoine EGO

Member
I wish it would be built-in, as a new command in B4A, so we can use it anytime.
Besides, Mr. Erel can develop the command better than I do.
Using Append & Pop is really tiring when we have many spans.
This command uses (Message As String) so you can anytime have a colored message without turning the message into CSBuilder.

Most of time we don't need more than a colored message!
 
Top