http://www.perlmonks.org?node_id=11126647


in reply to word auto-corrector in Curses::UI

but unfortunately it doesn't work if I put this at the top of the script
$input = Complete('prompt_string', \@completion_list);

That's the wrong place. You probably must make the mainloop aware of tab completion via set_binding(), and handle the completion in a subroutine. Another candidate is add_callback().

Edit:

use Curses::UI; use Term::Complete; ... sub complete { my @words = ( 'perl', 'pepper', 'peace' ); # thanks marto! my $input = Complete('',\@words); $editor->add_string($input); } ... $cui->set_binding(\&complete,"\t");

Note that with this binding you have to press <Tab> twice, one to invoke sub complete, then for the completion itself.

You will have to get the substring upon which the completion was invoked and strip that from $input and handle the newline which ends the completion routine by repositioning the cursor. Or just do a screen redraw (bound to ^L).

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: word auto-corrector in Curses::UI
by Bpl (Scribe) on Jan 09, 2021 at 12:05 UTC
    Wow, nice! Honestly I never though to bind the completition function with Curses::UI directly, Many thanks! Regards, Edoardo Mantovani
      EDIT:
      Aww, small problem now, it doesn't work! unfortunately when I double click TAB seems that the program pass to another perlio layer and not to STDIN! infact when I try to click the upside buttons (given by Curses::UI), I obtain the printing of the following lines to the screen:
      [M %! [ M#%![ M% ecc..

      which seems to be malformed input strings, probably there must be some
      pack($input)
      solution
      Regards,
      Edoardo Mantovani

        The problem is that after the first <Tab> Curses::UI gives control of the terminal to Term::Complete. It acts directly on the terminal, and Curses::UI is not aware of what it does. It is not another perlio layer. Curses::UI is not aware of any keystrokes whilst in Term::Complete::Complete(). From the POD of Term::Complete:

        The tty driver is put into raw mode and restored using an operating system specific command, in UNIX-like environments "stty".

        So, Curses::UI and Term::Complete are not aware of each other. Term::Complete doesn't define up/down keys. It is tricky to integrate both...

        With the snippet as provided, <Tab> must be pressed before attempting a completion; then you type the chars of a word you intend to complete. Subseqent <Tab> presses advance the completion until it is unambiguous. Then you press <Return> - completion done. Refresh the screen with ^L.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'