add some basic tracing using the new deparser
[scpubgit/DX.git] / lib / DX / ShellState.pm
CommitLineData
9eedd677 1package DX::ShellState;
2
bcee3a69 3use DX::Utils qw(deparse);
9eedd677 4use DX::Class;
5
6has template_query_state => (
7 is => 'ro', required => 1, isa => QueryState
8);
9
10has current_query_state => (
384a5e93 11 is => 'lazy', builder => 'new_query_state'
9eedd677 12);
13
d294025e 14has trace_these => (
15 is => 'ro', required => 1,
16);
17
9eedd677 18has mode => (is => 'ro', required => 1);
19
20sub new_query_state { $_[0]->template_query_state }
21
bcee3a69 22sub 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$//;
5b6cab1b 30 warn "${dp}\n";
bcee3a69 31 }
32}
33
d294025e 34sub 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 47sub with_new_query_state {
48 my ($self) = @_;
49 $self->but(
50 current_query_state => $self->new_query_state
51 );
52}
53
54sub with_mode {
55 my ($self, $new_mode) = @_;
56 return (
57 $self->but(mode => $new_mode),
58 [ mode => $new_mode ],
59 );
60}
61
621;