diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2018-07-07 12:51:32 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2018-07-07 12:51:32 +0200 |
commit | cee01f171a505cc91dcfa2f1ecb9c9f6e2a9bd17 (patch) | |
tree | 0ad1a31fff72e60e0ee72ae27a00733e6a35abca /bpkg | |
parent | 1e005c511529fc8d1b1d265f186e928af8aa313e (diff) |
Move prompt facility from bpkg to libbutl
Diffstat (limited to 'bpkg')
-rw-r--r-- | bpkg/utility.cxx | 43 | ||||
-rw-r--r-- | bpkg/utility.hxx | 10 |
2 files changed, 14 insertions, 39 deletions
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 <iostream> // cout, cin +#include <libbutl/prompt.mxx> #include <libbutl/process.mxx> #include <libbutl/fdstream.mxx> @@ -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 diff --git a/bpkg/utility.hxx b/bpkg/utility.hxx index 19d664e..c0c361f 100644 --- a/bpkg/utility.hxx +++ b/bpkg/utility.hxx @@ -88,13 +88,13 @@ namespace bpkg // extern bool stderr_term; // True if stderr is a terminal. - // Y/N prompt. The def argument, if specified, should be either 'y' - // or 'n'. It is used as the default answer, in case the user just - // hits enter. Issue diagnostics and throw failed if no answer could - // be extracted from STDOUT (e.g., because it was closed). + // Y/N prompt. See butl::yn_prompt() for details (this is a thin wrapper). + // + // Issue diagnostics and throw failed if no answer could be extracted from + // stdin (e.g., because it was closed). // bool - yn_prompt (const char* prompt, char def = '\0'); + yn_prompt (const string& prompt, char def = '\0'); // Filesystem. // |