How to adapt to Linux terminal back color in .NET 5/6? - linux

We have a .NET 5 console application that runs on Windows/Linux/Mac.
Our console application relies on the classic fore color scheme to highlight info/warning/error in console with fore colors gray/yellow/red. However we cannot gather the terminal back color as explained in Console.BackgroundColor documentation:
Unix systems don't provide any general mechanism to fetch the current
console colors. Because of that, BackgroundColor returns
(ConsoleColor)-1 until it is set in explicit way (using the setter).
For now we force the console backcolor to be black but this doesn't render nicely when the terminal back color is white:
If we could guess the terminal back color we could adapt our forecolor to the terminal back color (like black fore color if the terminal back color is white and vice-versa). But from answers to this question How to determine a terminal's background color? it looks there is no standard way of getting background color that works on Mac and all Linux.
What is the common way to address this?

Ok we found a solution this way:
A colored message has white forecolor on a dark enough backcolor, so no matter the user backcolor, colored messages appear highlighted
Once we modified the Console.ForegroundColor and Console.BackgroundColor to show a colored message, the astute is to call Console.ResetColor() that sets back fore and back colors to their default values
This works no matter the terminal's default colors.
One point to note is that you shoudn't call Console.WriteLine(text) that provokes some back color problems on the remaining line characters. Instead call Console.Write(text) before Console.ResetColor() and then line feed is obtained with a call to Console.WriteLine().
System.Console.WriteLine("This is an info A");
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.BackgroundColor = ConsoleColor.Red;
System.Console.Write("This is an error");
System.Console.ResetColor();
System.Console.WriteLine();
System.Console.WriteLine("This is an info B");
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.BackgroundColor = ConsoleColor.DarkYellow;
System.Console.Write("This is a warning");
System.Console.ResetColor();
System.Console.WriteLine();
System.Console.WriteLine("This is an info C");
System.Console.ForegroundColor = ConsoleColor.White;
System.Console.BackgroundColor = ConsoleColor.Green;
System.Console.Write("This is a success");
System.Console.ResetColor();
System.Console.WriteLine();
System.Console.WriteLine("This is an info D");

Related

Permanently change the color palette in the GTK color picker

When using the color picker widget GTK applications I often use a different color Palette to the one given by default, as shown in the picture below. While the program is running I can change the defaults colors and they stay changed, however, when I close the program those modifications disappear.
I wonder how I can make those modifications to persistent in disk.
From the tags you chose, the application name seems to be Dia. In the application, nothing lets you set this option. So the short answer is: no.
The issue is that Dia uses the now deprecated GtkColorSelectionDialog (in favor of GtkColorChooserDialog). In the deprecated version, there is a flag to tell the widget to show/hide the color palette, but that's pretty much the only control you have (see gtk_color_selection_set_has_palette).
In the new widget version (which, by the way, looks totally different), you have direct access to a gtk_color_chooser_add_palette:
void
gtk_color_chooser_add_palette (GtkColorChooser *chooser,
GtkOrientation orientation,
gint colors_per_line,
gint n_colors,
GdkRGBA *colors);
You can see you have much more options as far as customizing the palette is concerned. You even have the ability to decide the colors. This means you could save your current selection in the palette. Then, at application quit, you could save all the palette's colors in some sort of settings, and load them back at application start.
As a final note, I looked at the Dia source code and found that they seem to be looking to make the move to the new widget. Here is an excerpt:
// ...
window = self->color_select =
/*gtk_color_chooser_dialog_new (self->edit_color == FOREGROUND ?
_("Select foreground color") : _("Select background color"),
GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self))));*/
gtk_color_selection_dialog_new (self->edit_color == FOREGROUND ?
_("Select foreground color") : _("Select background color"));
selection = gtk_color_selection_dialog_get_color_selection (GTK_COLOR_SELECTION_DIALOG (self->color_select));
self->color_select_active = 1;
//gtk_color_chooser_set_use_alpha (GTK_COLOR_CHOOSER (window), TRUE);
gtk_color_selection_set_has_opacity_control (GTK_COLOR_SELECTION (selection), TRUE);
// ...
From the commented code, it seems they are trying to make the move...

