{ Temperatures: a vertical bar chart of the system, the CPU and every disk bay, from QNAP's getsysinfo.
  Build and run: fpc temps.pas && ./temps   (prints once and ends)
  Green below 45 °C, amber below 55 °C, red from 55 °C. An empty bay shows as "empty". }
program temps;
{$mode objfpc}{$H+}
uses sysutils, unix;

const
  Rows = 14;       { the chart is 14 lines tall, 5 °C a line, so it reaches 70 °C }
  Step = 5;
  Cell = 8;        { each bar takes 8 columns: 4 of bar and 4 of gap }
type
  TSensor = record
    Name: string;
    Value: integer;  { -1 when the bay is empty or the sensor gives no number }
  end;
var
  sensors: array of TSensor;
  model, s: string;
  i, r, bays: integer;

function Esc(const code: string): string; begin Result := #27'[' + code + 'm'; end;
function Rep(const s: string; n: integer): string; var i: integer; begin Result := ''; for i := 1 to n do Result := Result + s; end;
{ Letters of a UTF-8 text: every byte except the continuation bytes $80-$BF }
function Width(const s: string): integer; var c: char; begin Result := 0; for c in s do if not (c in [#$80..#$BF]) then Inc(Result); end;
function Center(const s: string; n: integer): string;
begin Result := Rep(' ', (n - Width(s)) div 2) + s + Rep(' ', n - Width(s) - (n - Width(s)) div 2); end;

{ The first line getsysinfo prints for a question, '' when there is no getsysinfo }
function Ask(const question: string): string;
var f: Text;
begin
  Result := '';
  if POpen(f, 'getsysinfo ' + question + ' 2>/dev/null', 'R') <> 0 then Exit;
  if not Eof(f) then ReadLn(f, Result);
  PClose(f);
end;

{ "50 C/122 F" gives 50; "-- C/-- F" and an empty answer give -1 }
procedure Add(const name, answer: string);
begin
  SetLength(sensors, Length(sensors) + 1);
  sensors[High(sensors)].Name := name;
  sensors[High(sensors)].Value := StrToIntDef(Copy(answer, 1, Pos(' ', answer + ' ') - 1), -1);
end;

function Level(t: integer): string;
begin
  if t >= 55 then Result := '31' else if t >= 45 then Result := '33' else Result := '32';
end;

begin
  model := Ask('model');
  if model = '' then
  begin
    WriteLn('getsysinfo is missing: this program reads the sensors of a QNAP NAS.');
    Halt(1);
  end;
  Add('System', Ask('systmp'));
  Add('CPU', Ask('cputmp'));
  bays := StrToIntDef(Ask('hdnum'), 0);
  for i := 1 to bays do Add('Disk ' + IntToStr(i), Ask('hdtmp ' + IntToStr(i)));

  WriteLn;
  WriteLn('  ', Esc('1;33'), 'Temperatures of ', model, Esc('0'));
  WriteLn;
  for r := Rows downto 1 do
  begin
    if r mod 2 = 0 then s := Esc('33') + Format('%3d °C ┤', [r * Step]) else s := Esc('33') + '       │';
    for i := 0 to High(sensors) do
    begin
      s := s + '  ';
      if sensors[i].Value >= r * Step then
        s := s + Esc(Level(sensors[i].Value)) + '████'
      else if sensors[i].Value >= r * Step - Step div 2 then
        s := s + Esc(Level(sensors[i].Value)) + '▄▄▄▄'
      else if r mod 2 = 0 then
        s := s + Esc('90') + '┈┈┈┈'
      else
        s := s + '    ';
      s := s + Esc('0') + '  ';
    end;
    WriteLn(s);
  end;
  WriteLn(Esc('33'), '       └', Rep('─', Cell * Length(sensors)), Esc('0'));
  s := '        '; for i := 0 to High(sensors) do s := s + Center(sensors[i].Name, Cell);
  WriteLn(Esc('1'), s, Esc('0'));
  s := '        ';
  for i := 0 to High(sensors) do
    if sensors[i].Value < 0 then s := s + Esc('90') + Center('empty', Cell)
    else s := s + Esc(Level(sensors[i].Value)) + Center(IntToStr(sensors[i].Value) + ' °C', Cell);
  WriteLn(s, Esc('0'));
  WriteLn;
  WriteLn('        ', Esc('32'), '██', Esc('0'), ' below 45 °C    ', Esc('33'), '██', Esc('0'), ' 45 to 54 °C    ',
    Esc('31'), '██', Esc('0'), ' 55 °C and above');
end.
