From cee01f171a505cc91dcfa2f1ecb9c9f6e2a9bd17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 7 Jul 2018 12:51:32 +0200 Subject: Move prompt facility from bpkg to libbutl --- bpkg/utility.cxx | 43 +++++++++---------------------------------- 1 file changed, 9 insertions(+), 34 deletions(-) (limited to 'bpkg/utility.cxx') diff --git a/bpkg/utility.cxx b/bpkg/utility.cxx index 8a456d0..c6f2fae 100644 --- a/bpkg/utility.cxx +++ b/bpkg/utility.cxx @@ -6,6 +6,7 @@ #include // cout, cin +#include #include #include @@ -85,42 +86,16 @@ namespace bpkg bool stderr_term; bool - yn_prompt (const char* prompt, char def) + yn_prompt (const string& p, char d) { - // Writing a robust Y/N prompt is more difficult than one would - // expect... - // - string a; - do + try { - *diag_stream << prompt << ' '; - - // getline() will set the failbit if it failed to extract anything, - // not even the delimiter and eofbit if it reached eof before seeing - // the delimiter. - // - getline (cin, a); - - bool f (cin.fail ()); - bool e (cin.eof ()); - - if (f || e) - *diag_stream << endl; // Assume no delimiter (newline). - - if (f) - fail << "unable to read y/n answer from STDOUT"; - - if (a.empty () && def != '\0') - { - // Don't treat eof as the default answer. We need to see the - // actual newline. - // - if (!e) - a = def; - } - } while (a != "y" && a != "n"); - - return a == "y"; + return butl::yn_prompt (p, d); + } + catch (io_error&) + { + fail << "unable to read y/n answer from stdin"; + } } bool -- cgit v1.1