Position.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/Types.h>
  8. namespace VT {
  9. class Position {
  10. public:
  11. Position() = default;
  12. Position(int row, int column)
  13. : m_row(row)
  14. , m_column(column)
  15. {
  16. }
  17. bool is_valid() const { return m_row >= 0 && m_column >= 0; }
  18. int row() const { return m_row; }
  19. int column() const { return m_column; }
  20. bool operator<(Position const& other) const
  21. {
  22. return m_row < other.m_row || (m_row == other.m_row && m_column < other.m_column);
  23. }
  24. bool operator<=(Position const& other) const
  25. {
  26. return *this < other || *this == other;
  27. }
  28. bool operator>=(Position const& other) const
  29. {
  30. return !(*this < other);
  31. }
  32. bool operator==(Position const& other) const
  33. {
  34. return m_row == other.m_row && m_column == other.m_column;
  35. }
  36. bool operator!=(Position const& other) const
  37. {
  38. return !(*this == other);
  39. }
  40. private:
  41. int m_row { -1 };
  42. int m_column { -1 };
  43. };
  44. struct CursorPosition {
  45. size_t row { 0 };
  46. size_t column { 0 };
  47. void clamp(u16 max_row, u16 max_column)
  48. {
  49. row = min(row, max_row);
  50. column = min(column, max_column);
  51. }
  52. };
  53. }