Commit 0d65791a authored by Zhiping Jiang (蒋志平)'s avatar Zhiping Jiang (蒋志平) 💬

initialize this repo

parent 16ba3853
cmake_minimum_required(VERSION 3.5)
PROJECT(EnhancedConsoleText)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
file(GLOB EnhancedConsoleText_Source src/*.*)
add_library(EnhancedConsoleText SHARED ${EnhancedConsoleText_Source})
//
// Created by 蒋志平 on 2018/4/19.
//
#include "EnhancedConsoleText.hxx"
namespace EnhancedConsoleText {
int lineWidth = 80;
float cjkSpaceRatio = 1.5;
std::wstring string2WString(std::string content) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(content);
}
std::string wstring2String(std::wstring content) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(content);
}
void println(const std::string &content, FontEffect fontEffect, LineAlignment lineAlignment, FrontColor frontColor,
BackgroundColor bgColor) {
std::regex ws_re("\n");
std::vector<std::string> lineSplits {
std::sregex_token_iterator(content.begin(), content.end(), ws_re, -1), {}
};
for(auto i = 0; i < lineSplits.size(); i++) {
std::cout << decorateString(lineSplits[i], fontEffect, lineAlignment, frontColor, bgColor)<<std::endl;
}
}
void press2Continue(const std::string content) {
println(content);
system("/bin/bash -c \"read -n 1 -s\"");
}
}
\ No newline at end of file
//
// Created by 蒋志平 on 2018/4/19.
//
#ifndef ENHANCEDCONSOLETEXT_HXX
#define ENHANCEDCONSOLETEXT_HXX
#include <iostream>
#include <string>
#include <random>
#include <locale>
#include <codecvt>
#include <regex>
#include <vector>
enum class FrontColor {
NoColor = 0,
Black = 30,
Red = 31,
Green = 32,
Yellow = 33,
Blue = 34,
Magenta = 35,
Cyan = 36,
While = 37,
};
enum class BackgroundColor {
NoColor = 0,
Black = 40,
Red = 41,
Green = 42,
Yellow = 43,
Blue = 44,
Magenta = 45,
Cyan = 46,
While = 47,
};
enum class FontEffect: int {
Reset = 0,
Bold = 1,
Underline = 4,
};
enum class LineAlignment {
Left,
Center,
Right,
};
template <typename Enumeration>
constexpr std::enable_if_t<std::is_enum<Enumeration>::value,
std::underlying_type_t<Enumeration>> as_integer(const Enumeration value)
{
return static_cast<std::underlying_type_t<Enumeration>>(value);
}
namespace EnhancedConsoleText {
extern int lineWidth;
extern float cjkSpaceRatio;
std::wstring string2WString(std::string content);
std::string wstring2String(std::wstring content);
std::string decorateString(const std::string &content, FontEffect fontEffect, LineAlignment lineAlignment, FrontColor frontColor,
BackgroundColor bgColor);
void println(const std::string &content, FontEffect frontEffect = FontEffect::Reset,
LineAlignment lineAlignment = LineAlignment::Left,
FrontColor frontColor = FrontColor::NoColor,
BackgroundColor bgColor = BackgroundColor::NoColor);
void press2Continue(const std::string content = "Press any key to continue...");
};
#endif //ENHANCEDCONSOLETEXT_HXX
//
// Created by 蒋志平 on 2018/4/20.
//
#include "EnhancedConsoleText.hxx"
namespace EnhancedConsoleText {
std::pair<std::string, std::string> fontEnhancement(FontEffect fontEffect, FrontColor frontColor, BackgroundColor bgColor) {
constexpr auto heading = "\033[";
constexpr auto ending = "m";
std::string beginningDeco = "";
std::string endingDeco = "0";
switch(fontEffect) {
case FontEffect::Bold: {
beginningDeco += "1;";
break;
}
case FontEffect::Underline: {
beginningDeco += "4;";
break;
}
default: {
beginningDeco += "0;";
break;
}
}
if (frontColor != FrontColor::NoColor) {
beginningDeco += std::to_string(static_cast<int>(frontColor));
beginningDeco += ";";
}
if (bgColor != BackgroundColor::NoColor) {
beginningDeco += std::to_string(static_cast<int>(bgColor));
beginningDeco += ";";
}
beginningDeco = std::regex_replace(beginningDeco, std::regex(";$"), ""); // remove the ending ";"
return std::make_pair(heading+beginningDeco+ending, heading+endingDeco+ending);
};
std::string stringSpace(int spaceCount) {
std::string spaceString = "";
for (int i = 0; i < spaceCount; ++i) {
spaceString += " ";
}
return spaceString;
}
std::string decorateString(const std::string &content, FontEffect fontEffect, LineAlignment lineAlignment, FrontColor frontColor,
BackgroundColor bgColor) {
auto wContent = string2WString(content);
float wLength = wContent.size();
wLength *= cjkSpaceRatio;
auto enhancingPrefixAndPostfix = fontEnhancement(fontEffect, frontColor, bgColor);
auto resultingString = enhancingPrefixAndPostfix.first + content + enhancingPrefixAndPostfix.second;
switch(lineAlignment) {
case LineAlignment::Left : {
int paddingSpaces_n = (lineWidth - wLength);
auto paddingSpaces = stringSpace(paddingSpaces_n);
resultingString = resultingString + paddingSpaces;
break;
}
case LineAlignment::Center : {
int paddingSpaces_n = (lineWidth - wLength)/2;
auto paddingSpaces = stringSpace(paddingSpaces_n);
resultingString = paddingSpaces + resultingString + paddingSpaces;
break;
}
case LineAlignment::Right : {
int paddingSpaces_n = (lineWidth - wLength);
auto paddingSpaces = stringSpace(paddingSpaces_n);
resultingString = paddingSpaces + resultingString;
break;
}
}
return resultingString;
}
}
//
// Created by 蒋志平 on 2018/4/20.
//
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment