| 1 | package App::JobLog::Command::configure; | ||
| 2 | $App::JobLog::Command::configure::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: examine or modify App::JobLog configuration | ||
| 4 | |||
| 5 | use App::JobLog -command; | ||
| 6 | use Modern::Perl; | ||
| 7 | use App::JobLog::Config qw( | ||
| 8 | day_length | ||
| 9 | editor | ||
| 10 | hidden_columns | ||
| 11 | merge | ||
| 12 | pay_period_length | ||
| 13 | precision | ||
| 14 | start_pay_period | ||
| 15 | sunday_begins_week | ||
| 16 | time_zone | ||
| 17 | workdays | ||
| 18 | DAYS | ||
| 19 | HIDABLE_COLUMNS | ||
| 20 | HOURS | ||
| 21 | MERGE | ||
| 22 | NONE_COLUMN | ||
| 23 | PERIOD | ||
| 24 | PRECISION | ||
| 25 | SUNDAY_BEGINS_WEEK | ||
| 26 | TIME_ZONE | ||
| 27 | WORKDAYS | ||
| 28 | ); | ||
| 29 | use autouse 'App::JobLog::TimeGrammar' => qw(parse); | ||
| 30 | no if $] >= 5.018, warnings => "experimental::smartmatch"; | ||
| 31 | |||
| 32 | sub execute { | ||
| 33 | my ( $self, $opt, $args ) = @_; | ||
| 34 | _list_params() if $opt->list; | ||
| 35 | if ( defined $opt->precision ) { | ||
| 36 | my $precision = precision( $opt->precision ); | ||
| 37 | say "precision set to $precision"; | ||
| 38 | } | ||
| 39 | if ( defined $opt->start_pay_period ) { | ||
| 40 | eval { | ||
| 41 | my ($s) = parse( $opt->start_pay_period ); | ||
| 42 | my $d = start_pay_period($s); | ||
| 43 | say 'beginning of pay period set to ' . $d->strftime('%F'); | ||
| 44 | }; | ||
| 45 | $self->usage_error( | ||
| 46 | 'could not understand date: ' . $opt->start_pay_period ) | ||
| 47 | if $@; | ||
| 48 | } | ||
| 49 | if ( defined $opt->length_pay_period ) { | ||
| 50 | my $length_pp = pay_period_length( $opt->length_pay_period ); | ||
| 51 | say "length of pay period in days set to $length_pp"; | ||
| 52 | } | ||
| 53 | if ( defined $opt->day_length ) { | ||
| 54 | my $day_length = day_length( $opt->day_length ); | ||
| 55 | say "length of work day set to $day_length"; | ||
| 56 | } | ||
| 57 | if ( defined $opt->workdays ) { | ||
| 58 | my $days = uc $opt->workdays; | ||
| 59 | my %days = map { $_ => 1 } split //, $days; | ||
| 60 | my @days; | ||
| 61 | for ( split //, DAYS ) { | ||
| 62 | push @days, $_ if $days{$_}; | ||
| 63 | } | ||
| 64 | $days = join '', @days; | ||
| 65 | $days = workdays($days); | ||
| 66 | say "workdays set to $days"; | ||
| 67 | } | ||
| 68 | if ( defined $opt->sunday_begins_week ) { | ||
| 69 | my $bool; | ||
| 70 | for ( $opt->sunday_begins_week ) { | ||
| 71 | when (/true/i) { $bool = 1 } | ||
| 72 | when (/false/i) { $bool = 0 } | ||
| 73 | default { $bool = $opt->sunday_begins_week || 0 }; | ||
| 74 | } | ||
| 75 | $bool = sunday_begins_week($bool); | ||
| 76 | say "Sunday begins week is now " . ( $bool ? 'true' : 'false' ); | ||
| 77 | } | ||
| 78 | if ( defined $opt->merge ) { | ||
| 79 | my $m = lc $opt->merge; | ||
| 80 | $m =~ s/^\s++|\s++$//g; | ||
| 81 | $m =~ s/\s++/ /g; | ||
| 82 | my $value = merge($m); | ||
| 83 | say "merge level is now '$value'"; | ||
| 84 | } | ||
| 85 | if ( defined $opt->editor ) { | ||
| 86 | my $value = editor( $opt->editor ); | ||
| 87 | say "log editor is now $value"; | ||
| 88 | } | ||
| 89 | if ( defined $opt->hidden_columns ) { | ||
| 90 | my @cols = map { my $v = $_; lc $v } @{ $opt->hidden_columns }; | ||
| 91 | my %cols = map { $_ => 1 } @cols; | ||
| 92 | my $value = join ' ', sort keys %cols; | ||
| 93 | $value = hidden_columns($value); | ||
| 94 | say "hidden columns: $value"; | ||
| 95 | } | ||
| 96 | if ( defined $opt->time_zone ) { | ||
| 97 | my $value = time_zone( $opt->time_zone ); | ||
| 98 | say "time zone is now $value"; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub usage_desc { '%c ' . __PACKAGE__->name . ' %o' } | ||
| 103 | |||
| 104 | sub abstract { 'set or display various parameters' } | ||
| 105 | |||
| 106 | sub options { | ||
| 107 | return ( | ||
| 108 | [ | ||
| 109 | 'precision=i', | ||
| 110 | 'decimal places of precision in display of time; ' | ||
| 111 | . 'e.g., --precision=1; ' | ||
| 112 | . 'default is ' | ||
| 113 | . PRECISION | ||
| 114 | ], | ||
| 115 | [ | ||
| 116 | 'start-pay-period=s', | ||
| 117 | 'the first day of some pay period; ' | ||
| 118 | . 'pay period boundaries will be calculated based on this date and the pay period length; ' | ||
| 119 | . 'e.g., --start-pay-period="June 14, 1912"' | ||
| 120 | ], | ||
| 121 | [ | ||
| 122 | 'time-zone=s', | ||
| 123 | 'time zone used in calendar calculations; default is ' . TIME_ZONE | ||
| 124 | ], | ||
| 125 | [ | ||
| 126 | 'sunday-begins-week=s', | ||
| 127 | 'whether Sundays should be regarded as the first day in the week; ' | ||
| 128 | . 'the alternative is Monday; default is ' | ||
| 129 | . ( SUNDAY_BEGINS_WEEK ? 'TRUE' : 'FALSE' ) | ||
| 130 | ], | ||
| 131 | [ | ||
| 132 | 'length-pay-period=i', | ||
| 133 | 'the length of the pay period in days; e.g., --length-pay-period 7; ' | ||
| 134 | . 'default is ' | ||
| 135 | . PERIOD | ||
| 136 | ], | ||
| 137 | [ | ||
| 138 | 'day-length=f', | ||
| 139 | 'length of workday; ' | ||
| 140 | . 'e.g., --day-length 7.5; ' | ||
| 141 | . 'default is: ' | ||
| 142 | . HOURS | ||
| 143 | ], | ||
| 144 | [ | ||
| 145 | 'workdays=s', | ||
| 146 | 'which days of the week you work represented as some subset of ' | ||
| 147 | . DAYS | ||
| 148 | . '; e.g., --workdays=MTWH; ' | ||
| 149 | . 'default is ' | ||
| 150 | . WORKDAYS | ||
| 151 | ], | ||
| 152 | [ | ||
| 153 | 'merge=s', | ||
| 154 | 'amount of merging of events in summaries; ' | ||
| 155 | . 'available options are : ' | ||
| 156 | . "'adjacent same tags', 'adjacent', 'all', 'none', 'same day same tags', 'same day', 'same tags'; " | ||
| 157 | . "default is '@{[MERGE]}'" | ||
| 158 | ], | ||
| 159 | [ | ||
| 160 | 'hidden-columns=s@', | ||
| 161 | 'columns not to display with the ' | ||
| 162 | . App::JobLog::Command::summary->name | ||
| 163 | . ' command; ' | ||
| 164 | . 'available options are: ' | ||
| 165 | . join( ', ', map { "'$_'" } @{ HIDABLE_COLUMNS() } ) . '; ' | ||
| 166 | . "default is '@{[NONE_COLUMN]}'; " | ||
| 167 | . 'multiple columns may be specified' | ||
| 168 | ], | ||
| 169 | [ 'editor=s', 'text editor to use when manually editing the log' ], | ||
| 170 | [ 'list|l', 'list all configuration parameters' ], | ||
| 171 | ); | ||
| 172 | } | ||
| 173 | |||
| 174 | # | ||
| 175 | # list values of all params | ||
| 176 | # | ||
| 177 | sub _list_params { | ||
| 178 | my @params = sort qw( | ||
| 179 | precision | ||
| 180 | day_length | ||
| 181 | editor | ||
| 182 | hidden_columns | ||
| 183 | merge | ||
| 184 | pay_period_length | ||
| 185 | start_pay_period | ||
| 186 | sunday_begins_week | ||
| 187 | time_zone | ||
| 188 | workdays | ||
| 189 | ); | ||
| 190 | my %booleans = map { $_ => 1 } qw( | ||
| 191 | sunday_begins_week | ||
| 192 | ); | ||
| 193 | my ( $l1, $l2, %h ) = ( 0, 0 ); | ||
| 194 | |||
| 195 | for my $method (@params) { | ||
| 196 | my $l = length $method; | ||
| 197 | my $value = eval "App::JobLog::Config::$method()"; | ||
| 198 | $value = $value ? 'true' : 'false' if $booleans{$method}; | ||
| 199 | $value = 'not defined' unless defined $value; | ||
| 200 | $value = $value->strftime('%F') if ref $value eq 'DateTime'; | ||
| 201 | $l1 = $l if $l > $l1; | ||
| 202 | $l = length $value; | ||
| 203 | $l2 = $l if $l > $l2; | ||
| 204 | $h{$method} = $value; | ||
| 205 | } | ||
| 206 | my $format = '%-' . $l1 . 's %' . $l2 . "s\n"; | ||
| 207 | for my $method (@params) { | ||
| 208 | my $value = $h{$method}; | ||
| 209 | $method =~ s/_/ /g; | ||
| 210 | printf $format, $method, $value; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | sub validate { | ||
| 215 | my ( $self, $opt, $args ) = @_; | ||
| 216 | |||
| 217 | $self->usage_error('specify some parameter to set or display') unless %$opt; | ||
| 218 | $self->usage_error('cannot parse work days') | ||
| 219 | if $opt->workdays && $opt->workdays !~ /^[SMTWHFA]*+$/i; | ||
| 220 | $self->usage_error( | ||
| 221 | 'cannot understand argument ' . $opt->sunday_begins_week ) | ||
| 222 | if $opt->sunday_begins_week | ||
| 223 | && $opt->sunday_begins_week !~ /^(?:true|false|[01])?$/i; | ||
| 224 | if ( defined $opt->merge ) { | ||
| 225 | my $m = lc $opt->merge; | ||
| 226 | $m =~ s/^\s++|\s++$//g; | ||
| 227 | $m =~ s/\s++/ /g; | ||
| 228 | if ( $m !~ | ||
| 229 | /^(?:adjacent|adjacent same tags|all|none|same day|same day same tags|same tags)$/ | ||
| 230 | ) | ||
| 231 | { | ||
| 232 | $self->usage_error( 'unknown merge option: ' . $opt->merge ); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | if ( defined $opt->hidden_columns ) { | ||
| 236 | my %h = map { $_ => 1 } @{ HIDABLE_COLUMNS() }; | ||
| 237 | my ( $found_none, $found_something ) = ( 0, 0 ); | ||
| 238 | for my $c ( @{ $opt->hidden_columns } ) { | ||
| 239 | my $col = lc $c; | ||
| 240 | $self->usage_error("unknown column: $c") unless $h{$col}; | ||
| 241 | if ( $col eq NONE_COLUMN ) { | ||
| 242 | $found_none ||= 1; | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | $found_something ||= 1; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | $self->usage_error( | ||
| 249 | "you have specified that something should be hidden and that nothing should be hidden" | ||
| 250 | ) if $found_none && $found_something; | ||
| 251 | } | ||
| 252 | if ( defined $opt->time_zone ) { | ||
| 253 | require DateTime::TimeZone; | ||
| 254 | eval { DateTime::TimeZone->new( name => $opt->time_zone ) }; | ||
| 255 | $self->usage_error( | ||
| 256 | 'DateTime::TimeZone does not like the time zone name ' | ||
| 257 | . $opt->time_zone | ||
| 258 | . "\n$@" ) | ||
| 259 | if $@; | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | 1; | ||
| 264 | |||
| 265 | __END__ |