{ Przeglądarka logów: przewija plik logu, ostrzeżenia na bursztynowo, błędy na czerwono, i znajduje następny błąd.
  Kompilacja i start: fpc viewer.pas && ./viewer kernel.txt   (dmesg > kernel.txt go tworzy; bez pliku: viewer.log)
  Góra/dół i PgUp/PgDn przewijają, Home i End skaczą, / albo s szuka następnego błędu, q kończy. }
program viewer;
{$mode objfpc}{$H+}
uses crt, sysutils, classes;

const
  Page = 21;        { wierszy logu na jednym ekranie: wiersze od 2 do 22 }
  Room = 72;        { znaków wiersza logu, które mieszczą się obok jego numeru }
var
  con: Text;
  log: TStringList;
  name, note: string;
  top, found: integer;
  c: char;

function Esc(const code: string): string; begin Result := #27'[' + code + 'm'; end;
function At(x, y: integer): string; begin Result := #27'[' + IntToStr(y) + ';' + IntToStr(x) + 'H'; end;
function Rep(const s: string; n: integer): string; var i: integer; begin Result := ''; for i := 1 to n do Result := Result + s; end;
function Max(a, b: integer): integer; begin if a > b then Result := a else Result := b; end;
function Min(a, b: integer): integer; begin if a < b then Result := a else Result := b; end;

{ Kolor wiersza według poziomu: czerwony dla błędu, bursztynowy dla ostrzeżenia }
function Tint(const line: string): string;
begin
  if Pos('ERROR', UpperCase(line)) > 0 then Result := '31'
  else if Pos('WARN', UpperCase(line)) > 0 then Result := '33'
  else Result := '0';
end;

procedure Draw;
var s, line: string; i, n: integer;
begin
  s := Esc('0') + At(1, 1) + Esc('30;43') + Format(' %-58s%20s ', [name, 'wierszy: ' + IntToStr(log.Count)]);
  for i := 0 to Page - 1 do
  begin
    n := top + i;
    s := s + Esc('0') + At(1, i + 2) + #27'[K';
    if n >= log.Count then Continue;
    line := log[n];
    if Length(line) > Room then line := Copy(line, 1, Room - 1) + Esc('1;33') + '›';
    if n = found then s := s + Esc('30;41') + Format('%5d ', [n + 1]) + Esc('1;37;41') + ' ' + line + Rep(' ', Room - Length(log[n]))
    else s := s + Esc('90') + Format('%5d ', [n + 1]) + Esc(Tint(log[n])) + ' ' + line;
  end;
  s := s + Esc('0') + At(1, 23) + #27'[K' + Esc('33') + note;
  s := s + At(1, 24) + Esc('30;47') + Format(' Wiersze %d-%d z %d  %3d %%', [top + 1, Min(top + Page, log.Count), log.Count,
    Min(top + Page, log.Count) * 100 div Max(log.Count, 1)]) + '   ↑ ↓ PgUp PgDn  Home End   / błąd   q koniec ' + #27'[K';
  Write(con, s + Esc('0')); Flush(con);
end;

{ Następny wiersz za zaznaczonym (albo od góry ekranu), w którym jest "error" pisane dowolnie }
procedure FindError;
var i, from: integer;
begin
  if found >= 0 then from := found + 1 else from := top;
  for i := from to log.Count - 1 do
    if Pos('ERROR', UpperCase(log[i])) > 0 then
    begin
      found := i; top := Max(0, Min(i - 3, log.Count - Page));
      note := Format('Błąd w wierszu %d.', [i + 1]);
      Exit;
    end;
  note := 'Niżej nie ma już błędów. / zaczyna od początku.';
  found := -1; top := 0;
end;

begin
  if ParamCount > 0 then name := ParamStr(1) else name := 'viewer.log';
  log := TStringList.Create;
  try
    log.LoadFromFile(name);
  except
    WriteLn('Nie można odczytać pliku ', name, '.'); Halt(1);
  end;
  Assign(con, ''); Rewrite(con);
  Write(con, #27'[?25l' + #27'[2J');
  top := 0; found := -1; note := '';
  repeat
    Draw;
    c := ReadKey; note := '';
    if c = #0 then
      case ReadKey of
        #72: top := top - 1;
        #80: top := top + 1;
        #73: top := top - Page;
        #81: top := top + Page;
        #71: top := 0;
        #79: top := log.Count - Page;
      end
    else if c in ['/', 's'] then FindError;
    top := Max(0, Min(top, log.Count - Page));
  until c in ['q', 'Q', #27];
  Write(con, Esc('0') + #27'[2J' + At(1, 1) + #27'[?25h'); Flush(con);
  log.Free;
end.
