Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

read.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. module.exports = read
  2. var readline = require('readline')
  3. var Mute = require('mute-stream')
  4. function read (opts, cb) {
  5. if (opts.num) {
  6. throw new Error('read() no longer accepts a char number limit')
  7. }
  8. if (typeof opts.default !== 'undefined' &&
  9. typeof opts.default !== 'string' &&
  10. typeof opts.default !== 'number') {
  11. throw new Error('default value must be string or number')
  12. }
  13. var input = opts.input || process.stdin
  14. var output = opts.output || process.stdout
  15. var prompt = (opts.prompt || '').trim() + ' '
  16. var silent = opts.silent
  17. var editDef = false
  18. var timeout = opts.timeout
  19. var def = opts.default || ''
  20. if (def) {
  21. if (silent) {
  22. prompt += '(<default hidden>) '
  23. } else if (opts.edit) {
  24. editDef = true
  25. } else {
  26. prompt += '(' + def + ') '
  27. }
  28. }
  29. var terminal = !!(opts.terminal || output.isTTY)
  30. var m = new Mute({ replace: opts.replace, prompt: prompt })
  31. m.pipe(output, {end: false})
  32. output = m
  33. var rlOpts = { input: input, output: output, terminal: terminal }
  34. if (process.version.match(/^v0\.6/)) {
  35. var rl = readline.createInterface(rlOpts.input, rlOpts.output)
  36. } else {
  37. var rl = readline.createInterface(rlOpts)
  38. }
  39. output.unmute()
  40. rl.setPrompt(prompt)
  41. rl.prompt()
  42. if (silent) {
  43. output.mute()
  44. } else if (editDef) {
  45. rl.line = def
  46. rl.cursor = def.length
  47. rl._refreshLine()
  48. }
  49. var called = false
  50. rl.on('line', onLine)
  51. rl.on('error', onError)
  52. rl.on('SIGINT', function () {
  53. rl.close()
  54. onError(new Error('canceled'))
  55. })
  56. var timer
  57. if (timeout) {
  58. timer = setTimeout(function () {
  59. onError(new Error('timed out'))
  60. }, timeout)
  61. }
  62. function done () {
  63. called = true
  64. rl.close()
  65. if (process.version.match(/^v0\.6/)) {
  66. rl.input.removeAllListeners('data')
  67. rl.input.removeAllListeners('keypress')
  68. rl.input.pause()
  69. }
  70. clearTimeout(timer)
  71. output.mute()
  72. output.end()
  73. }
  74. function onError (er) {
  75. if (called) return
  76. done()
  77. return cb(er)
  78. }
  79. function onLine (line) {
  80. if (called) return
  81. if (silent && terminal) {
  82. output.unmute()
  83. output.write('\r\n')
  84. }
  85. done()
  86. // truncate the \n at the end.
  87. line = line.replace(/\r?\n$/, '')
  88. var isDefault = !!(editDef && line === def)
  89. if (def && !line) {
  90. isDefault = true
  91. line = def
  92. }
  93. cb(null, line, isDefault)
  94. }
  95. }