Center correctly multi-chars letters in the text interface

This commit is contained in:
Olivier Teulière 2009-06-23 21:36:33 +00:00
parent adda457672
commit 795c44827d
26 changed files with 1576 additions and 1533 deletions

View file

@ -279,6 +279,40 @@ string padAndConvert(const wstring &iWstr, unsigned int iLength,
} }
string centerAndConvert(const wstring &iWstr, unsigned int iLength, char c)
{
int width = 0;
for (unsigned int i = 0; i < iWstr.size(); ++i)
{
int n = wcwidth(iWstr[i]);
if (n == -1)
{
ostringstream ss;
ss << "padAndConvert: non printable character: " << iWstr[i];
// XXX: Should we throw an exception instead? Just ignore the problem?
cerr << ss.str() << endl;;
//throw DicException(ss.str());
return convertToMb(iWstr);
}
width += n;
}
if ((unsigned int)width >= iLength)
return convertToMb(iWstr);
else
{
// Padding is needed
string s((iLength - width) / 2, c);
string res = s + convertToMb(iWstr) + s;
// If the string cannot be centered perfectly, pad again on the right
// (arbitrary; if needed, we could take the iLeftPad argument)
if (res.size() != iLength)
s.append(1, c);
return res;
}
}
unsigned int readFromUTF8(wchar_t *oString, unsigned int iWideSize, unsigned int readFromUTF8(wchar_t *oString, unsigned int iWideSize,
const char *iBuffer, unsigned int iBufSize, const char *iBuffer, unsigned int iBufSize,
const string &iContext) const string &iContext)

View file

@ -74,6 +74,15 @@ string truncOrPad(const string &iStr, unsigned int iWidth, char iChar = ' ');
string padAndConvert(const wstring &iWstr, unsigned int iLength, string padAndConvert(const wstring &iWstr, unsigned int iLength,
bool iLeftPad = true, char c = ' '); bool iLeftPad = true, char c = ' ');
/**
* Convert the given string into a multi-byte one. If the number of columns
* needed to display the resulting string is less than iLength, pad it with
* the given character (defaulting to space) on both left and right, trying
* to keep the string centered
*/
string centerAndConvert(const wstring &iWstr, unsigned int iLength,
char c = ' ');
/** /**
* Utility function to convert a char* buffer encoded in UTF-8 into a * Utility function to convert a char* buffer encoded in UTF-8 into a
* wchar_t* string * wchar_t* string

View file

@ -53,7 +53,7 @@ void GameIO::printBoard(ostream &out, const PublicGame &iGame)
if (iGame.getBoard().isVacant(row, col)) if (iGame.getBoard().isVacant(row, col))
out << " - "; out << " - ";
else else
out << padAndConvert(iGame.getBoard().getDisplayStr(row, col), 3); out << centerAndConvert(iGame.getBoard().getDisplayStr(row, col), 3);
} }
out << endl; out << endl;
} }