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.
Forum Updated to NodeBB v4.3 + New Features

Questions Concerning colorizing text in Ubuntu terminal.

Scheduled Pinned Locked Moved Unsolved The Lounge
62 Posts 6 Posters 12.4k Views 3 Watching
  • 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.
  • Cougar 0C Cougar 0

    @JonB , Success! We have color text!
    I now have code to work with and tweek!
    Thank you so very much! I learned a lot!!

    colortext.png

    Here is the code;

    #include "logger.h"
    
    void Logger::logEvent(const QString &messageType,
                          const QString &message)
    {
        QString colorizedMessage(message);
         
        if (colorizedMessage.contains(QRegularExpression("update"))) {
            colorizedMessage = QString("\e[1m\e[95m%1\e[0m").arg(colorizedMessage);
        }
        
                if (colorizedMessage.contains(QRegularExpression("check"))) {
            colorizedMessage = QString("\e[97m%1\e[0m").arg(colorizedMessage);
        }
        // Now `colorizedMessage` holds the original `message` with any escape sequences
        // so you can do with it as you will, e.g.
        
        // printing to display suppressed?
        if ( ! _suppressDisplay.contains(messageType) and ! _suppressDisplay.contains("all") )
        {
            QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
            logPrimitive() << QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) << endl;
        }
    
        // printing to logfile suppressed?
        if ( ! _suppressLog.contains(messageType) and ! _suppressLog.contains("all") )
        {
            // write message to log
            QString dateTimeStr(QDateTime::currentDateTime().toString("dd-MM-yyyy HH:mm:ss:zzz"));
            writeLogFile( QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, colorizedMessage) );
        }
    }
    

    I noticed it only colored update and not the word check.
    Not only update but the whole line is colored!
    Now that means I have tweeking to do!

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #61

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

    I noticed it only colored update and not the word check.

    The word check comes in your messageType parameter, not in the message parameter which is where the update ... string is. Hence is does not get colorized.

    You would either have to:

    • Subject the messageType parameter to the same escape sequence injection as you do on the message parameter. That is where factoring the colorizing code into its own function with a parameter would be a very good idea, as shown by @SGaist is his very first response.

    • First create the whole string to be output (QString fullMessage = QStringLiteral("[%1][%2]\t%3").arg(dateTimeStr, messageType, message)) and then subject that fullMessage to the colorization code before outputting it instead of just the message.

    But I said a long time ago I am not volunteering to write all the code... :)

    1 Reply Last reply
    3
    • Cougar 0C Offline
      Cougar 0C Offline
      Cougar 0
      wrote on last edited by
      #62

      I didn't expect you or anyone to write all the code for this, especially for nothing, that is why I offered to pay whoever could help me get this to work.
      I don't have the 5 or 6 years left it would take me to learn how to do all this!
      I will take it from here....
      Thanks for everything!

      Cougar

      1 Reply Last reply
      0

      • Login

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