cli.js 842 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * command line tool
  3. *
  4. * @author Zongmin Lei<leizongmin@gmail.com>
  5. */
  6. var xss = require("./");
  7. var readline = require("readline");
  8. var rl = readline.createInterface({
  9. input: process.stdin,
  10. output: process.stdout,
  11. });
  12. console.log('Enter a blank line to do xss(), enter "@quit" to exit.\n');
  13. function take(c, n) {
  14. var ret = "";
  15. for (var i = 0; i < n; i++) {
  16. ret += c;
  17. }
  18. return ret;
  19. }
  20. function setPrompt(line) {
  21. line = line.toString();
  22. rl.setPrompt("[" + line + "]" + take(" ", 5 - line.length));
  23. rl.prompt();
  24. }
  25. setPrompt(1);
  26. var html = [];
  27. rl.on("line", function (line) {
  28. if (line === "@quit") return process.exit();
  29. if (line === "") {
  30. console.log("");
  31. console.log(xss(html.join("\r\n")));
  32. console.log("");
  33. html = [];
  34. } else {
  35. html.push(line);
  36. }
  37. setPrompt(html.length + 1);
  38. });