From e6cfc9c482c9f1a1c6dfba10aadf1c5c6a32f0d3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 26 Nov 2015 09:35:23 +0200 Subject: Add --pager, --pager-option common options --- bpkg/help.cxx | 63 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 14 deletions(-) (limited to 'bpkg/help.cxx') diff --git a/bpkg/help.cxx b/bpkg/help.cxx index 31a7cdd..6c16088 100644 --- a/bpkg/help.cxx +++ b/bpkg/help.cxx @@ -30,22 +30,47 @@ namespace bpkg { struct pager { - pager (const string& name) + pager (const common_options& co, const string& name) { - // First try less. - // - try + cstrings args; + string prompt; + + bool up (co.pager_specified ()); // User's pager. + + if (up) + { + if (co.pager ().empty ()) + return; // No pager should be used. + + args.push_back (co.pager ().c_str ()); + } + else { - string prompt ("-Ps" + name + " (press q to quit, h for help)"); + // By default try less. + // + prompt = "-Ps" + name + " (press q to quit, h for help)"; + + args.push_back ("less"); + args.push_back ("-R"); // Handle ANSI color. + args.push_back (prompt.c_str ()); + } + + // Add extra pager options. + // + for (const string& o: co.pager_option ()) + args.push_back (o.c_str ()); - const char* args[] = { - "less", - "-R", // ANSI color - prompt.c_str (), - nullptr - }; + args.push_back (nullptr); - p_ = process (args, -1); // Redirect child's stdin to a pipe. + if (verb >= 2) + print_process (args); + + // Ignore errors and go without a pager unless the pager was specified + // by the user. + // + try + { + p_ = process (args.data (), -1); // Redirect child's stdin to a pipe. // Wait a bit and see if the pager has exited before reading // anything (e.g., because exec() couldn't find the program). @@ -63,14 +88,24 @@ namespace bpkg #else _close (p_.out_fd); #endif + if (up) + fail << "pager " << args[0] << " exited unexpectedly"; } else os_.open (p_.out_fd); } catch (const process_error& e) { + // Ignore unless it was a user-specified pager. + // + if (up) + error << "unable to execute " << args[0] << ": " << e.what (); + if (e.child ()) exit (1); + + if (up) + throw failed (); } } @@ -95,7 +130,7 @@ namespace bpkg }; int - help (const help_options&, const string& t, usage_function* usage) + help (const help_options& o, const string& t, usage_function* usage) { if (usage == nullptr) // Not a command. { @@ -108,7 +143,7 @@ namespace bpkg info << "run 'bpkg help' for more information"; } - pager p ("bpkg " + (t.empty () ? "help" : t)); + pager p (o, "bpkg " + (t.empty () ? "help" : t)); usage (p.stream (), cli::usage_para::none); // If the pager failed, assume it has issued some diagnostics. -- cgit v1.1