Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Questions Concerning colorizing text in Ubuntu terminal.

Questions Concerning colorizing text in Ubuntu terminal.

Scheduled Pinned Locked Moved Unsolved The Lounge
62 Posts 6 Posters 12.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cougar 0
    wrote on 9 Sept 2021, 18:54 last edited by Cougar 0
    #1

    Hello,
    I am asking for help in directing me to the correct forum for my questions.
    I have a Qt5 program that accesses a Ubuntu terminal, all the text is white.
    I also have this program in Perl, see pic.alt text
    I was hoping someone could help me with my questions about colorizing the text in terminal in Qt5. See pic.alt text
    Any help would be appreciated!
    Thanks!

    Cougar

    E 1 Reply Last reply 9 Sept 2021, 19:13
    0
    • C Cougar 0
      9 Sept 2021, 18:54

      Hello,
      I am asking for help in directing me to the correct forum for my questions.
      I have a Qt5 program that accesses a Ubuntu terminal, all the text is white.
      I also have this program in Perl, see pic.alt text
      I was hoping someone could help me with my questions about colorizing the text in terminal in Qt5. See pic.alt text
      Any help would be appreciated!
      Thanks!

      E Offline
      E Offline
      eyllanesc
      wrote on 9 Sept 2021, 19:13 last edited by eyllanesc 9 Sept 2021, 19:13
      #2

      @Cougar-0 Read https://en.wikipedia.org/wiki/ANSI_escape_code#Colors and https://stackoverflow.com/a/54062826/6622587

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Cougar 0
        wrote on 9 Sept 2021, 20:22 last edited by
        #3

        Thank you! Here is a snippet of the Perl code that colorizes the terminal.

        # color codes for fancy terminal output (not to file)
          $type = "\e[1m\e[91m$type\e[0m" if ($type =~ m/(fatal|fail|error|stop)/i);# bold red
          $type = "\e[91m$type\e[0m" if ($type =~ m/(refused|nodevice|timeout)/i);  # red
          $type = "\e[93m$type\e[0m" if ($type =~ m/(reset|warning|secure|unset)/i);# yellow
          $type = "\e[95m$type\e[0m" if ($type =~ m/(add|update|delete)/i);         # magenta
          $type = "\e[96m$type\e[0m" if ($type =~ m/(list|uplink)/i);               # cyan
          $type = "\e[94m$type\e[0m" if ($type =~ m/(beacon|syncer)/i);             # blue
          $type = "\e[92m$type\e[0m" if ($type =~ m/(stat|kfnew)/i);                # green
          $type = "\e[1m\e[92m$type\e[0m" if ($type =~ m/(info|debug)/i);           # bold green
        

        I was hoping Qt5 would be as easy as this.
        Thanks for helping!

        Cougar

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 9 Sept 2021, 21:57 last edited by
          #4

          Hi,

          AFAIK, you can pass these sequences to qDebug and they will be shown colorized on your terminal.

          The syntax will be a bit different for the if parts though.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cougar 0
            wrote on 10 Sept 2021, 07:12 last edited by Cougar 0 9 Oct 2021, 07:47
            #5

            Thanks!
            I searched, "Passing sequences in qDebug" , got nothing.

            Perl
            $type = "\e[91m$type\e[0m" if ($type =~ m/(refused|nodevice|timeout)/i);  # red
                Qt5
            printf("\\x1B[31mTexting\\033[0m\\t\\t");\line
            

            With both of those lines I can see the color will be red.
            With Perl I can see the words that will be highlighted red. (refused|nodevice|timeout)
            With Qt5 I have no idea what text is going to be highlighted.
            I'm hoping someone can help me with the Qt5 line with setting it up to highlight the text like in Perl. This is for Ubuntu.
            Thanks!

            Cougar

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 10 Sept 2021, 19:35 last edited by
              #6

              The same way you are doing with Perl: using regulate expression.

              QRegularExpression.

              There are other possibilities like simple string search.

              Up to you to benchmark what works best for your use case.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • C Offline
                C Offline
                Cougar 0
                wrote on 10 Sept 2021, 21:46 last edited by Cougar 0 9 Nov 2021, 01:20
                #7

                Thank you very much!
                Also I read this, "Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct".
                But there was no box or a way to mark as read so I see it everywhere like I am suppost to read it again, is that normal or am I missing something?

                I hope I am on to something that will work!

                
                	text
                        {
                         text: "(refused|nodevice|timeout)"
                         font.family: "Courier New"
                         font.pointSize: 12
                         color: "red"
                    }
                

                Cougar

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Cougar 0
                  wrote on 11 Sept 2021, 07:43 last edited by Cougar 0 9 Nov 2021, 08:08
                  #8

                  Amin, if this is against forum rules please delete!
                  After trying to get this to work I realize I'm too old, blind, deaf, with bad health to do this.
                  I would be willing to pay someone via PayPal to help me get this to work.
                  If interested please contact me at wpt1114 at yahoo.com to discuss the details.
                  Thanks!

                  Cougar

                  J S 2 Replies Last reply 11 Sept 2021, 09:16
                  0
                  • C Cougar 0
                    11 Sept 2021, 07:43

                    Amin, if this is against forum rules please delete!
                    After trying to get this to work I realize I'm too old, blind, deaf, with bad health to do this.
                    I would be willing to pay someone via PayPal to help me get this to work.
                    If interested please contact me at wpt1114 at yahoo.com to discuss the details.
                    Thanks!

                    J Offline
                    J Offline
                    JonB
                    wrote on 11 Sept 2021, 09:16 last edited by
                    #9

                    @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                    After trying to get this to work I realize I'm too old, blind, deaf, with bad health to do this.

                    Sorry to hear that. Do you mean you just want to translate the 10 lines of Perl code you wrote above to C++/Qt? I will post that for you if that's what you want (no charge!). But if you want someone to do your whole project please say so, because that's not me.

                    I hope I am on to something that will work!

                    Your code here looks like Qt's QML? Is that what you want to use, because again that's not me, or are you wanting a Qt widgets application?

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Cougar 0
                      wrote on 11 Sept 2021, 09:54 last edited by Cougar 0 9 Nov 2021, 20:23
                      #10

                      All I need is this;

                      #include "whatever file is/if needed!"
                      #include "whatever file is/if needed!"
                      
                      ( line of code or what's needed)
                      
                      [Rewrite the following Perl into C++/Qt.]
                      {
                             {
                             "$type = "\e[1m\e[91m$type\e[0m" if ($type =~ 
                             m/(fatal|fail|error|stop)/i);# bold red"
                             "$type = "\e[91m$type\e[0m" if ($type 
                               =~m/(refused|nodevice|timeout)/i);  # red" 
                             }
                      }
                      

                      The Qt5 code written completely that would replace the Perl lines of code above that highlights those 4 words in Bold Red and those 3 words in Red in a ubuntu terminal.
                      Once I have those 4 + 3 words highlighted in Bold Red and Red in terminal working, I can add the other words and colors to the file.
                      I hope I explained it for you.

                      JonB, thank you for offering to rewrite all 10 Perl lines into C++/Qt5 for me at no charge. Really, thank you so very much!
                      But if you could do just those two lines, then I can finish it and learn a little!
                      Again Thanks!

                      Cougar

                      1 Reply Last reply
                      0
                      • C Cougar 0
                        11 Sept 2021, 07:43

                        Amin, if this is against forum rules please delete!
                        After trying to get this to work I realize I'm too old, blind, deaf, with bad health to do this.
                        I would be willing to pay someone via PayPal to help me get this to work.
                        If interested please contact me at wpt1114 at yahoo.com to discuss the details.
                        Thanks!

                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 11 Sept 2021, 20:46 last edited by SGaist
                        #11

                        @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                        Amin, if this is against forum rules please delete!

                        It's a banner shown to everybody, you did nothing wrong.

                        From the top of my head something as below:.

                        WARNING THIS IS WRONG AND LEFT FOR EDUCATIONAL PURPOSE
                        #include <QRegularExpression>
                        
                        void printColorized(const QString& text)
                        {
                            QString toPrint;
                            if (text.contains(QRegularExpression("(fatal|fail|error|stop)")) {
                                toPrint = QString("\e[1m\e[91m%1\e[0m").arg(text);
                        }
                            if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
                                toPrint = QString("\e[91m%1\e[0m").arg(text);
                        }
                        qDebug() << toPrint;
                        }
                        

                        Here is the correct way to trigger the colorization of the terminal manually:

                        #include <QRegularExpression>
                        #include <QCoreApplication>
                        #include <QString>
                        #include <QtDebug>
                        
                        void printColorized(const QString& text)
                        {
                            if (text.contains(QRegularExpression("(fatal|fail|error|stop)"))) {
                            qDebug() << "\e[1m\e[91m" << text << "\e[0m";
                        }
                            if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
                                qDebug() << "\e[91m" << text << "\e[0m";
                        }
                        }
                        

                        [add fixed version as the original will just print the QString content SGaist]

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        C 1 Reply Last reply 13 Sept 2021, 20:19
                        4
                        • C Offline
                          C Offline
                          Cougar 0
                          wrote on 11 Sept 2021, 22:36 last edited by
                          #12

                          SGaist, Thank you so much I will give this a try!

                          Cougar

                          1 Reply Last reply
                          0
                          • C Offline
                            C Offline
                            Cougar 0
                            wrote on 13 Sept 2021, 03:59 last edited by Cougar 0
                            #13

                            After several tries with using the code above, I have had no success in getting it to work.
                            More info about my project.
                            My friend Tim is the true genius in this. I'm just an old man. It is his Perl and Qt code.
                            He is so busy with work, school and family, 25 hours a day, 8 days a week!
                            I was hoping to surprise him with getting the color text working in the terminal.
                            I told him what I was doing and he told me the reason he did not pursue the color text in Qt was because the color text in Perl messed with his log files, besides I am the one who misses the color text.
                            Here is links to the masterserver website and qt5 source code.
                            http://333networks.com/
                            http://git.333networks.com/MasterServer-Qt5/
                            Tim suggested I write a "Hello World" project implementing color text.
                            If anyone has or knows of any links to "Hello World" examples with text in color using Qt Creator please post.
                            He said he was not sure if Qt supports color text in terminal output like Perl does.
                            I will keep at this besides it gives me something to do, it's all for kicks and giggles for me.
                            I wish I had 5 years to take a course to learn all of this or a personal instructor to teach me.
                            If anyone feels I owe them please contact me through my email above.
                            Thanks for all those who helped!
                            My desk,
                            alt text
                            If interested my masterserver website:
                            gonespy.com!

                            Cougar

                            jsulmJ 1 Reply Last reply 13 Sept 2021, 14:27
                            0
                            • C Cougar 0
                              13 Sept 2021, 03:59

                              After several tries with using the code above, I have had no success in getting it to work.
                              More info about my project.
                              My friend Tim is the true genius in this. I'm just an old man. It is his Perl and Qt code.
                              He is so busy with work, school and family, 25 hours a day, 8 days a week!
                              I was hoping to surprise him with getting the color text working in the terminal.
                              I told him what I was doing and he told me the reason he did not pursue the color text in Qt was because the color text in Perl messed with his log files, besides I am the one who misses the color text.
                              Here is links to the masterserver website and qt5 source code.
                              http://333networks.com/
                              http://git.333networks.com/MasterServer-Qt5/
                              Tim suggested I write a "Hello World" project implementing color text.
                              If anyone has or knows of any links to "Hello World" examples with text in color using Qt Creator please post.
                              He said he was not sure if Qt supports color text in terminal output like Perl does.
                              I will keep at this besides it gives me something to do, it's all for kicks and giggles for me.
                              I wish I had 5 years to take a course to learn all of this or a personal instructor to teach me.
                              If anyone feels I owe them please contact me through my email above.
                              Thanks for all those who helped!
                              My desk,
                              alt text
                              If interested my masterserver website:
                              gonespy.com!

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 13 Sept 2021, 14:27 last edited by jsulm
                              #14

                              @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                              I have had no success in getting it to work

                              Can you show the code?
                              I think you need to escape the \
                              Like:

                              if (text.contains(QRegularExpression("(fatal|fail|error|stop)")) {
                                      toPrint = QString("\\e[1m\\e[91m%1\\e[0m").arg(text);
                              

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J 1 Reply Last reply 13 Sept 2021, 14:59
                              1
                              • jsulmJ jsulm
                                13 Sept 2021, 14:27

                                @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                                I have had no success in getting it to work

                                Can you show the code?
                                I think you need to escape the \
                                Like:

                                if (text.contains(QRegularExpression("(fatal|fail|error|stop)")) {
                                        toPrint = QString("\\e[1m\\e[91m%1\\e[0m").arg(text);
                                
                                J Offline
                                J Offline
                                JonB
                                wrote on 13 Sept 2021, 14:59 last edited by JonB
                                #15

                                @jsulm said in Questions Concerning colorizing text in Ubuntu terminal.:

                                I think you need to escape the \

                                toPrint = QString("\\e[1m\\e[91m%1\\e[0m").arg(text);

                                I don't think so :) It takes a brave man to challenge @SGaist, especially if he has typed in code inside backticks. But here he is right. You do not want "\\e" here, to deliver a literal \e. What you want is the C++ literal for "escape character" (ANSI sequences are all escape-[-....)

                                \e = \x1B = escape (non-standard GCC extension)

                                Seems like it's a GCC non-standard though, because I had never seen it before; \x1B (or \033) is the portable version....

                                1 Reply Last reply
                                1
                                • C Offline
                                  C Offline
                                  Cougar 0
                                  wrote on 13 Sept 2021, 16:46 last edited by Cougar 0
                                  #16

                                  @jsulm said in Questions Concerning colorizing text in Ubuntu terminal.:

                                  Can you show the code?

                                  Here is a link to download masterserver Qt5 and/or view all the cpp and h files.
                                  http://git.333networks.com/MasterServer-Qt5/commit/

                                  Cougar

                                  1 Reply Last reply
                                  0
                                  • S SGaist
                                    11 Sept 2021, 20:46

                                    @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                                    Amin, if this is against forum rules please delete!

                                    It's a banner shown to everybody, you did nothing wrong.

                                    From the top of my head something as below:.

                                    WARNING THIS IS WRONG AND LEFT FOR EDUCATIONAL PURPOSE
                                    #include <QRegularExpression>
                                    
                                    void printColorized(const QString& text)
                                    {
                                        QString toPrint;
                                        if (text.contains(QRegularExpression("(fatal|fail|error|stop)")) {
                                            toPrint = QString("\e[1m\e[91m%1\e[0m").arg(text);
                                    }
                                        if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
                                            toPrint = QString("\e[91m%1\e[0m").arg(text);
                                    }
                                    qDebug() << toPrint;
                                    }
                                    

                                    Here is the correct way to trigger the colorization of the terminal manually:

                                    #include <QRegularExpression>
                                    #include <QCoreApplication>
                                    #include <QString>
                                    #include <QtDebug>
                                    
                                    void printColorized(const QString& text)
                                    {
                                        if (text.contains(QRegularExpression("(fatal|fail|error|stop)"))) {
                                        qDebug() << "\e[1m\e[91m" << text << "\e[0m";
                                    }
                                        if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
                                            qDebug() << "\e[91m" << text << "\e[0m";
                                    }
                                    }
                                    

                                    [add fixed version as the original will just print the QString content SGaist]

                                    C Offline
                                    C Offline
                                    Cougar 0
                                    wrote on 13 Sept 2021, 20:19 last edited by
                                    #17

                                    @SGaist said in Questions Concerning colorizing text in Ubuntu terminal.:

                                    It's a banner shown to everybody, you did nothing wrong.

                                    Is there a way to mark as read so no more banner?
                                    I checked and did not find a way to mark as read.

                                    Cougar

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 13 Sept 2021, 21:28 last edited by
                                      #18

                                      @Cougar-0 said in Questions Concerning colorizing text in Ubuntu terminal.:

                                      Is there a way to mark as read so no more banner?

                                      No, it's a site wide banner.

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 13 Sept 2021, 21:30 last edited by SGaist
                                        #19

                                        Here is a fixed version of my original idea:

                                        #include <QRegularExpression>
                                        #include <QCoreApplication>
                                        #include <QString>
                                        #include <QtDebug>
                                        
                                        void printColorized(const QString& text)
                                        {
                                            if (text.contains(QRegularExpression("(fatal|fail|error|stop)"))) {
                                                qDebug() << "\e[1m\e[91m" << text << "\e[0m";
                                            } else if (text.contains(QRegularExpression("(refused|nodevice|timeout)"))) {
                                                qDebug() << "\e[91m" << text << "\e[0m";
                                            }
                                        }
                                        
                                        int main(int argc, char **argv)
                                        {
                                            QCoreApplication app(argc, argv);
                                            printColorized("fatal");
                                            printColorized("there is nodevice");
                                            return 0;
                                        }
                                        

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        1 Reply Last reply
                                        1
                                        • C Offline
                                          C Offline
                                          Cougar 0
                                          wrote on 14 Sept 2021, 07:31 last edited by
                                          #20

                                          Thank you, I will give that a try.

                                          Cougar

                                          1 Reply Last reply
                                          0

                                          3/62

                                          9 Sept 2021, 20:22

                                          topic:navigator.unread, 59
                                          • Login

                                          • Login or register to search.
                                          3 out of 62
                                          • First post
                                            3/62
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved