Antsy

 view release on metacpan or  search on metacpan

examples/subtasks.pl  view on Meta::CPAN

#!perl
use v5.38;
use utf8;

use FindBin;
use lib "$FindBin::Bin/../lib";

use Time::HiRes qw(sleep);
use Antsy 0.908 qw( cursor_hide cursor_show );

package Subtask {
	use Antsy 0.908 qw(
		bold faint text_green text_yellow text_red reset_all
		cursor_previous_line cursor_line_start
		erase_line erase_to_end_of_screen
		);

	my %STYLE = (
		running => [ 'â–¸', \&bold        ],
		ok      => [ '✔', \&text_green  ],
		warn    => [ '!', \&text_yellow ],
		fail    => [ '✘', \&text_red    ],
	);

	sub new ( $class, %args ) {
		my $self = bless {
			name   => $args{name},
			keep   => $args{keep} // 5,
			tty    => $args{tty},
			width  => $args{width} // 80,
			window => [],
		}, $class;
		$self->_header('running');
		return $self;
	}

	# Add an output line. On a terminal, redraw the window of the last
	# "keep" lines in place; otherwise just print the line.
	sub line ( $self, $text ) {
		unless( $self->{tty} ) {
			say "    $text";
			return;
		}

		my $window = $self->{window};
		my $shown  = @$window;
		push @$window, $text;
		shift @$window while @$window > $self->{keep};

		# Go back up to the first row of the window. When nothing is shown
		# yet, cursor_previous_line(0) just goes to the start of this row.
		print cursor_previous_line( $shown );
		print erase_line, faint, $self->_fit("    $_"), reset_all, "\n"
			for @$window;
	}

	# Collapse the window and redraw the header with its final status.
	sub finish ( $self, $status, $message = undef ) {
		if( $self->{tty} ) {
			my $up = @{ $self->{window} } + 1;
			print cursor_previous_line( $up ), erase_to_end_of_screen;
		}
		$self->{window} = [];

examples/subtasks.pl  view on Meta::CPAN

	192.0.2.101 - - [18/Oct/2026:09:14:18 -0400] "POST /api/cart HTTP/1.1" 500 128
	203.0.113.7 - - [18/Oct/2026:09:14:20 -0400] "GET /books/intermediate-perl HTTP/1.1" 200 21877
	203.0.113.7 - - [18/Oct/2026:09:14:21 -0400] "GET /img/cover-ip2.jpg HTTP/1.1" 200 131005
	198.51.100.23 - - [18/Oct/2026:09:14:24 -0400] "GET /checkout HTTP/1.1" 302 0
	198.51.100.23 - - [18/Oct/2026:09:14:25 -0400] "GET /login HTTP/1.1" 200 4410
	192.0.2.44 - - [18/Oct/2026:09:14:29 -0400] "GET /admin HTTP/1.1" 403 256
	203.0.113.90 - - [18/Oct/2026:09:14:31 -0400] "GET /robots.txt HTTP/1.1" 200 68
	198.51.100.8 - - [18/Oct/2026:09:14:33 -0400] "GET /books/mastering-perl HTTP/1.1" 200 20993
	192.0.2.101 - - [18/Oct/2026:09:14:36 -0400] "POST /api/cart HTTP/1.1" 200 932
	203.0.113.7 - - [18/Oct/2026:09:14:40 -0400] "GET /sitemap.xml HTTP/1.1" 200 2544
	198.51.100.23 - - [18/Oct/2026:09:14:42 -0400] "POST /login HTTP/1.1" 200 610
	LOG
}

sub sample_books () {
	(
		[ '9780596000271', 'The Lighthouse Keeper\'s Ledger' ],
		[ '9781492032137', 'Patterns in Quiet Water' ],
		[ '9781934356172', 'A Field Guide to Small Machines' ],
		[ '9781118119372', 'Twelve Bridges North' ],
		[ '9780321784148', 'The Cartographer\'s Apprentice' ],
		[ '9780981653907', 'Notes from the Night Ferry' ],
		[ '9780307610025', 'An Almanac of Tides' ],
		[ '9781558607231', 'Practical Clockwork' ],
		[ '9780865471269', 'The Orchard at Wexley' ],
		[ '9780201616224', 'Letters to a Young Engineer' ],
		[ '9781843560012', 'Salt and Signal' ],
		[ '9780596520687', 'The Long Way Round the Lake' ],
	);
}

# The Gettysburg Address (public domain).
sub sample_text () {
	<<~'TEXT';
	Four score and seven years ago our fathers brought forth on this
	continent, a new nation, conceived in Liberty, and dedicated to the
	proposition that all men are created equal. Now we are engaged in a
	great civil war, testing whether that nation, or any nation so conceived
	and so dedicated, can long endure. We are met on a great battle-field of
	that war. We have come to dedicate a portion of that field, as a final
	resting place for those who here gave their lives that that nation
	might live. It is altogether fitting and proper that we should do this.
	But, in a larger sense, we can not dedicate, we can not consecrate, we
	can not hallow, this ground. The brave men, living and dead, who
	struggled here, have consecrated it, far above our poor power to add or
	detract. The world will little note, nor long remember what we say
	here, but it can never forget what they did here. It is for us the
	living, rather, to be dedicated here to the unfinished work which they
	who fought here have thus far so nobly advanced. It is rather for us to
	be here dedicated to the great task remaining before us, that from
	these honored dead we take increased devotion to that cause for which
	they gave the last full measure of devotion, that we here highly
	resolve that these dead shall not have died in vain, that this nation,
	under God, shall have a new birth of freedom, and that government of the
	people, by the people, for the people, shall not perish from the earth.
	TEXT
}

__END__

=encoding utf8

=head1 NAME

subtasks.pl - show subtasks with persistent headers and scrolling output

=head1 SYNOPSIS

	$ perl examples/subtasks.pl

=head1 DESCRIPTION

Runs three sample subtasks one after the other, using sample data built
into the program, and draws all of its terminal output with L<Antsy>:

=over 4

=item * Parse web server log - parse an Apache-style access log

=item * Validate ISBNs - check ISBN-13 check digits for a list of books

=item * Count word frequency - tally words in the Gettysburg Address

=back

Each subtask prints a header line. While it runs, only its last few
output lines show under the header, and older lines scroll off. When it
finishes, those lines are erased and the header updates with a status
symbol, color, and summary: green for success, yellow for warnings, red
for failure. The next subtask's header appears directly below.

When output isn't a terminal, the program prints every header and line
as plain text with no cursor movement or color.

This started as a demonstration program in a separate project, which
also has versions that use L<Term::ANSIColor> and hand-written escape
sequences.

=head1 REQUIREMENTS

This needs Antsy 0.908 or later, which has the functions this program
uses, such as C<erase_to_end_of_screen> and C<cursor_line_start>, and
fixes C<cursor_previous_line(0)>. It loads the Antsy in this
distribution's F<lib> directory, so run it from a checkout of Antsy.
With an older Antsy, the program stops right away with Perl's
"version 0.908 required" error.

=head1 HOW THE OUTPUT WORKS

The program never uses a full-screen library such as curses. It prints
ordinary lines and uses a few Antsy functions to move back up over lines
it has already printed and redraw them in place:

=over 4

=item C<cursor_previous_line(N)>

Move the cursor up N rows, to the start of the row. C<cursor_previous_line(0)>
only moves to the start of the current row, which is what the program
needs before anything is shown under a header.



( run in 0.769 second using v1.01-cache-2.11-cpan-234fa220139 )