-2

I am making a project where I use escape codes to style printing, I know my escape key is "\033", however other OS's have different escape codes and I would like my project to be cross-platform and work on any OS. Is there any macro that provides the users escape code? if not, is there any table sort of table listing different the different ANSI escape codes for different os's

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
MaximV
  • 155
  • 11
  • 3
    Are you trying to reinvent ncurses, or do you just want to use ncurses? – Eljay May 25 '20 at 23:08
  • @Eljay neither, I am trying to make a simple logger which can format text in different colors and styles, a bit like spdlog, but simpler. – MaximV May 25 '20 at 23:16
  • 2
    Just curious, which OS uses different escape code from '\033'? – Berrysoft May 26 '20 at 01:27
  • 3
    ANSI escape codes are standardized (one I remember is `"\033[2J"` to clear the screen). It is common for terminals these days (like the Windows console) to not support them. Other platforms may support extensions to the standard sequences. – 1201ProgramAlarm May 26 '20 at 03:55
  • @Berrysoft it's not the escape character itself which differs, it's the whole sequence that starts with ESC and continues for some characters after. And some environments won't support any at all. – Mark Ransom May 26 '20 at 03:56
  • are you asing for the binary value of ESC or the standard values that come after ESC (color, chars, clear,....) – pm100 May 26 '20 at 03:56
  • To give a concrete example, I'm not aware of a text editor that will interpret the escape sequences to display characters in different colors. – Mark Ransom May 26 '20 at 04:01
  • `other OS's have different escape codes` Can you give an example? Which OS uses different ANSI escape codes? – KamilCuk May 26 '20 at 04:37
  • 1
    It is *terminals* which have different escape *sequences*, not operating systems, and as terminals are virtual these days and obey the VT100 escape sequences it is difficult to understand how you can oossibly have this problem. – user207421 May 26 '20 at 06:13
  • @user207421 what I was trying to say is that I have seen other people use other escape characters e.g. "\u001b" and I made the assumption that the escape code was related to the user's OS. – MaximV May 26 '20 at 12:37
  • 1
    Do you realize that `"\033"` and `"\u001b"` are identical values, just with different ways of writing them out? – Mark Ransom May 26 '20 at 17:32
  • 1
    @user207421 not all virtual terminals implement the VT100 escape sequences. The Windows console is a notable example. – Mark Ransom May 26 '20 at 17:34
  • 1
    @MarkRansom The [Microsft documentation](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences) doesn't agree with you. – user207421 May 27 '20 at 04:08
  • @user207421 interesting, thanks. That must be a recent change, I notice the page date is 2018. – Mark Ransom May 27 '20 at 14:22
  • 1
    @MarkRansom Unless I am much mistaken I was using VT100 codes in MS.DOS in 1987. – user207421 May 27 '20 at 20:33
  • @user207421 yes, that was supported optionally with ANSI.SYS. That capability was eventually lost in the Windows console. – Mark Ransom May 27 '20 at 20:47
  • Well that isn't an example of operating-system-specific escape codes, it's an example of a virtual terminal not having any escape codes at all, which bears out my original point. – user207421 May 27 '20 at 21:41

1 Answers1

1

ANSI escape sequences are standardized (it's even in the name : American National Standards Institute), and you shouldn't have any problems with them on different terminals (because it's not the OS that interprets escape sequences, but the terminal emulator).

You could be interested in using libraries like readline or ncurses. Be aware that in 2020, UTF-8 is everywhere.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
Telnooo
  • 64
  • 5