"Posting" escape characters

I’m interested in posting text in color when sclang is run from the command line. There was a similar question about posting in color in the post window, but this is about running sclang on its own.

I was trying to “post” commands with appropriate escape characters, e.g.:

"\x1b[32mgreen".postln;
"\\x1b[32mgreen".postln;
"\\\x\\1\\b\\[\\3\\2\\mgreen".postln;
"\"\\x1b[32m\"green".postln;
27.asAscii ++ "[32mgreen".postln;
"\\" ++ 27.asAscii ++ "[32mgreen".postln;
"^[32mgreen".postln;
"\\033[32mgreen".postln;
"\\u001[32mgreen".postln;

When running sclang with this file from the command line, none of the lines resulted in colored posting.
Posting through echo works (e.g. "echo \"\\x1b[32mgreen\"".unixCmd;), but this seems to break when posting hundreds of lines in close succession.

I have a workaround writing a string to a file and then piping it to echo, but that’s not ideal. Is there really no way to “post” escape codes from sclang?

For reference: it was helpful for me to see this gist listing escape sequences in various formats.

Hi, sorry to reopen this but I am having a similar issue. I am trying to run the following shell command on Windows 11:

"FFplay -i C:/Users/someUserName/My Drive/foo/bar/ZOOM0105.MOV".unixCmd

And it is not working because of the escape char. I’ve open the file under a directory without escape char and it runs perfectly.

I’ve tried several alternatives and none worked:
"FFplay -i C:/Users/someUserName/My \ Drive/foo/bar/ZOOM0105.MOV".unixCmd
"FFplay -i C:/Users/someUserName/My\ Drive/foo/bar/ZOOM0105.MOV".unixCmd
"FFplay -i C:/Users/someUserName/My^ Drive/foo/bar/ZOOM0105.MOV".unixCmd
"FFplay -i C:/Users/someUserName/My Drive/foo/bar/ZOOM0105.MOV".unixCmd`

I’ve read that it would be possible to insert the path into a "" but the language does not recognize this command:

"FFplay -i "C:/Users/someUserName/My Drive/foo/bar/ZOOM0105.MOV"".unixCmd

Any ides on how to execute the previous command? Or any alternatives on how to solve this issue?

Thanks a lot!

Should be like this, I believe:

"FFplay -i C:/Users/someUserName/My\\ Drive/foo/bar/ZOOM0105.MOV".unixCmd

A \ escapes the following character, so in your original string, it escapes the space – so the \ disappears.

To escape a backslash requires one \ to initiate the escape, and a second \ as the character to be escaped.

This gets confusing at times…

hjh

Unfortunately this is not working for the .unixCmd although when I "\\abc".postln it does show the escape char. Any other alternatives on how to solve it?

Perhaps not an escape char problem. It would help to see the error message.

hjh

When I try to run this

 "FFplay -i C:/Users/username/Downloads/video1 22.08.24.MOV".unixCmd;

It post some number and after the number a line break blank space:

-> 2268

or

-> 18216

When I run a command that does not have paths with escape chars it only post a number output (and does not throw this line break blank space):

 "FFplay -i C:/Users/username/Downloads/v2-1280-60fps.mp4".unixCmd;
-> 12952

or

-> 12792

What if you modify this to read (I added a backslash after video1):

"FFplay -i C:/Users/username/Downloads/video1\ 22.08.24.MOV".unixCmd;

Or, quote the path (note that double-quotes within a double-quoted string literal must be escaped):

"FFplay -i \"C:/Users/username/Downloads/video1 22.08.24.MOV\"".unixCmd;

If you have the full path in a variable or array, such as you’d obtain from .pathMatch, you can quote or escape characters when building the command string:

p = "C:/Users/username/Downloads/*.MOV".pathMatch;  // or however you want to get the paths

// play a random one
"FFplay -i \"%\"".format(p.choose).unixCmd;

// or, if you're sure spaces are the only problem
"FFplay -i %".format(p.choose.escapeChar($ )).unixCmd;

hjh

The second and third one are working perfectly, thank you enormously!

However the first one is not working.

Funny though, after trying some stuff at the terminal I have realized that with some chars you can actually change the colors:

This came to me when I tried to post:

"!recording!".postln;
"? This will print in orange".postln;
"! This will print in red".postln;

Probably this is related to Dmuse or other ASCII color convetion?
https://wiki.ccarh.org/wiki/Dmuse:_Color_and_upper-ASCII_codes

I wasn’t expecting to solve @MarcinP problem, but serendipity sometimes come out of the fast and light particles inside the supercollider. (I hope it also works on the terminal).

Ah blast it, this needs two backslashes.

"FFplay -i C:/Users/username/Downloads/video1\\ 22.08.24.MOV".unixCmd;

… because the command must contain the backslash, and to write the backslash into the string literal, the backslash must be backslashed.

(So you see, this is confusing for everyone! Including myself.)

hjh