App-ZFSCurses

 view release on metacpan or  search on metacpan

lib/App/ZFSCurses/UI/Snapshots.pm  view on Meta::CPAN

The App::ZFSCurses::UI::Snapshots module is in charge of drawing a list of ZFS
snapshots.

=cut

=head1 METHODS

=head2 new

Create an instance of App::ZFSCurses::UI.

=cut

sub new {
    my $class = shift;
    return bless {}, $class;
}

=head2 draw_and_run

Draw and run the UI. Also perform a couple of checks before actually running
the UI which are: 1) checking whether the 'zfs' command is actually installed,
2) checking whether the user is root (required for tweaking properties).

=cut

sub draw_and_run {
    my $self = shift;

    if ( $backend->is_zfs_installed() eq -1 ) {
        $cui->error(
            -message => $text->no_zfs_command_found(),
            -title   => $text->title(),
            -tfg     => 'red',
            -fg      => 'red',
            -buttons => [ { -label => '[ Exit ]' } ]
        );

        exit(-1);
    }

    if ( $< ne 0 ) {
        my $return = $cui->error(
            -message => $text->non_root_user(),
            -title   => $text->title(),
            -tfg     => 'red',
            -fg      => 'red',
            -buttons =>
              [ { -label => '[ Continue anyway ]' }, { -label => '[ Exit ]' } ]
        );

        exit(0) if $return eq 1;
    }

    my $main = $cui->add( 'main', 'Window' );

    my $header = $main->add(
        'header', 'Label',
        -text          => $text->title_snapshots(),
        -textalignment => 'left',
        -bold          => 1,
        -fg            => 'white',
        -bg            => 'blue',
        -y             => 0,
        -width         => -1,
        -paddingspaces => 1,
    );

    $main_window = $cui->add(
        'main_window', 'Window',
        -padtop    => 2,
        -padbottom => 3
    );

    $top_label = $main_window->add(
        'top_label', 'Label',
        -text    => $text->top_label_snapshots(),
        -padleft => 1,
        -fg      => 'blue',
        -width   => -1
    );

    draw_main_listbox();
    draw_buttons();

    $footer = $main->add(
        'footer', 'Label',
        -text          => $text->footer(),
        -textalignment => 'left',
        -bold          => 1,
        -bg            => 'black',
        -fg            => 'white',
        -y             => -1,
        -width         => -1,
        -paddingspaces => 1,
    );

    $header->draw();
    $footer->draw();

    $main_window->draw();
    $main_window->focus();

    $cui->set_binding( \&exit_dialog, "\cQ" );
    $cui->mainloop;
}

=head2 exit_dialog

Callback when Ctrl+q is pressed. Draw a dialog to ask user for confirmation if
he wants to quit the program.

=cut

sub exit_dialog {
    my $return = $cui->dialog(
        -message => $text->exit_dialog(),
        -tfg     => 'red',
        -fg      => 'red',
        -buttons => [ { -label => '[ Yes ]' }, { -label => '[ No ]' } ]
    );

    exit(0) if $return eq 0;
}

=head2 show_help

Callback when F1 is pressed. Draw a dialog to print info about the selected
property.

=cut

sub show_help {
    my $selected = $main_listbox->get;

    return if !defined $selected;
    chomp $selected;

    return if $selected =~ m/^(PROPERTY)/;

    my ( $property, undef, undef ) = split /\s+/, $selected;
    my $help_message = $text->help_messages()->{$property};

    my $dialog_msg;
    if ( defined $help_message and ( $help_message ne '' ) ) {
        $dialog_msg = $help_message;
    }
    else {
        $dialog_msg = $text->no_help_found($property);
    }



( run in 0.811 second using v1.01-cache-2.11-cpan-39bf76dae61 )