comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: wait does not perform as expected
Date: Wed, 22 Feb 2023 19:36:03 +0200	[thread overview]
Message-ID: <k5n243Fe43nU1@mid.individual.net> (raw)
In-Reply-To: <ac87b5c5-7c01-4af9-b9bf-8c3ffc3fe74dn@googlegroups.com>

On 2023-02-22 18:34, Daniel Gaudry wrote:
> hi
> the following code :


(I include only the relevant parts:)


>      while Timer < Skip_After loop
>         Timer := 1.0 + @;
>         delay 1.0;
>         ada.text_io.Put(Natural(Skip_After - Timer)'Img);
> 
>         --USER ENDS THE WAITING PERIOD  BEFORE IT'S END ?
> 
> 
>          TIO.LOOK_AHEAD(ITEM          => CHAR,
>                         END_OF_LINE   => HIT); >
>
>          ada.text_io.GET_IMMEDIATE(ITEM      => CHAR,
>                                    AVAILABLE => HIT);
>          IF HIT THEN
>            RETURN;
>          END IF;
> 
>        end loop;

Why do you call both Look_Ahead and Get_Immediate, but throw away the 
result of Look_Ahead?

The behaviour may depend on how you configure (via OS calls) the 
behaviour of your terminal emulator (the "shell window"), but for me, on 
Mac OS/X with the default configuration, it behaves as follows.

The first time Look_Ahead is called (first loop iteration) it waits for 
the user to press Return (Enter) before it delivers the look-ahead 
character to the program. It must do so because it must see if the next 
input is an end-of-line (Return/Enter) or a character (assuming there is 
no "unconsumed" input when the loop starts).

The program is therefore suspended in the first Look_Ahead call, and the 
timer loop and remaining-time output are also suspended, until you at 
least press Return/Enter.

If you do press Return/Enter, perhaps after pressing some other keys, 
the waiting Look_Ahead call returns either the (first) character you 
pressed before Return/Enter, or End_Of_Line (Hit is True). However, your 
code then throws those results away, and instead calls Get_Immediate.

On my system, Get_Immediate does /not/ use the inputs collected (but not 
"consumed") by Look_Ahead, but instead checks if there are _new_ input 
keystrokes. If there are none, Get_Immediate returns "not Available" 
(Hit is False) and so the loop continues.

When Look_Ahead is called again, in the further loop iterations, it sees 
that there now is unconsumed input (from the first call of Look_Ahead) 
so it returns immediately (with the same results as on the first call). 
Therefore the loop now works are you intend: if the user presses any key 
before the Skip_After time is exhausted, the subprogram returns when 
Get_Immediate sees that keystroke.

To fix the problem, remove the Look_Ahead call from the loop.

(For goodness sake, also clean up your "with" clauses -- only 
Ada.Text_IO is relevant for your code -- and remove the multiple renames 
of Ada.Text_IO. You seem to have at least "Atio" and "TIO" as renames 
for Ada.Text_IO. One would be plenty.)

  reply	other threads:[~2023-02-22 17:36 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-22 16:34 wait does not perform as expected Daniel Gaudry
2023-02-22 17:36 ` Niklas Holsti [this message]
2023-02-23 14:26   ` AdaMagica
2023-02-23 14:32     ` AdaMagica
2023-02-23 14:33     ` Daniel Gaudry
2023-02-23 14:39       ` AdaMagica
2023-02-23 17:15       ` Niklas Holsti
2023-02-23 17:35     ` Niklas Holsti
2023-02-23 17:49       ` Niklas Holsti
2023-02-23 18:14       ` Dmitry A. Kazakov
2023-02-23 18:29         ` Niklas Holsti
2023-02-23 18:47           ` Daniel Gaudry
2023-02-23 19:08             ` Niklas Holsti
2023-02-23 19:31             ` Niklas Holsti
2023-02-23 20:41           ` Dmitry A. Kazakov
2023-02-24 15:10             ` AdaMagica
2023-02-24 18:23               ` Niklas Holsti
2023-02-24 21:16 ` Jeffrey R.Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox