1 var Stream = require('stream')
3 module.exports = MuteStream
5 // var out = new MuteStream(process.stdout)
7 function MuteStream (opts) {
10 this.writable = this.readable = true
12 this.on('pipe', this._onpipe)
13 this.replace = opts.replace
15 // For readline-type situations
16 // This much at the start of a line being redrawn after a ctrl char
17 // is seen (such as backspace) won't be redrawn as the replacement
18 this._prompt = opts.prompt || null
19 this._hadControl = false
22 MuteStream.prototype = Object.create(Stream.prototype)
24 Object.defineProperty(MuteStream.prototype, 'constructor', {
29 MuteStream.prototype.mute = function () {
33 MuteStream.prototype.unmute = function () {
37 Object.defineProperty(MuteStream.prototype, '_onpipe', {
44 function onPipe (src) {
48 Object.defineProperty(MuteStream.prototype, 'isTTY', {
55 function getIsTTY () {
56 return( (this._dest) ? this._dest.isTTY
57 : (this._src) ? this._src.isTTY
62 // basically just get replace the getter/setter with a regular value
63 function setIsTTY (isTTY) {
64 Object.defineProperty(this, 'isTTY', {
72 Object.defineProperty(MuteStream.prototype, 'rows', {
74 return( this._dest ? this._dest.rows
75 : this._src ? this._src.rows
77 }, enumerable: true, configurable: true })
79 Object.defineProperty(MuteStream.prototype, 'columns', {
81 return( this._dest ? this._dest.columns
82 : this._src ? this._src.columns
84 }, enumerable: true, configurable: true })
87 MuteStream.prototype.pipe = function (dest, options) {
89 return Stream.prototype.pipe.call(this, dest, options)
92 MuteStream.prototype.pause = function () {
93 if (this._src) return this._src.pause()
96 MuteStream.prototype.resume = function () {
97 if (this._src) return this._src.resume()
100 MuteStream.prototype.write = function (c) {
102 if (!this.replace) return true
103 if (c.match(/^\u001b/)) {
104 if(c.indexOf(this._prompt) === 0) {
105 c = c.substr(this._prompt.length);
106 c = c.replace(/./g, this.replace);
107 c = this._prompt + c;
109 this._hadControl = true
110 return this.emit('data', c)
112 if (this._prompt && this._hadControl &&
113 c.indexOf(this._prompt) === 0) {
114 this._hadControl = false
115 this.emit('data', this._prompt)
116 c = c.substr(this._prompt.length)
118 c = c.toString().replace(/./g, this.replace)
124 MuteStream.prototype.end = function (c) {
126 if (c && this.replace) {
127 c = c.toString().replace(/./g, this.replace)
132 if (c) this.emit('data', c)
136 function proxy (fn) { return function () {
139 if (d && d[fn]) d[fn].apply(d, arguments)
140 if (s && s[fn]) s[fn].apply(s, arguments)
143 MuteStream.prototype.destroy = proxy('destroy')
144 MuteStream.prototype.destroySoon = proxy('destroySoon')
145 MuteStream.prototype.close = proxy('close')