1 module conbuf
is aliced
;
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
;
27 // we have to make some room; delete top line for this
29 char och
= cbuf
.ptr
[cbufhead
];
30 cbufhead
= (cbufhead
+1)%ConBufSize
;
31 if (cbufhead
== np || och
== '\n') break;
40 // ////////////////////////////////////////////////////////////////////////// //
41 public void conbufDump () {
44 stdout
.writeln("==========================");
46 if (cbuf
.ptr
[pp
] == '\n') stdout
.write('|');
47 stdout
.write(cbuf
.ptr
[pp
]);
49 if (cbuf
.ptr
[pp
] != '\n') stdout
.write('\n');
52 pp
= (pp
+1)%ConBufSize
;
57 // ////////////////////////////////////////////////////////////////////////// //
58 version(test_cbuf
) unittest {
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
));