~kiwec/ke

ref: 4e0a6570b5c10ffd1830ab776619aa0a2f95ddfb ke/src/Gutter.cc -rw-r--r-- 1.1 KiB
4e0a6570 — Wolf Clément Change file style 3 years ago
                                                                                
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
#include <cmath>
#include "Buffer.h"
#include "Line.h"
#include "Gutter.h"

void Gutter::draw(Buffer& buf) {
	const uint fg = TB_DEFAULT;
	const uint bg = 237;
	const uint clbg = 238;

	auto canvas = buf.get_canvas();
	uint max_width = width(buf);
	uint start = buf.first_visible_line_nb();
	uint end = start + canvas.height();
	auto current_line = buf.current_line();
	auto line = buf.m_text.get_line(start);

	uint i;
	for(i = start; i < end; line = line.next()) {
		bool isCurLine = line.number() == current_line.number();
		uint line_height = isCurLine ? line.height(buf) : 1;

		auto str = std::to_wstring(i);
		uint offset = max_width - str.length();

		for(uint y = 0; y < line_height; y++) {
			for(uint x = 0; x < max_width; x++) {
				wchar_t c = ' ';
				if(x >= offset && y == 0)
					c = str[x - offset];
				tb_change_cell(x, i - start, c, fg, isCurLine ? clbg : bg);
			}
			i++;
		}

		if(line.is_last()) break;
	}
}

uint Gutter::width(Buffer& buf) {
	auto nblines = buf.m_text.get_nb_lines();
	auto w = std::to_string(nblines).length();
	return w > 3 ? w : 3;
}