How to change log txt file color in node js

for example, something like this.
console.log("text", "red");
How can I set log txt file font color in node js. I have done this in terminal but want to same in log txt file.
is this possible to show different color for different message in log txt file just like in terminal.
How to get colors on the command line
When working on the command line, it can be both fun and extremely useful to colorize one's output. To colorize console output, you need to use ANSI escape codes. The module colors.js, available on npm, provides an extremely easy to use wrapper that makes adding colors a breeze.
First, install it to the directory you'd like to work in.
npm install colors
Now open up a little test script for yourself, and try something like this:
const colors = require('colors');
const stringOne = 'This is a plain string.';
const stringTwo = 'This string is red.'.red;
const stringThree = 'This string is blue.'.blue;
const today = new Date().toLocaleDateString(); // returns today's date in mm/dd/yyyy format
console.log(stringOne.black.bgMagenta);
console.log(stringOne.yellow.bgRed.bold);
console.log(`Today is: ${today}`.black.bgGreen);
console.log(stringTwo);
console.log(stringThree);
console.log(stringTwo.magenta);
console.log(stringThree.grey.bold);
There are several things to take note of here - first, the string object has been prototyped, so any color may be added simply by adding the property to the string! It works on string literals, template literals and on variables, as shown at the top of the example above.
Notice, also, from the second pair of console.log statements, that once set, a color value persists as part of the string. This is because under the hood, the proper ANSI color tags have been prepended and appended as necessary - anywhere the string gets passed where ANSI color codes are also supported, the color will remain.
The last pair of console.log statements are probably the most important. Because of the way colors.js and ANSI color codes work, if more than one color property is set on a string, only the first color property to be set on the string takes effect. This is because the colors function as 'state shifts' rather than as tags.
Let's look at a more explicit example. If you set the following properties with colors.js:
myString.red.blue.green
You can think of your terminal saying to itself, "Make this green. No, make this blue. No, make this red. No more color codes now? Red it is, then." The codes are read in the reverse order, and the last/'innermost' is applied. This can be extremely useful if you're using a library that sets its own default colors that you don't like - if you set a color code yourself on the string you pass in to the library, it will supersede the other author's color code(s).
The last thing to note is the final line of the example script. While a color code was set previously, a 'bold' code was not, so the example was made bold, but not given a different color.
Using colors without changing String.prototype
Now an instance of colors can also be used. Though this approach is slightly less nifty but is beginner friendly and is specially useful if you don't want to touch String.prototype. Some example of this are:
const colors = require('colors');
const stringOne = 'This is a plain string.';
const stringTwo = 'This string is red.';
const stringThree = 'This string is blue.';
const today = new Date().toLocaleDateString(); // returns today's date in mm/dd/yyyy format
console.log(colors.bgMagenta.black(stringOne));
console.log(colors.bold.bgRed.yellow(stringOne));
console.log(colors.bgGreen.black(`Today is: ${today}`));
console.log(colors.red(stringTwo));
console.log(colors.blue(stringThree));
console.log(colors.magenta.red(stringTwo));
console.log(colors.bold.grey.black.blue(stringThree));
Unlike the String.prototype approach, the chained methods on the colors instance are executed left to right i.e., the method closest to the string is finally applied. In the last console.log you can think of your terminal saying to itself, "Make this grey. Now, make this black. Now, make this blue. No more coloring methods now? Blue it is, then."
With the latest version of colors.js you can also define Custom Themes in color.js, which makes our code more Robust and allows better Encapsulation of data. A nice use case of this maybe:
var colors = require('colors');
colors.setTheme({
info: 'bgGreen',
help: 'cyan',
warn: 'yellow',
success: 'bgBlue',
error: 'red'
});
// outputs red text
console.log("this is an error".error);
// outputs text on blue background
console.log("this is a success message".success);
One last thing: the colors can look quite different in different terminals - sometimes, bold is bold, sometimes it's just a different color. Try it out and see for yourself!
For reference, here's the full list of available colors.js properties.
text colors
black
red
green
yellow
blue
magenta
cyan
white
gray
grey
background colors
bgBlack
bgRed
bgGreen
bgYellow
bgBlue
bgMagenta
bgCyan
bgWhite
styles
reset
bold
dim
italic
underline
inverse
hidden
strikethrough
extras
rainbow
zebra
america
trap
random

