summaryrefslogtreecommitdiff
path: root/src/squelch/tree_printer.cc
blob: 0dbceec910fb1fdc1c1f2cff1f80d34e0488d861 (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
#include "tome/squelch/tree_printer_fwd.hpp"
#include "tome/squelch/tree_printer.hpp"

#include "../z-term.h"

namespace squelch {

TreePrinter::TreePrinter() : m_indent(0)
{
	int wid, hgt;
	// Output window
	Term_get_size(&wid, &hgt);
	m_write_out_y = 1;
	m_write_out_x = 16;
	m_write_out_h = hgt - 4 - 1;
	m_write_out_w = wid - 1 - 15 - 1;
	// Set position
	reset();
	reset_scroll();
}

void TreePrinter::indent() {
	m_indent++;
}

void TreePrinter::dedent() {
	m_indent--;
}

void TreePrinter::reset() {
	m_write_x = 0;
	m_write_y = 0;
}

void TreePrinter::reset_scroll() {
	m_write_off_y = 0;
	m_write_off_x = 0;
}

void TreePrinter::scroll_up() {
	m_write_off_y--;
}

void TreePrinter::scroll_down() {
	m_write_off_y++;
}

void TreePrinter::scroll_left() {
	m_write_off_x++;
}

void TreePrinter::scroll_right() {
	m_write_off_x--;
}

void TreePrinter::write(uint8_t color, cptr line)
{
	cptr p = line;

	for (p = line; *p != '\0'; p++)
	{
		char c = *p;
		int x = m_write_x - m_write_off_x + 3*m_indent;
		int y = m_write_y - m_write_off_y;

		if (c != '\n')
		{
			if ((y >= 0) &&
			    (y < m_write_out_h) &&
			    (x >= 0) &&
			    (x < m_write_out_w))
			{
				Term_putch(x + m_write_out_x,
					   y + m_write_out_y,
					   color,
					   c);
			}

			m_write_x += 1;
		}
		else
		{
			m_write_x = 0;
			m_write_y += 1;
		}
	}
}

void TreePrinter::write(uint8_t color, std::string const &line)
{
	write(color, line.c_str());
}

} // namespace