Created
February 4, 2016 14:27
-
-
Save nibral/0d0936092409fd44ba31 to your computer and use it in GitHub Desktop.
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
// 標準入力から1行ずつ読み込み | |
var readLineFromStdin = function(callback) { | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
// 入力(の一部)が到着 | |
var fragment = ''; | |
process.stdin.on('data', function(chunk) { | |
if (chunk === '') { | |
return; | |
} | |
// 行の一部が到着した分(fragment)は次の処理に回す | |
var lines = chunk.split('\n'); | |
lines[0] = fragment + lines[0]; | |
fragment = lines.pop(); | |
// 各行をコールバックに渡す | |
lines.forEach(function(line) { | |
callback(line); | |
}); | |
}); | |
// 入力終了 | |
process.stdin.on('end', function() { | |
// 残ったデータを出力 | |
if (fragment !== '') { | |
callback(fragment); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment