fix: fix all old usage of UTF8 in std::ostream.
- use patch stream instead of all old use of UTF8 in std::ostream (reinterpret way).
This commit is contained in:
@ -1,14 +1,15 @@
|
||||
#include "tabulate.hpp"
|
||||
#include "wcwidth.hpp"
|
||||
#include "../num/safe_op.hpp"
|
||||
#include "../string/reinterpret.hpp"
|
||||
#include "../patch/stream.hpp"
|
||||
#include <stdexcept>
|
||||
#include <ranges>
|
||||
|
||||
#define WCWIDTH ::yycc::carton::wcwidth
|
||||
#define REINTERPRET ::yycc::string::reinterpret
|
||||
#define SAFEOP ::yycc::num::safe_op
|
||||
|
||||
using namespace yycc::patch::stream;
|
||||
|
||||
namespace yycc::carton::tabulate {
|
||||
|
||||
#pragma region Tabulate Width
|
||||
@ -94,38 +95,34 @@ namespace yycc::carton::tabulate {
|
||||
std::u8string_view spaces_view(spaces);
|
||||
|
||||
// Print table
|
||||
// Define a convenient macro
|
||||
#define CVT(data) REINTERPRET::as_ordinary_view(data)
|
||||
// Show header
|
||||
if (this->header_display) {
|
||||
dst << CVT(this->prefix_string);
|
||||
dst << this->prefix_string;
|
||||
for (const auto [index, item] : std::views::enumerate(header)) {
|
||||
auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), item.get_text_width());
|
||||
dst << CVT(item.get_text()) << CVT(spaces_view.substr(0, diff)) << " ";
|
||||
dst << item.get_text() << spaces_view.substr(0, diff) << " ";
|
||||
}
|
||||
dst << std::endl;
|
||||
}
|
||||
// Show bar
|
||||
if (this->bar_display) {
|
||||
dst << CVT(this->prefix_string);
|
||||
dst << this->prefix_string;
|
||||
auto bar_width = this->bar.get_text_width();
|
||||
for (auto index : std::views::iota(ZERO, n)) {
|
||||
auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), bar_width);
|
||||
dst << CVT(this->bar.get_text()) << CVT(spaces_view.substr(0, diff)) << " ";
|
||||
dst << this->bar.get_text() << spaces_view.substr(0, diff) << " ";
|
||||
}
|
||||
dst << std::endl;
|
||||
}
|
||||
// Show data
|
||||
for (const auto& row : this->rows) {
|
||||
dst << CVT(this->prefix_string);
|
||||
dst << this->prefix_string;
|
||||
for (const auto [index, item] : std::views::enumerate(row)) {
|
||||
auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), item.get_text_width());
|
||||
dst << CVT(item.get_text()) << CVT(spaces_view.substr(0, diff)) << " ";
|
||||
dst << item.get_text() << spaces_view.substr(0, diff) << " ";
|
||||
}
|
||||
dst << std::endl;
|
||||
}
|
||||
// Undef macro
|
||||
#undef CVT
|
||||
}
|
||||
|
||||
size_t Tabulate::get_column_count() const {
|
||||
|
@ -1,13 +1,13 @@
|
||||
#include "termcolor.hpp"
|
||||
#include "../flag_enum.hpp"
|
||||
#include "../string/reinterpret.hpp"
|
||||
#include "../patch/stream.hpp"
|
||||
#include <stdexcept>
|
||||
#include <bit>
|
||||
|
||||
#define FLAG_ENUM ::yycc::flag_enum
|
||||
#define REINTERPRET ::yycc::string::reinterpret
|
||||
|
||||
using namespace std::literals::string_view_literals;
|
||||
using namespace yycc::patch::stream;
|
||||
|
||||
namespace yycc::carton::termcolor {
|
||||
|
||||
@ -213,7 +213,7 @@ namespace yycc::carton::termcolor {
|
||||
}
|
||||
|
||||
void cprint(const std::u8string_view& words, Color foreground, Color background, Attribute styles, std::ostream& dst) {
|
||||
dst << REINTERPRET::as_ordinary_view(colored(words, foreground, background, styles));
|
||||
dst << colored(words, foreground, background, styles);
|
||||
}
|
||||
|
||||
void ecprint(const std::u8string_view& words, Color foreground, Color background, Attribute styles) {
|
||||
|
@ -6,11 +6,10 @@
|
||||
* @brief This namespace add UTF8 support for \c std::ostream.
|
||||
* @details
|
||||
* The operator overloads written in this namespace will give \c std::ostream ability to write UTF8 string and its char.
|
||||
* For using this feature, please directly use <TT>using namespace yycc::patch:stream;</TT> to import this namespace.
|
||||
* For using this feature, please directly use <TT>using namespace yycc::patch::stream;</TT> to import this namespace.
|
||||
*/
|
||||
namespace yycc::patch::stream {
|
||||
|
||||
// TODO: fix all REINTERPRET::as_ordinary_view polyfill for std::ostream.
|
||||
// TODO: replace all old way (C-style output) with this new way if possible.
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str);
|
||||
|
@ -1,12 +1,13 @@
|
||||
#include "panic.hpp"
|
||||
#include "../carton/termcolor.hpp"
|
||||
#include "../string/reinterpret.hpp"
|
||||
#include "../patch/stream.hpp"
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
#define TERMCOLOR ::yycc::carton::termcolor
|
||||
#define REINTERPRET ::yycc::string::reinterpret
|
||||
|
||||
using namespace yycc::patch::stream;
|
||||
|
||||
namespace yycc::rust::panic {
|
||||
|
||||
@ -23,13 +24,13 @@ namespace yycc::rust::panic {
|
||||
|
||||
// Print error message if we support it.
|
||||
// Setup color
|
||||
dst << REINTERPRET::as_ordinary_view(TERMCOLOR::foreground(TERMCOLOR::Color::Red));
|
||||
dst << TERMCOLOR::foreground(TERMCOLOR::Color::Red);
|
||||
// File name and line number message
|
||||
dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl;
|
||||
// User custom message
|
||||
dst << "note: " << msg << std::endl;
|
||||
// Restore color
|
||||
dst << REINTERPRET::as_ordinary_view(TERMCOLOR::reset());;
|
||||
dst << TERMCOLOR::reset();
|
||||
|
||||
// Make sure all messages are flushed into screen.
|
||||
dst.flush();
|
||||
|
Reference in New Issue
Block a user