diff --git a/bean-query-daemon-lib.pl b/bean-query-daemon-lib.pl index 33bc9b7..513ff3e 100644 --- a/bean-query-daemon-lib.pl +++ b/bean-query-daemon-lib.pl @@ -5,6 +5,8 @@ use strict; use warnings; +use IPC::Shareable; + our %BEANCOUNT_QUERY; my $IPC_GLUE = 'BeAn'; diff --git a/bean-query-goofy-daemon.plx b/bean-query-goofy-daemon.plx index a101ec1..f2a47e9 100755 --- a/bean-query-goofy-daemon.plx +++ b/bean-query-goofy-daemon.plx @@ -198,13 +198,26 @@ while (1) { $query{fifoName} = mktemp("REJECTED_beancount-query-fifo-this-file-does-not-exist_${$}_XXXXXXXXX"); (tied %query)->shunlock; } elsif (not defined $query{fifoName}) { - if (defined $query{format} and $query{format} ne 'text') { - print STDERR "format string $query{format} is not supported yet!\n"; - (tied %query)->shlock; - $query{question} = $query{format} = undef; - $query{fifoName} = mktemp("REJECTED_beancount-$query{format}_not_supported_${$}_XXXXXXXXX"); - (tied %query)->shunlock; - next; + if (defined $query{format}) { + unless ($query{format} =~ /^(?:text|csv)$/) { + print STDERR "format string $query{format} is not supported yet!\n"; + (tied %query)->shlock; + $query{question} = $query{format} = undef; + $query{fifoName} = mktemp("REJECTED_beancount-$query{format}_not_supported_${$}_XXXXXXXXX"); + (tied %query)->shunlock; + next; + } + if ($currentFormat ne $query{format}) { + $runningBeanQuery->send("set format $query{format}\n"); + $runningBeanQuery->expect(undef, 'beancount>') # *Don't* use regex here! + or die("Unable to find beancount prompt, output was instead: ". + $runningBeanQuery->before() . $runningBeanQuery->after()); + $currentFormat = $query{format}; + print STDERR "Switched formats to the $currentFormat..." if $VERBOSE > 3; + my $rbcBefore = $runningBeanQuery->before(); + die("Unable to change format to $currentFormat") + unless $rbcBefore =~ /format\s*:\s*$currentFormat/ixm; + } } print STDERR "Runing query: $query{question}\n" if $VERBOSE > 0; my $ques = $query{question}; @@ -237,7 +250,8 @@ while (1) { unless ($seenSeperator) { if ($line =~ /^\s*$/) { $seenSeperator = 1; - } elsif ($currentFormat eq 'text' and $line =~ /^\s*[\-\s]+\s*$/) { + } elsif (($currentFormat eq 'text' and $line =~ /^\s*[\-\s]+\s*$/) + or ($currentFormat eq 'csv' and $line =~ /^\s*(\S+,)*\S+\s*$/)){ $seenSeperator = 1; } $prevLine = $line;