Help for syntax highlighting



  • I did a little bit of research in Google and the forum, but can’t find a sollution so far.

    I often work with files like these:

    hh:mm:ss;Fehlernr.;Index;aktiv;CPU;Beschreibung;Par1;Par2;Par3;Par4;Information;
    76.533:22:14;21;1;0;A;“Error1”;1;0;21001;322;2;
    76.533:22:14;21;1;0;B;“Error1”;1;0;21001;322;4;
    76.533:19:51;21;1;0;A;“Error1”;1;0;21001;5337;2;
    76.533:19:40;21;1;0;A;“Error1”;1;0;21001;5337;2;
    76.533:19:21;21;1;0;A;“Error1”;1;0;21001;5337;2;
    76.533:18:46;31;7;0;A;“Error2”;0;0;31007;503;2;
    76.533:18:33;31;7;0;A;“Error2”;0;0;31007;503;2;
    60:12:02;26;4;0;A;“Error3”;0;0;26004;0;2;
    60:12:02;26;3;0;A;“Error3”;0;0;26003;2110;2;
    42:58:01;21;1;0;B;“Error1”;1;0;21001;322;4;
    42:57:20;21;1;0;A;“Error1”;1;0;21001;322;2;
    42:15:00;4;1;0;B;“Error4”;-32;-32000;4001;2597752;4;
    42:15:00;4;1;0;A;“Error4”;-32;-32000;4001;2597752;2;

    It would be really helpful if I could split these strings in different blocks and also colour them differently, to make it more readable.
    Best would be, if it would also stretch it out a little bit, that it becomes a little bit like a table.
    Is that possible with the UDL?

    my proposal



  • Hello Marco Bimbös,

    afaik, UDL cannot help you here.
    What could be done, in regards to formatting, is to use the Python Script plugin and a little script.
    I’have written here what needs to be done first.

    Regarding colorizing, it could be done but I don’t think a python script would be a good solution.
    From what I understand, colorizing is done when you activate a new document, repeatedly but
    a script runs once, normally.
    If you use callbacks like BUFFERACTIVATED then you need to take care that there is a logical
    way when the script should be executed and when not.
    Lastly, the addStyledText function is not that fast. Colorizing 100 lines of your provided sample
    takes up to 3 seconds.

    So, a python script could look like this

    a = [[] for x in editor.getLine(0).split(";")]   # create a list of lists using the first line in the doc
    
    def ssv_to_list(cont, l_num, tot_lines):        # convert semicolon separated values to a list
        cont = cont.split(";") 
        for n in range(len(cont)): 
            a[n].append(cont[n].replace("\r\n", ""))
        
    editor.forEachLine(ssv_to_list)     # well, self explaining I guess
    
    aLens = [max([len(x) for x in a[y]]) for y in range(len(a))]    # find out what the biggest string is
    
    def format_line(l_num):        # format the lines
        txt = ''
        spacer = 1     # can be used to increace space between columns
        for x in range(len(aLens) - 1):   # loop
            if x == 4 :     # your example used one left justified column
                txt += "{0:<{1}}".format(' ',spacer) + a[x][l_num].ljust(aLens[x])
            else:      # right justified
                txt += a[x][l_num].rjust(aLens[x]+spacer)
        return txt + '\r\n'   # add line breaks
    
    notepad.new()     # open a new doc
    big_txt = ''
    
    for l in range(len(a[0])):  # loop through the list of lists saved in ssv_to_list
        big_txt += format_line(l)     # create the new formatted text
    
    editor.addText(big_txt)     # add it to editor.
    

    Cheers
    Claudia



  • Nice job Claudia. I actually tried this out. The only (slight) negative is the x==4 part, which binds it to the data set rather tightly (but that was what the user wanted). I could see this being used to do a quick reformat on CSV data with a few tweaks. Again, good job.



  • Hello Scott,

    thank you for your kind words, you are right, this script, as it is, is only useful
    if you have a data set similar to the one Marco has.
    For those who want to have a more general solution replace the format_line
    function with this one

    def format_line(l_num):
        txt = ''
        spacer = 1
        for x in range(len(aLens) - 1):
            txt += a[x][l_num].rjust(aLens[x]+spacer)
        return txt + '\r\n'
    

    Cheers
    Claudia