PROGRAM escape(input, output);

var
  esc   : char;
  index : integer;

  PROCEDURE cls;
  BEGIN
    Write( esc + '[2J');
  END;

  PROCEDURE cursor(zeile, spalte: INTEGER);
  BEGIN
    Write(esc + '[', zeile:1, ';', spalte:1, 'H');
  END;

  procedure reset_attr;
  begin
    write( esc + '[0m');
  end;


BEGIN

  esc := chr(27);

  writeln('reset terminal');
  readln;
  write( esc + 'c');

  writeln('enable linewrap');
  readln;
  write( esc + '[7h');
  for index := 0 to 200 do
    write( index mod 10);

  writeln('disable linewrap');
  readln;
  write( esc + '[7l');
  for index := 0 to 200 do
    write( index mod 10);

  writeln('test fonts');
  readln;
  write( esc + '(');
  writeln( 'normal font');
  write( esc + ')');
  writeln( 'alternate font');
  write( esc + '(');
  writeln( 'back to normal');

  writeln('clear screen');
  readln;
  cls;

  write( esc + '[H');
  Writeln('set cursor');
  readln;
  for index := 1 to 9 do
  begin
    cursor( index, index);
    write( index);
  end;

  writeln( 'move cursor');
  readln;
  write( 'down');
  readln;
  for index := 1 to 13 do
  begin
    write( esc + '[B');
    write( index mod 10);
    write( esc + '[D');
  end;
  write( 'right');
  readln;
  for index := 1 to 20 do
  begin
    write( esc + '[2C');
    write( index mod 10);
    write( esc + '[D');
  end;
  write( 'up');
  readln;
  write( esc + '[13A');
  write( 'move cursor ready');

  writeln;
  Writeln('force cursor');
  readln;
  for index := 1 to 9 do
  begin
    Write(esc + '[', index:1, ';', index:1, 'f');
    write( index);
  end;

  writeln('save cursor');
  readln;
  write( '+');
  write( esc + '[s');
  write( 'some text');
  readln;
  write( esc + '[u');
  write( 'some other text');

  writeln;
  writeln('save cursor & attr');
  readln;
  write( '-');
  write( esc + '7');
  write( 'some text');
  readln;
  write( esc + '8');
  write( 'some other text');

  writeln( 'scroll up');
  readln;
  write( esc + '[S');
  readln;
  write( esc + 'M');
  readln;
  write( esc + '[2S');

  writeln( 'scroll down');
  readln;
  write( esc + '[T');
  readln;
  write( esc + 'D');
  readln;
  write( esc + '[2T');

  writeln;
  (* MISSING TESTS: *)
  (* scrolling *)
  (* tab control *)
  (* erasing text *)
  (* printing *)
  (* define key *)

  writeln( 'set display attributes');
  write( esc + '[0m'); writeln( 'reset');      reset_attr;
  write( esc + '[1m'); writeln( 'bright');     reset_attr;
  write( esc + '[2m'); writeln( 'dim');        reset_attr;
  write( esc + '[4m'); writeln( 'underscore'); reset_attr;
  write( esc + '[5m'); writeln( 'blink');      reset_attr;
  write( esc + '[7m'); writeln( 'reverse');    reset_attr;
  write( esc + '[8m'); writeln( 'hidden');     reset_attr;

  write('BG color: ');
  for index := 0 to 7 do
  begin
    write( esc + '[', 40 + index, 'm');
    writeln( ' ', index:2, ' ');
    reset_attr;
  end;
  writeln;
  write('FG color: ');
  for index := 0 to 7 do
  begin
    write( esc + '[', 30 + index, 'm');
    writeln( ' ', index:2, ' ');
    reset_attr;
  end;
  writeln;
  writeln('bright');
  write('BG color: ');
  for index := 0 to 7 do
  begin
    write( esc + '[', 40 + index, ';1m');
    writeln( ' ', index:2, ' ');
    reset_attr;
  end;
  writeln;
  write('FG color: ');
  for index := 0 to 7 do
  begin
    write( esc + '[', 30 + index, ';1m');
    writeln( ' ', index:2, ' ');
    reset_attr;
  end;
  writeln;

  writeln;
  writeln( 'ANSi sequence test ended.');

  reset_attr;
  cls;
END.