Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to force the use of colors in the terminal? #54

Closed
evandrocoan opened this issue Mar 5, 2017 · 4 comments
Closed

How to force the use of colors in the terminal? #54

evandrocoan opened this issue Mar 5, 2017 · 4 comments

Comments

@evandrocoan
Copy link

evandrocoan commented Mar 5, 2017

Defining DOCTEST_CONFIG_COLORS_ANSI is not working, only using ./main --force-colors=true works

My project source code files are like these:

doctest_main.cpp

/**
 * This tells to provide a main() - Only do this in one cpp file.
 */
#define DOCTEST_CONFIG_COLORS_ANSI
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

If here on doctest_main.cpp or everywhere else I tried, I defined the DOCTEST_CONFIG_COLORS_ANSI, it does nothing. It keeps not showing any colors. The only way to force color is to call the main application as ./main --force-colors=true.


doctest_tests.cpp

#include "project_sources_with_tests.cpp"
//...

project_sources_with_tests.cpp

#include "debug.h"

unsigned int factorial( unsigned int number )
{
    return number <= 1 ? number : factorial(number-1)*number;
}

TEST_CASE("testing the factorial function")
{
    CHECK(factorial(1) == 1);
    CHECK(factorial(2) == 2);
    CHECK(factorial(3) == 6);
    CHECK(factorial(10) == 3628800);
}

debug.h

/**
 * This is to view internal program data while execution. Default value: 0
 *
 *  0  - Disables this feature.
 *  1  - Basic debugging.
 *  2  - Run the `doctest` Unit Tests.
 */
#define DEBUG_LEVEL 1



#define DEBUG_LEVEL_DISABLED_DEBUG 0
#define DEBUG_LEVEL_BASIC_DEBUG    1
#define DEBUG_LEVEL_RUN_UNIT_TESTS 2

#if DEBUG_LEVEL > DEBUG_LEVEL_DISABLED_DEBUG

    #define DEBUG

    /**
     * Disables the `doctest` Unit Tests from being included/compiled to the binary output file.
     *
     * See:
     * https://github.com/onqtam/doctest/blob/master/doc/markdown/configuration.md
     */
    #if !( DEBUG_LEVEL & DEBUG_LEVEL_RUN_UNIT_TESTS )
        #define DOCTEST_CONFIG_DISABLE
    #endif

    #include "doctest.h"

#endif

I am running the project through a Sublime Text build system -> shell script -> Makefile:

project.sublime-project

{
    "folders":
    [
        {
            "path": ".",
        },
    ],
    "build_systems":
    [
        {
            "working_dir": "$project_path/source",
            "file_regex": "^(..[^:]*):([\\d+]):?(\\d+)?:? (.*)$|^\\(.*\\)\\((\\d+)\\)(.*)$",

            "name": "Doctest Driver Class",
            "cmd": ["sh", "make_run.sh", "doctest_tests"],

            "target": "ansi_color_build",
            "syntax": "Packages/ANSIescape/ANSI.tmLanguage"
        }
    ]
}

make_run.sh

FIRST_COMMAND_ARGUMENT=$1

if [[ $FIRST_COMMAND_ARGUMENT == "main" ]]
then
    make clean
    make

elif [[ $FIRST_COMMAND_ARGUMENT == "veryclean" ]]
then
    make $FIRST_COMMAND_ARGUMENT
    make

else
    make clean
    make $FIRST_COMMAND_ARGUMENT
fi

wait $!

Makefile

PROGRAM_MAIN_DEBUGGER = debug

DOCTEST_TESTS_FILE   = doctest_tests
DOCTEST_DRIVER_CLASS = doctest_main


doctest_tests: $(DOCTEST_DRIVER_CLASS).o $(PROGRAM_MAIN_DEBUGGER).h
	g++ --std=c++11 $(DOCTEST_DRIVER_CLASS).o $(DOCTEST_TESTS_FILE).cpp -o main
	./main

$(DOCTEST_DRIVER_CLASS).o: $(DOCTEST_DRIVER_CLASS).cpp
	g++ $(DOCTEST_DRIVER_CLASS).cpp -c -o $(DOCTEST_DRIVER_CLASS).o
@onqtam
Copy link
Member

onqtam commented Mar 5, 2017

DOCTEST_CONFIG_COLORS_ANSI is a compile time configuration. It should be auto-detected so there is no need to specify it.

The reason there are no colors is because of this runtime check - the point is to skip the ANSI color codes (which are textual characters) when the program is not outputting to a normal terminal - like when writing to a file. There are cases where this detection fails and reports that the process is not running in a TTY even if it is - this might happen when the current process is executed from a parent process and stdout is redirected or something similar - this happens with the ninja build system.

So something similar is happening in your case - if you don't want to pass that flag each time you could supply your own main (by using the DOCTEST_CONFIG_IMPLEMENT identifier) and give a default value to this command line option.

does this help?

@evandrocoan
Copy link
Author

Yes. Thank you for the time. And nice frameworks yours. While Catch is taking about 21 seconds to build and run simple example, yours takes about 2.8 seconds to run the same tests.

  1. How to improve compilation speed? catchorg/Catch2#836 How to improve compilation speed?

@onqtam
Copy link
Member

onqtam commented Mar 5, 2017

note that the code you have used from the doctest example contains stuff you might not need - like for example the 3 other defaults and the 1 override.

@onqtam onqtam changed the title Defining DOCTEST_CONFIG_COLORS_ANSI is not working, only using ./main --force-colors=true works How to force the use of colors in the terminal Mar 5, 2017
@onqtam onqtam changed the title How to force the use of colors in the terminal How to force the use of colors in the terminal? Mar 5, 2017
@evandrocoan
Copy link
Author

evandrocoan commented Mar 5, 2017

Thanks, the "test-case-exclude", "*math*" and "no-breaks", true override could get me trouble someday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants