我想使用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("\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;
他们都没有工作!每个语句都只打印明文。
我想通了 - 我太近了!
事实证明,字符序列\033
是ASCII转义字符,而不是带内信号的一部分。
这是一个非常简单的修复,使用ESC
定义的Ada.Characters.Latin_1
字符:
Put_Line (ESC & "[93m" & "Howdy!" & ESC & "[0m");
用橙色文字打印“你好”。
我想在Windows 10上运行它:它不能与DOS控制台或Powershell一起使用。见下图。
批处理文件工作,来自How to echo with different colors in the Windows command line
我相信这与这里解释的问题有关:UTF-8 on Windows with Ada
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;