News for hackers. Stuff that matters.
Re:ANSI escape codes (2019) by kyberias on Wednesday February 03, 2021 @04:46AM (#26012112)
Seems to me it's bad practice to combine the text you want to show with the formatting (escape codes) in string literals. Everything is then immediately dependent on specific type of UI technology (terminal).
Re:ANSI escape codes (2019) by iforgotpassword on Wednesday February 03, 2021 @06:00AM (#26012543)
Eh, depends on context, for bash scripts that might be just fine. You need to have some styling information go along with it anyways, so instead of having some dedicated markup language and then having an output library that converts that to whatever your output uses, you could just declare those ansi escape codes your "markup language" and should you ever need to output it to something else, say a web page, I'm sure there's code around that does the conversion. But I guess to make it more readable you might want to put those commonly used sequences into some variables so you can do

  echo "${esc_red}ERROR${esc_reset}: All messed up"
Re:ANSI escape codes (2019) by db48x on Wednesday February 03, 2021 @09:55AM (#26014087)
At least call tput or something so that your script doesn't do anything wacky just because you SSH'd in this time instead of using your customary terminal.
Re:ANSI escape codes (2019) by klodolph on Wednesday February 03, 2021 @10:35AM (#26014528)
For advanced features, sure. But basic colors are nearly universal.
Re:ANSI escape codes (2019) by db48x on Wednesday February 03, 2021 @10:40PM (#26022319)
Until someone runs it in eshell, which is a shell but not a terminal emulator and you have to configure an override just for that special program so that it isn't dumping unreadable garbage into the buffer.
Re:ANSI escape codes (2019) by klodolph on Thursday February 04, 2021 @01:54AM (#26023271)
Honestly, since eshell is the only example I can think of, I’d say that it’s more than fair to consider this a flaw in eshell.
Re:ANSI escape codes (2019) by tempodox on Wednesday February 03, 2021 @10:05AM (#26014204)
Of course. Just view those strings as the output of the formatting backend for the terminal. The article is about terminal escape codes, not software modularization.
Re:ANSI escape codes (2019) by kyberias on Wednesday February 03, 2021 @11:42AM (#26015463)
Well they wrote: "This tends to result in strings in our code that look a little like this."

That's why I think they have these strings in the code.

Re:ANSI escape codes (2019) by account42 on Wednesday February 03, 2021 @07:34AM (#26013063)
Depending on what you are doing you can always parse and translate the escape codes to something else for the systems where they are not supported. One advantage of using in-line escape codes in your string is that you can compose multi-colored text and buffer them in whatever strings your language provides.
Re:ANSI escape codes (2019) by kyberias on Wednesday February 03, 2021 @11:45AM (#26015505)
Sorry, I don't understand the benefit. Buffer them?

It's clearly harmful to write escape codes in string literals. They are not readable.

The escape code to change any text to red should appear in code exactly once.

This is analogous to HTML inline styles.