text: // Copyright (c) 2011-2017 The Cryptonote developers
  // Copyright (c) 2017-2018 The Circle Foundation & Conceal Devs
  // Copyright (c) 2018-2021 Conceal Network & Conceal Devs
  // Distributed under the MIT/X11 software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #pragma once
  
  #include <atomic>
  #include <functional>
  #include <map>
  #include <string>
  #include <thread>
  #include <vector>
  
  #include "BlockingQueue.h"
  #include "ConsoleTools.h"
  
  namespace common {
  
  class AsyncConsoleReader {
  
  public:
  
    AsyncConsoleReader();
    ~AsyncConsoleReader();
  
    void start();
    bool getline(std::string& line);
    void stop();
    bool stopped() const;
  
  private:
  
    void consoleThread();
    bool waitInput();
  
    std::atomic<bool> m_stop;
    std::thread m_thread;
    BlockingQueue<std::string> m_queue;
  };
  
  
  class ConsoleHandler {
  public:
  
    ~ConsoleHandler();
  
    typedef std::function<bool(const std::vector<std::string> &)> ConsoleCommandHandler;
  
    std::string getUsage() const;
    void setHandler(const std::string& command, const ConsoleCommandHandler& handler, const std::string& usage = "");
    void requestStop();
    bool runCommand(const std::vector<std::string>& cmdAndArgs);
  
    void start(bool startThread = true, const std::string& prompt = "", console::Color promptColor = console::Color::Default);
    void stop();
    void wait(); 
  
  private:
  
    typedef std::map<std::string, std::pair<ConsoleCommandHandler, std::string>> CommandHandlersMap;
  
    virtual void handleCommand(const std::string& cmd);
  
    void handlerThread();
  
    std::thread m_thread;
    std::string m_prompt;
    console::Color m_promptColor = console::Color::Default;
    CommandHandlersMap m_handlers;
    AsyncConsoleReader m_consoleReader;
  };
  
  }
  