Athari / CsConsoleFormat

.NET C# library for advanced formatting of console output [Apache]
Other
366 stars 32 forks source link

Render using default foreground and background colors #22

Open fedarovich opened 6 years ago

fedarovich commented 6 years ago

In the version 1.0 if foreground or background colors are not specified, the white and black colors are used for them. It looks unnatural if other colors are used in the terminal by default, like in Mas OS X Terminal.

It would be nice if it is possible to render the user interface using default colors.

As a workaround, it is possible to set Color to Console.ForegroundColor and Background to Console.BackgroundColor but it works only on Windows. On Linux and Mas OS these properties return (ConsoleColor)(-1) and it looks like there is no easy and universal way to determine the actual colors.

Athari commented 6 years ago

@fedarovich You can find another workaround which works on any platform in the discussion of the issue RenderDocumentToText adds an extra newline. According to the bug reporter, it even works faster.

But yes, it's something I need to do and looks like I can't delay it until implementing the whole set of OS-specific features planned for 2.0. 😆 I'd certainly prefer being able to "inherit" default/current console colors, but with formatting in *nix being built around ANSI codes, there's no way to get them.

I guess the only option I have is adding render preferences for things like colors. Line formatter should probably go there too.

Hmm, when "reset" ANSI code ([0m) is used, to which colors does it reset on macOS? Does Console.ResetColor() emit that on *nix? I think it resets to user-provided colors from app's settings on Windows, but I need to test it. If .NET Core went with that (ConsoleColor)(-1) value, maybe I can treat it specifically as a reset...

fedarovich commented 6 years ago

On Mac OS (and I guess on other *nix) Console.ResetColor() resets the colors to their default values (set in Terminal options). So I suppose using ResetColor() is the right way to go.

One more interesting thing I've discovered is that the default colors are not among the 16 ANSI colors in Mac OS Terminal. The default BG and FG colors and white and black correspondingly (in the default theme) but if you try to use ConsoleColor.White, it is rendered like light gray, not white. The actual settings of default BG and FG colors and ANSI colors mapping are done in the Terminal settings. I guess it may work the same way in some Linux terminals too. So it looks like ResetColor() is the only way to support default BG and FG on *nix.

Athari commented 6 years ago

@fedarovich

One more interesting thing I've discovered is that the default colors are not among the 16 ANSI colors in Mac OS Terminal.

I guess 16 "standard" colors can be mapped to any colors. It works even on Windows, where 16 colors can be set for system and per application. RGB values vary a lot between systems and terminals even wihout any customization.

Or do you mean you've actually tested all 16 colors and colors which are produced after a reset are not among them?

fedarovich commented 6 years ago

Yes, I mean exactly that. I've tested all 16 colors and the color after reset (or before any changes to back/fore ground) are not among them. screen shot 2018-04-06 at 12 19 31 am But it's even more interesting. You can set background image for the terminal in Mac OS (and I guess in some Linux terminals too). screen shot 2018-04-06 at 12 21 53 am As you can see on the screenshot above, in this case text background after reset is actually transparent.

Athari commented 6 years ago

@fedarovich Well, that's new. Haven't considered that. Thanks for the info.

fedarovich commented 6 years ago

Yes, that was a surprise for me too. Meanwhile, I've checked Ubuntu terminal. It also has the similar features. It's even possible to make semitransparent background: screenshot from 2018-04-06 10-34-09