← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/stock-amounts into lp:widelands

 

Benedikt Straub has proposed merging lp:~widelands-dev/widelands/stock-amounts into lp:widelands.

Commit message:
Very large amounts in WaresDisplays are shortened as "10k", "10M", "10G" to prevent text from flowing over on the left.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1791458 in widelands: "Number text too long in stock inventory"
  https://bugs.launchpad.net/widelands/+bug/1791458

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/stock-amounts/+merge/354558

Numbers from 0 to 9999 are displayed normally, then abbreviated 10k up to 999k, then 1M to 999M, then 1G.
A whitespace between number and suffix ("10 k") would look nicer but sadly isn´t possible; testing, I found that "972 M" overflows again, while "972M" just fits. The current font and layout don´t permit more than 4 chars total here.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/stock-amounts into lp:widelands.
=== modified file 'src/wui/waresdisplay.cc'
--- src/wui/waresdisplay.cc	2018-07-08 13:53:45 +0000
+++ src/wui/waresdisplay.cc	2018-09-10 09:50:43 +0000
@@ -22,7 +22,7 @@
 #include <cstdio>
 #include <utility>
 
-#include <boost/lexical_cast.hpp>
+#include <boost/format.hpp>
 
 #include "base/i18n.h"
 #include "base/wexception.h"
@@ -385,12 +385,31 @@
 	remove_all_warelists();
 }
 
+const char* unit_suffixes[] = {
+	"",
+	/** TRANSLATORS: This is a suffix for large numbers (5k = 5,000) */
+	_("k"),
+	/** TRANSLATORS: This is a suffix for large numbers (5M = 5,000,000) */
+	_("M"),
+	/** TRANSLATORS: This is a suffix for large numbers (5G = 5,000,000,000) */
+	_("G")
+};
+static std::string get_amount_string(uint32_t amount) {
+	uint8_t size = 0;
+	while (amount >= (size ? 1000 : 10000)) {
+		amount /= 1000;
+		size++;
+	}
+	/** TRANSLATORS: the first placeholder is a number and the second one a size suffix, e.g. 50 k */
+	return (boost::format(_("%1%%2%")) % amount % unit_suffixes[size]).str();
+}
+
 std::string WaresDisplay::info_for_ware(Widelands::DescriptionIndex ware) {
 	int totalstock = 0;
 	for (const Widelands::WareList* warelist : warelists_) {
 		totalstock += warelist->stock(ware);
 	}
-	return boost::lexical_cast<std::string>(totalstock);
+	return get_amount_string(totalstock);
 }
 
 /*
@@ -420,7 +439,7 @@
 				       "<div width=26 background=454545><p align=center><img src=\"" +
 				       tribe.get_ware_descr(c->first)->icon_filename() +
 				       "\"></p></div><div width=26 background=000000><p><font size=9>" +
-				       boost::lexical_cast<std::string>(static_cast<int32_t>(c->second)) +
+				       get_amount_string(c->second) +
 				       "</font></p></div></p></div>";
 			}
 	return ret;


Follow ups