terminal.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // let Utils = require('./utils')
  2. exports.render = function (qrData, options, cb) {
  3. const size = qrData.modules.size
  4. const data = qrData.modules.data
  5. // let opts = Utils.getOptions(options)
  6. // use same scheme as https://github.com/gtanner/qrcode-terminal because it actually works! =)
  7. const black = '\x1b[40m \x1b[0m'
  8. const white = '\x1b[47m \x1b[0m'
  9. let output = ''
  10. const hMargin = Array(size + 3).join(white)
  11. const vMargin = Array(2).join(white)
  12. output += hMargin + '\n'
  13. for (let i = 0; i < size; ++i) {
  14. output += white
  15. for (let j = 0; j < size; j++) {
  16. // let topModule = data[i * size + j]
  17. // let bottomModule = data[(i + 1) * size + j]
  18. output += data[i * size + j] ? black : white// getBlockChar(topModule, bottomModule)
  19. }
  20. // output += white+'\n'
  21. output += vMargin + '\n'
  22. }
  23. output += hMargin + '\n'
  24. if (typeof cb === 'function') {
  25. cb(null, output)
  26. }
  27. return output
  28. }
  29. /*
  30. exports.renderToFile = function renderToFile (path, qrData, options, cb) {
  31. if (typeof cb === 'undefined') {
  32. cb = options
  33. options = undefined
  34. }
  35. let fs = require('fs')
  36. let utf8 = exports.render(qrData, options)
  37. fs.writeFile(path, utf8, cb)
  38. }
  39. */