51 vtMode() = enableVtMode();
60 if(vtMode()) os <<
"\x1b[" << m.
n <<
"F" <<
"\x1b[" << m.
n <<
"M" << std::flush;
64 static std::ostream&
reset(std::ostream& os)
66 if(vtMode()) os <<
"\x1b[0m";
70 static std::ostream&
bold(std::ostream& os)
72 if(vtMode()) os <<
"\x1b[1m";
76 static std::ostream&
red(std::ostream& os)
78 if(vtMode()) os <<
"\x1b[38;2;255;64;64m";
82 static std::ostream&
green(std::ostream& os)
84 if(vtMode()) os <<
"\x1b[38;2;0;255;64m";
88 static std::ostream&
yellow(std::ostream& os)
90 if(vtMode()) os <<
"\x1b[38;2;255;255;64m";
94 static std::ostream&
black(std::ostream& os)
96 if(vtMode()) os <<
"\x1b[30m";
100 static std::ostream&
onRed (std::ostream& os)
102 if(vtMode()) os <<
"\x1b[48;2;210;120;120m";
106 static std::ostream&
onGreen (std::ostream& os)
108 if(vtMode()) os <<
"\x1b[48;2;120;200;150m";
114 if(vtMode()) os <<
"\x1b[48;2;235;220;160m";
118 static std::ostream&
onCyan (std::ostream& os)
120 if(vtMode()) os <<
"\x1b[48;2;150;210;220m";
124 static std::ostream&
onWhite (std::ostream& os)
126 if(vtMode()) os <<
"\x1b[48;2;240;240;240m";
130 static std::ostream&
out()
138 std::string userInput;
139 std::getline(std::cin, userInput);
141 if (userInput.empty())
147 template<
typename... Args>
148 static void info(
const Args&... args)
150 Guard g(mutex(),
out());
154 template<
typename... Args>
155 static void error(
const Args&... args)
157 Guard g(mutex(),
out());
161 template<
typename... Args>
164 Guard g(mutex(),
out());
168 template<
typename... Args>
169 static void trace(
const Args&... args)
171 Guard g(mutex(),
out());
175 template<
typename... Args>
176 static void out(
const Args&... args)
178 Guard g(mutex(),
out());
179 out() << append(args...) << std::endl;
184 Guard g(mutex(),
out());
186 const size_t width = 40;
188 if (total == 0) total = 1;
189 cur = (std::min)(cur, total);
191 const double r = double(cur) / double(total);
192 const auto filled =
static_cast<std::size_t
>(r * width);
194 out() <<
'\r' <<
'[' << std::string(filled,
'#') << std::string(width - filled,
' ')
195 <<
"] " << std::setw(3) << int(r * 100) <<
'%' << std::flush;
197 if (cur == total)
out() <<
'\n';
213 Guard g(mutex(),
out());
217 out() <<
"[" << sessionId <<
"] ";
219 const auto printMsg = [&](
const auto msg)
221 out() << msg.toString();
226 out() <<
"\n\t" << text;
229 if (!ONIXS_ICEBOE_MESSAGING_NAMESPACE::processTypified(message, printMsg))
235 template <
typename ItemType>
238 Guard g(mutex(),
out());
240 for (
size_t index = 0; index < items.size(); ++index)
241 out() <<
"\t" <<
bold <<
black <<
onGreen <<
"[" << (index + 1) <<
"]" <<
reset <<
" " << items[index].text_ <<
"\n";
244 template <
typename ItemType>
247 Guard g(mutex(),
out());
252 for (
const auto& item : list)
266 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - lastUpdateTime());
271 static void appendToStream(std::ostringstream& oss,
const T& value)
276 template<
typename T,
typename... Args>
277 static void appendToStream(std::ostringstream& oss,
const T& value,
const Args&... args)
280 appendToStream(oss, args...);
283 template <
typename... Args>
284 static auto append(
const Args&... args)
286 std::ostringstream oss;
287 appendToStream(oss,
args...);
293 static bool enableVtMode()
295 const HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
296 if (h == INVALID_HANDLE_VALUE)
300 if (!GetConsoleMode(h, &mode))
303 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
305 return SetConsoleMode(h, mode) != 0;
308 static bool& vtMode()
310 static bool value =
true;
314 using Mutex = std::recursive_mutex;
316 struct Guard :
public std::lock_guard<Mutex>
318 Guard(Mutex& mutex, std::ostream& s)
319 : std::lock_guard<Mutex>(mutex)
325 try { s_ <<
reset << std::flush; }
catch(...){}
332 static Mutex& mutex()
338 static std::chrono::time_point<std::chrono::steady_clock>& lastUpdateTime()
340 static auto lastUpdateTime = std::chrono::steady_clock::now();
341 return lastUpdateTime;
344 static void setLastUpdateTime()
346 lastUpdateTime() = std::chrono::steady_clock::now();
355 MenuItem(
const std::string& text,
const std::string& commandText,
const std::initializer_list<Command>& items)
361 MenuItem(
const std::string& text,
const std::string& commandText,
const Command& command)
377 Menu(
const std::initializer_list<MenuItem>& items)
387 while (!stopRequested_)
402 stopRequested_ =
true;
406 bool stopRequested_ =
false;
409 std::vector<MenuItem> items_;
412 void processRequest()
const
414 static const auto CommandExecutionTimeout = std::chrono::milliseconds(500);
419 Screen::info(
"Select operation to perform. Enter a choice (1 - Show options):");
421 size_t selectedItemNumber = 0;
428 selectedItemNumber = userInput - 1;
435 if(selectedItemNumber < items_.size())
438 Screen::warning(
"Invalid command requested. Repeat a choice (1 - Show options):");
443 const auto& selectedItem = items_[selectedItemNumber];
447 const auto commands = selectedItem.commands_;
448 for(
auto command : commands)
457 std::this_thread::sleep_for(CommandExecutionTimeout);
460 catch (
const std::exception& ex)
472 template <
typename EntryList,
typename Pr
int>
473 static void outputItems(
const EntryList& items, Print print = [](
auto item){
return toStr(item);},
size_t itemsPerPage = 100)
481 size_t itemsToOutput = itemsPerPage;
484 for (
const auto& item : items)
486 if (0 == --itemsToOutput)
488 itemsToOutput = itemsPerPage;
490 if (!shouldContinue())
501 static bool shouldContinue()
503 Screen::info(
"Press <Enter> to view next page or Enter 'q' to abort current action: ");
506 return userInput.empty();
#define ONIXS_ICEBOE_NAMESPACE
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE
MessageTemplateId templateId() const noexcept
std::function< void()> Command
std::string formatMsg(const Message message, typename std::enable_if< isFormattableMsg< Message >::value, void * >::type=nullptr)
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.
auto print(OrderPtr order)
T parseUserInput(const std::string &userInput, typename std::enable_if< std::is_integral< T >::value, void * >::type=nullptr)
bool args(const Options &options, const Char *option, Iterator target, size_t minQty=0, size_t maxQty=static_cast< size_t >(-1))
static void outputItems(const EntryList &items, Print print=[](auto item){ return toStr(item);}, size_t itemsPerPage=100)
Outputs list items.
An utility class for console-based input-output.
static void traceMsg(const std::string &sessionId, SbeMessage message, Direction direction)
static void error(const Args &... args)
static std::ostream & onGreen(std::ostream &os)
static std::ostream & yellow(std::ostream &os)
static DeleteLast deleteLast(size_t n)
static void trace(const Args &... args)
static std::ostream & onRed(std::ostream &os)
static std::ostream & red(std::ostream &os)
static std::ostream & onCyan(std::ostream &os)
static std::string toStr(Direction direction)
static void printProgress(size_t cur, size_t total)
static std::ostream & out()
static std::ostream & onWhite(std::ostream &os)
static void warning(const Args &... args)
static std::ostream & bold(std::ostream &os)
static void out(const Args &... args)
static std::chrono::milliseconds sinceLstUpdate()
static std::ostream & green(std::ostream &os)
friend std::ostream & operator<<(std::ostream &os, const DeleteLast &m)
static void info(const Args &... args)
static void outList(const ItemType &list)
static void outItems(const ItemType &items)
static T getInput(const T &defaultValue={})
static std::ostream & black(std::ostream &os)
static std::ostream & onYellow(std::ostream &os)
static std::ostream & reset(std::ostream &os)