1

I'd like to use ANSI escape sequences to print styled text in Ada.

This is what I've tried:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;

procedure Main is
begin
  -- No ESC character
  Put_Line("\033[93mHowdy!\033[0m");
  Put_Line("033[31;1;4mHello\033[0m");
  -- With ESC character
  Put_Line(ESC & "\033[93m" & "Howdy!" & ESC & "\033[0m");
  Put_Line(ESC & "033[93m" & "Howdy!" & ESC & "033[0m");
  Put_Line(ESC & "033[31;1;4mHello" & ESC & "\033[0m");
  Put_Line(ESC & "Howdy"); -- Prints "owdy", i.e. escapes the H
end;

None of them work! Every statement just prints the plaintext.

jonsca
  • 10,218
  • 26
  • 54
  • 62
haz
  • 2,034
  • 4
  • 28
  • 52

2 Answers2

4

I figured it out -- I was so close!

It turns out that the character sequence \033 is the ASCII escape character, and not part of the in-band signal.

It's a very simple fix, using the ESC character as defined by Ada.Characters.Latin_1:

Put_Line (ESC & "[93m" & "Howdy!" & ESC & "[0m");

Prints "Howdy" in orange text.

haz
  • 2,034
  • 4
  • 28
  • 52
  • As far as I can tell this would only work on Unix-like terminals, isn’t it ? – LoneWanderer Aug 04 '18 at 12:04
  • Probably. Ive only used Unix machines so I wouldn't know otherwise - sorry. – haz Aug 04 '18 at 15:04
  • @LoneWanderer - No, IIRC, ANSI control-codes are at the hardware-level, like EGA or VGA. – Shark8 Aug 05 '18 at 14:05
  • Maybe you should have a look here (in french): http://www.les-ziboux.rasama.org/ada-commandes-echappement-ansi.html There is a full package definition for ANSI console in Ada – LoneWanderer Aug 31 '18 at 23:26
0

I wanted to exercise this on Windows 10 : it is not working with either DOS console or Powershell. See picture below.

Batch file works and comes from How to echo with different colors in the Windows command line

I believe this is related to the issue explained here : UTF-8 on Windows with Ada

Source code http://tpcg.io/N1wORl or :

with Ada.Text_IO;
with Ada.Wide_Text_IO;
with Ada.Characters.Latin_1;
with Ada.Characters.Wide_Latin_1;

pragma Wide_Character_Encoding (Utf8);

procedure hello is

begin

   Ada.Text_IO.Put_Line ("");

   Ada.Text_IO.Put_Line ("Ada.Text_IO");
   Ada.Text_IO.Put (Ada.Characters.Latin_1.Percent_Sign);
   Ada.Text_IO.Put
     (Ada.Characters.Latin_1.ESC &
      "[93m" &
      "Howdy!" &
      Ada.Characters.Latin_1.ESC &
      "[0m");
   Ada.Text_IO.Put_Line ("");
   Ada.Text_IO.Put_Line ("");

   Ada.Text_IO.Put_Line ("Ada.Wide_Text_IO");
   Ada.Wide_Text_IO.Set_Output (File => Ada.Wide_Text_IO.Standard_Output);
   Ada.Wide_Text_IO.Set_Error (File => Ada.Wide_Text_IO.Standard_Error);
   Ada.Wide_Text_IO.Put (Ada.Characters.Wide_Latin_1.Percent_Sign);
   Ada.Wide_Text_IO.Put_Line ("");
   Ada.Wide_Text_IO.Put
     (Ada.Characters.Wide_Latin_1.ESC &
      "[93m" &
      "Howdy!" &
      Ada.Characters.Wide_Latin_1.ESC &
      "[0m");

   Ada.Text_IO.Put_Line ("");
   Ada.Text_IO.Put_Line ("");
end;

Bat file vs Ada output

LoneWanderer
  • 3,058
  • 1
  • 23
  • 41