On Sunday, 21 October 2012 at 22:32:35 UTC, Walter Bright wrote:
On 10/21/2012 12:28 PM, Robik wrote:
> Simple example:
>
> import std.stdio, colord;
> void main()
> {
>      setConsoleColors(Fg.red, Bg.blue);
>      writeln("Red text on blue background.");
>      resetConsoleColors(); // Bring back initial state
> }

Need a method to get the current state, and reset the current state. Otherwise, nested calls to the console functions will screw up the state.

I.e.:

    auto save = getConsoleState();
    setConsoleColors(Fg.red, Bg.blue);
    writeln("Red text on blue background.");
    setConsoleState(save); // Bring back initial state

Or better:

    auto save = getConsoleState();
    scope (exit) setConsoleState(save);
    setConsoleColors(Fg.red, Bg.blue);
    writeln("Red text on blue background.");

On Windows, setting color to initial sets console colors to ones that were set before launch of the program. On Posix it sets default (ANSI remove formatting). I will try to check if it is possible to get current colors on Posix.

Reply via email to