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) {
89 return Stream.prototype.pipe.call(this, dest)
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 this._hadControl = true
105 return this.emit('data', c)
107 if (this._prompt && this._hadControl &&
108 c.indexOf(this._prompt) === 0) {
109 this._hadControl = false
110 this.emit('data', this._prompt)
111 c = c.substr(this._prompt.length)
113 c = c.toString().replace(/./g, this.replace)
119 MuteStream.prototype.end = function (c) {
121 if (c && this.replace) {
122 c = c.toString().replace(/./g, this.replace)
127 if (c) this.emit('data', c)
131 function proxy (fn) { return function () {
134 if (d && d[fn]) d[fn].apply(d, arguments)
135 if (s && s[fn]) s[fn].apply(s, arguments)
138 MuteStream.prototype.destroy = proxy('destroy')
139 MuteStream.prototype.destroySoon = proxy('destroySoon')
140 MuteStream.prototype.close = proxy('close')