Ideally, this line of PolyML code should give desired result:
print "\033[31m RED \033[0m NORMAL \n";
But the \033 turns out to be just an exclamation mark, not a special symbol for color encoding.
I use the following "way-around" approach, but it doesn't allow to do anything interactively: I just take the output of my program and color it.
echo "\\\\033[31m RED \\\\033[0m NORMAL \\\\n" | xargs echo -e
What are the possible solutions to this problem? Is it possible to solve it within the standard PolyML instruments?
added: I checked how Ocaml do the same thing with
Printf.printf "\033[31m RED \033[0m NORMAL \n";;
-- situation is the same: no color obtained.
p.s. this question is not a dublicate because it is about differences between echo -e and print in ML languages
\033 is a character escape sequence which in bash and many other languages is interpreted as the character with ASCII code corresponding to the octal number 33.
In OCaml however, this escape sequence is interpreted as decimal. We can either convert the number from octal (33) to decimal (27) and continue to use this syntax, or use the correct syntax for octal escape sequences (\o027). Or we could even use hex (\x1b) if we want to be a bit more adventurous.
All of these will work in OCaml, and possibly also PolyML (if you replace Printf.printf with print of course):
Printf.printf "\027[31m RED \027[0m NORMAL \n";;
Printf.printf "\o033[31m RED \o033[0m NORMAL \n";;
Printf.printf "\x1b[31m RED \x1b[0m NORMAL \n";;
Source: The OCaml manual Chapter 9.1: Lexical conventions
Thanks to this question:
OCaml color console output
The correct answer is to use code \027 instead of \033 both in Ocaml and PolyML:
print "\027[31m RED \027[0m NORMAL \n";
Related
I thought this might be easy but not so much
I want to color the output of a command based on delimeters, in my case
apt-show-versions -u
and want to color the packages' names based on the colon seperator, or on the word 'to'. It seems to run into wanting a parser not a filter.
using color xterm on Linux and PuTTY
Others have been interested in similar functionality, I suggest you check the tools (grc/grcat) mentioned there.
You might be able to get away with sed-magic, though. I'm not sure what you want to colour exactly, and neither do I know what the output of apt-show-versions looks like, but this colours everything preceding a colon and the word "to":
cat << EOF | sed -e "s/^[^:]*/\x1b[31m&\x1b[0m/g" | sed -e "s/to/\x1b[31m&\x1b[0m/g"
foo: 1 to 2
bar: 3 to 4
quux: 5 to 6
EOF
You can paste that into a terminal and see if it's what you're looking for. Essentially, it searches for occurences of regular expressions and surrounds it with ANSI colour codes:
s/X/Y&Y/g : replace X by surrounding with Y, in the entire input (g flag), or, quoting man sed:
s/regexp/replacement/
Attempt to match regexp against the pattern space. If success‐
ful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that
portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching
sub-expressions in the regexp.
^[^:]* : from beginning of line, match everything until you encounter a :
\x1b : Hex 27, an escape sequence (see here for more!)
[31m : ANSI colour code for red
[0m : ANSI colour code for "reset to normal output"
If anything, this post taught me that sed captures matches in & ;-) Hope you gained some insight, too!
This question already has an answer here:
Prevent "echo" from interpreting backslash escapes
(1 answer)
Closed 4 years ago.
I have a bash script that contains lines:
remote_installer_svc_args="$local_cifs_mount/eset-remote-installer.args"
svc_arg_x86="%SYSTEMROOT%\\$(basename $remote_temp_dir)\\$(basename $INSTALLER_BAT)"
svc_arg_x64="%SYSTEMROOT%\\$(basename $remote_temp_dir)\\$(basename $INSTALLER_BAT)"
echo "$svc_arg_x86" > $remote_installer_svc_args
echo "$svc_arg_x64" >> $remote_installer_svc_args
It should produce a file that looks like this (in notepad++ on windows):
instead the file looks like this:
or in vim:
What is wrong with the script? Because when I copy those lines into bash it works, only if I run the script it does produce those strange characters...
You've run into part of the mess of inconsistent behavior that plagues the echo command. Specifically, some versions of echo (in some modes) interpret escape (backslash) sequences in the string they're asked to print. Others don't. When you ask echo to print %SYSTEMROOT%\era_rd_6HbUKJTR\EraAgentInstaller.bat, it might see the \e part and think it's supposed to convert that to the ASCII escape character.
Note that there are two different characters being called "escape" here: The backslash is used by the shell as an escape character, meaning that it and the characters immediately following it have some special meaning. The ASCII escape, on the other hand, is treated as a special character by the terminal (and vim and some other things) in a somewhat similar manner. Since the ASCII escape is a nonprinting character, when notepad++ and vim have to display it, they show some sort of alternate representation ("ESC" or "^]").
Anyway, since echo is inconsistent about its treatment of the backslash character, it's best to avoid it for strings that might contain backslash. Use printf instead (see "Why is printf better than echo?" on unix.se). It's a little more complicated to use, but not too bad. The main things to realize are that the first argument to printf is a "format" string that's used to control how the rest of the arguments are printed, and that unlike echo it doesn't automatically add a newline to the end.
What you want to use is:
printf '%s\n' "$svc_arg_x86" > $remote_installer_svc_args
printf '%s\n' "$svc_arg_x64" >> $remote_installer_svc_args
Or you can simplify it to:
printf '%s\n' "$svc_arg_x86" "$svc_arg_x64" > $remote_installer_svc_args
That first argument, %s\n, says to print a plain string followed by a newline. Backslash escapes in the format string are always interpreted, but strings formatted with the %s format never have escapes interpreted. Note that in the single-command version, the format string gets applied to each of the other two arguments, so each gets a newline at the end, so each winds up on a separate line in the output file.
HiI am using node JS for my app, and I want to print ascii symbols in terminal.Here is a table for ascii symbols. Please check Extended ASCII Codes field. I want to print square or circle, for example 178 or 219.
Can anyone say me, how can do it?Thank you
Like several other languages, Javascript suffers from The UTF‐16
Curse. Except that Javascript has an even worse form of it, The UCS‐2
Curse. Things like charCodeAt and fromCharCode only ever deal with
16‐bit quantities, not with real, 21‐bit Unicode code points.
Therefore, if you want to print out something like 𝒜, U+1D49C,
MATHEMATICAL SCRIPT CAPITAL A, you have to specify not one character
but two “char units”: "\uD835\uDC9C".
Please refer to this link: https://dheeb.files.wordpress.com/2011/07/gbu.pdf
Your desired character is not a printable ASCII character. On linux you can print all the printable ascii characters by running this command:
for((i=32;i<=127;i++)) do printf \\$(printf '%03o\t' "$i"); done;printf "\n"
or
man ascii
So what you can do is to print unicode characters. Here is a list of all the available unicode characters, and you can select one which is looking almost identical with your desired character.
http://unicode-table.com/en/#2764
I've tested on a windows terminal but it is still not showing the desired character, but it's working on linux. If it's still not working you had to make sure to set LANGUAGE="en_US.UTF-8" in /etc/rc.conf and LANG="en_US.UTF-8" in /etc/locale.conf.
So printing out something like this on node console:
console.log('\u2592 start typing...');
will output this result:
▒ start typing...
Actually, if you only care about ASCII that should not be a real problem at all. You only have to properly escape them. A good reference for this is https://mathiasbynens.be/notes/javascript-escapes
console.log('\xB2 \xDB')
Works for me with recentish node under Windows (cmd shell) and mac OS. For ASCII characters you can just convert them to hex and prepend them with \x in your strings. Give it a try with node -e "console.log('\xB2')"
And when you try this answer, and it works, you might want to try:
node -e "console.log('\x07')"
So I started to learn Lua(5.1) and I saw that thing called literal strings. And I have no idea what these do. The manual says \a is a bell but when I type
print('hello\athere')
The IDE prints a weird square with 'bel' written on it.
So if someone could help me and explain every one of them[Literal Strings]. that would be really helpful.
p.s. i use Sublime Text 3
Only ASCII between 0x20 and 0x7E are printable characters. How other characters are output, including '\a' and '\b', is up to the implementation.
'\a', the ASCII 7 for BEL, is designed to be used to alert. Typical terminal would make an audible or visible alert when outputing '\a'. Your IDE choose to show a different output other than an alert. That's OK since it's up to the implementation.
Such sequences are called "escape sequences", and are found in many different languages. They are used to encode non-printable characters such as newlines in literal (hardcoded) strings.
Lua supports the following escape sequences:
\a: Bell
\b: Backspace
\f: Form feed
\n: Newline
\r: Carriage return
\t: Tab
\v: Vertical tab
\\: Backslash
\": Double quote
\': Single quote
\nnn: Octal value (nnn is 3 octal digits)
\xNN: Hex value (Lua5.2/LuaJIT, NN is two hex digits)
A literal is not more than a value inside the code, e.g.: 'some text'.
The '\a' is something different. A special "char", that is used to output a sound (was using the pc-speaker some aeons ago).
I'm working on a terminal program under Linux. I consider adding colorized output.
The task is not really hard, so I succeeded with the following:
[3]> (format t "~a[1;31mred text~a[0m" #\escape #\escape)
red text ; this text is really red and bold in terminal ;-)
NIL
But the code is ugly: I don't know how to put char #\escape (decimal value 27) into a string in 'inline' fashion. For example C++ code from this thread:
cout << "\033[1;31mbold red text\033[0m\n";
Here is #\Escape as \033 (octal). Is there something similar in Common Lisp?
My effort naïf doesn't work as intended:
[4]> (format t "#\escape1;31mred test#\escape[0m")
#escape1;31mred test#escape[0m
NIL
You can type the characters manually…
You don't have to do anything special to have the control characters (or other "unusual" characters) in your strings. You just need to be able to type them into the editor. How easy it will be will depend on your editor. In Emacs, and in at least some terminal emulators, you can press Ctrl-Q followed by another character to insert that character literally. Thus, you can press Ctrl-Q followed by Escape to insert a literal #\escape character. How it appears will depend on the terminal emulator or editor. In my terminal, the literal Escape character is displayed as ^[. Thus, I end up with:
(format t "^[[1;31mhello^[[0!")
; ** **
; result of C-Q Esc
This gives me some red text, as in your example:
…or use a library to make it easier!
If you don't want your source to contain characters that might not be easily readable, you might look into something like Edi Weitz's CL-INTERPOL:
CL-INTERPOL is a library for Common Lisp which modifies the
reader so that you can have interpolation within strings similar to
Perl or Unix Shell scripts. It also provides various ways to insert
arbitrary characters into literal strings even if your editor/IDE
doesn't support them. Here's an example:
* (let ((a 42))
#?"foo: \xC4\N{Latin capital letter U with diaeresis}\nbar: ${a}")
"foo: ÄÜ
bar: 42"
Using CL-INTERPOL, this becomes easy:
* (interpol:enable-interpol-syntax)
* (format t #?"\e[1;31mhello\e[0m!")
hello! ; there's color here, honest!
NIL