From b6bc72c449f22ff158252f6599b235b55f533e33 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Tue, 23 Jun 2009 14:56:15 -0400 Subject: [PATCH] Enhancement - confirm3 - Added a tri-state confirmation function for confirming bulk operations allowing the user to answer yes/no/all to and optionally allow, disallow or bulk-allow big changes. --- src/util.cpp | 34 ++++++++++++++++++++++++++++++++++ src/util.h | 1 + 2 files changed, 35 insertions(+) diff --git a/src/util.cpp b/src/util.cpp index fdf934ac3..9c59002c8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -74,6 +74,40 @@ bool confirm (const std::string& question) return (answer == "y" || answer == "ye" || answer == "yes") ? true : false; // TODO i18n } +//////////////////////////////////////////////////////////////////////////////// +// 0 = no +// 1 = yes +// 2 = all +int confirm3 (const std::string& question) +{ + std::vector options; + options.push_back ("yes"); + options.push_back ("no"); + options.push_back ("all"); + + std::string answer; + std::vector matches; + + do + { + std::cout << question + << " (" + << options[0] << "/" + << options[1] << "/" + << options[2] + << ") "; + + std::getline (std::cin, answer); + answer = trim (answer); + autoComplete (answer, options, matches); + } + while (matches.size () != 1); + + if (matches[0] == "yes") return 1; + else if (matches[0] == "all") return 2; + else return 0; +} + //////////////////////////////////////////////////////////////////////////////// void delay (float f) { diff --git a/src/util.h b/src/util.h index b72e8303f..19f7af6f2 100644 --- a/src/util.h +++ b/src/util.h @@ -51,6 +51,7 @@ for (typeof (c) *foreach_p = & (c); \ // util.cpp bool confirm (const std::string&); +int confirm3 (const std::string&); void delay (float); std::string formatSeconds (time_t); std::string formatSecondsCompact (time_t);