File: ansiterm.cpp

package info (click to toggle)
vfu 4.06-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,088 kB
  • ctags: 2,184
  • sloc: cpp: 14,482; ansic: 4,091; perl: 581; makefile: 540; sh: 57
file content (227 lines) | stat: -rw-r--r-- 4,391 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *
 * (c) Vladi Belperchinov-Shabanski "Cade" <cade@biscom.net> 1998-2003
 *
 * SEE `README',`LICENSE' OR `COPYING' FILE FOR LICENSE AND OTHER DETAILS!
 *
 * $Id: ansiterm.cpp,v 1.3 2003/01/21 19:56:35 cade Exp $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ansiterm.h"

  int Ansi_MAXX = 80;
  int Ansi_MAXY = 25;
  int Ansi_X    =  1;
  int Ansi_Y    =  1;

  int a_fg;
  int a_bg;
  int a_ta;

  int ansi_o_ta;

  int ANSI = 0;

  static int colortab( int color ) // convert normal colors to ANSI ones
  {
    switch(color)
      {
      case cBLACK   : return 0;
      case cBLUE    : return 4;
      case cGREEN   : return 2;
      case cCYAN    : return 6;
      case cRED     : return 1;
      case cMAGENTA : return 5;
      case cYELLOW  : return 3;
      case cWHITE   : return 7;
      }
    return 7;
  }

  int AnsiInit( int pANSI )
  {
    if ( pANSI != -1)
      ANSI = pANSI;
    else
      {
      ANSI = 0;
      char *buf = getenv( "TERM" );
      if ( buf && strcasecmp( buf, "ANSI"  ) == 0 ) ANSI = 1;
      if ( buf && strcasecmp( buf, "vt100" ) == 0 ) ANSI = 1;
      }
    if (getenv( "TERMX" )) Ansi_MAXX = atoi( getenv( "TERMX" ) );
    if (getenv( "TERMY" )) Ansi_MAXY = atoi( getenv( "TERMY" ) );
    if ( Ansi_MAXX == 0 ) Ansi_MAXX = 80;
    if ( Ansi_MAXY == 0 ) Ansi_MAXY = 25;
    Ansi_X = 1;
    Ansi_Y = 1;
    AnsiTA( cNORMAL );

    return 0;
  };

  void AnsiDone()
  {
    if (!ANSI) return;
    AnsiTA( cNORMAL );
  };


  void AnsiSuspend() // suspends console (before system() for example)
  {
    if (!ANSI) return;
    ansi_o_ta = a_ta;
    AnsiTA( cNORMAL );
  }

  void AnsiRestore() // restores console after suspend
  {
    if (!ANSI) return;
    AnsiTA( ansi_o_ta );
  }

  void AnsiCE( int attr ) // clear to end-of-line
  {
    if (!ANSI) return;
    if ( attr != -1 )
      {
      int save_ta = a_ta;
      AnsiTA( attr );
      printf( "\033[K" );
      AnsiTA( save_ta );
      }
    else
      {
      printf( "\033[K" );
      }
  };

  void AnsiCS( int attr ) // clear screen
  {
    if (!ANSI) return;
    if ( attr != -1 )
      {
      int save_ta = a_ta;
      AnsiTA( attr );
      printf( "\033[2J" );
      AnsiTA( save_ta );
      }
    else
      {
      printf( "\033[2J" );
      }
  };


  void AnsiOut( int x, int y, const char *s )
  {
    AnsiXY( x, y );
    AnsiPuts( s );
  };

  void AnsiOut( int x, int y, const char *s, int attr )
  {
    AnsiXY( x, y );
    AnsiPuts( s, attr );
  };

  void AnsiPuts( const char *s )
  {
    printf( s );
  };

  void AnsiPuts( const char *s, int attr )
  {
    int _ta = a_ta;
    AnsiTA( attr );
    printf( s );
    AnsiTA( _ta  );
  };

  void AnsiCHide() { return; } // cursor hide
  void AnsiCShow() { return; } // cursor show

  int AnsiMaxX() { return Ansi_MAXX; }
  int AnsiMaxY() { return Ansi_MAXY; }
  int AnsiX() { return -1; }
  int AnsiY() { return -1; }

  void AnsiFG( int color )
  {
    AnsiTA( CONCOLOR(color, a_bg) );
  }

  void AnsiBG( int color )
  {
    AnsiTA( CONCOLOR( a_fg, color ) );
  }

  void AnsiTA( int attr )
  {
    if (!ANSI) return;
    a_ta = attr;
    a_fg = COLORFG( a_ta );
    a_bg = COLORBG( a_ta );

    int _l = (a_bg>7)?5:0;    // blink
    int _h = (a_fg>7)?1:0;    // hi
    int _f = a_fg;
    int _b = a_bg;
    if ( _f > 7 ) _f -= 8;
    if ( _b > 7 ) _b -= 8;
    _f = colortab(_f)+30;  // fore
    _b = colortab(_b)+40;  // back

    // hi,blink,fg,bg
    printf( "\033[0;%s%s%d;%dm", _h?"1;":"", _l?"5;":"", _f, _b );
  }

  void AnsiXY( int x, int y ) // go to x,y
  {
    if (!ANSI) return;
    if ( x < 1 || y < 1 || x > Ansi_MAXX || y > Ansi_MAXY ) return;
    printf( "\033[%d;%dH", y, x );
  }

  int AnsiKbHit() { return 1; }
  int AnsiGetch() { return fgetc(stdin); }
  void AnsiBeep()  { printf( "\007" ); }


#ifdef TEST3
int main()
{
  AnsiInit();
  AnsiCS();

  AnsiOut( 1, 1, "<-" );
  AnsiXY( 10, 10 );
  AnsiTA( 79 );
  AnsiPuts( "  ANSI terminal Test  " );
  AnsiXY( 1, 11 );
  int z;
  for ( z = 0; z < 256; z++ )
    {
    char tmp[16];
    sprintf( tmp, " %3d ", z );
    AnsiTA(z); AnsiPuts( tmp );
    }
  AnsiTA( cNORMAL );

  for ( z = 1; z <= AnsiMaxX(); z++ )
    AnsiOut( z, 1, "*", z );
  for ( z = 1; z <= AnsiMaxX(); z++ )
    AnsiOut( z, AnsiMaxY()-1, "*", z );
  int c = AnsiGetch();
  if (c) c = c; // :) test


  AnsiDone();
}
#endif

 // eof ansiterm.cpp