Thread: Does coloring the output reduce portability?

  1. #1
    Registered User
    Join Date
    Aug 2018

    Does coloring the output reduce portability?

    I would like to know whether coloring the output of a program is a good practice and if it reduces portability. For example, printing an error message in red:
    #include <stdio.h>
    #include <stdlib.h>
    #define RED "\x1b[1;31m"
    #define NO_COLOR "\x1b[0m"
    printf("%sAn error ocurred.%s\n", RED, NO_COLOR);
    When I compile and run the program it shows color both in the terminal emulator and the virtual terminal (switching by pressing Ctrl + Alt + F1) in my GNU/Linux system. Some GNU utilities also have an optional --color flag to enable color, but I wonder if this can reduce portability in other cases like executing it in Windows.

    Should colors be used?

    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Yes, adding colour will reduce portability.

    And the --colour option is the way to do if you want to add eye candy.

    Also, colour can really mess up peoples attempts to do something like
    yourprog | anotherprog

    If yourprog blindly outputs colour regardless, then any attempt to parse or redirect will cause unnecessary additional grief.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Well, you could abstract the color behind a function which would have a different implementation for different OSes. You could also try to detect whether your program outputs directly to the console or to some file, and decide to enable or disable colors accordingly ("--color=auto" does exactly that).
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bison/Yacc shift/reduce reduce/reduce
    By AnthonyGould in forum Tech Board
    Replies: 0
    Last Post: 11-20-2009, 08:15 AM
  2. Coloring My Text
    By bjl in forum C++ Programming
    Replies: 10
    Last Post: 02-05-2008, 07:37 PM
  3. Yacc: reduce/reduce
    By alphaoide in forum Tech Board
    Replies: 2
    Last Post: 04-27-2005, 08:03 PM
  4. Code-Coloring
    By Vber in forum A Brief History of
    Replies: 7
    Last Post: 01-08-2003, 11:29 PM
  5. Program Portability (code portability)
    By Perica in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2002, 10:03 AM

Tags for this Thread