comp.lang.ada
 help / color / mirror / Atom feed
From: "Björn Persson" <spam-away@nowhere.nil>
Subject: Re: Cursor control question - ncurses and alternatives
Date: Fri, 13 Oct 2006 22:41:52 GMT
Date: 2006-10-13T22:41:52+00:00	[thread overview]
Message-ID: <QCUXg.19847$E02.7732@newsb.telia.net> (raw)
In-Reply-To: <pan.2006.10.13.16.30.06.922066@linuxchip.demon.co.uk.uk.uk>

Dr. Adrian Wrigley wrote:
> At the most basic level, I want to move the cursor to the start
> of the line, update the prompt, and move the cursor back to
> where it was.  How can I do this?

While there are escape sequences for querying, saving and restoring the 
cursor position, I think you'll get the best result if you know what's 
in the command buffer so that you can update the entire command line. 
That means you'll have to do the line editing yourself. It could look 
something like this:

with Text_IO;
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure TimDat is

    Count : Integer := 1000;

    Message : String := "Hello";

    Buffer : Unbounded_String;

    procedure Update is
    begin
       Text_IO.Put (ESC & "[2K" & CR & Integer'Image (Count) & " - " &
                    Message & " > " & To_String(Buffer));
    end Update;

    task ShowClock;

    task body ShowClock is
    begin
       loop
          Count := Count + 1;
          Update;
          delay 1.0;
       end loop;
    end ShowClock;

    Keypress : Character;

begin

    loop
       Text_IO.Get_Immediate (Keypress);
       case Keypress is
          when DEL =>
             if Length (Buffer) > 0 then
                -- Delete the last character.
                Head (Buffer, Length (Buffer) - 1);
                Update;
             end if;
          when ESC =>
             -- Handle arrow keys and stuff.
             null;
          when LF | CR =>
             Text_IO.New_Line;
             Text_IO.Put_Line ("Command was """ & To_String(Buffer) &
                               """.");
             Head (Buffer, 0);  -- reset
             Update;
          -- and so on for other special characters
          when others =>
             Append (Buffer, Keypress);
             Update;
       end case;
    end loop;

end TimDat;

There are most likely race conditions in that code, but I'm sure you can 
fix that. You'll also need to decide what to do when the command line 
gets so long that it wraps. To know whether it wraps you need to find 
out how wide the window is.

For advanced line editing you may want to look at the Readline library � 
at least for inspiration.

-- 
Bj�rn Persson                              PGP key A88682FD
                    omb jor ers @sv ge.
                    r o.b n.p son eri nu



  parent reply	other threads:[~2006-10-13 22:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-13 16:32 Cursor control question - ncurses and alternatives Dr. Adrian Wrigley
2006-10-13 18:16 ` Dmitry A. Kazakov
2006-10-13 21:13 ` Jeffrey R. Carter
2006-10-13 22:41 ` Björn Persson [this message]
2006-10-14  2:38   ` Dr. Adrian Wrigley
2006-10-14 12:53     ` Dr. Adrian Wrigley
replies disabled

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