秋·风

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

忽略lazarus console in/output窗显示的转义序列

lazarus自带console in/output窗不支持解释转义序列,只能把 \x1b 显示为不可见字符或乱码,就像把颜色代码当作普通文本显示了出来:

image

在终端显示同样的信息清晰多了:

image

处理方法:

既然不支持,就把这些符号滤掉就可以,打开lazarus/ide/packages/idedebugger/PseudoTerminalDlg.pp
找到:

  buffer := TTerminalStringList.Create;
  try
    buffer.Text := AText;     (* Decides what line breaks it wants to swallow   *)
    case RadioGroupRight.ItemIndex of
      0: for i := 0 to buffer.Count - 1 do
           buffer[i] :=widen(buffer[i]);
      1: for i := 0 to buffer.Count - 1 do
           buffer[i] := withControlPictures(buffer[i], CheckGroupRight.Checked[1]);
      2: for i := 0 to buffer.Count - 1 do
           buffer[i] := withIso2047(buffer[i], CheckGroupRight.Checked[1])
    otherwise

按红色的代码修改:

  buffer := TTerminalStringList.Create;
  try
    buffer.Text := AText;     (* Decides what line breaks it wants to swallow   *)
    case RadioGroupRight.ItemIndex of
      0: for i := 0 to buffer.Count - 1 do
           buffer[i] :=StripAnsiManual(widen(buffer[i]));
      1: for i := 0 to buffer.Count - 1 do
           buffer[i] := withControlPictures(buffer[i], CheckGroupRight.Checked[1]);
      2: for i := 0 to buffer.Count - 1 do
           buffer[i] := withIso2047(buffer[i], CheckGroupRight.Checked[1])
    otherwise

并在

procedure TPseudoConsoleDlg.AddOutput(const AText: String);
const
  dot = #$C2#$B7;                        // ·
var
  lineLimit, numLength, i: integer;

366行前添加以下代码:

function StripAnsiManual(const Input: string): string;
var
  i: Integer;
  InEscape: Boolean;
begin
  Result := '';
  InEscape := False;
  i := 1;
  while i <= Length(Input) do
  begin
    if (Input[i] = #27) then // #27 就是 ESC 字符
    begin
      InEscape := True;
      Inc(i);
      Continue;
    end;

    if InEscape then
    begin
      // 转义序列的结束字母通常在 A-Z 或 a-z 之间 (比如 m, H, J)
      // 也可能是 @ 等
      if (Input[i] >= 'A') and (Input[i] <= 'Z') or
         (Input[i] >= 'a') and (Input[i] <= 'z') then
        InEscape := False;
      Inc(i);
      Continue;
    end;

    // 不是转义码的部分,保留下来
    Result := Result + Input[i];
    Inc(i);
  end;
end;

保存后重新编译lazarus。
现在的Console in/output显示的信息和终端显示的一样了:

image

posted on 2026-05-13 15:13  秋·风  阅读(96)  评论(0)    收藏  举报