Commit | Line | Data |
9eedd677 |
1 | package DX::ShellState; |
2 | |
bcee3a69 |
3 | use DX::Utils qw(deparse); |
9eedd677 |
4 | use DX::Class; |
5 | |
6 | has template_query_state => ( |
7 | is => 'ro', required => 1, isa => QueryState |
8 | ); |
9 | |
10 | has current_query_state => ( |
384a5e93 |
11 | is => 'lazy', builder => 'new_query_state' |
9eedd677 |
12 | ); |
13 | |
d294025e |
14 | has trace_these => ( |
15 | is => 'ro', required => 1, |
16 | ); |
17 | |
9eedd677 |
18 | has mode => (is => 'ro', required => 1); |
19 | |
20 | sub new_query_state { $_[0]->template_query_state } |
21 | |
bcee3a69 |
22 | sub trace_sub { |
d294025e |
23 | my ($self) = @_; |
bcee3a69 |
24 | sub { |
25 | my ($tag, $thing) = @_; |
d294025e |
26 | my ($part) = split /\./, $tag; |
27 | return unless $self->trace_these->{$part}; |
bcee3a69 |
28 | my $dp = deparse($thing); |
29 | $dp =~ s/\n$//; |
30 | warn "${tag}: ${dp}\n"; |
31 | } |
32 | } |
33 | |
d294025e |
34 | sub with_trace_changes { |
35 | my ($self, @changes) = @_; |
36 | my %trace = %{$self->trace_these}; |
37 | foreach my $change (@changes) { |
38 | if ($change =~ /^\+?(\w+)/) { |
39 | $trace{$1} = 1; |
40 | } elsif ($change =~ /^-(\w+)/) { |
41 | delete $trace{$1}; |
42 | } |
43 | } |
44 | return $self->but(trace_these => \%trace); |
45 | } |
46 | |
9eedd677 |
47 | sub with_new_query_state { |
48 | my ($self) = @_; |
49 | $self->but( |
50 | current_query_state => $self->new_query_state |
51 | ); |
52 | } |
53 | |
54 | sub with_mode { |
55 | my ($self, $new_mode) = @_; |
56 | return ( |
57 | $self->but(mode => $new_mode), |
58 | [ mode => $new_mode ], |
59 | ); |
60 | } |
61 | |
62 | 1; |