#!/usr/bin/perl

use warnings;
use strict;

use Curses;

my $curses = new Curses;

open(my $fh, "<", $ARGV[0]) or die;

my @file = <$fh>;
my $lines = $#file - 1;

my $w = newpad($#file + 1, $COLS);

for my $i (0 .. $#file)
{
   $w->addstr($i, 0, $file[$i]);
}

my $pointer = 0;

clear;
$w->prefresh($pointer, 0, 0, 0, $LINES - 1, $COLS - 1);

while (my $char = $w->getch)
{
   if ($char eq "j" and $pointer < $lines - $LINES + 2)
   {
      $pointer++;
      $w->prefresh($pointer, 0, 0, 0, $LINES - 1, $COLS - 1);
   }
   elsif ($char eq "k" and $pointer > 0)
   {
      $pointer--;
      $w->prefresh($pointer, 0, 0, 0, $LINES - 1, $COLS - 1);
   }
   elsif ($char eq "q")
   {
      endwin;
      exit(0);
   }
}
