History.cpp 786 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "History.h"
  7. void History::push(StringView history_item)
  8. {
  9. if (!m_items.is_empty() && m_items[m_current_history_item] == history_item)
  10. return;
  11. m_items.shrink(m_current_history_item + 1);
  12. m_items.append(history_item);
  13. m_current_history_item++;
  14. }
  15. String History::current()
  16. {
  17. if (m_current_history_item == -1)
  18. return {};
  19. return m_items[m_current_history_item];
  20. }
  21. void History::go_back()
  22. {
  23. VERIFY(can_go_back());
  24. m_current_history_item--;
  25. }
  26. void History::go_forward()
  27. {
  28. VERIFY(can_go_forward());
  29. m_current_history_item++;
  30. }
  31. void History::clear()
  32. {
  33. m_items = {};
  34. m_current_history_item = -1;
  35. }