How do I print colors into the console with D? - colors

I have tried escape sequences with the writeln() function, I also tried to use them with the printf() function imported from the std.c.stdlib module, but it only prints an empty line.
printf("\0x1B[5;32;40m Blink Text");
printf("\e[5;32;40m Blink Text\e[m");
writeln("\0x1b\x5b1;31;40m\tColor");
None of these work.
I have tried everything I can think of, is there a way?
Searching the D website's library reference didn't help me.
EDIT: SOLUTION
Okay, so I have tried to import the function SetConsoleTextAttribute, as Mars kindly suggested:
extern (Windows) bool SetConsoleTextAttribute(void*, ushort);
I also imported the other function (Which I simply guessed I need to import, as I have no previous experience with Win programming)
extern (Windows) void* GetStdHandle(uint);
And simply called the two functions
auto handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_BLUE);
writeln("In Color");
This works perfectly, thank you all so much for your time and help

Like CyberShadow pointed out, you have to use \x1B, or \033. It should work fine, as long as you're on Linux. Windows doesn't support those codes though. Here you have to use the API function SetConsoleTextAttribute from std.c.windows.windows.

There is a typo in your string: use \x1B instead of \0x1B.
D doesn't support the \e escape code in strings, use \x1B.

You may also try a helper module like http://www.digitalmars.com/d/archives/digitalmars/D/Color_your_terminal_s_output_146182.html

Related

Can I format variables like strings in python?

I want to use printing command bellow in many places of my script. But I need to keep replacing "Survived" with some other string.
print(df.Survived.value_counts())
Can I automate the process by formating variable the same way as string? So if I want to replace "Survived" with "different" can I use something like:
var = 'different'
text = 'df.{}.value_counts()'.format(var)
print(text)
unfortunately this prints out "df.different.value_counts()" as as a string, while I need to print the value of df.different.value_counts()
I'm pretty sure alot of IDEs, have this option that is called refactoring, and it allows you to change a similar line of code/string on every line of code to what you need it to be.
I'm aware of VSCode's way of refactoring, is by selecting a part of the code and right click to select the option called change all occurances. This will replace the exact code on every line if it exists.
But if you want to do what you proposed, then eval('df.{}.value_counts()'.format(var)) is an option, but this is very unsecured and dangerous, so a more safer approach would be importing the ast module and using it's literal_eval function which is safer. ast.literal_eval('df.{}.value_counts()'.format(var)).
if ast.literal_eval() doesn't work then try this final solution that works.
def cat():
return 1
text = locals()['df.{}.value_counts'.format(var)]()
Found the way: print(df[var].value_counts())

Matlab function defintion method that takes string as the definition (MFILE)

there is this method I used the other day and I have forgotten the details, which in we used a syntax like this:
f=//command//(x,'sin(x)');
something like this.
im not sure if the syntax is fully correct, or what the right command is. but after this we could simply ask for the f(x) value like this:
x= 0;
y= f(x);
and then the results were y=0;
What you are asking for is usually not recommendable. Please check if a simple anonymous function also fits your requirements:
f=#(x)(sin(x))
In case you really need to evaluate from a string:
f=str2func('#(x)sin(x)')
I would advice against the second option unless absolutely required, it can lead to hard to debug errors.
well I found the answer myself and it was "inline" command; :)
f=inline('sin(x+y+z)','x','y','z');
you can add as much variables as needed too.

Accessing C# sources

I was just wondering if there is a way to find an algorithm for already existing functions.
For example if I wanted to know how Math.Pow() works the only things googling ever gets me to are the return values and a source code for Math library that defines pow(duble x, double y) as:
public static extern double Pow(double x, double y);
and nothing else is said about it.
Is there a place where the source codes can be found?
You can't see the source directly from .NET Framework because its an extern function, meaning it's implemented in a lower-level library--probably the CLR.
You can look at the summary,parameters, and return comments though if that helps you...
Hover over the function (In my case Math.Cos) and Hit F12
Expand the comment region in the metadata

How to use strstrip for parsing a string in two parts

I would like to know hot to parse a string like this "hello world" into "helloworld" using the strstrip kernel function. I am developing a Linux Kernel char device and this functions causes me a Kernel Panic (or Kernel Opss).
The way I'm using this function is the following:
char result[100];
strcpy(result, "hello world");
strstrip(result);
strstrip(&result); //Also tried this
strstrip("100+200"); //Also tried this
The Kernel error is caused as soon as the strstrip line gets executed. What is the proper way to call this function?
Actually strstrip helps to remove the white spaces at the front. It does not remove all the white spaces with in the string.
Please look at the below example.
char result[100];
strcpy(result, " hello world from stack exchange");
printk("\n before: %s",result);
strcpy(result, strstrip((char*)result));
printk("\n after: %s",result);
Hope it helps.
srtstrip() is a wrapper function for strim() (http://lxr.linux.no/linux+v3.11.2/lib/string.c#L361) in modern kernels. As it will attempt to modify the string itself, you cannot call it with a static string as you have in the third attempt.
The second attempt you have is passing a pointer to an array variable which is also a pointer. So you are passing a char** which if you look at the link above you can see is not correct.
The first attempt should not cause a kernel error, but you do not appear to be receiving the return value in a a local variable. What kind of error are you receiving? I will update this answer if you can provide that information.
In the end though as Balamurugan A points out, this function does not do what you seem to think it does. strsep() (http://lxr.linux.no/linux+v3.11.2/lib/string.c#L485) may help you out here but it will only be a stepping stone to removing all spaces. You will actually have to copy the string into a new buffer word by word as there is not way to simply "shift memory contents", as it were.

where is lstrncpy's head file?

I know lstrcpy is used for string copy for both unicode and ascii,
But I can not use lstrncpy, because I can't find the head file.
What is the name of the head file, I googled it, and find someone is using it.
Many thanks!
As per the MSDN docs include Windows.h
It is delcared in Winbase.h you need to include Windows.h
James.
If you know the length anyway, you can simply use memcpy (..., length * sizeof (char_type)); which will work for any character type.
you must use strsafe.h or just include windows.h
If you are asking about the Windows lstrcpy() function, it is declared in winbase.h, which normally gets included via windows.h

Resources