time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3095B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # stdin
     22 
     23 > Read data from [`stdin`][@stdlib/streams/node/stdin].
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var stdin = require( '@stdlib/process/read-stdin' );
     31 ```
     32 
     33 #### stdin( \[encoding,] clbk )
     34 
     35 Reads data from [`stdin`][@stdlib/streams/node/stdin].
     36 
     37 <!-- run-disable -->
     38 
     39 ```javascript
     40 function onRead( error, data ) {
     41     if ( error ) {
     42         return console.error( 'Error: %s', error.message );
     43     }
     44     console.log( data.toString() );
     45     // => '...'
     46 }
     47 
     48 stdin( onRead );
     49 ```
     50 
     51 By default, returned `data` is a [`Buffer`][buffer]. To return a `string` of a specified encoding, provide an `encoding` parameter.
     52 
     53 <!-- run-disable -->
     54 
     55 ```javascript
     56 function onRead( error, data ) {
     57     if ( error ) {
     58         return console.error( 'Error: %s', error.message );
     59     }
     60     console.log( data );
     61     // => '...'
     62 }
     63 
     64 stdin( 'utf8', onRead );
     65 ```
     66 
     67 When a file's calling Node.js process is running in a [TTY][tty] context (i.e., no [`stdin`][@stdlib/streams/node/stdin]), `data` will either be an empty [`Buffer`][buffer] (no encoding provided) or an empty `string` (encoding provided).
     68 
     69 <!-- run-disable -->
     70 
     71 ```javascript
     72 var stream = require( '@stdlib/streams/node/stdin' );
     73 
     74 function onRead( error, data ) {
     75     if ( error ) {
     76         return console.error( 'Error: %s', error.message );
     77     }
     78     console.log( data );
     79     // => ''
     80 }
     81 
     82 stream.isTTY = true;
     83 
     84 stdin( 'utf8', onRead );
     85 ```
     86 
     87 </section>
     88 
     89 <!-- /.usage -->
     90 
     91 <section class="examples">
     92 
     93 ## Examples
     94 
     95 <!-- run-disable -->
     96 
     97 <!-- eslint no-undef: "error" -->
     98 
     99 ```javascript
    100 var string2buffer = require( '@stdlib/buffer/from-string' );
    101 var stream = require( '@stdlib/streams/node/stdin' );
    102 var stdin = require( '@stdlib/process/read-stdin' );
    103 
    104 function onRead( error, data ) {
    105     if ( error ) {
    106         console.error( 'Error: %s', error.message );
    107     } else {
    108         console.log( data.toString() );
    109         // => 'beep boop'
    110     }
    111 }
    112 
    113 // Fake not being in a terminal context:
    114 stream.isTTY = false;
    115 
    116 // Provide a callback to consume all data from `stdin`:
    117 stdin( onRead );
    118 
    119 // Push some data to `stdin`:
    120 stream.push( string2buffer( 'beep' ) );
    121 stream.push( string2buffer( ' ' ) );
    122 stream.push( string2buffer( 'boop' ) );
    123 
    124 // End the stream:
    125 stream.push( null );
    126 ```
    127 
    128 </section>
    129 
    130 <!-- /.examples -->
    131 
    132 <section class="links">
    133 
    134 [buffer]: https://nodejs.org/api/buffer.html
    135 
    136 [tty]: https://nodejs.org/api/tty.html#tty_tty
    137 
    138 [@stdlib/streams/node/stdin]: https://www.npmjs.com/package/@stdlib/streams-node-stdin
    139 
    140 </section>
    141 
    142 <!-- /.links -->