App-VTide

 view release on metacpan or  search on metacpan

lib/App/VTide/Command/Run.pm  view on Meta::CPAN

package App::VTide::Command::Run;

# Created on: 2016-01-30 15:06:40
# Create by:  Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use Moo;
use warnings;
use version;
use Carp                qw/carp longmess/;
use English             qw/ -no_match_vars /;
use Hash::Merge::Simple qw/ merge /;
use Path::Tiny;
use File::stat;
use File::chdir;
use IO::Prompt qw/prompt/;
use Algorithm::Cron;
use List::MoreUtils qw/uniq/;

extends 'App::VTide::Command';

our $VERSION = version->new('1.0.6');
our $NAME    = 'run';
our $OPTIONS = [ 'name|n=s', 'test|T!', 'save|s=s', 'verbose|v+', ];
our $LOCAL   = 1;
sub details_sub { return ( $NAME, $OPTIONS, $LOCAL ) }

has first => (
    is      => 'rw',
    default => 1,
);

has base => ( is => 'rw', default => $CWD );

sub run {
    my ($self) = @_;

    my ($name) = $self->session_dir( $self->defaults->{name} );
    my $cmd = $self->options->files->[0] || '';
    $ENV{VTIDE_TERM} = $cmd;

    my $params = $self->params($cmd);
    my @cmd    = $self->command($params);
    $self->log( 'START', @cmd );

    @ARGV = ();
    if (
        !(
               $self->first
            && ( $params->{watch} || $params->{cron} )
            && $params->{wait}
        )
      )
    {

        if ( $params->{clear} ) {
            system 'clear';
        }
        if ( $self->first ) {
            print "Running $name - $cmd\n";
        }

        if ( $params->{heading} ) {

            # show terminal heading if desired
            print $params->{heading}, "\n";
        }

        if ( !$self->defaults->{test} && $params->{wait} ) {
            print join ' ', @cmd, "\n";
            print "Press enter to start : ";
            my $ans = <ARGV>;
            if ( !$ans || !ord $ans ) {
                print "\n";
                return;
            }

lib/App/VTide/Command/Run.pm  view on Meta::CPAN

        return 1 if $done;

        # return if asked to quit
        return if !defined $done;

        if ( $params->{cron_verbose} ) {
            print {*STDERR} "\33[2K\r" . pretty_time( $next_time - time );
        }
        if ( $next_time <= time ) {
            if ( $params->{cron_verbose} ) {
                print {*STDERR} "\33[2K\r";
            }
            return 1;
        }
    }

    return;
}

sub pretty_time {
    my ($time) = @_;

    my $pretty  = '';
    my $days    = int $time / ( 24 * 60 * 60 );
    my $hours   = int $time / ( 60 * 60 ) - $days * 24;
    my $minutes = int $time / 60 - $days * 24 * 60 - $hours * 60;
    my $seconds = $time % 60;

    return "$days days $hours hours $minutes minutes $seconds seconds";
}

sub params {
    my ( $self, $cmd ) = @_;

    if ( !$cmd ) {
        warn "No \$cmd passed to params()\n", longmess();
    }

    my $config = $self->config->get;
    my $params = $config->{terminals}{$cmd} || {};

    if ( ref $params eq 'ARRAY' ) {
        $params = { command => @{$params} ? $params : '' };
    }

    if ( !$params->{command} && !$params->{edit} ) {
        $params->{command} = 'bash';
        $params->{wait}    = 0;
    }

    return merge $config->{default} || {}, $params;
}

sub command_param {
    my ( $self, $param ) = @_;

    my ($user_param) = $param =~ /^[{]:(\w+):[}]$/;

    return $param if !$user_param;

    my $value = prompt "$user_param : ";
    chomp $value;

    return $value;
}

sub command {
    my ( $self, $params, $recurse ) = @_;

    if ( !$params->{edit} ) {
        return
          ref $params->{command}
          ? map { $self->command_param($_) } @{ $params->{command} }
          : ( $params->{command} );
    }

    my $editor =
      ref $params->{editor}{command}
      ? $params->{editor}{command}
      : $self->config->get->{editor}{command};

    my @globs =
      ref $params->{edit} ? @{ $params->{edit} } : ( $params->{edit} );

    my $title = $params->{title} || $globs[0];
    my $max   = 15;
    if ( length $title > $max ) {
        $title = substr $title, ( length $title ) - $max, $max + 1;
    }

    eval { require Term::Title; }
      and Term::Title::set_titlebar($title);
    system 'tmux', 'rename-window', $title;

    my $helper_text = $self->config->get->{editor}{helper};
    my $helper;
    eval {
        if ($helper_text) {
            $helper = eval $helper_text;    ## no critic
            die "No helper generated!" if !$helper;
        }
        elsif ( $self->defaults->{verbose} ) {
            warn "No helper text";
        }
        1;
    } or do {
        warn $@;
        warn $helper_text;
    };

    my $groups = $self->config->get->{editor}{files};
    my @files  = $self->_globs2files( $groups, $helper, $recurse, @globs );

    return ( @$editor, @files );
}

sub _globs2files {
    my ( $self, $groups, $helper, $recurse, @globs ) = @_;
    my @files;
    my $count = 0;



( run in 1.353 second using v1.01-cache-2.11-cpan-6aa56a78535 )