{$i-}

program EXPLORER (input, output);

uses
  CORE, dos, crt;

type
  string15 = string[22];

var
  ISDIR      : array[1..1777] of boolean;
  FILENAME   : array[1..1777] of string15;
  TOTAL      : word;
  T          : text;

procedure LISTFILES;
  var
    s  : searchrec;
  begin
    TOTAL:= 0;
    findfirst ('*.*', archive + directory + hidden, s);
    while (doserror = 0) do begin
      inc (TOTAL);
      case s.attr of
        directory: ISDIR[TOTAL]:= true;
        else ISDIR[TOTAL]:= false;
      end;
      FILENAME[TOTAL]:= S.NAME;
      findnext (S);
    end;
  end;

function WalkDirs : string;
  var
    HORIZON, Y       : word;
    SELECTED_A_FILE  : boolean;
    CH               : char;
    ORIGIN           : string;
    CURRENT          : string;
    C                : word;
  begin
    getdir (0, ORIGIN);
    HORIZON:= 0;
    Y:= 1;
    SELECTED_A_FILE:= false;
    LISTFILES;
    textcolor (MAGENTA);
    repeat
      fillchar (mem[$A000:0], 64000, LIGHTGRAY);
      for C:= 1 to 22 do begin
        directvideo:= false;
        if C = Y then textbackground (BROWN) else
          textbackground (LIGHTGRAY);
        gotoxy (15, C+1);
        PRINTMCGA (FILENAME[C+HORIZON]);
      end;
      CH:= readkey;
      case CH of
        #13: begin
          if ISDIR[HORIZON+ Y] then begin
            chdir (FILENAME[HORIZON+ Y]);
            LISTFILES;
            HORIZON:= 0;
            Y:= 1;
          end else
            SELECTED_A_FILE:= true;
        end;
        #0: begin
          CH:= readkey;
          case CH of
            'H': if Y>1 then Y:= Y-1 else if HORIZON > 0 then dec (HORIZON, 1);
            'P': if Y<22 then inc (Y, 1) else if Y+HORIZON < TOTAL then HORIZON:= HORIZON+1;
          end;
        end;
      end;
    until SELECTED_A_FILE or (CH = char(27));
    getdir (0, CURRENT);
    if CURRENT[length(CURRENT)] <> '\' then
      CURRENT:= CURRENT + '\';
    chdir (ORIGIN);
    case SELECTED_A_FILE of
      true: WalkDirs:= CURRENT + FILENAME[Y+HORIZON];
      false: WalkDirs:= '';
    end;
  end;

var
  TEKST : string;
  C0    : word;

begin
  for C0:= 1 to 1777 do begin
    FILENAME[C0]:= '';
  end;
  MCGAINIT;
  TEKST:= WalkDirs;
  textmode (CO80);
  writeln (TEKST);
  assign (T, 'OUTPUT.DAT');
  rewrite (T);
  writeln (T, TEKST);
  close (T);
end.
