forked from unconed/TermKit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
var view = require('view/view'), | ||
reader = require('shell/reader'), | ||
parseArgs = require('misc').parseArgs; | ||
|
||
exports.main = function (tokens, pipes, exit, environment) { | ||
var out = new view.bridge(pipes.viewOut); | ||
|
||
var args = parseArgs(tokens), | ||
options = args.options; | ||
|
||
// Buffered mime reader handler. | ||
var handler = { | ||
|
||
/** | ||
* Pipe open, headers found. | ||
*/ | ||
begin: function (headers) { | ||
// Override content-type, output rest. | ||
headers.set('Content-Type', [ 'application/octet-stream', { schema: 'termkit.hex' } ]); | ||
pipes.dataOut.write(headers.generate()); | ||
}, | ||
|
||
/** | ||
* Pipe data. | ||
*/ | ||
data: function (data) { | ||
// Pipe out. | ||
pipes.dataOut.write(data); | ||
}, | ||
|
||
/** | ||
* Pipe closed. | ||
*/ | ||
end: function (exit) { | ||
// Quit immediately. | ||
exit(); | ||
}, | ||
}; | ||
|
||
// Reader callbacks | ||
var errors = 0; | ||
// Open | ||
function readerOpen() { | ||
return handler; | ||
}; | ||
// Close | ||
function readerClose() { | ||
exit(errors == 0); | ||
}; | ||
// Error | ||
function readerError(error) { | ||
errors++; | ||
out.print(error); | ||
}; | ||
|
||
// Spawn appropriate reader. | ||
if (files.length) { | ||
// Spawn files reader. | ||
var pipe = new reader.filesReader(files, readerOpen, readerClose, readerError); | ||
} | ||
else { | ||
// Spawn data reader. | ||
var pipe = new reader.dataReader(pipes.dataIn, readerOpen, readerClose, readerError); | ||
} | ||
}; |