added storage buffer for console output
[dd2d.git] / conbuf.d
blobe620468adfea8279e1c5a9adc803d186000e6b76
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 enum ConBufSize = 256*1024;
12 //enum ConBufSize = 64;
14 // each line in buffer ends with '\n'; we don't keep offsets or lengthes, as
15 // it's fairly easy to search in buffer, and drawing console is not a common
16 // thing, so it doesn't have to be superfast.
17 __gshared char[ConBufSize] cbuf = 0;
18 __gshared int cbufhead, cbuftail; // `cbuftail` points *at* last char
19 shared static this () { cbuf.ptr[0] = '\n'; }
22 // ////////////////////////////////////////////////////////////////////////// //
23 public void cbufPut (const(char)[] chrs...) nothrow @trusted @nogc {
24 foreach (char ch; chrs) {
25 int np = (cbuftail+1)%ConBufSize;
26 if (np == cbufhead) {
27 // we have to make some room; delete top line for this
28 for (;;) {
29 char och = cbuf.ptr[cbufhead];
30 cbufhead = (cbufhead+1)%ConBufSize;
31 if (cbufhead == np || och == '\n') break;
34 cbuf.ptr[np] = ch;
35 cbuftail = np;
40 // ////////////////////////////////////////////////////////////////////////// //
41 public void conbufDump () {
42 import std.stdio;
43 int pp = cbufhead;
44 stdout.writeln("==========================");
45 for (;;) {
46 if (cbuf.ptr[pp] == '\n') stdout.write('|');
47 stdout.write(cbuf.ptr[pp]);
48 if (pp == cbuftail) {
49 if (cbuf.ptr[pp] != '\n') stdout.write('\n');
50 break;
52 pp = (pp+1)%ConBufSize;
57 // ////////////////////////////////////////////////////////////////////////// //
58 version(test_cbuf) unittest {
60 conbufDump();
61 cbufPut("boo\n"); conbufDump();
62 cbufPut("this is another line\n"); conbufDump();
63 cbufPut("one more line\n"); conbufDump();
64 cbufPut("foo\n"); conbufDump();
65 cbufPut("more lines!\n"); conbufDump();
66 cbufPut("and even more lines!\n"); conbufDump();
67 foreach (immutable idx; 0..256) {
68 import std.string : format;
69 cbufPut("line %s\n".format(idx));
70 conbufDump();