From b7d6ec83648f145190cab9044d321f3ef6d4e8e7 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 2 Aug 2017 14:44:42 +0100 Subject: [PATCH] simplewallet: add (out of sync) or (no daemon) markers in the prompt Should help people who don't realize why they haven't seen their monero yet. --- contrib/epee/include/console_handler.h | 27 +++++++++++++++----------- src/simplewallet/simplewallet.cpp | 16 +++++++++++++-- src/simplewallet/simplewallet.h | 1 + 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/contrib/epee/include/console_handler.h b/contrib/epee/include/console_handler.h index e780ad4de..a3b2d30eb 100644 --- a/contrib/epee/include/console_handler.h +++ b/contrib/epee/include/console_handler.h @@ -293,13 +293,13 @@ namespace epee } template - bool run(t_server* psrv, chain_handler ch_handler, const std::string& prompt = "#", const std::string& usage = "") + bool run(t_server* psrv, chain_handler ch_handler, std::function prompt, const std::string& usage = "") { return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(psrv, cmd); }, [&] { psrv->send_stop_signal(); }); } template - bool run(chain_handler ch_handler, const std::string& prompt = "#", const std::string& usage = "", std::function exit_handler = NULL) + bool run(chain_handler ch_handler, std::function prompt, const std::string& usage = "", std::function exit_handler = NULL) { return run(prompt, usage, [&](const std::string& cmd) { return ch_handler(cmd); }, exit_handler); } @@ -312,18 +312,19 @@ namespace epee void print_prompt() { - if (!m_prompt.empty()) + std::string prompt = m_prompt(); + if (!prompt.empty()) { #ifdef HAVE_READLINE - std::string color_prompt = "\001\033[1;33m\002" + m_prompt; - if (' ' != m_prompt.back()) + std::string color_prompt = "\001\033[1;33m\002" + prompt; + if (' ' != prompt.back()) color_prompt += " "; color_prompt += "\001\033[0m\002"; m_stdin_reader.get_readline_buffer().set_prompt(color_prompt); #else epee::set_console_color(epee::console_color_yellow, true); - std::cout << m_prompt; - if (' ' != m_prompt.back()) + std::cout << prompt; + if (' ' != prompt.back()) std::cout << ' '; epee::reset_console_color(); std::cout.flush(); @@ -333,7 +334,7 @@ namespace epee private: template - bool run(const std::string& prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function exit_handler) + bool run(std::function prompt, const std::string& usage, const t_cmd_handler& cmd_handler, std::function exit_handler) { bool continue_handle = true; m_prompt = prompt; @@ -394,7 +395,7 @@ namespace epee private: async_stdin_reader m_stdin_reader; std::atomic m_running = {true}; - std::string m_prompt; + std::function m_prompt; }; @@ -516,19 +517,23 @@ namespace epee std::unique_ptr m_console_thread; async_console_handler m_console_handler; public: - bool start_handling(const std::string& prompt, const std::string& usage_string = "", std::function exit_handler = NULL) + bool start_handling(std::function prompt, const std::string& usage_string = "", std::function exit_handler = NULL) { m_console_thread.reset(new boost::thread(boost::bind(&console_handlers_binder::run_handling, this, prompt, usage_string, exit_handler))); m_console_thread->detach(); return true; } + bool start_handling(const std::string &prompt, const std::string& usage_string = "", std::function exit_handler = NULL) + { + return start_handling([prompt](){ return prompt; }, usage_string, exit_handler); + } void stop_handling() { m_console_handler.stop(); } - bool run_handling(const std::string& prompt, const std::string& usage_string, std::function exit_handler = NULL) + bool run_handling(std::function prompt, const std::string& usage_string, std::function exit_handler = NULL) { return m_console_handler.run(boost::bind(&console_handlers_binder::process_command_str, this, _1), prompt, usage_string, exit_handler); } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index adf2fde80..29d31c7a5 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3956,6 +3956,19 @@ void simple_wallet::wallet_idle_thread() } } //---------------------------------------------------------------------------------------------------- +std::string simple_wallet::get_prompt() const +{ + std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6); + std::string prompt = std::string("[") + tr("wallet") + " " + addr_start; + uint32_t version; + if (!m_wallet->check_connection(&version)) + prompt += tr(" (no daemon)"); + else if (!m_wallet->is_synced()) + prompt += tr(" (out of sync)"); + prompt += "]: "; + return prompt; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::run() { // check and display warning, but go on anyway @@ -3966,9 +3979,8 @@ bool simple_wallet::run() m_auto_refresh_enabled = m_wallet->auto_refresh(); m_idle_thread = boost::thread([&]{wallet_idle_thread();}); - std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6); message_writer(console_color_green, false) << "Background refresh thread started"; - return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", ""); + return m_cmd_binder.run_handling([this](){return get_prompt();}, ""); } //---------------------------------------------------------------------------------------------------- void simple_wallet::stop() diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index e04352295..b473953e0 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -182,6 +182,7 @@ namespace cryptonote bool accept_loaded_tx(const tools::wallet2::unsigned_tx_set &txs); bool accept_loaded_tx(const tools::wallet2::signed_tx_set &txs); bool print_ring_members(const std::vector& ptx_vector, std::ostream& ostr); + std::string get_prompt() const; /*! * \brief Prints the seed with a nice message