Search code examples
perltui

Perl print line over Prompt


My script asks for download URLs and sends them to the download queue. The progress of the download should be printed back.

I don't find a way to keep the prompt on bottom and do the status over it. I tried a search on CPAN, but I found no module for it.

#!/usr/bin/perl
use 5.14.0;
use strict;
use warnings;
use Term::UI;
use Term::ReadLine;
use threads;
use Thread::Queue;

sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] }

my $q = Thread::Queue->new(); # A new empty queue
my $thr = threads->create(
  sub {
    while (defined(my $item = $q->dequeue())) {
      say "Downloading: ".$item;
      sleep 1;
      #$q->enqueue(1..10) if $item eq '10';
      $q->enqueue(rndStr rand (15)+5, 'a'..'z', 0..9);
    }
  }
);

$q->enqueue(rndStr 10, 'a'..'z', 0..9);
my $url;
my $term = Term::ReadLine->new('brand');
while ($url ne 'end'){
  $url = $term->get_reply( 
      prompt => 'URL to download',
      default => 'end' );
  $q->enqueue($url);
}
say "Finishing remaining downloads";
$q->enqueue(undef);
$thr->join();

Solution

  • The basic just of what you are trying to do is use ANSI codes to move the cursor around. Something such as ncurses (windows version) will allow you do this.

    Alternatively you can do it yourself with raw ASCII/ANSI codes (as explained by these two links)

    Or lastly you could use a Perl Module Win32::Console::ANSI which is designed to help you do this.

    As this is a perl question I would suggest looking at Win32::Console::ANSI.