File: watchconsolesize.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (69 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
program WatchConsoleSize;

(* This console-mode program for Linux or other unix implementations reports	*)
(* on the initial console size and outputs a message every time it gets a	*)
(* SIGWINCH indicating that the console window has been resized.		*)
(*										*)
(* It DOES NOT attempt any formatted output using escape sequences etc. MarkMLl	*)

uses
  SysUtils, Keyboard, Crt, TermIO, BaseUnix;

var
  signalCount: integer= 0;


procedure reportSize;

var
  winSize: TWinSize;

begin
  Write(signalCount, ': ');
  FillChar(winSize, sizeof(winSize), 0);
  if IsaTty(StdInputHandle) = 1 then
    if fpioctl(StdInputHandle, TIOCGWINSZ, @winSize) >= 0 then
      Write(winSize.ws_row, ' x ', winSize.ws_col);
  WriteLn;
  signalCount += 1
end { reportSize } ;


procedure winchHandler(sig: longint; {%H-}info: PSigInfo; {%H-}context: PSigContext); cdecl;

begin
  case sig of
    SIGWINCH: reportSize
  otherwise
  end
end { winchHandler } ;


function hookWinch(): boolean;

var
  action: SigActionRec;

begin
  FillChar(action{%H-}, SizeOf(action), 0);
  action.Sa_Handler := @winchHandler;
  action.Sa_Flags := SA_SIGINFO;
  hookWinch := fpSigAction(SIGWINCH, @action, nil) = 0
end { hookWinch } ;


begin
  WriteLn('This header line comprises 50 characters plus EOL.');
  WriteLn;
  WriteLn('Press key to terminate.'); // http://ars.userfriendly.org/cartoons/?id=20030128
  reportSize;
  if not hookWinch() then
    WriteLn('Failed: SIGWINCH not hooked, error ', fpGetErrNo)
  else begin
    while not KeyPressed() do
      Sleep(10);
    ReadKey
  end;
  WriteLn('It ends here.')
end.