diff options
author | Manoj Srivastava <srivasta@debian.org> | 2020-05-22 19:57:41 -0700 |
---|---|---|
committer | Manoj Srivastava <srivasta@debian.org> | 2020-05-22 20:02:19 -0700 |
commit | c3d2579ad8d7eb33059aa8fdbaf5b564411a57f2 (patch) | |
tree | 1570cda0676fdcf4171a69a7fe313c1b89a52b0c /src/squelch/cursor.cc | |
parent | 986b7742bf244b4073ecca0723615f70be8a1ab6 (diff) | |
parent | 4e9b9c402ed95bf9a17fd6d795bc49bb4128a6fa (diff) |
Merge branch 'upstream' into debian-cmake-fixes
Diffstat (limited to 'src/squelch/cursor.cc')
-rw-r--r-- | src/squelch/cursor.cc | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/squelch/cursor.cc b/src/squelch/cursor.cc new file mode 100644 index 00000000..3a3bec46 --- /dev/null +++ b/src/squelch/cursor.cc @@ -0,0 +1,96 @@ +#include "tome/squelch/cursor_fwd.hpp" +#include "tome/squelch/cursor.hpp" + +#include <algorithm> +#include <cassert> + +#include "tome/squelch/condition.hpp" + +namespace squelch { + +bool Cursor::is_selected(Condition const *condition) const +{ + return std::end(m_conditions) != + std::find(std::begin(m_conditions), + std::end(m_conditions), + condition); +} + +Condition *Cursor::pop() +{ + assert(!m_conditions.empty()); + Condition *c = m_conditions.back(); + m_conditions.pop_back(); + return c; +} + +Condition *Cursor::current() +{ + assert(!m_conditions.empty()); + return m_conditions.back(); +} + +void Cursor::move_right() +{ + if (m_conditions.empty()) { + return; + } + // Go right if the currently selected condition has children. + std::shared_ptr<Condition> c = current()->first_child(); + if (c) + { + push(c.get()); + } +} + +void Cursor::move_left() +{ + if (size() > 1) + { + pop(); + } +} + +void Cursor::move_up() +{ + if (size() > 1) + { + Condition *prev_top = pop(); + + // Find previous child + std::shared_ptr<Condition> prev_condition = + current()->previous_child(prev_top); + + // Do we have a previous child? + if (prev_condition) + { + push(prev_condition.get()); + } + else + { + push(prev_top); + } + } +} + +void Cursor::move_down() +{ + if (size() > 1) + { + Condition *prev_top = pop(); + + std::shared_ptr<Condition> next_condition = + current()->next_child(prev_top); + + if (next_condition) + { + push(next_condition.get()); + } + else + { + push(prev_top); + } + } +} + +} // namespace |