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 => ( |
cfae7810 |
11 | is => 'lazy', builder => 'new_query_state', isa => QueryState |
9eedd677 |
12 | ); |
13 | |
d294025e |
14 | has trace_these => ( |
cfae7810 |
15 | is => 'ro', required => 1, isa => HashRef[Str], |
d294025e |
16 | ); |
17 | |
cfae7810 |
18 | has mode => (is => 'ro', required => 1, isa => ShellMode); |
9eedd677 |
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; |
af4a440a |
27 | my $traces = $self->trace_these; |
28 | return unless $traces->{$part} or $traces->{'*'} or $ENV{DX_TRACE}; |
bcee3a69 |
29 | my $dp = deparse($thing); |
30 | $dp =~ s/\n$//; |
5b6cab1b |
31 | warn "${dp}\n"; |
bcee3a69 |
32 | } |
33 | } |
34 | |
d294025e |
35 | sub with_trace_changes { |
36 | my ($self, @changes) = @_; |
37 | my %trace = %{$self->trace_these}; |
38 | foreach my $change (@changes) { |
af4a440a |
39 | if ($change =~ /^\+?(\S+)/) { |
d294025e |
40 | $trace{$1} = 1; |
af4a440a |
41 | } elsif ($change =~ /^-(\S+)/) { |
d294025e |
42 | delete $trace{$1}; |
43 | } |
44 | } |
45 | return $self->but(trace_these => \%trace); |
46 | } |
47 | |
9eedd677 |
48 | sub with_new_query_state { |
49 | my ($self) = @_; |
50 | $self->but( |
51 | current_query_state => $self->new_query_state |
52 | ); |
53 | } |
54 | |
55 | sub with_mode { |
56 | my ($self, $new_mode) = @_; |
57 | return ( |
58 | $self->but(mode => $new_mode), |
59 | [ mode => $new_mode ], |
60 | ); |
61 | } |
62 | |
63 | 1; |