You can use the following class to align text such as paragraphs to be centered, right, or left on a console. Also it supports some more methods. I have written this so it will be helpful for some and also for me. I'm not that pro so have wasted a lot of time to test and design this small class. So hope some of you will get some value from it. :) ... Also please test and tell me if there are unexpected results. Please feel free to comment about the class. I would really appreciate it. :)
/**
* Email: sinaru_52@yahoo.com
*/
#ifndef CONSOLE_H
#define CONSOLE_H
#include <iostream>
using namespace std;
/**
* This class is designed to format string outputs on the console. You can align
* strings to right side, left side, or center. Support newline character inside
* the string. Do not support backspace character.
* @param str
*/
class Console
{
public:
/**
* The string will be right aligned and shown to on the standard console.
* This method does not modifies the passed string.
* @param str
*/
void printRight(string str)
{
cout << getFrontNewLines(str);
cout << alignRight(str) << endl;
}
/**
* The string will be left aligned and shown to on the standard console.
* This method does not modifies the passed string.
* @param str
*/
void printLeft(string str)
{
cout << getFrontNewLines(str);
cout << alignLeft(str) << endl;
}
/**
* The string will be Centered and shown to on the standard console.
* This method does not modifies the passed string.
* @param str
*/
void printCenter(string str)
{
cout << getFrontNewLines(str);
cout << alignCenter(str) << endl;
}
/**
* The passed string will be to be right aligned according to the console.
* This method modidifies the original string. You can either use this to
* assign to another string or just use the method to modify the original
* string itself.
* @param str
* @return formatted text.
*/
string alignRight(string& str)
{
int len = str.length();
int loc = getBreakLoc(str);
if (0 < loc)
{
string tem;
if (str[loc] == '\n')
tem = str.substr(0, loc);
else
tem = str.substr(0, loc + 1);
str = str.substr(loc + 1, len - 1);
removeTrailingSpaces(tem);
return getRightSpace(tem) + tem + '\n' + alignRight(str);
}
else
{
string newLines = "";
int i = 0;
while (str[i] == '\n')
{
newLines = newLines + '\n';
i++;
}
if (i != 0)
str = str.substr(i, len-1);
removeTrailingSpaces(str);
return newLines + getRightSpace(str) + str;
}
}
/**
* The passed string will be to be left aligned according to the console.
* This method modidifies the original string. You can either use this to
* assign to another string or just use the method to modify the original
* string itself.
* @param str
* @return formatted text.
*/
string alignLeft(string & str)
{
int len = str.length();
int loc = getBreakLoc(str);
if (0 < loc)
{
string tem;
if (str[loc] == '\n')
tem = str.substr(0, loc);
else
tem = str.substr(0, loc + 1);
str = str.substr(loc + 1, len - 1);
removeFrontSpaces(tem);
return getLeftSpace() + tem + '\n' + alignLeft(str);
}
else
{
string newLines = "";
int i = 0;
while (str[i] == '\n')
{
newLines = newLines + '\n';
i++;
}
if (i != 0)
str = str.substr(i, len - 1);
removeFrontSpaces(str);
return newLines + getLeftSpace() + str;
}
}
/**
* The passed string will be to be centered aligned according to the console.
* This method modidifies the original string. You can either use this to
* assign to another string or just use the method to modify the original
* string itself.
* @param str
* @return formatted text.
*/
string alignCenter(string &str)
{
int len = str.length();
int loc = getBreakLoc(str);
if (0 < loc)
{
string tem;
if (str[loc] == ' ' || str[loc] == '\n')
tem = str.substr(0, loc);
else
tem = str.substr(0, loc + 1);
str = str.substr(loc + 1, len - 1);
return getCenterSpace(tem) + tem + '\n' + alignCenter(str);
}
else
{
string newLines = "";
int i = 0;
while (str[i] == '\n')
{
newLines = newLines + '\n';
i++;
}
if (i != 0)
str = str.substr(i, len - 1);
return newLines + getCenterSpace(str) + str;
}
}
/**
* You can use this method to set the pargraph width
* @param width
*/
void setParaWidth(int width)
{
if (width < 0)
{
cout << "Error: paragraph width cannot be a negative value." << endl;
return;
}
if (width > conWidth)
{
cout << "Error: Paragraph width is more than console width(ie. " <<
conWidth << "). width set to " << conWidth << "." << endl;
return;
}
paraWidth = width;
}
/**
* This add a left gap to left aligned strings. You can only insert a gap
* if the paragraph width is less than console width.
* @param gap
*/
void setLeftGap(int gap)
{
if(conWidth - paraWidth == 0)
{
cout <<"Error: You don't have space to set a left gap because\n"
"paragraph width is equal to console width. Reducing paragraph\n"
"width can fix this problem."<<endl;
return;
}
if (gap < 0)
{
cout << "Error: Left gap cannot be a negative value." << endl;
return;
}
if (gap > conWidth - paraWidth)
{
cout << "Error: Left gap is too large. The maximum value you can set is "
<< conWidth - paraWidth <<"." << endl;
return;
}
leftGap = gap;
}
/**
* This add a right gap to right aligned strings. You can only insert a gap
* if the paragraph width is less than console width.
* @param gap
*/
void setRightGap(int gap)
{
if(conWidth - paraWidth == 0)
{
cout <<"Error: You don't have space to set a right gap because\n"
"paragraph width is equal to console width. Reducing paragraph\n"
"width can fix this problem."<<endl;
return;
}
if (gap < 0)
{
cout << "Error: Right gap cannot be a negative value." << endl;
return;
}
if (gap > conWidth - paraWidth)
{
cout << "Error: Right gap is too large. The maximum value you can set is "
<< conWidth - paraWidth <<"." << endl;
return;
}
rightGap = gap;
}
void updateConWidth()
{
conWidth = getConWidth();
}
Console()
{
conWidth = getConWidth();
paraWidth = conWidth;
leftGap = 0;
rightGap = 0;
}
private:
void removeTrailingSpaces(string& str)
{
int len = str.length();
int i;
for (i = len - 1; i >= 0; i--)
if (str[i] != ' ')
break;
str = str.substr(0, i + 1);
}
void removeFrontSpaces(string& str)
{
int len = str.length();
int i;
for (i = 0; i < len; i++)
if (str[i] != ' ')
break;
str = str.substr(i, len + 1);
}
string getCenterSpace(const string& str)
{
string spaces = "";
int noofspaces = (conWidth - str.length()) / 2;
for (int i = 0; i < noofspaces; i++)
spaces = spaces + ' ';
return spaces;
}
string getRightSpace(const string& str)
{
string spaces = "";
int noofspaces = (conWidth - str.length()) - rightGap;
int i;
for (i = 0; i < noofspaces; i++)
spaces = spaces + ' ';
return spaces;
}
string getLeftSpace()
{
string spaces = "";
for (int i = 0; i < leftGap; i++)
spaces = spaces + ' ';
return spaces;
}
int getConWidth()
{
//Update this function according to your OS and return the console
// width
return 80;
}
string getFrontNewLines(string& str)
{
int len = str.length();
string newLines="";
int i;
for (i = 0; i < len; i++)
if (str[i] != '\n')
break;
newLines = str.substr(0, i);
str = str.substr(i, len);
return newLines;
}
int getBreakLoc(string &str)
{
int length = str.length();
for (int i = 0; i < length && i < paraWidth; i++)
if (str[i] == '\n')
return i;
if (length > paraWidth)
{
int loc = paraWidth - 1;
if (str[loc] != '\n' &&
(str[loc + 1] != ' ' && str[loc + 1] != '\n'))
{
for (; loc > 0; loc--)
if (str[loc] == ' ' || str[loc] == '\n')
return loc;
if (loc == 0)
{
loc = paraWidth - 1;
str.insert(loc, "-");
return loc;
}
}
else
return paraWidth - 1;
}
else
return -1;
}
int leftGap;
int rightGap;
int conWidth;
int paraWidth;
string firstNewLines;
};
#endif /* CONSOLE_H */