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 <kevin@sb.org>
This commit is contained in:
Kevin Ballard
2012-10-08 20:57:59 -07:00
parent 28ed30c356
commit efbe9b2bab
+26 -1
View File
@@ -35,6 +35,10 @@
#include <list> #include <list>
#include <typeinfo> #include <typeinfo>
#include <limits.h> #include <limits.h>
#include <string>
#include <sstream>
#include <vector>
#include <iomanip>
#include "terminaloverlay.h" #include "terminaloverlay.h"
@@ -171,6 +175,27 @@ NotificationEngine::NotificationEngine()
message_expiration( -1 ) 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<int> 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<int>::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 void NotificationEngine::apply( Framebuffer &fb ) const
{ {
uint64_t now = timestamp(); uint64_t now = timestamp();
@@ -222,7 +247,7 @@ void NotificationEngine::apply( Framebuffer &fb ) const
if ( message.empty() && (!time_expired) ) { if ( message.empty() && (!time_expired) ) {
return; return;
} else if ( message.empty() && time_expired ) { } 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) ) { } else if ( (!message.empty()) && (!time_expired) ) {
swprintf( tmp, 128, L"mosh: %ls [To quit: Ctrl-^ .]", message.c_str() ); swprintf( tmp, 128, L"mosh: %ls [To quit: Ctrl-^ .]", message.c_str() );
} else { } else {