App-Ordo

 view release on metacpan or  search on metacpan

lib/App/Ordo.pm  view on Meta::CPAN

    # Strip surrounding quotes
    @args = map { /^['"](.*)['"]$/ ? $1 : $_ } @args;
    return @args;
}

# ------------------------------------------------------------------
# Time formatting helpers
# ------------------------------------------------------------------
sub epoch_to_tminus {
    my $epoch = shift;
    return '' unless $epoch && $epoch =~ /^\d+$/;
    my $current_epoch = time;
    my $diff = $current_epoch - $epoch;
    return $diff < 60 ? "${diff}s ago"
         : $diff < 3600 ? int($diff/60) . "m ago"
         : $diff < 86400 ? int($diff/3600) . "h ago"
         : int($diff/86400) . "d ago";
}

sub epoch_to_duration {
    my ($start, $end) = @_;
    return '' unless $start;
    $end //= time;
    my $diff_seconds = abs($end - $start);
    my $days = int($diff_seconds / (24 * 60 * 60));
    $diff_seconds %= (24 * 60 * 60);
    my $hours = int($diff_seconds / (60 * 60));
    $diff_seconds %= (60 * 60);
    my $minutes = int($diff_seconds / 60);
    my $seconds = $diff_seconds % 60;
    my $duration = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
    if ($days) {
        $duration .= " +$days" . 'd';
    }
    return $duration;
}

# ------------------------------------------------------------------
# Pager (optional but useful)
# ------------------------------------------------------------------
sub less {
    my ($string) = @_;
    my @lines = split /\n/, $string;
    $_ .= "\n" for @lines;
    push @lines, "\n";
    my ($width, $height) = GetTerminalSize();
    $height -= 1;
    my $total_lines = @lines;
    if ($total_lines <= $height) {
        print for @lines;
        return;
    }
    my $current = 0;
    ReadMode 'cbreak';
    while (1) {
        system("clear");
        for my $i ($current .. $current + $height - 1) {
            last if $i >= $total_lines;
            print $lines[$i];
        }
        my $percent = int(($current + $height) / $total_lines * 100);
        printf "\033[7m(line %d of %d, %d%%) [q quit, space page down, b page up, j/k lines]\033[0m\n",
            $current + $height > $total_lines ? $total_lines : $current + $height,
            $total_lines, $percent;
        my $key = ReadKey(0);
        last if $key eq 'q' || $key eq 'Q';
        $current += $height if $key eq ' ';
        $current -= $height if $key eq 'b';
        $current++ if $key eq 'j' || $key eq "\n" || $key eq "\r";
        $current-- if $key eq 'k';
        $current = 0 if $current < 0;
        $current = $total_lines - $height if $current > $total_lines - $height;
    }
    ReadMode 'normal';
    print "\n";
}

1;



( run in 1.888 second using v1.01-cache-2.11-cpan-7fcb06a456a )