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

问题描述 投票:0回答:2

我想使用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;

他们都没有工作!每个语句都只打印明文。

colors terminal ada
2个回答
2
投票

我想通了 - 我太近了!

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

这是一个非常简单的修复,使用ESC定义的Ada.Characters.Latin_1字符:

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

用橙色文字打印“你好”。


0
投票

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

批处理文件工作,来自How to echo with different colors in the Windows command line

我相信这与这里解释的问题有关:UTF-8 on Windows with Ada

源代码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;

Bat file vs Ada output

© www.soinside.com 2019 - 2024. All rights reserved.