Line.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/AnyOf.h>
  9. #include <AK/DistinctNumeric.h>
  10. #include <AK/Noncopyable.h>
  11. #include <AK/Vector.h>
  12. #include <LibVT/Attribute.h>
  13. #include <LibVT/Position.h>
  14. #include <LibVT/XtermColors.h>
  15. namespace VT {
  16. AK_TYPEDEF_DISTINCT_ORDERED_ID(u32, Mark);
  17. inline static constexpr Mark Unmarked = 0;
  18. class Line {
  19. AK_MAKE_NONCOPYABLE(Line);
  20. AK_MAKE_NONMOVABLE(Line);
  21. public:
  22. explicit Line(size_t length);
  23. ~Line() = default;
  24. struct Cell {
  25. u32 code_point { ' ' };
  26. Attribute attribute;
  27. bool operator!=(Cell const& other) const { return code_point != other.code_point || attribute != other.attribute; }
  28. };
  29. Attribute const& attribute_at(size_t index) const { return m_cells[index].attribute; }
  30. Attribute& attribute_at(size_t index) { return m_cells[index].attribute; }
  31. Cell& cell_at(size_t index) { return m_cells[index]; }
  32. Cell const& cell_at(size_t index) const { return m_cells[index]; }
  33. void clear(Attribute const& attribute = Attribute())
  34. {
  35. m_terminated_at.clear();
  36. m_mark = Unmarked;
  37. clear_range(0, m_cells.size() - 1, attribute);
  38. }
  39. void clear_range(size_t first_column, size_t last_column, Attribute const& attribute = Attribute());
  40. bool has_only_one_background_color() const;
  41. bool is_empty() const
  42. {
  43. return !any_of(m_cells, [](auto& cell) { return cell != Cell(); });
  44. }
  45. size_t length() const
  46. {
  47. return m_cells.size();
  48. }
  49. void set_length(size_t);
  50. void rewrap(size_t new_length, Line* next_line, CursorPosition* cursor, bool cursor_is_on_next_line = true);
  51. u32 code_point(size_t index) const
  52. {
  53. return m_cells[index].code_point;
  54. }
  55. void set_code_point(size_t index, u32 code_point)
  56. {
  57. if (m_terminated_at.has_value()) {
  58. if (index > *m_terminated_at) {
  59. m_terminated_at = index + 1;
  60. }
  61. }
  62. m_cells[index].code_point = code_point;
  63. }
  64. bool is_dirty() const { return m_dirty; }
  65. void set_dirty(bool b) { m_dirty = b; }
  66. Optional<Mark> mark() const
  67. {
  68. return m_mark == Unmarked ? OptionalNone {} : Optional<Mark>(m_mark);
  69. }
  70. void set_marked(Mark mark)
  71. {
  72. set_dirty(m_mark != mark);
  73. m_mark = mark;
  74. }
  75. Optional<u16> termination_column() const { return m_terminated_at; }
  76. void set_terminated(u16 column) { m_terminated_at = column; }
  77. private:
  78. void take_cells_from_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor);
  79. void push_cells_into_next_line(size_t new_length, Line* next_line, bool cursor_is_on_next_line, CursorPosition* cursor);
  80. Vector<Cell> m_cells;
  81. Mark m_mark { Unmarked };
  82. bool m_dirty { false };
  83. // Note: The alignment is 8, so this member lives in the padding (that already existed before it was introduced)
  84. [[no_unique_address]] Optional<u16> m_terminated_at;
  85. };
  86. }