EnhancedConsoleText.cxx 3.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
// 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);
    }

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    void print(const std::string &content, FontEffect fontEffect, LineAlignment lineAlignment, FrontColor frontColor,
                 BackgroundColor bgColor) {
        std::regex newlineRegex("\n");
        std::smatch match;

        std::vector<std::string> lineSplits {
                std::sregex_token_iterator(content.begin(), content.end(), newlineRegex, -1), {}
        };

        for(auto i = 0; i < lineSplits.size(); i++) {
            std::cout << decorateString(lineSplits[i], fontEffect, lineAlignment, frontColor, bgColor)<<std::endl;
        }

    }

36 37
    void println(const std::string &content, FontEffect fontEffect, LineAlignment lineAlignment, FrontColor frontColor,
               BackgroundColor bgColor) {
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
        std::regex newlineRegex("\n");
        std::regex headingSpaceRegex("^\\s*");
        std::regex tailingSpaceRegex("\\s*$");
        std::smatch match;

        auto headingNewlines = 0;
        auto trailingNewlines = 0;

        std::string spaceTrimedContent;

        try {
            if (std::regex_search(content, match, headingSpaceRegex)) {
                auto headingSpaceStr = match.str();
                headingNewlines = headingSpaceStr.size();
            }

            if (std::regex_search(content, match, tailingSpaceRegex) && match.size() > 0) {
                auto trailingSpaceStr = match.str(0);
                trailingNewlines = trailingSpaceStr.size();
            }

            spaceTrimedContent = std::regex_replace(content, headingSpaceRegex, "");
            spaceTrimedContent = std::regex_replace(spaceTrimedContent, tailingSpaceRegex, "");

        } catch (std::regex_error& e) {
            // Syntax error in the regular expression
            std::cout<<e.what()<<std::endl;
        }

67
        std::vector<std::string> lineSplits {
68
                std::sregex_token_iterator(spaceTrimedContent.begin(), spaceTrimedContent.end(), newlineRegex, -1), {}
69 70
        };

71 72 73
        for(auto i = 0; i < headingNewlines; i ++)
            std::cout<<std::endl;

74 75 76
        for(auto i = 0; i < lineSplits.size(); i++) {
            std::cout << decorateString(lineSplits[i], fontEffect, lineAlignment, frontColor, bgColor)<<std::endl;
        }
77 78 79

        for(auto i = 0; i < trailingNewlines; i ++)
            std::cout<<std::endl;
80 81 82 83
    }

    void press2Continue(const std::string content) {
        println(content);
84 85 86
    // very ugly way to suppress warning
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wunused-result" 
87
        system("/bin/bash -c \"read -n 1 -s\"");
88
    #pragma GCC diagnostic pop 
89 90
    }
}