NASutils Knowledge BaseUnofficialPolski

Pascal · projectA greeting screen with large letters in Free Pascal

This program prints a greeting at login: the word NAS in letters six lines tall, a greeting by the hour beside it, then a framed table with the model, the kernel, the uptime, the load and the memory of the NAS. It prints once and ends.

Programming on the NAS › Pascal in 30 lessons

  ███╗   ██╗ █████╗ ███████╗
  ████╗  ██║██╔══██╗██╔════╝    Good morning.
  ██╔██╗ ██║███████║███████╗    Welcome to TBS-h574TX.
  ██║╚██╗██║██╔══██║╚════██║
  ██║ ╚████║██║  ██║███████║    Friday, 27 November 2026, 09:30
  ╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝

  ┌──────────────┬─────────────────────────────────────────────────────────┐
  │ NAS          │ TBS-h574TX                                              │
  ├──────────────┼─────────────────────────────────────────────────────────┤
  │ Kernel       │ 6.6.129-qnap                                            │
  │ Uptime       │ 1 d 13 h 10 min                                         │
  │ Load         │ 1.17  0.90  0.88   (1, 5 and 15 minutes)                │
  │ Memory       │ 5.6 of 15.4 GiB used  ███████░░░░░░░░░░░░░ 36 %         │
  └──────────────┴─────────────────────────────────────────────────────────┘
The greeting at 09:30 on Friday, 27 November 2026: "Good morning", the model TBS-h574TX, kernel 6.6.129-qnap, 1 d 13 h 10 min of uptime and 5.6 of 15.4 GiB of memory in use.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/pascal/hello.pas

hello.pas Download hello.pas

{ Hello screen: the word NAS in large letters, a greeting by the hour and a table with the model, kernel,
  uptime, load and memory. Build and run: fpc hello.pas && ./hello   (prints once and ends)
  It writes plain escape codes without crt, so the output also works in a login script. }
program hello;
{$mode objfpc}{$H+}
uses sysutils, dateutils, classes, unix;

type
  TRow = record
    Name, Value: string;
  end;

const
  Big: array[1..6] of string = (
    '███╗   ██╗ █████╗ ███████╗',
    '████╗  ██║██╔══██╗██╔════╝',
    '██╔██╗ ██║███████║███████╗',
    '██║╚██╗██║██╔══██║╚════██║',
    '██║ ╚████║██║  ██║███████║',
    '╚═╝  ╚═══╝╚═╝  ╚═╝╚══════╝');
  Shades: array[1..6] of string = ('93', '93', '33', '33', '33', '90');
  Days: array[1..7] of string = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
  Months: array[1..12] of string = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
    'August', 'September', 'October', 'November', 'December');

var
  rows: array of TRow;
  side: array[1..6] of string;
  when: TDateTime;
  greet, model, load: string;
  total, avail: double;
  up, i: 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;

{ Columns on the screen: UTF-8 continuation bytes ($80-$BF) and colour codes do not count }
function Width(const s: string): integer;
var i: integer; skip: boolean = false;
begin
  Result := 0;
  for i := 1 to Length(s) do begin
    if s[i] = #27 then skip := true;
    if not skip and not (s[i] in [#$80..#$BF]) then Inc(Result);
    if skip and (s[i] = 'm') then skip := false;
  end;
end;

function Pad(const s: string; n: integer): string; begin Result := s + Rep(' ', n - Width(s)); end;

{ The first line a command prints, '' when it prints nothing or is missing }
function Ask(const cmd: string): string;
var f: Text;
begin
  Result := '';
  if POpen(f, cmd + ' 2>/dev/null', 'R') <> 0 then Exit;
  if not Eof(f) then ReadLn(f, Result);
  PClose(f);
end;

function ProcFile(const name: string): string;
begin
  Result := GetEnvironmentVariable('PROC');
  if Result = '' then Result := '/proc';
  Result := Result + '/' + name;
end;

function Field(const s: string; n: integer): string;
begin Result := s.Split([' ', #9], TStringSplitOptions.ExcludeEmpty)[n]; end;

procedure Add(const name, value: string);
begin
  SetLength(rows, Length(rows) + 1);
  rows[High(rows)].Name := name; rows[High(rows)].Value := value;
end;

{ Reads a file of $PROC line by line: the size of a /proc file is 0, so it is read as text, not as a block }
procedure Meminfo;
var f: Text; s: string;
begin
  Assign(f, ProcFile('meminfo')); Reset(f);
  while not Eof(f) do begin
    ReadLn(f, s);
    if s.StartsWith('MemTotal:') then total := StrToFloat(Field(s, 1))
    else if s.StartsWith('MemAvailable:') then avail := StrToFloat(Field(s, 1));
  end;
  Close(f);
end;

begin
  if GetEnvironmentVariable('NOW') <> '' then when := ScanDateTime('yyyy-mm-dd hh:nn:ss', GetEnvironmentVariable('NOW'))
  else when := Now;
  case HourOf(when) of
    5..11: greet := 'Good morning';
    12..17: greet := 'Good afternoon';
  else greet := 'Good evening';
  end;
  model := Ask('getsysinfo model');
  if model = '' then model := Ask('uname -n');
  side[2] := Esc('1;93') + greet + '.';
  side[3] := Esc('0') + 'Welcome to ' + Esc('1') + model + Esc('0') + '.';
  side[5] := Esc('0') + Days[DayOfTheWeek(when)] + ', ' + IntToStr(DayOf(when)) + ' ' + Months[MonthOf(when)]
    + ' ' + IntToStr(YearOf(when)) + ', ' + FormatDateTime('hh:nn', when);

  WriteLn;
  for i := 1 to 6 do WriteLn('  ', Esc(Shades[i]), Big[i], Esc('0'), '    ', side[i], Esc('0'));
  WriteLn;

  Add('NAS', model);
  Add('Kernel', Field(Ask('cat ' + ProcFile('version')), 2));
  up := Trunc(StrToFloat(Field(Ask('cat ' + ProcFile('uptime')), 0)));
  Add('Uptime', Format('%d d %d h %d min', [up div 86400, up div 3600 mod 24, up div 60 mod 60]));
  load := Ask('cat ' + ProcFile('loadavg'));
  Add('Load', Field(load, 0) + '  ' + Field(load, 1) + '  ' + Field(load, 2) + '   (1, 5 and 15 minutes)');
  Meminfo;
  Add('Memory', Format('%.1f of %.1f GiB used  ', [(total - avail) / 1048576, total / 1048576])
    + Esc('32') + Rep('█', Round((total - avail) / total * 20)) + Esc('90') + Rep('░', 20 - Round((total - avail) / total * 20))
    + Esc('0') + Format(' %.0f %%', [(total - avail) / total * 100]));

  WriteLn('  ', Esc('33'), '┌', Rep('─', 14), '┬', Rep('─', 57), '┐');
  for i := 0 to High(rows) do
  begin
    WriteLn('  │ ', Esc('1;33'), Pad(rows[i].Name, 12), Esc('0;33'), ' │ ', Esc('0'), Pad(rows[i].Value, 55), Esc('33'), ' │');
    if i = 0 then WriteLn('  ├', Rep('─', 14), '┼', Rep('─', 57), '┤');
  end;
  WriteLn('  └', Rep('─', 14), '┴', Rep('─', 57), '┘', Esc('0'));
  WriteLn;
end.

What you learn

  • A record TRow keeps a name and a value together, and a dynamic array of them grows with SetLength.
  • ScanDateTime('yyyy-mm-dd hh:nn:ss', ...) reads the time out of a text, and HourOf gives its hour for the greeting.
  • case HourOf(when) of 5..11: tests a range of numbers in one line.
  • The characters ┌ ┬ ┐ ├ ┼ ┤ └ ┴ ┘ join a two-column table where its lines meet.

How it works

The program does not use crt. It needs no keys, and crt's Write counts bytes as columns, so plain WriteLn with colour codes prints the UTF-8 letters as they are. The output also works when a login script runs the program.

First the program takes the time from NOW, or from the clock without it, and picks the greeting: morning from 5, afternoon from 12, evening from 18. The model comes from getsysinfo model; on a PC without it, uname -n names the computer. The six lines of the big word stand in the constant Big, each with its own shade of amber, and the greeting and the date stand in side beside lines 2, 3 and 5.

Then Add puts one row after the other into rows: the model, the kernel version (the third word of /proc/version), the uptime, the load and the memory with a bar. The table prints a divider after the first row.

Notes to the lines

  • 8–11The record of one table row.
  • 14–20The word NAS in block letters, one string a line.
  • 21Bright amber at the top, plain amber in the middle and grey at the bottom give the word a shade.
  • 39–49Width counts columns: colour codes and the continuation bytes of UTF-8 letters do not count.
  • 54–61Ask returns the first line a command prints; 2>/dev/null hides the error when the command is missing.
  • 79–90/proc/meminfo is read as a text file line by line; a file of /proc reports a size of 0, so it is not read as one block.
  • 93–94Without NOW the program takes Now, the clock of the NAS.
  • 104DayOfTheWeek returns 1 for Monday and 7 for Sunday, so the array Days starts at Monday.
  • 108Each line of the big word gets its colour, then the text of side four spaces to the right.
  • 112Field(..., 2) takes the third word of "Linux version 6.6.129-qnap ...": the kernel release.
  • 118–120Memory in use is MemTotal minus MemAvailable; the bar has 20 cells.
  • 125Pad fills the name to 12 and the value to 55 columns, so the right edge stands straight.
  • 126The divider after the first row sets the model apart from the facts.

Run it

fpc hello.pas && ./hello

The program prints the screen and ends. To see it at every SSH login, start ./hello from your login file; the Bash project "A greeting at the SSH login" shows which file QTS reads.

Try it yourself

Add a row "Processes" with the fourth field of /proc/loadavg. Change the colours in Shades to '96' and '36' for a cyan word.

More projects in Pascal