toeb / cmakepp

An Enhancement Suite for the CMake Build System
Other
433 stars 37 forks source link

Portable colored messages system #75

Open Manu343726 opened 9 years ago

Manu343726 commented 9 years ago

I think being able to print colorful messages worths the effort of getting all the ANSI scape codes putting them into a color map (The "palette"). The same for Windows and its SetConsoleTextAttribute() from the Win32 API.

I will be glad to help with this.

toeb commented 9 years ago

I have played around with that a bit - but no solution comes to mind that uses pure cmake.
What you are suggesting involves creating a crossplatform c++ executable which is wrapped into a cmake function. (see for example my implementation of millis())

I would very much like to have color ouput/ better console control.

I suggest creating an executable for windows which understands the ansi color codes and outputs the text accordingly - that way we would have a abstracted infterface for color ouput. This interface can then be used to create convenience functions in cmakepp. I am not very happy about adding C++ to cmakepp but I see the necessity

The important part is that it works on both cmd.exe and powershell under windows

A problem I foresee is that it might be very slow.

Another similar topic is changing console ouput afterwards - to be able to render text dynamically.

currently this only works with the last line using \r. consider:

## renders progress to the console
function(show_progress)
  set(n ${ARGN})
  foreach(i RANGE 1 ${n})
  string_repeat("=" ${i})
  ans(res)
  math(EXPR i2 "${n} - ${i}")
  string_repeat(" " ${i2})
  ans(res2)
    echo_append("\r${i}/${n} [${res}${res2}] ")
    sleep(1)
  endforeach()
endfunction()

I would be nice if the whole console buffer could be modified.

toeb commented 9 years ago

i played around with windows SetConsoleTextAttribute. the problem I am facing is that I am not able to access the text attributes for the cmake parent process.

so i copied the following code: form stack overflow

#include <iostream>
#include <windows.h>
using namespace std; // std::cout, std::cin

int main()
{
  HANDLE hConsole;
  int k;

  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  // you can loop k higher to see more color choices
  for(k = 1; k < 5; k++)
  {
  // pick the colorattribute k you want
  SetConsoleTextAttribute(hConsole, k);
  cout << k << " I want to be nice today!" << endl;
  }
 SetConsoleTextAttribute(hConsole, 86);
  cout << 1 << " I want to be nice today!" << endl;

  return 0;
}

running it will result in color output in cmd.exe as well as powershell. however when running it within cmake using execute_process(COMMAND app.exe) generates the output but does not change the color. I am speculating that it might be possible to get the console handle from the parent process which can then be modified by SetConsoleTextAttribute

So If you can find a way of getting that to work I can build the rest in cmake.

I need a executable which works like cmake -P echo_append ... except that you can specify a color code at the beginning.

toeb commented 9 years ago

There is just now way around the cmake stdout which allows to transport colors . this would have to be implmented in cmake.

sdarriet commented 9 years ago

On linux there is a workaround that could be done by retrieving the current /dev/pts/xxx device.

execute_process(COMMAND "tty" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE cmd_out)

and then

execute_process(COMMAND app.exe OUTPUT_FILE "${cmd_out}")

Now your app.exe will print colors when executed with execute_process() cmake command. Maybe you can do something similar on Windows with cmake.