LibVT/Kernel: Make VT::Attribute::Flags enum class, use AK EnumBits

Noticed the TODO in `Attribute.h` and realized we have as solution
to this problem already. :^)
This commit is contained in:
Brian Gianforcaro 2022-03-17 23:14:50 -07:00 committed by Andreas Kling
parent 8601f74d5f
commit 913374163c
Notes: sideshowbarker 2024-07-17 17:44:43 +09:00
4 changed files with 22 additions and 22 deletions

View File

@ -306,7 +306,7 @@ void VirtualConsole::flush_dirty_lines()
auto& cell = cell_at(column, visual_row);
auto foreground_color = terminal_to_standard_color(cell.attribute.effective_foreground_color());
if (cell.attribute.flags & VT::Attribute::Flags::Bold)
if (has_flag(cell.attribute.flags, VT::Attribute::Flags::Bold))
foreground_color = (Graphics::Console::Color)((u8)foreground_color | 0x08);
GraphicsManagement::the().console()->write(column,
visual_row,

View File

@ -6,6 +6,7 @@
#pragma once
#include <AK/EnumBits.h>
#include <AK/Noncopyable.h>
#include <AK/Vector.h>
#include <LibVT/Color.h>
@ -35,15 +36,12 @@ struct Attribute {
Color foreground_color { default_foreground_color };
Color background_color { default_background_color };
constexpr Color effective_background_color() const { return flags & Negative ? foreground_color : background_color; }
constexpr Color effective_foreground_color() const { return flags & Negative ? background_color : foreground_color; }
#ifndef KERNEL
String href;
String href_id;
#endif
enum Flags : u8 {
enum class Flags : u8 {
NoAttributes = 0x00,
Bold = 0x01,
Italic = 0x02,
@ -52,12 +50,14 @@ struct Attribute {
Blink = 0x10,
Touched = 0x20,
};
AK_ENUM_BITWISE_FRIEND_OPERATORS(Flags);
constexpr bool is_untouched() const { return !(flags & Touched); }
constexpr Color effective_background_color() const { return has_flag(flags, Flags::Negative) ? foreground_color : background_color; }
constexpr Color effective_foreground_color() const { return has_flag(flags, Flags::Negative) ? background_color : foreground_color; }
// TODO: it would be really nice if we had a helper for enums that
// exposed bit ops for class enums...
u8 flags { Flags::NoAttributes };
constexpr bool is_untouched() const { return has_flag(flags, Flags::Touched); }
Flags flags { Flags::NoAttributes };
constexpr bool operator==(const Attribute& other) const
{

View File

@ -250,34 +250,34 @@ void Terminal::SGR(Parameters params)
m_current_state.attribute.reset();
break;
case 1:
m_current_state.attribute.flags |= Attribute::Bold;
m_current_state.attribute.flags |= Attribute::Flags::Bold;
break;
case 3:
m_current_state.attribute.flags |= Attribute::Italic;
m_current_state.attribute.flags |= Attribute::Flags::Italic;
break;
case 4:
m_current_state.attribute.flags |= Attribute::Underline;
m_current_state.attribute.flags |= Attribute::Flags::Underline;
break;
case 5:
m_current_state.attribute.flags |= Attribute::Blink;
m_current_state.attribute.flags |= Attribute::Flags::Blink;
break;
case 7:
m_current_state.attribute.flags |= Attribute::Negative;
m_current_state.attribute.flags |= Attribute::Flags::Negative;
break;
case 22:
m_current_state.attribute.flags &= ~Attribute::Bold;
m_current_state.attribute.flags &= ~Attribute::Flags::Bold;
break;
case 23:
m_current_state.attribute.flags &= ~Attribute::Italic;
m_current_state.attribute.flags &= ~Attribute::Flags::Italic;
break;
case 24:
m_current_state.attribute.flags &= ~Attribute::Underline;
m_current_state.attribute.flags &= ~Attribute::Flags::Underline;
break;
case 25:
m_current_state.attribute.flags &= ~Attribute::Blink;
m_current_state.attribute.flags &= ~Attribute::Flags::Blink;
break;
case 27:
m_current_state.attribute.flags &= ~Attribute::Negative;
m_current_state.attribute.flags &= ~Attribute::Flags::Negative;
break;
case 30:
case 31:
@ -872,7 +872,7 @@ void Terminal::put_character_at(unsigned row, unsigned column, u32 code_point)
auto& line = active_buffer()[row];
line.set_code_point(column, code_point);
line.attribute_at(column) = m_current_state.attribute;
line.attribute_at(column).flags |= Attribute::Touched;
line.attribute_at(column).flags |= Attribute::Flags::Touched;
line.set_dirty(true);
m_last_code_point = code_point;

View File

@ -342,7 +342,7 @@ void TerminalWidget::paint_event(GUI::PaintEvent& event)
auto underline_style = UnderlineStyle::None;
auto underline_color = text_color;
if (attribute.flags & VT::Attribute::Underline) {
if (has_flag(attribute.flags, VT::Attribute::Flags::Underline)) {
// Content has specified underline
underline_style = UnderlineStyle::Solid;
} else if (!attribute.href.is_empty()) {
@ -410,7 +410,7 @@ void TerminalWidget::paint_event(GUI::PaintEvent& event)
painter.draw_glyph_or_emoji(
character_rect.location(),
code_point,
attribute.flags & VT::Attribute::Bold ? bold_font : font,
has_flag(attribute.flags, VT::Attribute::Flags::Bold) ? bold_font : font,
text_color);
}
}