summaryrefslogtreecommitdiff
path: root/fpcsrc/ide/fpdpansi.pas
blob: f46466f3ce5342f27788113e1459c2ae08364467 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{
    This file is part of text IDE
    Copyright (c) 2000 by Pierre Muller

    Unit to export current screen buffer to an ansi file

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
unit fpdpansi;

interface

uses
  objects,
  video;

function ExportBufferToAnsiFile(var Buffer : TVideoBuf;xmin,xmax,ymin,ymax,linesize : sw_integer;
           SaveAsText : boolean;var f : text) : boolean;

implementation

uses
  wutils;

const
{ Foreground and background color constants }
  Black         = 0;
  Blue          = 1;
  Green         = 2;
  Cyan          = 3;
  Red           = 4;
  Magenta       = 5;
  Brown         = 6;
  LightGray     = 7;

const ColorTab : array[0..7] of byte =
      (Black,Red,Green,Brown,Blue,Magenta,Cyan,LightGray);

{$i-}
function ExportBufferToAnsiFile(var Buffer  : TVideoBuf;xmin,xmax,ymin,ymax,linesize : sw_integer;
           SaveAsText : boolean;var f : text) : boolean;
var
  CurColor : byte;
  CurForColor, CurBackColor : byte;
  CurIsBold, CurIsBlinking : boolean;

  procedure ChangeColor(NewColor : byte);
    var
      ForColor, BackColor : byte;
      IsBold, IsBlinking : boolean;
    begin
      ForColor:=NewColor and 7;
      BackColor:=(NewColor and $70) shr 4;
      IsBold:=(NewColor and 8) <> 0;
      IsBlinking:=(NewColor and $80) <> 0;
      if IsBlinking<>CurIsBlinking then
        begin
          if IsBlinking then
            Write(f,#27'[5m')
          else
            Write(f,#27'[25m');
          CurIsBlinking:=IsBlinking;
        end;
      if IsBold<>CurIsBold then
        begin
          if IsBold then
            Write(f,#27'[1m')
          else
            Write(f,#27'[21m');
          CurIsBold:=IsBold;
        end;
      if CurForColor<>ForColor then
        begin
          Write(f,#27'['+inttostr(ColorTab[ForColor]+30)+'m');
          CurForColor:=ForColor;
        end;
      if CurBackColor<>BackColor then
        begin
          Write(f,#27'['+inttostr(ColorTab[BackColor]+40)+'m');
          CurBackColor:=BackColor;
        end;
      CurColor:=NewColor;
    end;
var
  Ch : char;
  textAttr : byte;
  i, j : sw_integer;
begin
  CurColor:=0;
  for i:=ymin to ymax do
    begin
      for j:=xmin to xmax do
        begin
          ch:=chr(Buffer[i*linesize+j] and $ff);
          textattr:=Buffer[i*linesize+j] shr 8;
          if (textattr<>CurColor) and not SaveAsText then
            ChangeColor(textattr);
          { Escape escape, by printing two #27 PM }
          if (ch=#27) or (ord(ch)<=16) then
            Write(f,#27);
          Write(f,ch);
        end;
      writeln(f);
    end;
  ExportBufferToAnsiFile:=(IOResult=0);
end;

end.