From 4d921c67c38832a5f8314abd716103a90b6434a1 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 6 Jun 2020 11:51:12 +0800 Subject: [PATCH] [cli-reports] implement --report list Lists available reports. --- gnucash/gnucash-cli.cpp | 5 ++++- gnucash/gnucash-commands.cpp | 21 +++++++++++++++++++++ gnucash/gnucash-commands.hpp | 2 +- gnucash/report/report-core.scm | 24 +++++++++++++----------- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp index d801473f5e..771c11e213 100644 --- a/gnucash/gnucash-cli.cpp +++ b/gnucash/gnucash-cli.cpp @@ -100,7 +100,8 @@ Gnucash::GnucashCli::configure_program_options (void) bpo::options_description report_options(_("Report Generation Options")); report_options.add_options() ("report,R", bpo::value (&m_report_cmd), - _("Execute report related commands. Currently only one command is supported.\n\n" + _("Execute report related commands. The following commands are supported.\n\n" + " list: \tLists available reports.\n" " run: \tRun the named report in the given GnuCash datafile.\n")) ("name", bpo::value (&m_report_name), _("Name of the report to run\n")) @@ -150,6 +151,8 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a return Gnucash::run_report(m_file_to_load, m_report_name, m_export_type, m_output_file); } + else if (*m_report_cmd == "list") + return Gnucash::report_list (); else { std::cerr << bl::format (bl::translate("Unknown report command '{1}'")) % *m_report_cmd << "\n\n" diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp index e84d25cbf0..428e8aed59 100644 --- a/gnucash/gnucash-commands.cpp +++ b/gnucash/gnucash-commands.cpp @@ -197,6 +197,20 @@ scm_run_report (void *data, return; } +static void +scm_report_list ([[maybe_unused]] void *data, + [[maybe_unused]] int argc, [[maybe_unused]] char **argv) +{ + scm_c_eval_string("(debug-set! stack 200000)"); + scm_c_use_module ("gnucash app-utils"); + scm_c_use_module ("gnucash reports"); + gnc_report_init (); + + scm_call_0 (scm_c_eval_string ("gnc:cmdline-report-list")); + gnc_shutdown (0); + return; +} + int Gnucash::add_quotes (const bo_str& uri) { @@ -221,3 +235,10 @@ Gnucash::run_report (const bo_str& file_to_load, return 0; } + +int +Gnucash::report_list (void) +{ + scm_boot_guile (0, nullptr, scm_report_list, NULL); + return 0; +} diff --git a/gnucash/gnucash-commands.hpp b/gnucash/gnucash-commands.hpp index 328d18513f..db0b7c6df4 100644 --- a/gnucash/gnucash-commands.hpp +++ b/gnucash/gnucash-commands.hpp @@ -37,6 +37,6 @@ namespace Gnucash { const bo_str& run_report, const bo_str& export_type, const bo_str& output_file); - + int report_list (void); } #endif diff --git a/gnucash/report/report-core.scm b/gnucash/report/report-core.scm index a2f341b769..d9d2182b80 100644 --- a/gnucash/report/report-core.scm +++ b/gnucash/report/report-core.scm @@ -799,6 +799,18 @@ not found."))) (assoc-ref parent-export-types export-type) output-file) (display "done!\n" (current-error-port)))))) +(define-public (gnc:cmdline-report-list) + (for-each + (lambda (template) + (stderr-log "* ~a ~a\n" + (if (gnc:report-template-parent-type template) "C" " ") + (gnc:report-template-name template))) + (sort (hash-fold + (lambda (k v p) (if (gnc:report-template-in-menu? v) (cons v p) p)) + '() *gnc:_report-templates_*) + (lambda (a b) + (string (gnc:find-report-template report) list) (hash-fold @@ -817,17 +829,7 @@ not found."))) (cond ((null? templates) (stderr-log "Cannot find ~s. Valid reports:\n" report) - (for-each - (lambda (pair) - (when (gnc:report-template-in-menu? (cdr pair)) - (stderr-log "* ~a ~a\n" - (if (gnc:report-template-parent-type (cdr pair)) "C" " ") - (gnc:report-template-name (cdr pair))))) - (sort (hash-map->list cons *gnc:_report-templates_*) - (lambda (a b) - (string