status-bar-item.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { Emitter } = require('atom');
  2. module.exports = class StatusBarItem {
  3. constructor() {
  4. this.element = document.createElement('a');
  5. this.element.className = 'line-ending-tile inline-block';
  6. this.emitter = new Emitter();
  7. this.setLineEndings(new Set());
  8. }
  9. setLineEndings(lineEndings) {
  10. this.lineEndings = lineEndings;
  11. this.element.textContent = lineEndingName(lineEndings);
  12. this.emitter.emit('did-change');
  13. }
  14. onDidChange(callback) {
  15. return this.emitter.on('did-change', callback);
  16. }
  17. hasLineEnding(lineEnding) {
  18. return this.lineEndings.has(lineEnding);
  19. }
  20. description() {
  21. return lineEndingDescription(this.lineEndings);
  22. }
  23. onClick(callback) {
  24. this.element.addEventListener('click', callback);
  25. }
  26. };
  27. function lineEndingName(lineEndings) {
  28. if (lineEndings.size > 1) {
  29. return 'Mixed';
  30. } else if (lineEndings.has('\n')) {
  31. return 'LF';
  32. } else if (lineEndings.has('\r\n')) {
  33. return 'CRLF';
  34. } else {
  35. return '';
  36. }
  37. }
  38. function lineEndingDescription(lineEndings) {
  39. switch (lineEndingName(lineEndings)) {
  40. case 'Mixed':
  41. return 'mixed';
  42. case 'LF':
  43. return 'LF (Unix)';
  44. case 'CRLF':
  45. return 'CRLF (Windows)';
  46. default:
  47. return 'unknown';
  48. }
  49. }