-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hout.cpp
66 lines (57 loc) · 1.98 KB
/
Hout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Hout.h"
#include <algorithm>
#include <iostream>
#include <iomanip>
namespace Hout {
std::wstring::size_type terminal_size=79; //Default terminal size for Win32 = 80 columns (79 characters + 1 new-line symbol)
}
void Hout::Paragraph(const wchar_t* str, std::wstring::size_type indent, const wchar_t* padding)
{
if (indent>=terminal_size) {
std::wcout<<(padding?padding:L"")<<str<<std::endl;
return;
}
std::wstring::size_type paragraph_width=terminal_size-indent;
std::wstring::size_type break_point;
std::wstring output=str;
for (;;) {
if (output.length()<=paragraph_width) {
std::wcout<<std::setw(indent)<<std::right<<(padding?std::wstring(padding, std::min(indent, wcslen(padding))):L"")<<std::setw(paragraph_width)<<std::left<<output<<std::endl;
break;
} else {
if ((break_point=output.rfind(L' ', paragraph_width-1))==std::wstring::npos) break_point=paragraph_width;
else break_point++;
std::wcout<<std::setw(indent)<<std::right<<(padding?std::wstring(padding, std::min(indent, wcslen(padding))):L"")<<std::setw(paragraph_width)<<std::left<<output.substr(0, break_point)<<std::endl;
output.erase(0, break_point);
}
if (padding) padding=NULL;
}
}
void Hout::Separator(const wchar_t* str, std::wstring::size_type indent, wchar_t filler)
{
if (indent>=terminal_size) {
std::wcout<<str<<std::endl;
return;
}
std::wstring padding(indent, L' ');
std::wstring output=str;
output.resize(terminal_size-indent, filler);
std::wcout<<padding<<output.c_str()<<std::endl; //Passing as c_str() so excessing NULLs will be omitted
}
void Hout::EmptyLine()
{
std::wcout<<std::endl;
}
void Hout::SetTerminalSize(std::wstring::size_type size)
{
//Windows console will move every characters that doesn't fit in console width to the next line, including new-line symbol
//So we should reserve one last character for the new-line symbol
if (size)
terminal_size=size-1;
else
terminal_size=0;
}
std::wstring::size_type Hout::GetTerminalSize()
{
return terminal_size;
}