From efbe9b2bab988b40d1a8e8895be1e99b6975192d Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Mon, 8 Oct 2012 20:57:59 -0700 Subject: [PATCH 1/3] Make the 'lost contact' string more human-readable Teach the 'lost contact' warning string about time units larger than seconds. After 60 esconds it will switch over to using a time display that looks like 1:23, or 1:02:34. Fixes #321. Signed-off-by: Kevin Ballard --- src/frontend/terminaloverlay.cc | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/frontend/terminaloverlay.cc b/src/frontend/terminaloverlay.cc index 1c14a0d94..65d60f8d8 100644 --- a/src/frontend/terminaloverlay.cc +++ b/src/frontend/terminaloverlay.cc @@ -35,6 +35,10 @@ #include #include #include +#include +#include +#include +#include #include "terminaloverlay.h" @@ -171,6 +175,27 @@ NotificationEngine::NotificationEngine() message_expiration( -1 ) {} +static std::string human_readable_duration(int num_seconds) { + static int divisions[3] = {60, 60, 24}; + std::stringstream buf; + if (num_seconds < divisions[0]) { + buf << num_seconds << " seconds"; + } else { + std::vector components; + for (unsigned int d = 0; d < sizeof(divisions)/sizeof(divisions[0]) && num_seconds > 0; num_seconds /= divisions[d++]) { + int comp = num_seconds % divisions[d]; + components.push_back(comp); + } + if (num_seconds > 0) components.push_back(num_seconds); + std::vector::const_reverse_iterator iter = components.rbegin(); + buf << *(iter++); + for (; iter != components.rend(); ++iter) { + buf << ":" << std::setw(2) << std::setfill('0') << *iter; + } + } + return buf.str(); +} + void NotificationEngine::apply( Framebuffer &fb ) const { uint64_t now = timestamp(); @@ -222,7 +247,7 @@ void NotificationEngine::apply( Framebuffer &fb ) const if ( message.empty() && (!time_expired) ) { return; } else if ( message.empty() && time_expired ) { - swprintf( tmp, 128, L"mosh: Last %s %.0f seconds ago. [To quit: Ctrl-^ .]", explanation, time_elapsed ); + swprintf( tmp, 128, L"mosh: Last %s %s ago. [To quit: Ctrl-^ .]", explanation, human_readable_duration(time_elapsed).c_str() ); } else if ( (!message.empty()) && (!time_expired) ) { swprintf( tmp, 128, L"mosh: %ls [To quit: Ctrl-^ .]", message.c_str() ); } else { From cdd00fee421879a7622529cda218008848cc4429 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Mon, 5 Nov 2012 21:57:02 -0500 Subject: [PATCH 2/3] Simplify implementation of human_readable_duration() --- src/frontend/terminaloverlay.cc | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/frontend/terminaloverlay.cc b/src/frontend/terminaloverlay.cc index 65d60f8d8..5e040d868 100644 --- a/src/frontend/terminaloverlay.cc +++ b/src/frontend/terminaloverlay.cc @@ -35,10 +35,6 @@ #include #include #include -#include -#include -#include -#include #include "terminaloverlay.h" @@ -176,24 +172,16 @@ NotificationEngine::NotificationEngine() {} static std::string human_readable_duration(int num_seconds) { - static int divisions[3] = {60, 60, 24}; - std::stringstream buf; - if (num_seconds < divisions[0]) { - buf << num_seconds << " seconds"; - } else { - std::vector components; - for (unsigned int d = 0; d < sizeof(divisions)/sizeof(divisions[0]) && num_seconds > 0; num_seconds /= divisions[d++]) { - int comp = num_seconds % divisions[d]; - components.push_back(comp); - } - if (num_seconds > 0) components.push_back(num_seconds); - std::vector::const_reverse_iterator iter = components.rbegin(); - buf << *(iter++); - for (; iter != components.rend(); ++iter) { - buf << ":" << std::setw(2) << std::setfill('0') << *iter; - } - } - return buf.str(); + char tmp[ 128 ]; + if ( num_seconds < 60 ) { + snprintf( tmp, 128, "%d seconds", num_seconds ); + } else if ( num_seconds < 3600 ) { + snprintf( tmp, 128, "%d:%02d", num_seconds / 60, num_seconds % 60 ); + } else { + snprintf( tmp, 128, "%d:%02d:%02d", num_seconds / 3600, + (num_seconds / 60) % 60, num_seconds % 60 ); + } + return tmp; } void NotificationEngine::apply( Framebuffer &fb ) const From b018e3a1abc9a6f5a8b742a59807b6a9843777e4 Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Mon, 5 Nov 2012 22:02:46 -0500 Subject: [PATCH 3/3] Make "elapsed time" human readable even when other error is displayed --- src/frontend/terminaloverlay.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/frontend/terminaloverlay.cc b/src/frontend/terminaloverlay.cc index 5e040d868..819584b53 100644 --- a/src/frontend/terminaloverlay.cc +++ b/src/frontend/terminaloverlay.cc @@ -171,10 +171,10 @@ NotificationEngine::NotificationEngine() message_expiration( -1 ) {} -static std::string human_readable_duration(int num_seconds) { +static std::string human_readable_duration( int num_seconds, const std::string seconds_abbr ) { char tmp[ 128 ]; if ( num_seconds < 60 ) { - snprintf( tmp, 128, "%d seconds", num_seconds ); + snprintf( tmp, 128, "%d %s", num_seconds, seconds_abbr.c_str() ); } else if ( num_seconds < 3600 ) { snprintf( tmp, 128, "%d:%02d", num_seconds / 60, num_seconds % 60 ); } else { @@ -235,12 +235,13 @@ void NotificationEngine::apply( Framebuffer &fb ) const if ( message.empty() && (!time_expired) ) { return; } else if ( message.empty() && time_expired ) { - swprintf( tmp, 128, L"mosh: Last %s %s ago. [To quit: Ctrl-^ .]", explanation, human_readable_duration(time_elapsed).c_str() ); + swprintf( tmp, 128, L"mosh: Last %s %s ago. [To quit: Ctrl-^ .]", explanation, + human_readable_duration( time_elapsed, "seconds" ).c_str() ); } else if ( (!message.empty()) && (!time_expired) ) { swprintf( tmp, 128, L"mosh: %ls [To quit: Ctrl-^ .]", message.c_str() ); } else { - swprintf( tmp, 128, L"mosh: %ls (%.0f s without %s.) [To quit: Ctrl-^ .]", message.c_str(), - time_elapsed, explanation ); + swprintf( tmp, 128, L"mosh: %ls (%s without %s.) [To quit: Ctrl-^ .]", message.c_str(), + human_readable_duration( time_elapsed, "s" ).c_str(), explanation ); } wstring string_to_draw( tmp );