-1

I am having ANSI escape-sequences inline in code that works, but I cannot get it to work when reading the same string from at text file.

dOut.writeBytes("\033[0;31;1m> help (?) - Get help\n");

(dOut = DataOutputStream)

This prints red text og black background.

When reading the exact same line from a text file it does not work, it prints the line as pure text.

                BufferedReader menuReader = new BufferedReader(new FileReader("help.txt"));
                while ((menuLine = menuReader.readLine()) != null) {
                    dOut.writeBytes(menuLine + "\n");
                }
                menuReader.close();

Text file has only one line: \033[0;31;1m> help (?) - Get help

Nesrene
  • 107
  • 7
  • Please [edit] your question to include a [mcve], which can be compiled and tested by others. It should show how it generates the red text when used directly and how it generates only pure text when read from a file. – Progman Jun 02 '19 at 19:22
  • 1
    Do you have the text `"\033"` (4 characters) inside the file or do you have the byte 0x1B inside the file? – Progman Jun 02 '19 at 19:40
  • Thanks for answering, Progman :-) The text, not the byte. I do not know how to get 0x1B into the text... – Nesrene Jun 02 '19 at 19:44
  • With any hexeditor of your choice or via java with a `FileOutputStream`. – Progman Jun 02 '19 at 19:52
  • So, how do I get about to substitude the text "\033" (or whatever) with the byte 0x1B in code? I know how to replace substrings, but not bytes... – Nesrene Jun 02 '19 at 20:07
  • normal `replace()` call, where you replace `"\\033"` with `"\033"`, so you replace the 4 character long string with the 0x1B byte. Or you save the 0x1B byte inside the file so you don't need to replace the text when reading the file. – Progman Jun 02 '19 at 20:32
  • Ok, Progman, I will test this as soon as I'm back @ my machine, thanks :-) – Nesrene Jun 04 '19 at 03:46

2 Answers2

0

Write a parser that recognizes particular patterns and convert them into desired string.

mentallurg
  • 4,967
  • 5
  • 28
  • 36
0

The colouring syntax is usually specific to the shell being used e.g. one syntax might work in Bash Shell on Linux but will fail with Cygwin Bash Shell on Windows. Moreover some terminals might not print all the colours combinations e.g. black background with light grey text sometimes doesn't work.

As per this answer you have to use a unicode syntax. To get red text on white background use below:

String redFg = "\u001B[31m";
String blackBg = "\u001B[40m";
System.out.println(blackBg + redFg + "> help (?) - Get help");

In your file you are using \033 which is an octal value equal to \001B hex. You would have to convert your formatting syntax to the one supported by Java.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Yes, thank you Karol. And that works fine when setting the colors in code, but I need to set the colors in the text file :-) I've updated the question with some more info. – Nesrene Jun 02 '19 at 19:37