summaryrefslogtreecommitdiff
path: root/src/squelch/cursor.cc
blob: 3a3bec4678f9a5d4917a6ea9adea81da2b7f34e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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