Attribute.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Noncopyable.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <LibVT/Color.h>
  11. #include <LibVT/XtermColors.h>
  12. namespace VT {
  13. struct Attribute {
  14. static constexpr Color default_foreground_color = Color::named(Color::ANSIColor::DefaultForeground);
  15. static constexpr Color default_background_color = Color::named(Color::ANSIColor::DefaultBackground);
  16. void reset()
  17. {
  18. foreground_color = default_foreground_color;
  19. background_color = default_background_color;
  20. flags = Flags::NoAttributes;
  21. #ifndef KERNEL
  22. href = {};
  23. href_id = {};
  24. #endif
  25. }
  26. Color foreground_color { default_foreground_color };
  27. Color background_color { default_background_color };
  28. constexpr Color effective_background_color() const { return flags & Negative ? foreground_color : background_color; }
  29. constexpr Color effective_foreground_color() const { return flags & Negative ? background_color : foreground_color; }
  30. #ifndef KERNEL
  31. String href;
  32. String href_id;
  33. #endif
  34. enum Flags : u8 {
  35. NoAttributes = 0x00,
  36. Bold = 0x01,
  37. Italic = 0x02,
  38. Underline = 0x04,
  39. Negative = 0x08,
  40. Blink = 0x10,
  41. Touched = 0x20,
  42. };
  43. constexpr bool is_untouched() const { return !(flags & Touched); }
  44. // TODO: it would be really nice if we had a helper for enums that
  45. // exposed bit ops for class enums...
  46. u8 flags { Flags::NoAttributes };
  47. constexpr bool operator==(const Attribute& other) const
  48. {
  49. return foreground_color == other.foreground_color && background_color == other.background_color && flags == other.flags;
  50. }
  51. constexpr bool operator!=(const Attribute& other) const
  52. {
  53. return !(*this == other);
  54. }
  55. };
  56. }