simple, slow and stupid console rendering
[dd2d.git] / conbuf.d
blobe6c7077c522bb798c03174838a94352f3484385f
1 module conbuf is aliced;
2 private:
4 // ////////////////////////////////////////////////////////////////////////// //
5 import core.sync.mutex : Mutex;
6 __gshared Mutex conbufLock;
7 shared static this () { conbufLock = new Mutex(); }
10 // ////////////////////////////////////////////////////////////////////////// //
11 version(test_cbuf)
12 enum ConBufSize = 64;
13 else
14 enum ConBufSize = 256*1024;
16 // each line in buffer ends with '\n'; we don't keep offsets or lengthes, as
17 // it's fairly easy to search in buffer, and drawing console is not a common
18 // thing, so it doesn't have to be superfast.
19 __gshared char[ConBufSize] cbuf = 0;
20 __gshared int cbufhead, cbuftail; // `cbuftail` points *at* last char
21 shared static this () { cbuf.ptr[0] = '\n'; }
23 shared ulong changeCount = 1;
24 public @property ulong cbufLastChange () nothrow @trusted @nogc { import core.atomic; return atomicLoad(changeCount); }
27 // ////////////////////////////////////////////////////////////////////////// //
28 public void cbufPut (const(char)[] chrs...) nothrow @trusted @nogc {
29 if (chrs.length) {
30 import core.atomic;
31 atomicOp!"+="(changeCount, 1);
33 foreach (char ch; chrs) {
34 int np = (cbuftail+1)%ConBufSize;
35 if (np == cbufhead) {
36 // we have to make some room; delete top line for this
37 for (;;) {
38 char och = cbuf.ptr[cbufhead];
39 cbufhead = (cbufhead+1)%ConBufSize;
40 if (cbufhead == np || och == '\n') break;
43 cbuf.ptr[np] = ch;
44 cbuftail = np;
49 // ////////////////////////////////////////////////////////////////////////// //
50 // warning! don't modify conbuf while the range is active!
51 public auto conbufLinesRev () nothrow @trusted @nogc {
52 static struct Line {
53 nothrow @trusted @nogc:
54 private:
55 int h, t; // head and tail, to check validity
56 int sp = -1, ep;
58 public:
59 @property auto front () const { pragma(inline, true); return (sp >= 0 ? cbuf.ptr[sp] : '\x00'); }
60 @property bool empty () const { pragma(inline, true); return (sp < 0 || h != cbufhead || t != cbuftail); }
61 @property auto save () { pragma(inline, true); return Line(h, t, sp, ep); }
62 void popFront () { pragma(inline, true); if (sp < 0 || (sp = (sp+1)%ConBufSize) == ep) sp = -1; }
63 @property usize opDollar () { pragma(inline, true); return (sp >= 0 ? (sp > ep ? ep+ConBufSize-sp : ep-sp) : 0); }
64 alias length = opDollar;
65 char opIndex (usize pos) { pragma(inline, true); return (sp >= 0 ? cbuf.ptr[sp+pos] : '\x00'); }
68 static struct Range {
69 nothrow @trusted @nogc:
70 private:
71 int h, t; // head and tail, to check validity
72 int pos; // position of prev line
73 Line line;
75 void toLineStart () {
76 line.ep = pos;
77 while (pos != cbufhead) {
78 int p = (pos+ConBufSize-1)%ConBufSize;
79 if (cbuf.ptr[p] == '\n') break;
80 pos = p;
82 line.sp = pos;
83 line.h = h;
84 line.t = t;
87 public:
88 @property auto front () pure { pragma(inline, true); return line; }
89 @property bool empty () const { pragma(inline, true); return (pos < 0 || pos == h || h != cbufhead || t != cbuftail); }
90 @property auto save () { pragma(inline, true); return Range(h, t, pos, line); }
91 void popFront () {
92 if (pos < 0 || pos == h || h != cbufhead || t != cbuftail) { line = Line.init; h = t = pos = -1; return; }
93 pos = (pos+ConBufSize-1)%ConBufSize;
94 toLineStart();
98 Range res;
99 res.h = cbufhead;
100 res.pos = res.t = cbuftail;
101 if (cbuf.ptr[res.pos] != '\n') res.pos = (res.pos+1)%ConBufSize;
102 res.toLineStart();
103 //{ import std.stdio; writeln("pos=", res.pos, "; head=", res.h, "; tail=", res.t, "; llen=", res.line.length, "; [", res.line, "]"); }
104 return res;
108 // ////////////////////////////////////////////////////////////////////////// //
109 public void conbufDump () {
110 import std.stdio;
111 int pp = cbufhead;
112 stdout.writeln("==========================");
113 for (;;) {
114 if (cbuf.ptr[pp] == '\n') stdout.write('|');
115 stdout.write(cbuf.ptr[pp]);
116 if (pp == cbuftail) {
117 if (cbuf.ptr[pp] != '\n') stdout.write('\n');
118 break;
120 pp = (pp+1)%ConBufSize;
122 //foreach (auto s; conbufLinesRev) stdout.writeln(s, "|");
126 // ////////////////////////////////////////////////////////////////////////// //
127 version(test_cbuf) unittest {
128 conbufDump();
129 cbufPut("boo\n"); conbufDump();
130 cbufPut("this is another line\n"); conbufDump();
131 cbufPut("one more line\n"); conbufDump();
132 cbufPut("foo\n"); conbufDump();
133 cbufPut("more lines!\n"); conbufDump();
134 cbufPut("and even more lines!\n"); conbufDump();
135 foreach (immutable idx; 0..256) {
136 import std.string : format;
137 cbufPut("line %s\n".format(idx));
138 conbufDump();