How to change log txt file color in node js - 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

Related

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

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");

ANSI Colors not showing where they are supposed to show

I would like for the entire text that pops up when you use the help() function to be affected by the color code that I used, but instead when I run the script the only part that is affected by said color code is a "None" text below the help() result
print(f'\033[34m {help("for")} \033[m')
This is the single line of code and how it looks in the terminal
The (pretty simple) workaround that I found is to just print the color codes on separate lines
Example:
print('\033[41m')
print(help('for'))
print('\033[m')
That code will make it so the output of the help('for') function has a red background

How to print text with multiple different types of colors in he same line in Python 3?

I am trying to do some visualizing data on the terminal and I am doing lots of printing to do that. The issue I am having is that certain character symbols look the same. I figured that coloring them differently would help me see the differences. I see from this link that there is a way to do it, but I don't understand what I am looking at. There is no explanation for what is going on in those solutions.
How do you specify, in the same line, text with different colors?
I should also mention that I am building the printable string OUTSIDE of the call to print(). How do you build the colorful string outside of the print() call?
A rewritten form of my question:
1. Colorize the text I print to the string with multiple types of colors.
2. Colorize the string BEFORE it gets sent to the "print()" call.
A couple examples would be great.
Using colorama just like the answer you linked is doing:
from colorama import Fore, Style
my_str = f"{Fore.BLUE}Hello, {Style.RESET_ALL} guys. {Fore.RED} I should be red."
print(my_str)
This gives me:
As you can see Fore.<color name> changes the color of the text after it, until the Style.RESET_ALL. After that you can change the color of the text again.
There could be multiple ways to achieve this. One which doesn't require any extra packages is to use ANSI color codes. Look at this link. Below are some examples.
s = "\033[1;32;40m Bright Green on black \033[1;31;43m Red on yellow \033[1;34;42m Blue on green \033[1;37;40m"
print(s)
Here in first code \033[1;32;40m, \033[ is the escape code followed by 1 for bold, 32 for bright green text and 40 for black background. The 3 codes are separated by ; and ended with m. Adding all the 3 codes (1, 32 and 40 here) isn't mandatory though.
output:
Other ways to achieve this can be found here.

How to show whitespace for given scope in a Sublime Text color scheme

Is there any way to set white space visible for a given scope?
I'm working on modifying a color scheme to suite my liking and would like to be able to show spaces within a given scope. I haven't seen anything suggesting it's possible within the color-scheme documentation on Sublime's website.
For my specific case, and I imagine there's other useful cases, I'm working with Markdown and want to highlight a double-space line-break. I'm able to set the background, but this doesn't look quite right. I'm hoping to be able to make whitespace visible for this small scope and change the foreground color to make it stick out.
The short answer to your question is no; or rather, Yes, but only in the way that you've already discovered.
Color schemes can only apply foreground/background colors to scopes as well as bold/italic font weights. So assuming that there is a specific scope detected by the syntax you're using that is used for the things you're trying to highlight, the only thing the color scheme can do is alter the background color to make them visible.
The only thing that can render white space natively is the draw_white_space setting, which at the moment only allows you to turn it off everywhere, turn it on everywhere, or turn it on only for selected text. In this case that doesn't really help.
There are possibilities for something like this in the plugin realm though (these examples can be tested by opening the Sublime console with View > Show Console or Ctrl+` and entering the code in there; they also assume that you're using the default Markdown syntax):
view.add_regions("whitespace", view.find_by_selector("punctuation.definition.hard-line-break.markdown"), "comment", flags=sublime.DRAW_NO_FILL)
This will cause all of the hard line breaks to be outlined as if they were find results; the color is selected by the scope (which is comment here); that would make them visible without making the whole character position have a background color.
view.add_regions("whitespace", view.find_by_selector("punctuation.definition.hard-line-break.markdown"), "comment", "dot", flags=sublime.HIDDEN)
This will add a dot (colored as a comment) in the gutter for lines that end with this scope; you can also combine this with the previous example to outline them and also call attention in the gutter.
style = '<style>.w { color: darkgray; }</style>'
content = '<body id="whitespace">' + style + '<span class="w">ยทยท</span></body>'
phantom_set = sublime.PhantomSet(view, "whitespace")
phantoms = [sublime.Phantom(r, content, sublime.LAYOUT_INLINE) for r in view.find_by_selector("punctuation.definition.hard-line-break.markdown")]
phantom_set.update(phantoms)
This uses Sublime's ability to apply inline HTML phantoms into the document in order to inject a small inline sequence of two unicode center dots immediately between the actual whitespace and the text that comes before it. Here the content can be what you like if you can generate the appropriate HTML; we're just applying a color to the text in this example.
A potential downside here is that the characters you see in the inline HTML aren't considered to be part of the document flow; the cursor will skip over them in one chunk, and they're followed by the actual whitespace.
The result of this example looks like this:
Going the plugin route, you'd need an event handler like on_load() to apply these when a file is loaded and on_modified() to re-update them after modifications are made to the buffer. There may or may not be a package that already exists that has implemented this.

renderer.material.color giving strange results

I have a weird problem, that i can't figure out.
I want to change a gameobjects base color via renderer.material.color.
But for whatever reason, this only works for some colors.
This is my code:
first, I declare the colors:
var color_movement_available = Color(0.17,0.68,0.05,1);
var color_movement_available_hover = Color(0.33,1.00,0.17,1);
then i assign them, like this
case ("movement_available") :
renderer.material.color = color_movement_available;
break;
case ("movement_available_hover") :
renderer.material.color = color_movement_available_hover;
break;
However, when I test the script, the hovering color (pinkish) will not show.
I checked in the inspector and the color is the one i aimed for and is correctly switched.
When i change it to a bright (basecolor) green, red, blue or yellow, it works as supposed.
Other colors will produce white and some wont change anything.
Has anyone an idea what's causing this effect, or better yet, how to solve it?
What i tried so far:
switching the material renderer to diffuse (from transparent/diffuse):
same results
changing the materials initial basecolor to grey (from white):
also, no change
Maybe this has something to do with the way colors are applied ...
cheers
Edit: Screenshots:
Also, apparently, colors that are close to each other, like the same color only darker/lighter appear to be the ones producing white
I'm guessing you have a mesh renderer on your object.
Try GetComponent< MeshRenderer >().material.color

Resources