| 1 | package Yakuake::Sessions::TraitFor::Management; | ||
| 2 | |||
| 3 | use namespace::autoclean; | ||
| 4 | |||
| 5 | use Class::Usul::Constants qw( EXCEPTION_CLASS FAILED OK SPC TRUE ); | ||
| 6 | use Class::Usul::Functions qw( emit throw ); | ||
| 7 | use File::DataClass::Types qw( NonEmptySimpleStr ); | ||
| 8 | use Unexpected::Functions qw( PathNotFound ); | ||
| 9 | use Moo::Role; | ||
| 10 | use Class::Usul::Options; | ||
| 11 | |||
| 12 | requires qw( dump dumper extra_argv file load loc | ||
| 13 | profile_dir profile_path run_cmd ); | ||
| 14 | |||
| 15 | # Object attributes (public) | ||
| 16 | option 'editor' => is => 'lazy', isa => NonEmptySimpleStr, | ||
| 17 | documentation => 'Which text editor to use', | ||
| 18 | default => sub { $_[ 0 ]->config->editor }, format => 's'; | ||
| 19 | |||
| 20 | # Public methods | ||
| 21 | sub create : method { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | unshift @{ $self->extra_argv }, $self->profile_path->assert_filepath; | ||
| 25 | |||
| 26 | return $self->dump; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub delete : method { | ||
| 30 | my $self = shift; my $path = $self->profile_path; | ||
| 31 | |||
| 32 | $path->exists or throw class => PathNotFound, args => [ $path ]; | ||
| 33 | $path->unlink; | ||
| 34 | return OK; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub edit : method { | ||
| 38 | my $self = shift; my $path = $self->profile_path; | ||
| 39 | |||
| 40 | $self->run_cmd( $self->editor.SPC.$path, { async => TRUE, } ); | ||
| 41 | return OK; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub list : method { | ||
| 45 | my $self = shift; my @suffixes = @{ $self->extensions }; | ||
| 46 | |||
| 47 | emit map { $_->basename( @suffixes ) } $self->profile_dir->all_files; | ||
| 48 | return OK; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub select : method { | ||
| 52 | my $self = shift; | ||
| 53 | my @profiles = map { $_->basename( @{ $self->extensions } ) } | ||
| 54 | $self->profile_dir->all_files; | ||
| 55 | my @options = map { ucfirst $_ } @profiles; | ||
| 56 | my $prompt = 'Select a profile from the following list'; | ||
| 57 | my $index = $self->get_option( $prompt, undef, TRUE, undef, \@options ); | ||
| 58 | |||
| 59 | $index < 0 and return FAILED; | ||
| 60 | $self->unshift_argv( $profiles[ $index ] ); $self->load; | ||
| 61 | return OK; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub show : method { | ||
| 65 | my $self = shift; $self->dumper( $self->load( TRUE ) ); return OK; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ |