File tree Expand file tree Collapse file tree 1 file changed +58
-1
lines changed Expand file tree Collapse file tree 1 file changed +58
-1
lines changed Original file line number Diff line number Diff line change 1
1
const op = { } ;
2
+ const Null = require ( './null.js' ) ;
3
+ const Hash = require ( './hash.js' ) ;
4
+ const core = require ( './core.js' ) ;
5
+
2
6
exports . op = op ;
3
7
8
+ class BufferedConsole {
9
+ constructor ( output ) {
10
+ this . buffered = [ ] ;
11
+ this . output = output ;
12
+ }
13
+
14
+ $$writefh ( buf ) {
15
+ const buffer = core . toRawBuffer ( buf ) ;
16
+ this . $$write ( buffer . toString ( ) ) ;
17
+ }
18
+
19
+ $$write ( str ) {
20
+ const lines = str . split ( / \n / ) ;
21
+ if ( lines . length === 0 ) return ;
22
+ for ( let i = 0 ; i < lines . length - 1 ; i ++ ) {
23
+ if ( i === 0 ) {
24
+ this . output ( this . buffered . join ( '' ) + lines [ 0 ] ) ;
25
+ this . buffered . length = 0 ;
26
+ } else {
27
+ this . output ( lines [ i ] ) ;
28
+ }
29
+ }
30
+
31
+ if ( lines [ lines . length - 1 ] !== '' ) {
32
+ this . buffered . push ( lines [ lines . length - 1 ] ) ;
33
+ }
34
+ }
35
+ }
36
+
37
+ const STDOUT = new BufferedConsole ( output => console . log ( output ) ) ;
38
+ const STDERR = new BufferedConsole ( output => console . error ( output ) ) ;
39
+
4
40
op . say = function ( arg ) {
5
- console . log ( arg ) ;
41
+ STDOUT . $$write ( arg + '\n' ) ;
42
+ return arg ;
43
+ } ;
44
+
45
+ op . print = function ( arg ) {
46
+ STDOUT . $$write ( arg ) ;
6
47
return arg ;
7
48
} ;
49
+
50
+ op . getstdin = function ( arg ) {
51
+ return Null ;
52
+ } ;
53
+
54
+ op . getstdout = function ( arg ) {
55
+ return STDOUT ;
56
+ } ;
57
+
58
+ op . getstderr = function ( arg ) {
59
+ return STDERR ;
60
+ } ;
61
+
62
+ op . getenvhash = function ( ) {
63
+ return new Hash ( ) ;
64
+ } ;
You can’t perform that action at this time.
0 commit comments