helpers.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * helpers.js - helpers for blessed
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. /**
  7. * Modules
  8. */
  9. var fs = require('fs');
  10. var unicode = require('./unicode');
  11. /**
  12. * Helpers
  13. */
  14. var helpers = exports;
  15. helpers.merge = function(a, b) {
  16. Object.keys(b).forEach(function(key) {
  17. a[key] = b[key];
  18. });
  19. return a;
  20. };
  21. helpers.asort = function(obj) {
  22. return obj.sort(function(a, b) {
  23. a = a.name.toLowerCase();
  24. b = b.name.toLowerCase();
  25. if (a[0] === '.' && b[0] === '.') {
  26. a = a[1];
  27. b = b[1];
  28. } else {
  29. a = a[0];
  30. b = b[0];
  31. }
  32. return a > b ? 1 : (a < b ? -1 : 0);
  33. });
  34. };
  35. helpers.hsort = function(obj) {
  36. return obj.sort(function(a, b) {
  37. return b.index - a.index;
  38. });
  39. };
  40. helpers.findFile = function(start, target) {
  41. return (function read(dir) {
  42. var files, file, stat, out;
  43. if (dir === '/dev' || dir === '/sys'
  44. || dir === '/proc' || dir === '/net') {
  45. return null;
  46. }
  47. try {
  48. files = fs.readdirSync(dir);
  49. } catch (e) {
  50. files = [];
  51. }
  52. for (var i = 0; i < files.length; i++) {
  53. file = files[i];
  54. if (file === target) {
  55. return (dir === '/' ? '' : dir) + '/' + file;
  56. }
  57. try {
  58. stat = fs.lstatSync((dir === '/' ? '' : dir) + '/' + file);
  59. } catch (e) {
  60. stat = null;
  61. }
  62. if (stat && stat.isDirectory() && !stat.isSymbolicLink()) {
  63. out = read((dir === '/' ? '' : dir) + '/' + file);
  64. if (out) return out;
  65. }
  66. }
  67. return null;
  68. })(start);
  69. };
  70. // Escape text for tag-enabled elements.
  71. helpers.escape = function(text) {
  72. return text.replace(/[{}]/g, function(ch) {
  73. return ch === '{' ? '{open}' : '{close}';
  74. });
  75. };
  76. helpers.parseTags = function(text, screen) {
  77. return helpers.Element.prototype._parseTags.call(
  78. { parseTags: true, screen: screen || helpers.Screen.global }, text);
  79. };
  80. helpers.generateTags = function(style, text) {
  81. var open = ''
  82. , close = '';
  83. Object.keys(style || {}).forEach(function(key) {
  84. var val = style[key];
  85. if (typeof val === 'string') {
  86. val = val.replace(/^light(?!-)/, 'light-');
  87. val = val.replace(/^bright(?!-)/, 'bright-');
  88. open = '{' + val + '-' + key + '}' + open;
  89. close += '{/' + val + '-' + key + '}';
  90. } else {
  91. if (val === true) {
  92. open = '{' + key + '}' + open;
  93. close += '{/' + key + '}';
  94. }
  95. }
  96. });
  97. if (text != null) {
  98. return open + text + close;
  99. }
  100. return {
  101. open: open,
  102. close: close
  103. };
  104. };
  105. helpers.attrToBinary = function(style, element) {
  106. return helpers.Element.prototype.sattr.call(element || {}, style);
  107. };
  108. helpers.stripTags = function(text) {
  109. if (!text) return '';
  110. return text
  111. .replace(/{(\/?)([\w\-,;!#]*)}/g, '')
  112. .replace(/\x1b\[[\d;]*m/g, '');
  113. };
  114. helpers.cleanTags = function(text) {
  115. return helpers.stripTags(text).trim();
  116. };
  117. helpers.dropUnicode = function(text) {
  118. if (!text) return '';
  119. return text
  120. .replace(unicode.chars.all, '??')
  121. .replace(unicode.chars.combining, '')
  122. .replace(unicode.chars.surrogate, '?');
  123. };
  124. helpers.__defineGetter__('Screen', function() {
  125. if (!helpers._screen) {
  126. helpers._screen = require('./widgets/screen');
  127. }
  128. return helpers._screen;
  129. });
  130. helpers.__defineGetter__('Element', function() {
  131. if (!helpers._element) {
  132. helpers._element = require('./widgets/element');
  133. }
  134. return helpers._element;
  135. });