aboutsummaryrefslogtreecommitdiff
path: root/bpkg/utility.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2018-07-07 12:51:32 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2018-07-07 12:51:32 +0200
commitcee01f171a505cc91dcfa2f1ecb9c9f6e2a9bd17 (patch)
tree0ad1a31fff72e60e0ee72ae27a00733e6a35abca /bpkg/utility.cxx
parent1e005c511529fc8d1b1d265f186e928af8aa313e (diff)
Move prompt facility from bpkg to libbutl
Diffstat (limited to 'bpkg/utility.cxx')
-rw-r--r--bpkg/utility.cxx43
1 files changed, 9 insertions, 34 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