|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +var linspace = require( '@stdlib/array/base/linspace' ); |
| 22 | +var array = require( '@stdlib/ndarray/array' ); |
| 23 | +var stdout = require( '@stdlib/streams/node/stdout' ); |
| 24 | +var UnicodeTable = require( './../lib' ); |
| 25 | + |
| 26 | +// Define the number of table rows and columns: |
| 27 | +var nrows = 10; |
| 28 | +var ncols = 5; |
| 29 | + |
| 30 | +// Generate a data set: |
| 31 | +var start = 0; |
| 32 | +var end = 49; |
| 33 | +var data = array( linspace( start, end, nrows*ncols ), { |
| 34 | + 'shape': [ nrows, ncols ] |
| 35 | +}); |
| 36 | + |
| 37 | +// Define column headers: |
| 38 | +var headers = [ 'A', 'B', 'C', 'D', 'E' ]; |
| 39 | + |
| 40 | +// Create a table: |
| 41 | +var table = new UnicodeTable( data, { |
| 42 | + 'headers': headers, |
| 43 | + 'rowSeparator': '-', |
| 44 | + 'columnSeparator': '#$%', |
| 45 | + 'maxWidth': 200, |
| 46 | + 'marginLeft': 5, |
| 47 | + 'marginRight': 5, |
| 48 | + 'align': 'right', |
| 49 | + 'bufferSize': nrows |
| 50 | +}); |
| 51 | + |
| 52 | +// Render the table: |
| 53 | +var str = table.render(); |
| 54 | +stdout.write( str+'\n' ); |
| 55 | + |
| 56 | +// Compute the number of lines: |
| 57 | +var N = str.split( '\n' ).length; |
| 58 | + |
| 59 | +// Periodically update the table with new data: |
| 60 | +var id = setInterval( onInterval, 2000 ); |
| 61 | +setTimeout( onTimeout, 11000 ); |
| 62 | + |
| 63 | +function onInterval() { |
| 64 | + start = end; |
| 65 | + end += ncols - 1; |
| 66 | + table.push( linspace( start, end, ncols ) ); |
| 67 | + clearPreviousLines( N ); |
| 68 | + stdout.write( table.render()+'\n' ); |
| 69 | +} |
| 70 | + |
| 71 | +function onTimeout() { |
| 72 | + clearInterval( id ); |
| 73 | +} |
| 74 | + |
| 75 | +function clearPreviousLines( count ) { |
| 76 | + var i; |
| 77 | + for ( i = 0; i < count; i++ ) { |
| 78 | + stdout.write( '\x1b[F' ); // move cursor up one line |
| 79 | + stdout.write( '\x1b[2K' ); // clear entire line |
| 80 | + } |
| 81 | +} |
0 commit comments