From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a83ac04244a76ea9 X-Google-Attributes: gid103376,public From: Bill Eriksson Subject: Re: Terminal IO, and menus Date: 1999/04/01 Message-ID: <37039EF8.A8617315@bigfoot.com> X-Deja-AN: 461597816 X-NNTP-Posting-Host: 62.20.137.122 Content-Transfer-Encoding: 7bit References: <7drb1p$48k$1@nw001t.infi.net> Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@internet.telia.com Organization: Telia Internet Services Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-04-01T00:00:00+00:00 List-Id: As an alternative, you may use this (Compiled it from some Ada text-books): package Screen is --------------- -- Types --------------- type Mode_Type is (Text, Ansi); type Text_Attribute_Type is (All_Off, Bold, Blinking, Inverse, Hidden); type Foreground_Color_Type is (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White); type Background_Color_Type is (Black, Red, Green, Yellow, Blue, Magenta, Cyan, White); -------------------------------------- -- Enumeration Representation Clauses: -------------------------------------- for Text_Attribute_Type'Size use Integer'Size; for Text_Attribute_Type use (All_Off => 0, Bold => 1, Blinking => 5, Inverse => 7, Hidden => 8); --Colors according to Standard ISO 6429 : for Foreground_Color_Type use (Black => 30, Red => 31, Green => 32, Yellow => 33, Blue => 34, Magenta => 35, Cyan => 36, White => 37); for Background_Color_Type use (Black => 40, Red => 41, Green => 42, Yellow => 43, Blue => 44, Magenta => 45, Cyan => 46, White => 47); ------------------------------------------------------------------- procedure Set_Size(Max_Row, Max_Col : in Integer); -- -- Purpose - Sets Screen Size. -- Default Size: Max_Row :=24, Max_Col :=80 -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- function Get_Max_Row return Integer; -- -- Purpose - Gets Max_Row. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- function Get_Max_Col return Integer; -- -- Purpose - Gets Max_Col. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Set_Mode(To : in Mode_Type); -- -- Purpose - Sets Screen Control Mode. -- Default Mode: Text -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- function Get_Mode return Mode_Type; -- -- Purpose - Gets Actual Screen Control Mode. -- -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Clear; -- -- Purpose - Clear Screen -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Put(Text: in String); -- -- Purpose - Writes a String at actual position on Screen. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Put_Line(Text: in String); -- -- Purpose - Writes a String at actual position on Screen and -- then executes New_Line. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Position(In_row,In_col : in Integer); -- -- Purpose - Places the Cursor to specified position. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Erase_To_Eol; -- -- Purpose - Erases from actual position to end of line. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Bell; -- -- Purpose - Makes the "beep" sound. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Set_Text_Attribute( To: Text_Attribute_Type); -- -- Purpose - Sets Text Attributes. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Set_Color( Foreground : Foreground_Color_Type; Background : Background_Color_Type); -- -- Purpose - Sets Color Attributes. -- -- Exceptions - None -- Others - None ------------------------------------------------------------------- end Screen; with Text_Io; with Unchecked_Conversion; package body Screen is -------------------------- -- Types -------------------------- type Screen_Type is record Max_Row : Integer; Max_col : Integer; Terminal_Mode : Mode_Type; end record; -------------------------- -- Constants -------------------------- Terminal_Clear : constant String := Ascii.Esc & "[2J"; Terminal_Bell : constant String := " " & Ascii.Bel; Erase_To_End_Of_Line : constant String := Ascii.Esc & "[K"; -------------------------- -- Variables -------------------------- Scr : Screen_Type := (24, 80, Text); -- Default -------------------------- -- Instantiations -------------------------- function Text_Attribute_To_Integer is new Unchecked_Conversion(Text_Attribute_Type, Integer); function Fg_Color_To_Integer is new Unchecked_Conversion(Foreground_Color_Type, Integer); function Bg_Color_To_Integer is new Unchecked_Conversion(Background_Color_Type, Integer); ------------------------------------------------------------------- procedure Set_Size(Max_Row, Max_Col : in Integer) is ------------------------------------------------------------------- begin Scr.Max_Row := Max_Row; Scr.Max_Col := Max_Col; end Set_Size; ------------------------------------------------------------------- ------------------------------------------------------------------- function Get_Max_Row return Integer is ------------------------------------------------------------------- begin return Scr.Max_Row; end Get_Max_Row; ------------------------------------------------------------------- ------------------------------------------------------------------- function Get_Max_Col return Integer is ------------------------------------------------------------------- begin return Scr.Max_Col; end Get_Max_Col; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Set_Mode(To : in Mode_Type ) is ------------------------------------------------------------------- begin Scr.Terminal_Mode := To; end Set_Mode; ------------------------------------------------------------------- ------------------------------------------------------------------- function Get_Mode return Mode_Type is ------------------------------------------------------------------- begin return Scr.Terminal_Mode; end Get_Mode; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Clear is ------------------------------------------------------------------- begin -- Clear if Scr.Terminal_Mode = Ansi then Text_Io.Put(Terminal_Clear); else Text_Io.New_Page; end if; end Clear; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Put(Text: in String) is ------------------------------------------------------------------- begin -- Put Text_Io.Put(Text); end Put; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Put_Line(Text: in String) is ------------------------------------------------------------------- begin -- Put_Line Text_Io.Put_Line(Text); end Put_Line; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Position(In_Row, In_Col: in Integer) is ------------------------------------------------------------------- begin if Scr.Terminal_Mode = Ansi then Text_Io.Put(Ascii.Esc & '[' & Integer'Image(In_Row)(2..Integer'Image(In_Row)'Last) & ';' & Integer'Image(In_Col)(2..Integer'Image(In_Col)'Last) & 'H'); else null; -- Text_Io's Set_Line and Set_Col will not solve this end if; end Position; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Erase_To_Eol is ------------------------------------------------------------------- begin if Scr.Terminal_Mode = Ansi then Text_Io.Put(Erase_To_End_Of_Line); else null; end if; end Erase_To_Eol; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Bell is ------------------------------------------------------------------- begin -- Will work in any mode ? Text_Io.Put(Terminal_Bell); end Bell; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Set_Text_Attribute(To: Text_Attribute_Type) is ------------------------------------------------------------------- Attr : constant String := Integer'Image(Text_Attribute_To_Integer(To)); begin if Scr.Terminal_Mode = Ansi then Text_Io.Put(Ascii.Esc & "[" & Attr(2..Attr'Last) & "m"); else null; end if; end Set_Text_Attribute; ------------------------------------------------------------------- ------------------------------------------------------------------- procedure Set_Color(Foreground: Foreground_Color_Type; Background: Background_Color_Type) is ------------------------------------------------------------------- Fg : constant String := Integer'Image(Fg_Color_To_Integer(Foreground)); Bg : constant String := Integer'Image(Bg_Color_To_Integer(Background)); begin if Scr.Terminal_Mode = Ansi then Text_Io.Put(Ascii.Esc & "[" & Fg(2..Fg'Last) & ";" & Bg(2..Bg'Last) & "m"); else null; end if; end Set_Color; ------------------------------------------------------------------- end Screen; Matthew Heaney skrev: > "Heath Isler" writes: > > > My question is is there a way in the stadard packages to Clear the Screen > > and postition the cursor in a specific location, i.e. Locate_Cursor (x => > > 10, y =>15); ? I am plan on using this for menus and data input. > > No, there's nothing in the Ada predefined packages to do that. > > > > I have also looked through a lot of packages on the net, and some of > > them seem to fit my need. I not sure if I should use one of the > > Terminal IO or menu packages. Can anyone recommend a package to use? > > In the GNAT source distribution, the dining philosophers example does > terminal I/O using escape sequences. > > --:::::::::: > --screen.ads > --:::::::::: > package Screen is > > -- simple ANSI terminal emulator > -- Michael Feldman, The George Washington University > -- July, 1995 > > ScreenHeight : constant Integer := 24; > ScreenWidth : constant Integer := 80; > > subtype Height is Integer range 1..ScreenHeight; > subtype Width is Integer range 1..ScreenWidth; > > type Position is record > Row : Height := 1; > Column: Width := 1; > end record; > > procedure Beep; > -- Pre: none > -- Post: the terminal beeps once > > procedure ClearScreen; > -- Pre: none > -- Post: the terminal screen is cleared > > procedure MoveCursor (To: in Position); > -- Pre: To is defined > -- Post: the terminal cursor is moved to the given position > > end Screen; > > --:::::::::: > --screen.adb > --:::::::::: > with Text_IO; > package body Screen is > > -- simple ANSI terminal emulator > -- Michael Feldman, The George Washington University > -- July, 1995 > > -- These procedures will work correctly only if the actual > -- terminal is ANSI compatible. ANSI.SYS on a DOS machine > -- will suffice. > > package Int_IO is new Text_IO.Integer_IO (Num => Integer); > > procedure Beep is > begin > Text_IO.Put (Item => ASCII.BEL); > end Beep; > > procedure ClearScreen is > begin > Text_IO.Put (Item => ASCII.ESC); > Text_IO.Put (Item => "[2J"); > end ClearScreen; > > procedure MoveCursor (To: in Position) is > begin > Text_IO.New_Line; > Text_IO.Put (Item => ASCII.ESC); > Text_IO.Put ("["); > Int_IO.Put (Item => To.Row, Width => 1); > Text_IO.Put (Item => ';'); > Int_IO.Put (Item => To.Column, Width => 1); > Text_IO.Put (Item => 'f'); > end MoveCursor; > > end Screen;