Remove terminal border colors from vim colorschemes

I want the color scheme to span completely across the terminal boundaries. I am using Color Scheme Scroller Plugin to switch between different theme. I have uploaded a .gif file so that you can clearly see what I want to get fixed. Vim colorschemes doesn't completely change the color of editor. There are some terminal color's borders left around the vim's overridden color scheme. How would I fix it.
Please check the image on this link. Stackoverflow doesn't allow uploading an image > 2Mb
You can't do that from Vim itself.
Terminal emulators use that padding to preserve readability when characters are displayed next to the borders of the window. The programs you run in your terminal have no knowledge of that padding and thus no ability to change it.
But you can read the documentation of your terminal emulator or take a look at its source code to find a way to enable/adjust/disable that padding.
FWIW, there's no way to change that in Terminal but it can be done for iTerm.
Alternatively, you could simply set the background color of your terminal to the one used in your vim colorscheme.
The image appears to depict behavior outside vim's control:
it is using a terminal emulator (could be xterm, could be some other).
the terminal emulator draws character cells on a window
those cells form a grid; the window may extend beyond the grid
the window can have a background color
the grid can have a background color
within the grid, most terminals provide some capability of drawing text with specific foreground and background colors
the grid can have a default background color which is not any of the specified colors
outside the grid, the window can also have a default background color
normally, the grid- and window-default backgrounds are the same
the window can be resized to (more or less) arbitrary sizes
the grid is constrained to exact character sizes
because of this difference, the window can have areas outside the grid which use its default color, and not match the grid's background color.
escape sequences which could affect the grid- and window-background colors are doing erases (see for example the ncurses FAQ My terminal shows some uncolored spaces).
though it is conceivable that erasures within the grid could affect those outside areas, doing that generally leads to odd visual effects.

Chrome Extension Badge - Text Color

A quick question regarding the Chrome Badge/Browser Action API.
Is there a method to set the badge text colour? (much like you can with the back colour).
I'm sure I remember seeing a reference to a method such as SetBadgeTextColor() somewhere on the Internet (not sure where, mind).
Edit: Chromium Wiki - Browser Actions Proposal
The link above is where I saw a method to set the badge text colour. Was this proposal ever implemented?
You cannot change the text color directly.
What you can do is to paint the base image on a canvas, then draw the text using your desired color and finally use chrome.browserAction.setIcon to update the badge.
chrome ext detects badge bg color (light or dark) and set text color automatically (black or white). if your background color is around the boundary of those. try to increase or decrease your bg color code e.g your bg color is red #FF0000 then chrome will detects it as light but when you decrease the color code hex to #FE0000 it will detected as dark, and it's still solid red in our eyes

Coloring console output in windows

I was trying to find if it is possible to colour console output in windows system. I found that Console - Ansi but i cant find any information about coloring output in windows prompt.
I woudl appreciate information about my problem.
It's been a while, but I think the correct usage is:
setRGB [SetColor Foreground Yellow, SetColor Background Red]
to set yellow writing on a red background. Also setTitle followed by a String sets the console window title.
It worked for me like this:
set the foregroundcolor to Yellow
setSGR [SetColor Foreground Vivid Yellow]
and then reset it to normal after putting your String on the screen
putStrLn "ayellowstring"
setSGR [Reset]
You need
import System.Console.ANSI
to do this
PowerShell and the console setting will be the solution.Console only have 16 colors,but you can config any 16 colors you like(i.e. you can make gray scale console)
this link explain how to

Resources