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

color and ANSI escape code #184

Open
flagarde opened this issue Aug 22, 2022 · 12 comments
Open

color and ANSI escape code #184

flagarde opened this issue Aug 22, 2022 · 12 comments
Labels
question Further information is requested

Comments

@flagarde
Copy link
Collaborator

Maybe we could adapt this library https://github.com/ikalnytskyi/termcolor it looks very convenient and not so big and cpp-terminal have already some function provided.. From the license it seems you could take some part of the code to integrate it.

I like he syntax

int main(int /*argc*/, char** /*argv*/)
{
    std::cout << termcolor::red << "Hello, ";                   // 16 colors
    std::cout << termcolor::color<100> << "Colorful ";          // 256 colors
    std::cout << termcolor::color<211, 54, 130> << "World!";    // true colors
    std::cout << std::endl;
    return 0;
}

It seems possible to include all the escape code in this way

@MCWertGaming
Copy link
Collaborator

MCWertGaming commented Aug 22, 2022

in cpp-terminal we have:

int main(int /*argc*/, char** /*argv*/)
{
    std::cout << Term::color(Term::fg::red) << "Hello, ";                   // 16 colors
    std::cout << Term::color_8bit_fg(100) << "Colorful ";          // 256 colors
    std::cout << Term::color_24bit(211, 54, 130) << "World!";    // true colors
    std::cout << std::endl;
    return 0;
}

soon we also have Term::color_auto() which takes 4bit / 24bit colors and outputs 24/8bit colors depending on terminal support.

What exactly would you like to see? we can make overloads if you would prefer to have everything in a single function.

@MCWertGaming MCWertGaming added the question Further information is requested label Aug 22, 2022
@flagarde
Copy link
Collaborator Author

This form is nice :) I like it.. I was was refereing to implementation too, it takes care of the old Windows standard so we could have a look and see how it's done

On other think it has is the ability to deal with clog cerr, that's it could be nice to have a the ability to turn on off the colorization for any reason

@MCWertGaming
Copy link
Collaborator

Yeah we need to look into the implementation for older windows versions as discussed in #173. Technically we can turn off colors when the terminal is not interactive, that would require some work though. I guess that turning off colors through some kind of flag / option should be implemented by the user since there are many ways to do it.

@flagarde
Copy link
Collaborator Author

flagarde commented Aug 24, 2022

I think the library linked turn it on and off if it tty or not

It's not enought to turn it on/off based on tty it would be very nice to have a way to turn off/on on demand. I think the library linked do it

@MCWertGaming
Copy link
Collaborator

well a user can simple create a wrapper themselves, I wouldn't like to force colors off on non TTY. It might be possible that the program runs in a CI task that supports coloring output regardless. we already have is_stding_a_tty() that returns true if the terminal is attached to a TTY.

@MCWertGaming
Copy link
Collaborator

Moved the discussion to #188.

@flagarde
Copy link
Collaborator Author

flagarde commented Aug 24, 2022

I'm thinking about this kind of things
https://bixense.com/clicolors/

And for people who suffer some visual problem, they could disable the colors.

Other people like to see life in black and white..

As we are dealing with color it would be nice to have such feature..

@MCWertGaming
Copy link
Collaborator

I'm currently implementing autocolor for that. It's using RGB structs that can simply change their stored value.

you would do

void render(Term::RGB color1, Term::RGB color2, Term::RGB color3) {
  std::cout << auto_color(color1) << "Hello " << auto_color(color2) << "Colorful" << auto_color(color3) << "World" << Term::color_reset() << std::flush;
}

int main() {
  Term::Terminal(false);
  Term::RGB color1 = bit4_to_rgb(Term::FG::RED);
  Term::RGB color2 = bit8_to_rgb(230);
  Term::RGB color3 = Term::RGB{20, 30, 40};
  redner(color1, color2, color3);
}

you can just implement something like

struct colors {
  Term::RGB color;
};
int main() {
  colors col;
  if(Term::is_stdin_a_tty()) {
    col.color = bit4_to_rgb(Term::fg::red);
  } else {
    col.color = empty_rgb();
   }
}

What about that? Otherwise what would you propose? I wouldn't check some random ENV as that might be overlooked by developers who haven't read the code.

@MCWertGaming
Copy link
Collaborator

Hey @flagarde anything else you want to to see in the library?

@flagarde
Copy link
Collaborator Author

I trying to add the color for old windows.

@flagarde
Copy link
Collaborator Author

It seems the cerr is not taken into account on the library. It would be very nice to deal with hi to as you did with cout

@MCWertGaming
Copy link
Collaborator

Really nice! I hope that you find a well solution, I'm currently working on the window class update, because I need the managed renderer for a few of my projects. If you need help, feel free to make a draft PR and I can take a look ^^

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

No branches or pull requests

2 participants