| 1 | package Astro::Montenbruck::Utils::Theme; | ||
| 2 | use 5.22.0; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings qw/experimental/; | ||
| 6 | use feature qw/switch/; | ||
| 7 | use Carp qw/croak/; | ||
| 8 | |||
| 9 | use Readonly; | ||
| 10 | use Term::ANSIColor; | ||
| 11 | use Memoize; | ||
| 12 | |||
| 13 | use Astro::Montenbruck::Utils::Theme::Dark; | ||
| 14 | use Astro::Montenbruck::Utils::Theme::Light; | ||
| 15 | use Astro::Montenbruck::Utils::Theme::Colorless; | ||
| 16 | |||
| 17 | our $VERSION = 0.01; | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my ( $class, %arg ) = @_; | ||
| 21 | bless { | ||
| 22 | _name => $arg{name}, | ||
| 23 | _scheme => $arg{scheme} | ||
| 24 | }, $class | ||
| 25 | } | ||
| 26 | |||
| 27 | sub scheme { shift->{_scheme} } | ||
| 28 | |||
| 29 | sub name { shift->{_name} } | ||
| 30 | |||
| 31 | sub decorate { | ||
| 32 | my $self = shift; | ||
| 33 | colored @_; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub print_data { | ||
| 37 | my $self = shift; | ||
| 38 | my $title = shift; | ||
| 39 | my $data = shift; | ||
| 40 | my %arg = (title_width => 20, highlited => 0, @_); | ||
| 41 | |||
| 42 | my $sch = $self->scheme; | ||
| 43 | my $fmt = '%-' . $arg{title_width} . 's'; | ||
| 44 | my $data_color = $arg{highlited} ? $sch->{data_row_selected} | ||
| 45 | : $sch->{data_row_data}; | ||
| 46 | print $self->decorate (sprintf($fmt, $title), $sch->{data_row_title}); | ||
| 47 | print $self->decorate(': ', $sch->{data_row_data}); | ||
| 48 | $data = " $data" unless $data =~ /^[-+ ].*/; | ||
| 49 | say $self->decorate( $data, $data_color); | ||
| 50 | } | ||
| 51 | |||
| 52 | memoize('_get_theme'); | ||
| 53 | sub _get_theme { | ||
| 54 | my $name = shift; | ||
| 55 | given($name) { | ||
| 56 | when ('dark') { | ||
| 57 | return Astro::Montenbruck::Utils::Theme::Dark->new() | ||
| 58 | } | ||
| 59 | when ('light') { | ||
| 60 | return Astro::Montenbruck::Utils::Theme::Light->new() | ||
| 61 | } | ||
| 62 | when ('colorless') { | ||
| 63 | return Astro::Montenbruck::Utils::Theme::Colorless->new() | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub create { | ||
| 69 | my $class = shift; | ||
| 70 | my $theme = shift; | ||
| 71 | croak "Unknown theme name: \"$theme\"" | ||
| 72 | unless grep /^$theme$/, qw/dark light colorless/; | ||
| 73 | _get_theme($theme, @_); | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | __END__ |