在 Ada 中打印彩色文本——ANSI 转义码似乎无法正常工作

Printing coloured text in Ada -- ANSI escape codes seem impossible to get working

我想使用 ANSI 转义序列在 Ada 中打印样式文本。

这是我试过的:

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("3[93mHowdy!3[0m");
  Put_Line("033[31;1;4mHello3[0m");
  -- With ESC character
  Put_Line(ESC & "3[93m" & "Howdy!" & ESC & "3[0m");
  Put_Line(ESC & "033[93m" & "Howdy!" & ESC & "033[0m");
  Put_Line(ESC & "033[31;1;4mHello" & ESC & "3[0m");
  Put_Line(ESC & "Howdy"); -- Prints "owdy", i.e. escapes the H
end;

None 其中有效!每个语句只打印明文。

我想通了 -- 我太接近了!

事实证明,字符序列 3 ASCII 转义字符,而不是带内信号的一部分。

这是一个非常简单的修复,使用由 Ada.Characters.Latin_1:

定义的 ESC 字符
Put_Line (ESC & "[93m" & "Howdy!" & ESC & "[0m");

以橙色文本打印 "Howdy"。

我想在 Windows 10 上练习这个:它不能与 DOS 控制台或 Powershell 一起使用。见下图。

批处理文件有效并来自 How to echo with different colors in the Windows command line

我认为这与此处解释的问题有关:

源代码http://tpcg.io/N1wORl或:

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;