Convert highlighted_char_t to a struct

This commit is contained in:
Mahmoud Al-Qudsi 2020-07-04 20:23:50 -05:00
parent 44944146e2
commit b8a16a8ba0
3 changed files with 11 additions and 7 deletions

View File

@ -2346,7 +2346,7 @@ struct pager_layout_testcase_t {
wcstring text;
for (const auto &p : sd.line(0).text) {
text.push_back(p.first);
text.push_back(p.character);
}
if (text != expected) {
std::fwprintf(stderr, L"width %zu got %zu<%ls>, expected %zu<%ls>\n", this->width,

View File

@ -87,7 +87,7 @@ static int fish_wcwidth_min_0(wchar_t widechar) { return std::max(0, fish_wcwidt
int line_t::wcswidth_min_0(size_t max) const {
int result = 0;
for (size_t idx = 0, end = std::min(max, text.size()); idx < end; idx++) {
result += fish_wcwidth_min_0(text[idx].first);
result += fish_wcwidth_min_0(text[idx].character);
}
return result;
}

View File

@ -31,8 +31,12 @@ class page_rendering_t;
/// A class representing a single line of a screen.
struct line_t {
struct highlighted_char_t {
highlight_spec_t highlight;
wchar_t character;
};
/// A pair of a character, and the color with which to draw it.
using highlighted_char_t = std::pair<wchar_t, highlight_spec_t>;
std::vector<highlighted_char_t> text{};
bool is_soft_wrapped{false};
size_t indentation{0};
@ -43,12 +47,12 @@ struct line_t {
void clear(void) { text.clear(); }
/// Append a single character \p txt to the line with color \p c.
void append(wchar_t c, highlight_spec_t color) { text.push_back({c, color}); }
void append(wchar_t c, highlight_spec_t color) { text.push_back({color, c}); }
/// Append a nul-terminated string \p txt to the line, giving each character \p color.
void append(const wchar_t *txt, highlight_spec_t color) {
for (size_t i = 0; txt[i]; i++) {
text.push_back({txt[i], color});
text.push_back({color, txt[i]});
}
}
@ -56,10 +60,10 @@ struct line_t {
size_t size() const { return text.size(); }
/// \return the character at a char index.
wchar_t char_at(size_t idx) const { return text.at(idx).first; }
wchar_t char_at(size_t idx) const { return text.at(idx).character; }
/// \return the color at a char index.
highlight_spec_t color_at(size_t idx) const { return text.at(idx).second; }
highlight_spec_t color_at(size_t idx) const { return text.at(idx).highlight; }
/// Append the contents of \p line to this line.
void append_line(const line_t &line) {