summaryrefslogtreecommitdiff
path: root/src/scrollpad.cpp
blob: 27af910b77b9faf25d67f828cc8fe35884ed59c3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/***************************************************************************
 *   Copyright (C) 2008-2014 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include <cassert>
#include <boost/regex.hpp>
#include <iostream>

#include "scrollpad.h"

namespace {

template <typename PropT>
bool regexSearch(NC::Buffer &buf, PropT begin, const std::string &ws, PropT end, boost::regex::flag_type flags, size_t id)
{
	try {
		boost::regex rx(ws, flags);
		auto first = boost::sregex_iterator(buf.str().begin(), buf.str().end(), rx);
		auto last = boost::sregex_iterator();
		bool success = first != last;
		for (; first != last; ++first)
		{
			buf.addProperty(first->position(), begin, id);
			buf.addProperty(first->position() + first->length(), end, id);
		}
		return success;
	} catch (boost::bad_expression &e) {
		std::cerr << "regexSearch: bad_expression: " << e.what() << "\n";
		return false;
	}
}

}

namespace NC {

Scrollpad::Scrollpad(size_t startx,
size_t starty,
size_t width,
size_t height,
const std::string &title,
Color color,
Border border)
: Window(startx, starty, width, height, title, color, border),
m_beginning(0),
m_real_height(height)
{
}

void Scrollpad::refresh()
{
	assert(m_real_height >= m_height);
	size_t max_beginning = m_real_height - m_height;
	m_beginning = std::min(m_beginning, max_beginning);
	prefresh(m_window, m_beginning, 0, m_start_y, m_start_x, m_start_y+m_height-1, m_start_x+m_width-1);
}

void Scrollpad::resize(size_t new_width, size_t new_height)
{
	adjustDimensions(new_width, new_height);
	recreate(new_width, new_height);
	flush();
}

void Scrollpad::scroll(Scroll where)
{
	assert(m_real_height >= m_height);
	size_t max_beginning = m_real_height - m_height;
	switch (where)
	{
		case Scroll::Up:
		{
			if (m_beginning > 0)
				--m_beginning;
			break;
		}
		case Scroll::Down:
		{
			if (m_beginning < max_beginning)
				++m_beginning;
			break;
		}
		case Scroll::PageUp:
		{
			if (m_beginning > m_height)
				m_beginning -= m_height;
			else
				m_beginning = 0;
			break;
		}
		case Scroll::PageDown:
		{
			m_beginning = std::min(m_beginning + m_height, max_beginning);
			break;
		}
		case Scroll::Home:
		{
			m_beginning = 0;
			break;
		}
		case Scroll::End:
		{
			m_beginning = max_beginning;
			break;
		}
	}
}

void Scrollpad::clear()
{
	m_real_height = m_height;
	m_buffer.clear();
	werase(m_window);
	delwin(m_window);
	m_window = newpad(m_height, m_width);
	setTimeout(m_window_timeout);
	setColor(m_color);
}

const std::string &Scrollpad::buffer()
{
	return m_buffer.str();
}

void Scrollpad::flush()
{
	auto &w = static_cast<Window &>(*this);
	const auto &s = m_buffer.str();
	const auto &ps = m_buffer.properties();
	auto p = ps.begin();
	size_t i = 0;
	
	auto load_properties = [&]() {
		for (; p != ps.end() && p->first == i; ++p)
			w << p->second;
	};
	auto write_whitespace = [&]() {
		for (; i < s.length() && iswspace(s[i]); ++i)
		{
			load_properties();
			w << s[i];
		}
	};
	auto write_word = [&](bool load_properties_) {
		for (; i < s.length() && !iswspace(s[i]); ++i)
		{
			if (load_properties_)
				load_properties();
			w << s[i];
		}
	};
	auto write_buffer = [&](bool generate_height_only) -> size_t {
		int new_y;
		size_t height = 1;
		size_t old_i;
		auto old_p = p;
		int x, y;
		i = 0;
		p = ps.begin();
		y = getY();
		while (i < s.length())
		{
			// write all whitespaces.
			write_whitespace();
			
			// if we are generating height, check difference
			// between previous Y coord and current one and
			// update height accordingly.
			if (generate_height_only)
			{
				new_y = getY();
				height += new_y - y;
				y = new_y;
			}
			
			if (i == s.length())
				break;
			
			// save current string position state and get current
			// coordinates as we are before the beginning of a word.
			old_i = i;
			old_p = p;
			x = getX();
			y = getY();
			
			// write word to test if it overflows, but do not load properties
			// yet since if it overflows, we do not want to load them twice.
			write_word(false);
			
			// restore previous indexes state
			i = old_i;
			p = old_p;
			
			// get new Y coord to see if word overflew into next line.
			new_y = getY();
			if (new_y != y)
			{
				if (generate_height_only)
				{
					// if it did, let's update height...
					++height;
				}
				else
				{
					// ...or go to old coordinates, erase
					// part of the string from previous line...
					goToXY(x, y);
					wclrtoeol(m_window);
				}
				
				// ...start at the beginning of next line...
				++y;
				goToXY(0, y);
				
				// ...write word again, this time with properties...
				write_word(true);
				
				if (generate_height_only)
				{
					// ... and check for potential
					// difference in Y coordinates again.
					new_y = getY();
					height += new_y - y;
				}
			}
			else
			{
				// word didn't overflow, rewrite it with properties.
				goToXY(x, y);
				write_word(true);
			}
			
			if (generate_height_only)
			{
				// move to the first line, since when we do
				// generation, m_real_height = m_height and we
				// don't have many lines to use.
				goToXY(getX(), 0);
				y = 0;
			}
		}
		// load remaining properties if there are any
		for (; p != ps.end(); ++p)
			w << p->second;
		return height;
	};
	m_real_height = std::max(write_buffer(true), m_height);
	if (m_real_height > m_height)
		recreate(m_width, m_real_height);
	else
		werase(m_window);
	write_buffer(false);
}

void Scrollpad::reset()
{
	m_beginning = 0;
}

bool Scrollpad::setProperties(Color begin, const std::string &s, Color end, size_t flags, size_t id)
{
	return regexSearch(m_buffer, std::move(begin), s, std::move(end), id, flags);
}

bool Scrollpad::setProperties(Format begin, const std::string &s, Format end, size_t flags, size_t id)
{
	return regexSearch(m_buffer, begin, s, end, flags, id);
}

void Scrollpad::removeProperties(size_t id)
{
	m_buffer.removeProperties(id);
}

}