1 /**
2 Copyright: Copyright (c) 2015-2018 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.text.linebuffer;
7 
8 struct LineBuffer
9 {
10 	import std.array : empty;
11 	import std.format : formattedWrite;
12 	import voxelman.container.buffer;
13 
14 	Buffer!char lines;
15 	Buffer!size_t lineSizes;
16 	bool scrollToBottom;
17 
18 	void clear()
19 	{
20 		lines.clear();
21 		lineSizes.clear();
22 	}
23 
24 	void put(in char[] str)
25 	{
26 		import std.regex : regex, splitter;
27 		import std.algorithm : map;
28 		import std.range;
29 
30 		if (str.empty) return;
31 		auto splittedLines = splitter(str, regex("(\r\n|\r|\n|\v|\f)"));
32 
33 		foreach(first; splittedLines.takeOne())
34 		{
35 			lines.put(first);
36 			if (!lineSizes.data.empty)
37 			{
38 				lineSizes.data[$-1] += first.length;
39 			}
40 			else
41 			{
42 				lineSizes.put(first.length);
43 			}
44 		}
45 
46 		// process other lines
47 		foreach(line; splittedLines.drop(1))
48 		{
49 			++lineSizes.data[$-1];
50 			lines.put("\n");
51 			lines.put(line);
52 			lineSizes.put(line.length);
53 		}
54 
55 		lines.stealthPut('\0');
56 
57 		scrollToBottom = true;
58 	}
59 
60 	void putf(Args...)(const(char)[] fmt, Args args)
61 	{
62 		formattedWrite(&this, fmt, args);
63 	}
64 
65 	void putfln(Args...)(const(char)[] fmt, Args args)
66 	{
67 		formattedWrite(&this, fmt, args);
68 		put("\n");
69 	}
70 
71 	void putln(const(char)[] str)
72 	{
73 		put(str);
74 		put("\n");
75 	}
76 /*
77 	void draw()
78 	{
79 		char* lineStart = lines.data.ptr;
80 		foreach(lineSize; lineSizes.data)
81 		if (lineSize > 0)
82 		{
83 			igPushTextWrapPos(igGetWindowContentRegionWidth());
84 			igTextUnformatted(lineStart, lineStart+lineSize);
85 			igPopTextWrapPos();
86 			lineStart += lineSize;
87 		}
88 
89 		if (scrollToBottom)
90 			igSetScrollHere(1.0f);
91 		scrollToBottom = false;
92 	}
93 
94 	void drawSelectable()
95 	{
96 		igPushStyleVarVec(ImGuiStyleVar_FramePadding, ImVec2(6,6));
97 
98 		ImVec2 size;
99 		igGetContentRegionAvail(&size);
100 		size.x -= 12;
101 		size.y -= 12;
102 
103 		igPushStyleColor(ImGuiCol_FrameBg, ImVec4(0, 0, 0, 0));
104 		if (lines.data.length)
105 			igInputTextMultiline("##multiline", lines.data.ptr, lines.data.length, size, ImGuiInputTextFlags_ReadOnly);
106 		else
107 		{
108 			char[1] str;
109 			igInputTextMultiline("##multiline", str.ptr, 0, size, ImGuiInputTextFlags_ReadOnly);
110 		}
111 		igPopStyleColor();
112 
113 		igPopStyleVar();
114 
115 		if (scrollToBottom)
116 			igSetScrollHere(1.0f);
117 		scrollToBottom = false;
118 	}*/
119 }