Spider | Previous | Next | Full Table of Contents | Index | Program List | Copyright
The Ada.Text_IO
package provides operations for
reading from the terminal keyboard and writing to the screen, but it
provides no direct operations for controlling the screen in
interesting ways, such as moving the cursor to a given row-column
position before writing. Doing this requires an additional package
which uses Ada.Text_IO
to send control
characters to the teminal; the control characters act as
instructions to it instead of data it should display. Because this
package, which we will call Screen
, is not part of
standard Ada, we provide it with this book. The details of just how
this package operates are left until Chapter 7, but it is possible
for you to use the package without understanding its innards.
A package consists of two files, the specification file and
the body file. As discussed in Section 3.6, the specification
gives the "contract with the user," or list of promised resources.
The body delivers the actual source code for the procedures and
functions promised by the specification. Because the input/output and
Ada.Calendar
packages are supplied in precompiled form
by all Ada compilers, we have seen only their specifications; the
source code for the bodies is not available to us. Other packages may
be supplied to you in source-code form, with both the specification
and body files provided. Screen
is one of these
packages.
Program 3.8 shows the specification for
Screen
.
Program 3.8
Specification for Screen Package
PACKAGE Screen IS ------------------------------------------------------------------------ --| Procedures for drawing pictures on ANSI Terminal Screen --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ -- constants; the number of rows and columns on the terminal Screen_Depth : CONSTANT Integer := 24; Screen_Width : CONSTANT Integer := 80; -- subtypes giving the ranges of acceptable inputs -- to the cursor-positioning operation SUBTYPE Depth IS Integer RANGE 1..Screen_Depth; SUBTYPE Width IS Integer RANGE 1..Screen_Width; PROCEDURE Beep; -- Pre: None -- Post: Terminal makes its beep sound once PROCEDURE ClearScreen; -- Pre: None -- Post: Terminal Screen is cleared PROCEDURE MoveCursor (Column : Width; Row : Depth); -- Pre: Column and Row have been assigned in-range values -- Post: Cursor is moved to the given spot on the screen END Screen;
This package provides two constants, Screen_Width
and Screen_Depth
, corresponding to the number of columns
(usually 80) and rows (usually 24) on the screen. There are also two
subtypes, Width
and Depth
, giving the
ranges for valid cursor positions (1..Screen_Depth
and
1..Screen_Width,
respectively).
The package provides three procedures. The first two, Beep
and ClearScreen
, take no parameters: A procedure
call statement
Screen.Beep;
causes the terminal to beep; a procedure call statement
Screen.ClearScreen;
causes the screen to go blank, erasing all previous information
from it. The last procedure, MoveCursor
, takes row and
column parameters, so that, for example,
Screen.MoveCursor (Row => 10, Column => 22); Text_IO.Put (Item => '*');
has the effect of displaying an asterisk in the location of row 10, column 22. Finally,
Screen.MoveCursor (Row => 5, Column => 10); Text_IO.Put (Item => "-----");
displays the string -----
in row 5, columns 10
through 14, inclusive.
Note the style of comments documenting each of these procedures. These are called preconditions and postconditions and are used to describe each procedure's assumptions and behavior in an informal but structured way. We'll come back to this subject in more detail in Chapter 6; meanwhile, you can get used to reading this style of documenting our packages.
Program 3.9 gives the body file for this package. You might not understand exactly how the procedures work. Don't worry about this right now; we will return to it in Chapter 7.
Program 3.9
Body of Screen Package
WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PACKAGE BODY Screen IS ------------------------------------------------------------------------ --| Body of screen-handling package --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ PROCEDURE Beep IS BEGIN Ada.Text_IO.Put (Item => ASCII.BEL); END Beep; PROCEDURE ClearScreen IS BEGIN Ada.Text_IO.Put (Item => ASCII.ESC); Ada.Text_IO.Put (Item => "[2J"); END ClearScreen; PROCEDURE MoveCursor (Column : Width; Row : Depth) IS BEGIN Ada.Text_IO.Put (Item => ASCII.ESC); Ada.Text_IO.Put ("["); Ada.Integer_Text_IO.Put (Item => Row, Width => 1); Ada.Text_IO.Put (Item => ';'); Ada.Integer_Text_IO.Put (Item => Column, Width => 1); Ada.Text_IO.Put (Item => 'f'); END MoveCursor; END Screen;
Ada's standard packages--the ones whose names begin with
Ada.
--come wity the Ada compiler and do not need to be
compiled. Before any package can be used by other programs, it must
be compiled. The specification must be compiled first, then the body.
To use the screen package, you must have a copy of the specification
and body files available in your computer's file system. If you do
not, you must type them in exactly as shown in
Programs 3.8 and 3.9,
then compile them both. If you subsequently modify the specification
file, you must recompile both it and the body, and all other programs
that use the package as well. If you do not modify either file, you
will not have to recompile it; your Ada compiler's library system
will keep it available for use with any program with the context
clause
WITH Screen;
As an example of the use of the screen package, consider Program 3.10, which first clears the screen, then beeps three times, then draws a "smiley face" in the center of the screen. After each beep, there is a statement
DELAY 0.1;
which causes the computer to wait 0.1 second before sending the next beep. This is done so that even on a very fast computer you will hear three distinct beeps.
Program 3.10
Smiley: A Program that Uses Package Screen
WITH Ada.Text_IO; WITH Screen; PROCEDURE Smiley IS ------------------------------------------------------------------------ --| Draws a "smiley face" in the center of the terminal screen --| Author: Michael B. Feldman, The George Washington University --| Last Modified: July 1995 ------------------------------------------------------------------------ BEGIN -- Smiley Screen.ClearScreen; Screen.Beep; DELAY 0.1; Screen.Beep; DELAY 0.1; Screen.Beep; DELAY 0.1; Screen.MoveCursor (Row => 7, Column => 34); Ada.Text_IO.Put (Item => "HAVE A NICE DAY!"); Screen.MoveCursor (Row => 9, Column => 39); Ada.Text_IO.Put (Item => "_____"); Screen.MoveCursor (Row => 10, Column => 37); Ada.Text_IO.Put (Item => "/ \"); Screen.MoveCursor (Row => 11, Column => 36); Ada.Text_IO.Put (Item => "/ \"); Screen.MoveCursor (Row => 12, Column => 35); Ada.Text_IO.Put (Item => "| |"); Screen.MoveCursor (Row => 13, Column => 35); Ada.Text_IO.Put (Item => "| O O |"); Screen.MoveCursor (Row => 14, Column => 36); Ada.Text_IO.Put (Item => "\ o /"); Screen.MoveCursor (Row => 15, Column => 37); Ada.Text_IO.Put (Item => "\ \___/ /"); Screen.MoveCursor (Row => 16, Column => 38); Ada.Text_IO.Put (Item => "\ /"); Screen.MoveCursor (Row => 17, Column => 39); Ada.Text_IO.Put (Item => "-----"); Screen.MoveCursor (Row => 24, Column => 1); END Smiley;
Sample Run
HAVE A NICE DAY! _____ / \ / \ | | | O O | \ o / \ \___/ / \ / -----
There is one more thing you need
to know about Screen
. Even though all Ada compilers
support the same Ada language, not all Ada programs can show correct
output on all terminals because different kinds of terminals have
different characteristics. This package assumes that the terminal you
are using responds to ANSI control sequences. Most UNIX and VMS
terminals do. So does an IBM-PC or compatible computer, provided that
the ANSI.SYS device driver is listed in the computer's CONFIG.SYS
file. If you run Smiley
but your screen does not look
like the sample run, see your computer center or teacher or, if you
are using your own PC-compatible, check whether ANSI.SYS is properly
installed.
Copyright ©1996 by Addison-Wesley Publishing Company, Inc.