example_sync.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var ExternalEditor = require('./main');
  2. var readline = require('readline');
  3. var rl = readline.createInterface({
  4. input: process.stdin,
  5. output: null
  6. });
  7. var message = '\n\n# Please Write a message\n# Any line starting with # is ignored';
  8. process.stdout.write('Please write a message. (press enter to launch your preferred editor)');
  9. editor = new ExternalEditor(message);
  10. rl.on('line', function () {
  11. try {
  12. // Get response, remove all lines starting with #, remove any trailing newlines.
  13. var response = editor.run().replace(/^#.*\n?/gm, '').replace(/\n+$/g, '').trim();
  14. if (editor.last_exit_status !== 0) {
  15. process.stderr.write("WARN: The editor exited with a non-zero status\n\n")
  16. }
  17. if (response.length === 0) {
  18. readline.moveCursor(process.stdout, 0, -1);
  19. process.stdout.write('Your message was empty, please try again. (press enter to launch your preferred editor)');
  20. } else {
  21. process.stdout.write('Your Message:\n');
  22. process.stdout.write(response);
  23. process.stdout.write('\n');
  24. rl.close();
  25. }
  26. } catch (err) {
  27. process.stderr.write(err.message);
  28. process.stdout.write('\n');
  29. rl.close();
  30. }
  31. });