screen.js 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  1. /**
  2. * screen.js - screen node 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 path = require('path')
  10. , fs = require('fs')
  11. , cp = require('child_process');
  12. var colors = require('../colors')
  13. , program = require('../program')
  14. , unicode = require('../unicode');
  15. var nextTick = global.setImmediate || process.nextTick.bind(process);
  16. var helpers = require('../helpers');
  17. var Node = require('./node');
  18. var Log = require('./log');
  19. var Element = require('./element');
  20. var Box = require('./box');
  21. /**
  22. * Screen
  23. */
  24. function Screen(options) {
  25. var self = this;
  26. if (!(this instanceof Node)) {
  27. return new Screen(options);
  28. }
  29. Screen.bind(this);
  30. options = options || {};
  31. if (options.rsety && options.listen) {
  32. options = { program: options };
  33. }
  34. this.program = options.program;
  35. if (!this.program) {
  36. this.program = program({
  37. input: options.input,
  38. output: options.output,
  39. log: options.log,
  40. debug: options.debug,
  41. dump: options.dump,
  42. terminal: options.terminal || options.term,
  43. resizeTimeout: options.resizeTimeout,
  44. forceUnicode: options.forceUnicode,
  45. tput: true,
  46. buffer: true,
  47. zero: true
  48. });
  49. } else {
  50. this.program.setupTput();
  51. this.program.useBuffer = true;
  52. this.program.zero = true;
  53. this.program.options.resizeTimeout = options.resizeTimeout;
  54. if (options.forceUnicode != null) {
  55. this.program.tput.features.unicode = options.forceUnicode;
  56. this.program.tput.unicode = options.forceUnicode;
  57. }
  58. }
  59. this.tput = this.program.tput;
  60. Node.call(this, options);
  61. this.autoPadding = options.autoPadding !== false;
  62. this.tabc = Array((options.tabSize || 4) + 1).join(' ');
  63. this.dockBorders = options.dockBorders;
  64. this.ignoreLocked = options.ignoreLocked || [];
  65. this._unicode = this.tput.unicode || this.tput.numbers.U8 === 1;
  66. this.fullUnicode = this.options.fullUnicode && this._unicode;
  67. this.dattr = ((0 << 18) | (0x1ff << 9)) | 0x1ff;
  68. this.renders = 0;
  69. this.position = {
  70. left: this.left = this.aleft = this.rleft = 0,
  71. right: this.right = this.aright = this.rright = 0,
  72. top: this.top = this.atop = this.rtop = 0,
  73. bottom: this.bottom = this.abottom = this.rbottom = 0,
  74. get height() { return self.height; },
  75. get width() { return self.width; }
  76. };
  77. this.ileft = 0;
  78. this.itop = 0;
  79. this.iright = 0;
  80. this.ibottom = 0;
  81. this.iheight = 0;
  82. this.iwidth = 0;
  83. this.padding = {
  84. left: 0,
  85. top: 0,
  86. right: 0,
  87. bottom: 0
  88. };
  89. this.hover = null;
  90. this.history = [];
  91. this.clickable = [];
  92. this.keyable = [];
  93. this.grabKeys = false;
  94. this.lockKeys = false;
  95. this.focused;
  96. this._buf = '';
  97. this._ci = -1;
  98. if (options.title) {
  99. this.title = options.title;
  100. }
  101. options.cursor = options.cursor || {
  102. artificial: options.artificialCursor,
  103. shape: options.cursorShape,
  104. blink: options.cursorBlink,
  105. color: options.cursorColor
  106. };
  107. this.cursor = {
  108. artificial: options.cursor.artificial || false,
  109. shape: options.cursor.shape || 'block',
  110. blink: options.cursor.blink || false,
  111. color: options.cursor.color || null,
  112. _set: false,
  113. _state: 1,
  114. _hidden: true
  115. };
  116. this.program.on('resize', function() {
  117. self.alloc();
  118. self.render();
  119. (function emit(el) {
  120. el.emit('resize');
  121. el.children.forEach(emit);
  122. })(self);
  123. });
  124. this.program.on('focus', function() {
  125. self.emit('focus');
  126. });
  127. this.program.on('blur', function() {
  128. self.emit('blur');
  129. });
  130. this.program.on('warning', function(text) {
  131. self.emit('warning', text);
  132. });
  133. this.on('newListener', function fn(type) {
  134. if (type === 'keypress' || type.indexOf('key ') === 0 || type === 'mouse') {
  135. if (type === 'keypress' || type.indexOf('key ') === 0) self._listenKeys();
  136. if (type === 'mouse') self._listenMouse();
  137. }
  138. if (type === 'mouse'
  139. || type === 'click'
  140. || type === 'mouseover'
  141. || type === 'mouseout'
  142. || type === 'mousedown'
  143. || type === 'mouseup'
  144. || type === 'mousewheel'
  145. || type === 'wheeldown'
  146. || type === 'wheelup'
  147. || type === 'mousemove') {
  148. self._listenMouse();
  149. }
  150. });
  151. this.setMaxListeners(Infinity);
  152. this.enter();
  153. this.postEnter();
  154. }
  155. Screen.global = null;
  156. Screen.total = 0;
  157. Screen.instances = [];
  158. Screen.bind = function(screen) {
  159. if (!Screen.global) {
  160. Screen.global = screen;
  161. }
  162. if (!~Screen.instances.indexOf(screen)) {
  163. Screen.instances.push(screen);
  164. screen.index = Screen.total;
  165. Screen.total++;
  166. }
  167. if (Screen._bound) return;
  168. Screen._bound = true;
  169. process.on('uncaughtException', Screen._exceptionHandler = function(err) {
  170. if (process.listeners('uncaughtException').length > 1) {
  171. return;
  172. }
  173. Screen.instances.slice().forEach(function(screen) {
  174. screen.destroy();
  175. });
  176. err = err || new Error('Uncaught Exception.');
  177. console.error(err.stack ? err.stack + '' : err + '');
  178. nextTick(function() {
  179. process.exit(1);
  180. });
  181. });
  182. ['SIGTERM', 'SIGINT', 'SIGQUIT'].forEach(function(signal) {
  183. var name = '_' + signal.toLowerCase() + 'Handler';
  184. process.on(signal, Screen[name] = function() {
  185. if (process.listeners(signal).length > 1) {
  186. return;
  187. }
  188. nextTick(function() {
  189. process.exit(0);
  190. });
  191. });
  192. });
  193. process.on('exit', Screen._exitHandler = function() {
  194. Screen.instances.slice().forEach(function(screen) {
  195. screen.destroy();
  196. });
  197. });
  198. };
  199. Screen.prototype.__proto__ = Node.prototype;
  200. Screen.prototype.type = 'screen';
  201. Screen.prototype.__defineGetter__('title', function() {
  202. return this.program.title;
  203. });
  204. Screen.prototype.__defineSetter__('title', function(title) {
  205. return this.program.title = title;
  206. });
  207. Screen.prototype.__defineGetter__('terminal', function() {
  208. return this.program.terminal;
  209. });
  210. Screen.prototype.__defineSetter__('terminal', function(terminal) {
  211. this.setTerminal(terminal);
  212. return this.program.terminal;
  213. });
  214. Screen.prototype.setTerminal = function(terminal) {
  215. var entered = !!this.program.isAlt;
  216. if (entered) {
  217. this._buf = '';
  218. this.program._buf = '';
  219. this.leave();
  220. }
  221. this.program.setTerminal(terminal);
  222. this.tput = this.program.tput;
  223. if (entered) {
  224. this.enter();
  225. }
  226. };
  227. Screen.prototype.enter = function() {
  228. if (this.program.isAlt) return;
  229. if (!this.cursor._set) {
  230. if (this.options.cursor.shape) {
  231. this.cursorShape(this.cursor.shape, this.cursor.blink);
  232. }
  233. if (this.options.cursor.color) {
  234. this.cursorColor(this.cursor.color);
  235. }
  236. }
  237. if (process.platform === 'win32') {
  238. try {
  239. cp.execSync('cls', { stdio: 'ignore', timeout: 1000 });
  240. } catch (e) {
  241. ;
  242. }
  243. }
  244. this.program.alternateBuffer();
  245. this.program.put.keypad_xmit();
  246. this.program.csr(0, this.height - 1);
  247. this.program.hideCursor();
  248. this.program.cup(0, 0);
  249. // We need this for tmux now:
  250. if (this.tput.strings.ena_acs) {
  251. this.program._write(this.tput.enacs());
  252. }
  253. this.alloc();
  254. };
  255. Screen.prototype.leave = function() {
  256. if (!this.program.isAlt) return;
  257. this.program.put.keypad_local();
  258. if (this.program.scrollTop !== 0
  259. || this.program.scrollBottom !== this.rows - 1) {
  260. this.program.csr(0, this.height - 1);
  261. }
  262. // XXX For some reason if alloc/clear() is before this
  263. // line, it doesn't work on linux console.
  264. this.program.showCursor();
  265. this.alloc();
  266. if (this._listenedMouse) {
  267. this.program.disableMouse();
  268. }
  269. this.program.normalBuffer();
  270. if (this.cursor._set) this.cursorReset();
  271. this.program.flush();
  272. if (process.platform === 'win32') {
  273. try {
  274. cp.execSync('cls', { stdio: 'ignore', timeout: 1000 });
  275. } catch (e) {
  276. ;
  277. }
  278. }
  279. };
  280. Screen.prototype.postEnter = function() {
  281. var self = this;
  282. if (this.options.debug) {
  283. this.debugLog = new Log({
  284. screen: this,
  285. parent: this,
  286. hidden: true,
  287. draggable: true,
  288. left: 'center',
  289. top: 'center',
  290. width: '30%',
  291. height: '30%',
  292. border: 'line',
  293. label: ' {bold}Debug Log{/bold} ',
  294. tags: true,
  295. keys: true,
  296. vi: true,
  297. mouse: true,
  298. scrollbar: {
  299. ch: ' ',
  300. track: {
  301. bg: 'yellow'
  302. },
  303. style: {
  304. inverse: true
  305. }
  306. }
  307. });
  308. this.debugLog.toggle = function() {
  309. if (self.debugLog.hidden) {
  310. self.saveFocus();
  311. self.debugLog.show();
  312. self.debugLog.setFront();
  313. self.debugLog.focus();
  314. } else {
  315. self.debugLog.hide();
  316. self.restoreFocus();
  317. }
  318. self.render();
  319. };
  320. this.debugLog.key(['q', 'escape'], self.debugLog.toggle);
  321. this.key('f12', self.debugLog.toggle);
  322. }
  323. if (this.options.warnings) {
  324. this.on('warning', function(text) {
  325. var warning = new Box({
  326. screen: self,
  327. parent: self,
  328. left: 'center',
  329. top: 'center',
  330. width: 'shrink',
  331. padding: 1,
  332. height: 'shrink',
  333. align: 'center',
  334. valign: 'middle',
  335. border: 'line',
  336. label: ' {red-fg}{bold}WARNING{/} ',
  337. content: '{bold}' + text + '{/bold}',
  338. tags: true
  339. });
  340. self.render();
  341. var timeout = setTimeout(function() {
  342. warning.destroy();
  343. self.render();
  344. }, 1500);
  345. if (timeout.unref) {
  346. timeout.unref();
  347. }
  348. });
  349. }
  350. };
  351. Screen.prototype._destroy = Screen.prototype.destroy;
  352. Screen.prototype.destroy = function() {
  353. this.leave();
  354. var index = Screen.instances.indexOf(this);
  355. if (~index) {
  356. Screen.instances.splice(index, 1);
  357. Screen.total--;
  358. Screen.global = Screen.instances[0];
  359. if (Screen.total === 0) {
  360. Screen.global = null;
  361. process.removeListener('uncaughtException', Screen._exceptionHandler);
  362. process.removeListener('SIGTERM', Screen._sigtermHandler);
  363. process.removeListener('SIGINT', Screen._sigintHandler);
  364. process.removeListener('SIGQUIT', Screen._sigquitHandler);
  365. process.removeListener('exit', Screen._exitHandler);
  366. delete Screen._exceptionHandler;
  367. delete Screen._sigtermHandler;
  368. delete Screen._sigintHandler;
  369. delete Screen._sigquitHandler;
  370. delete Screen._exitHandler;
  371. delete Screen._bound;
  372. }
  373. this.destroyed = true;
  374. this.emit('destroy');
  375. this._destroy();
  376. }
  377. this.program.destroy();
  378. };
  379. Screen.prototype.log = function() {
  380. return this.program.log.apply(this.program, arguments);
  381. };
  382. Screen.prototype.debug = function() {
  383. if (this.debugLog) {
  384. this.debugLog.log.apply(this.debugLog, arguments);
  385. }
  386. return this.program.debug.apply(this.program, arguments);
  387. };
  388. Screen.prototype._listenMouse = function(el) {
  389. var self = this;
  390. if (el && !~this.clickable.indexOf(el)) {
  391. el.clickable = true;
  392. this.clickable.push(el);
  393. }
  394. if (this._listenedMouse) return;
  395. this._listenedMouse = true;
  396. this.program.enableMouse();
  397. if (this.options.sendFocus) {
  398. this.program.setMouse({ sendFocus: true }, true);
  399. }
  400. this.on('render', function() {
  401. self._needsClickableSort = true;
  402. });
  403. this.program.on('mouse', function(data) {
  404. if (self.lockKeys) return;
  405. if (self._needsClickableSort) {
  406. self.clickable = helpers.hsort(self.clickable);
  407. self._needsClickableSort = false;
  408. }
  409. var i = 0
  410. , el
  411. , set
  412. , pos;
  413. for (; i < self.clickable.length; i++) {
  414. el = self.clickable[i];
  415. if (el.detached || !el.visible) {
  416. continue;
  417. }
  418. // if (self.grabMouse && self.focused !== el
  419. // && !el.hasAncestor(self.focused)) continue;
  420. pos = el.lpos;
  421. if (!pos) continue;
  422. if (data.x >= pos.xi && data.x < pos.xl
  423. && data.y >= pos.yi && data.y < pos.yl) {
  424. el.emit('mouse', data);
  425. if (data.action === 'mousedown') {
  426. self.mouseDown = el;
  427. } else if (data.action === 'mouseup') {
  428. (self.mouseDown || el).emit('click', data);
  429. self.mouseDown = null;
  430. } else if (data.action === 'mousemove') {
  431. if (self.hover && el.index > self.hover.index) {
  432. set = false;
  433. }
  434. if (self.hover !== el && !set) {
  435. if (self.hover) {
  436. self.hover.emit('mouseout', data);
  437. }
  438. el.emit('mouseover', data);
  439. self.hover = el;
  440. }
  441. set = true;
  442. }
  443. el.emit(data.action, data);
  444. break;
  445. }
  446. }
  447. // Just mouseover?
  448. if ((data.action === 'mousemove'
  449. || data.action === 'mousedown'
  450. || data.action === 'mouseup')
  451. && self.hover
  452. && !set) {
  453. self.hover.emit('mouseout', data);
  454. self.hover = null;
  455. }
  456. self.emit('mouse', data);
  457. self.emit(data.action, data);
  458. });
  459. // Autofocus highest element.
  460. // this.on('element click', function(el, data) {
  461. // var target;
  462. // do {
  463. // if (el.clickable === true && el.options.autoFocus !== false) {
  464. // target = el;
  465. // }
  466. // } while (el = el.parent);
  467. // if (target) target.focus();
  468. // });
  469. // Autofocus elements with the appropriate option.
  470. this.on('element click', function(el) {
  471. if (el.clickable === true && el.options.autoFocus !== false) {
  472. el.focus();
  473. }
  474. });
  475. };
  476. Screen.prototype.enableMouse = function(el) {
  477. this._listenMouse(el);
  478. };
  479. Screen.prototype._listenKeys = function(el) {
  480. var self = this;
  481. if (el && !~this.keyable.indexOf(el)) {
  482. el.keyable = true;
  483. this.keyable.push(el);
  484. }
  485. if (this._listenedKeys) return;
  486. this._listenedKeys = true;
  487. // NOTE: The event emissions used to be reversed:
  488. // element + screen
  489. // They are now:
  490. // screen + element
  491. // After the first keypress emitted, the handler
  492. // checks to make sure grabKeys, lockKeys, and focused
  493. // weren't changed, and handles those situations appropriately.
  494. this.program.on('keypress', function(ch, key) {
  495. if (self.lockKeys && !~self.ignoreLocked.indexOf(key.full)) {
  496. return;
  497. }
  498. var focused = self.focused
  499. , grabKeys = self.grabKeys;
  500. if (!grabKeys || ~self.ignoreLocked.indexOf(key.full)) {
  501. self.emit('keypress', ch, key);
  502. self.emit('key ' + key.full, ch, key);
  503. }
  504. // If something changed from the screen key handler, stop.
  505. if (self.grabKeys !== grabKeys || self.lockKeys) {
  506. return;
  507. }
  508. if (focused && focused.keyable) {
  509. focused.emit('keypress', ch, key);
  510. focused.emit('key ' + key.full, ch, key);
  511. }
  512. });
  513. };
  514. Screen.prototype.enableKeys = function(el) {
  515. this._listenKeys(el);
  516. };
  517. Screen.prototype.enableInput = function(el) {
  518. this._listenMouse(el);
  519. this._listenKeys(el);
  520. };
  521. Screen.prototype._initHover = function() {
  522. var self = this;
  523. if (this._hoverText) {
  524. return;
  525. }
  526. this._hoverText = new Box({
  527. screen: this,
  528. left: 0,
  529. top: 0,
  530. tags: false,
  531. height: 'shrink',
  532. width: 'shrink',
  533. border: 'line',
  534. style: {
  535. border: {
  536. fg: 'default'
  537. },
  538. bg: 'default',
  539. fg: 'default'
  540. }
  541. });
  542. this.on('mousemove', function(data) {
  543. if (self._hoverText.detached) return;
  544. self._hoverText.rleft = data.x + 1;
  545. self._hoverText.rtop = data.y;
  546. self.render();
  547. });
  548. this.on('element mouseover', function(el, data) {
  549. if (!el._hoverOptions) return;
  550. self._hoverText.parseTags = el.parseTags;
  551. self._hoverText.setContent(el._hoverOptions.text);
  552. self.append(self._hoverText);
  553. self._hoverText.rleft = data.x + 1;
  554. self._hoverText.rtop = data.y;
  555. self.render();
  556. });
  557. this.on('element mouseout', function() {
  558. if (self._hoverText.detached) return;
  559. self._hoverText.detach();
  560. self.render();
  561. });
  562. // XXX This can cause problems if the
  563. // terminal does not support allMotion.
  564. // Workaround: check to see if content is set.
  565. this.on('element mouseup', function(el) {
  566. if (!self._hoverText.getContent()) return;
  567. if (!el._hoverOptions) return;
  568. self.append(self._hoverText);
  569. self.render();
  570. });
  571. };
  572. Screen.prototype.__defineGetter__('cols', function() {
  573. return this.program.cols;
  574. });
  575. Screen.prototype.__defineGetter__('rows', function() {
  576. return this.program.rows;
  577. });
  578. Screen.prototype.__defineGetter__('width', function() {
  579. return this.program.cols;
  580. });
  581. Screen.prototype.__defineGetter__('height', function() {
  582. return this.program.rows;
  583. });
  584. Screen.prototype.alloc = function(dirty) {
  585. var x, y;
  586. this.lines = [];
  587. for (y = 0; y < this.rows; y++) {
  588. this.lines[y] = [];
  589. for (x = 0; x < this.cols; x++) {
  590. this.lines[y][x] = [this.dattr, ' '];
  591. }
  592. this.lines[y].dirty = !!dirty;
  593. }
  594. this.olines = [];
  595. for (y = 0; y < this.rows; y++) {
  596. this.olines[y] = [];
  597. for (x = 0; x < this.cols; x++) {
  598. this.olines[y][x] = [this.dattr, ' '];
  599. }
  600. }
  601. this.program.clear();
  602. };
  603. Screen.prototype.realloc = function() {
  604. return this.alloc(true);
  605. };
  606. Screen.prototype.render = function() {
  607. var self = this;
  608. if (this.destroyed) return;
  609. this.emit('prerender');
  610. this._borderStops = {};
  611. // TODO: Possibly get rid of .dirty altogether.
  612. // TODO: Could possibly drop .dirty and just clear the `lines` buffer every
  613. // time before a screen.render. This way clearRegion doesn't have to be
  614. // called in arbitrary places for the sake of clearing a spot where an
  615. // element used to be (e.g. when an element moves or is hidden). There could
  616. // be some overhead though.
  617. // this.screen.clearRegion(0, this.cols, 0, this.rows);
  618. this._ci = 0;
  619. this.children.forEach(function(el) {
  620. el.index = self._ci++;
  621. //el._rendering = true;
  622. el.render();
  623. //el._rendering = false;
  624. });
  625. this._ci = -1;
  626. if (this.screen.dockBorders) {
  627. this._dockBorders();
  628. }
  629. this.draw(0, this.lines.length - 1);
  630. // XXX Workaround to deal with cursor pos before the screen has rendered and
  631. // lpos is not reliable (stale).
  632. if (this.focused && this.focused._updateCursor) {
  633. this.focused._updateCursor(true);
  634. }
  635. this.renders++;
  636. this.emit('render');
  637. };
  638. Screen.prototype.blankLine = function(ch, dirty) {
  639. var out = [];
  640. for (var x = 0; x < this.cols; x++) {
  641. out[x] = [this.dattr, ch || ' '];
  642. }
  643. out.dirty = dirty;
  644. return out;
  645. };
  646. Screen.prototype.insertLine = function(n, y, top, bottom) {
  647. // if (y === top) return this.insertLineNC(n, y, top, bottom);
  648. if (!this.tput.strings.change_scroll_region
  649. || !this.tput.strings.delete_line
  650. || !this.tput.strings.insert_line) return;
  651. this._buf += this.tput.csr(top, bottom);
  652. this._buf += this.tput.cup(y, 0);
  653. this._buf += this.tput.il(n);
  654. this._buf += this.tput.csr(0, this.height - 1);
  655. var j = bottom + 1;
  656. while (n--) {
  657. this.lines.splice(y, 0, this.blankLine());
  658. this.lines.splice(j, 1);
  659. this.olines.splice(y, 0, this.blankLine());
  660. this.olines.splice(j, 1);
  661. }
  662. };
  663. Screen.prototype.deleteLine = function(n, y, top, bottom) {
  664. // if (y === top) return this.deleteLineNC(n, y, top, bottom);
  665. if (!this.tput.strings.change_scroll_region
  666. || !this.tput.strings.delete_line
  667. || !this.tput.strings.insert_line) return;
  668. this._buf += this.tput.csr(top, bottom);
  669. this._buf += this.tput.cup(y, 0);
  670. this._buf += this.tput.dl(n);
  671. this._buf += this.tput.csr(0, this.height - 1);
  672. var j = bottom + 1;
  673. while (n--) {
  674. this.lines.splice(j, 0, this.blankLine());
  675. this.lines.splice(y, 1);
  676. this.olines.splice(j, 0, this.blankLine());
  677. this.olines.splice(y, 1);
  678. }
  679. };
  680. // This is how ncurses does it.
  681. // Scroll down (up cursor-wise).
  682. // This will only work for top line deletion as opposed to arbitrary lines.
  683. Screen.prototype.insertLineNC = function(n, y, top, bottom) {
  684. if (!this.tput.strings.change_scroll_region
  685. || !this.tput.strings.delete_line) return;
  686. this._buf += this.tput.csr(top, bottom);
  687. this._buf += this.tput.cup(top, 0);
  688. this._buf += this.tput.dl(n);
  689. this._buf += this.tput.csr(0, this.height - 1);
  690. var j = bottom + 1;
  691. while (n--) {
  692. this.lines.splice(j, 0, this.blankLine());
  693. this.lines.splice(y, 1);
  694. this.olines.splice(j, 0, this.blankLine());
  695. this.olines.splice(y, 1);
  696. }
  697. };
  698. // This is how ncurses does it.
  699. // Scroll up (down cursor-wise).
  700. // This will only work for bottom line deletion as opposed to arbitrary lines.
  701. Screen.prototype.deleteLineNC = function(n, y, top, bottom) {
  702. if (!this.tput.strings.change_scroll_region
  703. || !this.tput.strings.delete_line) return;
  704. this._buf += this.tput.csr(top, bottom);
  705. this._buf += this.tput.cup(bottom, 0);
  706. this._buf += Array(n + 1).join('\n');
  707. this._buf += this.tput.csr(0, this.height - 1);
  708. var j = bottom + 1;
  709. while (n--) {
  710. this.lines.splice(j, 0, this.blankLine());
  711. this.lines.splice(y, 1);
  712. this.olines.splice(j, 0, this.blankLine());
  713. this.olines.splice(y, 1);
  714. }
  715. };
  716. Screen.prototype.insertBottom = function(top, bottom) {
  717. return this.deleteLine(1, top, top, bottom);
  718. };
  719. Screen.prototype.insertTop = function(top, bottom) {
  720. return this.insertLine(1, top, top, bottom);
  721. };
  722. Screen.prototype.deleteBottom = function(top, bottom) {
  723. return this.clearRegion(0, this.width, bottom, bottom);
  724. };
  725. Screen.prototype.deleteTop = function(top, bottom) {
  726. // Same as: return this.insertBottom(top, bottom);
  727. return this.deleteLine(1, top, top, bottom);
  728. };
  729. // Parse the sides of an element to determine
  730. // whether an element has uniform cells on
  731. // both sides. If it does, we can use CSR to
  732. // optimize scrolling on a scrollable element.
  733. // Not exactly sure how worthwile this is.
  734. // This will cause a performance/cpu-usage hit,
  735. // but will it be less or greater than the
  736. // performance hit of slow-rendering scrollable
  737. // boxes with clean sides?
  738. Screen.prototype.cleanSides = function(el) {
  739. var pos = el.lpos;
  740. if (!pos) {
  741. return false;
  742. }
  743. if (pos._cleanSides != null) {
  744. return pos._cleanSides;
  745. }
  746. if (pos.xi <= 0 && pos.xl >= this.width) {
  747. return pos._cleanSides = true;
  748. }
  749. if (this.options.fastCSR) {
  750. // Maybe just do this instead of parsing.
  751. if (pos.yi < 0) return pos._cleanSides = false;
  752. if (pos.yl > this.height) return pos._cleanSides = false;
  753. if (this.width - (pos.xl - pos.xi) < 40) {
  754. return pos._cleanSides = true;
  755. }
  756. return pos._cleanSides = false;
  757. }
  758. if (!this.options.smartCSR) {
  759. return false;
  760. }
  761. // The scrollbar can't update properly, and there's also a
  762. // chance that the scrollbar may get moved around senselessly.
  763. // NOTE: In pratice, this doesn't seem to be the case.
  764. // if (this.scrollbar) {
  765. // return pos._cleanSides = false;
  766. // }
  767. // Doesn't matter if we're only a height of 1.
  768. // if ((pos.yl - el.ibottom) - (pos.yi + el.itop) <= 1) {
  769. // return pos._cleanSides = false;
  770. // }
  771. var yi = pos.yi + el.itop
  772. , yl = pos.yl - el.ibottom
  773. , first
  774. , ch
  775. , x
  776. , y;
  777. if (pos.yi < 0) return pos._cleanSides = false;
  778. if (pos.yl > this.height) return pos._cleanSides = false;
  779. if (pos.xi - 1 < 0) return pos._cleanSides = true;
  780. if (pos.xl > this.width) return pos._cleanSides = true;
  781. for (x = pos.xi - 1; x >= 0; x--) {
  782. if (!this.olines[yi]) break;
  783. first = this.olines[yi][x];
  784. for (y = yi; y < yl; y++) {
  785. if (!this.olines[y] || !this.olines[y][x]) break;
  786. ch = this.olines[y][x];
  787. if (ch[0] !== first[0] || ch[1] !== first[1]) {
  788. return pos._cleanSides = false;
  789. }
  790. }
  791. }
  792. for (x = pos.xl; x < this.width; x++) {
  793. if (!this.olines[yi]) break;
  794. first = this.olines[yi][x];
  795. for (y = yi; y < yl; y++) {
  796. if (!this.olines[y] || !this.olines[y][x]) break;
  797. ch = this.olines[y][x];
  798. if (ch[0] !== first[0] || ch[1] !== first[1]) {
  799. return pos._cleanSides = false;
  800. }
  801. }
  802. }
  803. return pos._cleanSides = true;
  804. };
  805. Screen.prototype._dockBorders = function() {
  806. var lines = this.lines
  807. , stops = this._borderStops
  808. , i
  809. , y
  810. , x
  811. , ch;
  812. // var keys, stop;
  813. //
  814. // keys = Object.keys(this._borderStops)
  815. // .map(function(k) { return +k; })
  816. // .sort(function(a, b) { return a - b; });
  817. //
  818. // for (i = 0; i < keys.length; i++) {
  819. // y = keys[i];
  820. // if (!lines[y]) continue;
  821. // stop = this._borderStops[y];
  822. // for (x = stop.xi; x < stop.xl; x++) {
  823. stops = Object.keys(stops)
  824. .map(function(k) { return +k; })
  825. .sort(function(a, b) { return a - b; });
  826. for (i = 0; i < stops.length; i++) {
  827. y = stops[i];
  828. if (!lines[y]) continue;
  829. for (x = 0; x < this.width; x++) {
  830. ch = lines[y][x][1];
  831. if (angles[ch]) {
  832. lines[y][x][1] = this._getAngle(lines, x, y);
  833. lines[y].dirty = true;
  834. }
  835. }
  836. }
  837. };
  838. Screen.prototype._getAngle = function(lines, x, y) {
  839. var angle = 0
  840. , attr = lines[y][x][0]
  841. , ch = lines[y][x][1];
  842. if (lines[y][x - 1] && langles[lines[y][x - 1][1]]) {
  843. if (!this.options.ignoreDockContrast) {
  844. if (lines[y][x - 1][0] !== attr) return ch;
  845. }
  846. angle |= 1 << 3;
  847. }
  848. if (lines[y - 1] && uangles[lines[y - 1][x][1]]) {
  849. if (!this.options.ignoreDockContrast) {
  850. if (lines[y - 1][x][0] !== attr) return ch;
  851. }
  852. angle |= 1 << 2;
  853. }
  854. if (lines[y][x + 1] && rangles[lines[y][x + 1][1]]) {
  855. if (!this.options.ignoreDockContrast) {
  856. if (lines[y][x + 1][0] !== attr) return ch;
  857. }
  858. angle |= 1 << 1;
  859. }
  860. if (lines[y + 1] && dangles[lines[y + 1][x][1]]) {
  861. if (!this.options.ignoreDockContrast) {
  862. if (lines[y + 1][x][0] !== attr) return ch;
  863. }
  864. angle |= 1 << 0;
  865. }
  866. // Experimental: fixes this situation:
  867. // +----------+
  868. // | <-- empty space here, should be a T angle
  869. // +-------+ |
  870. // | | |
  871. // +-------+ |
  872. // | |
  873. // +----------+
  874. // if (uangles[lines[y][x][1]]) {
  875. // if (lines[y + 1] && cdangles[lines[y + 1][x][1]]) {
  876. // if (!this.options.ignoreDockContrast) {
  877. // if (lines[y + 1][x][0] !== attr) return ch;
  878. // }
  879. // angle |= 1 << 0;
  880. // }
  881. // }
  882. return angleTable[angle] || ch;
  883. };
  884. Screen.prototype.draw = function(start, end) {
  885. // this.emit('predraw');
  886. var x
  887. , y
  888. , line
  889. , out
  890. , ch
  891. , data
  892. , attr
  893. , fg
  894. , bg
  895. , flags;
  896. var main = ''
  897. , pre
  898. , post;
  899. var clr
  900. , neq
  901. , xx;
  902. var lx = -1
  903. , ly = -1
  904. , o;
  905. var acs;
  906. if (this._buf) {
  907. main += this._buf;
  908. this._buf = '';
  909. }
  910. for (y = start; y <= end; y++) {
  911. line = this.lines[y];
  912. o = this.olines[y];
  913. if (!line.dirty && !(this.cursor.artificial && y === this.program.y)) {
  914. continue;
  915. }
  916. line.dirty = false;
  917. out = '';
  918. attr = this.dattr;
  919. for (x = 0; x < line.length; x++) {
  920. data = line[x][0];
  921. ch = line[x][1];
  922. // Render the artificial cursor.
  923. if (this.cursor.artificial
  924. && !this.cursor._hidden
  925. && this.cursor._state
  926. && x === this.program.x
  927. && y === this.program.y) {
  928. var cattr = this._cursorAttr(this.cursor, data);
  929. if (cattr.ch) ch = cattr.ch;
  930. data = cattr.attr;
  931. }
  932. // Take advantage of xterm's back_color_erase feature by using a
  933. // lookahead. Stop spitting out so many damn spaces. NOTE: Is checking
  934. // the bg for non BCE terminals worth the overhead?
  935. if (this.options.useBCE
  936. && ch === ' '
  937. && (this.tput.bools.back_color_erase
  938. || (data & 0x1ff) === (this.dattr & 0x1ff))
  939. && ((data >> 18) & 8) === ((this.dattr >> 18) & 8)) {
  940. clr = true;
  941. neq = false;
  942. for (xx = x; xx < line.length; xx++) {
  943. if (line[xx][0] !== data || line[xx][1] !== ' ') {
  944. clr = false;
  945. break;
  946. }
  947. if (line[xx][0] !== o[xx][0] || line[xx][1] !== o[xx][1]) {
  948. neq = true;
  949. }
  950. }
  951. if (clr && neq) {
  952. lx = -1, ly = -1;
  953. if (data !== attr) {
  954. out += this.codeAttr(data);
  955. attr = data;
  956. }
  957. out += this.tput.cup(y, x);
  958. out += this.tput.el();
  959. for (xx = x; xx < line.length; xx++) {
  960. o[xx][0] = data;
  961. o[xx][1] = ' ';
  962. }
  963. break;
  964. }
  965. // If there's more than 10 spaces, use EL regardless
  966. // and start over drawing the rest of line. Might
  967. // not be worth it. Try to use ECH if the terminal
  968. // supports it. Maybe only try to use ECH here.
  969. // //if (this.tput.strings.erase_chars)
  970. // if (!clr && neq && (xx - x) > 10) {
  971. // lx = -1, ly = -1;
  972. // if (data !== attr) {
  973. // out += this.codeAttr(data);
  974. // attr = data;
  975. // }
  976. // out += this.tput.cup(y, x);
  977. // if (this.tput.strings.erase_chars) {
  978. // // Use erase_chars to avoid erasing the whole line.
  979. // out += this.tput.ech(xx - x);
  980. // } else {
  981. // out += this.tput.el();
  982. // }
  983. // if (this.tput.strings.parm_right_cursor) {
  984. // out += this.tput.cuf(xx - x);
  985. // } else {
  986. // out += this.tput.cup(y, xx);
  987. // }
  988. // this.fillRegion(data, ' ',
  989. // x, this.tput.strings.erase_chars ? xx : line.length,
  990. // y, y + 1);
  991. // x = xx - 1;
  992. // continue;
  993. // }
  994. // Skip to the next line if the
  995. // rest of the line is already drawn.
  996. // if (!neq) {
  997. // for (; xx < line.length; xx++) {
  998. // if (line[xx][0] !== o[xx][0] || line[xx][1] !== o[xx][1]) {
  999. // neq = true;
  1000. // break;
  1001. // }
  1002. // }
  1003. // if (!neq) {
  1004. // attr = data;
  1005. // break;
  1006. // }
  1007. // }
  1008. }
  1009. // Optimize by comparing the real output
  1010. // buffer to the pending output buffer.
  1011. if (data === o[x][0] && ch === o[x][1]) {
  1012. if (lx === -1) {
  1013. lx = x;
  1014. ly = y;
  1015. }
  1016. continue;
  1017. } else if (lx !== -1) {
  1018. if (this.tput.strings.parm_right_cursor) {
  1019. out += y === ly
  1020. ? this.tput.cuf(x - lx)
  1021. : this.tput.cup(y, x);
  1022. } else {
  1023. out += this.tput.cup(y, x);
  1024. }
  1025. lx = -1, ly = -1;
  1026. }
  1027. o[x][0] = data;
  1028. o[x][1] = ch;
  1029. if (data !== attr) {
  1030. if (attr !== this.dattr) {
  1031. out += '\x1b[m';
  1032. }
  1033. if (data !== this.dattr) {
  1034. out += '\x1b[';
  1035. bg = data & 0x1ff;
  1036. fg = (data >> 9) & 0x1ff;
  1037. flags = data >> 18;
  1038. // bold
  1039. if (flags & 1) {
  1040. out += '1;';
  1041. }
  1042. // underline
  1043. if (flags & 2) {
  1044. out += '4;';
  1045. }
  1046. // blink
  1047. if (flags & 4) {
  1048. out += '5;';
  1049. }
  1050. // inverse
  1051. if (flags & 8) {
  1052. out += '7;';
  1053. }
  1054. // invisible
  1055. if (flags & 16) {
  1056. out += '8;';
  1057. }
  1058. if (bg !== 0x1ff) {
  1059. bg = this._reduceColor(bg);
  1060. if (bg < 16) {
  1061. if (bg < 8) {
  1062. bg += 40;
  1063. } else if (bg < 16) {
  1064. bg -= 8;
  1065. bg += 100;
  1066. }
  1067. out += bg + ';';
  1068. } else {
  1069. out += '48;5;' + bg + ';';
  1070. }
  1071. }
  1072. if (fg !== 0x1ff) {
  1073. fg = this._reduceColor(fg);
  1074. if (fg < 16) {
  1075. if (fg < 8) {
  1076. fg += 30;
  1077. } else if (fg < 16) {
  1078. fg -= 8;
  1079. fg += 90;
  1080. }
  1081. out += fg + ';';
  1082. } else {
  1083. out += '38;5;' + fg + ';';
  1084. }
  1085. }
  1086. if (out[out.length - 1] === ';') out = out.slice(0, -1);
  1087. out += 'm';
  1088. }
  1089. }
  1090. // If we find a double-width char, eat the next character which should be
  1091. // a space due to parseContent's behavior.
  1092. if (this.fullUnicode) {
  1093. // If this is a surrogate pair double-width char, we can ignore it
  1094. // because parseContent already counted it as length=2.
  1095. if (unicode.charWidth(line[x][1]) === 2) {
  1096. // NOTE: At cols=44, the bug that is avoided
  1097. // by the angles check occurs in widget-unicode:
  1098. // Might also need: `line[x + 1][0] !== line[x][0]`
  1099. // for borderless boxes?
  1100. if (x === line.length - 1 || angles[line[x + 1][1]]) {
  1101. // If we're at the end, we don't have enough space for a
  1102. // double-width. Overwrite it with a space and ignore.
  1103. ch = ' ';
  1104. o[x][1] = '\0';
  1105. } else {
  1106. // ALWAYS refresh double-width chars because this special cursor
  1107. // behavior is needed. There may be a more efficient way of doing
  1108. // this. See above.
  1109. o[x][1] = '\0';
  1110. // Eat the next character by moving forward and marking as a
  1111. // space (which it is).
  1112. o[++x][1] = '\0';
  1113. }
  1114. }
  1115. }
  1116. // Attempt to use ACS for supported characters.
  1117. // This is not ideal, but it's how ncurses works.
  1118. // There are a lot of terminals that support ACS
  1119. // *and UTF8, but do not declare U8. So ACS ends
  1120. // up being used (slower than utf8). Terminals
  1121. // that do not support ACS and do not explicitly
  1122. // support UTF8 get their unicode characters
  1123. // replaced with really ugly ascii characters.
  1124. // It is possible there is a terminal out there
  1125. // somewhere that does not support ACS, but
  1126. // supports UTF8, but I imagine it's unlikely.
  1127. // Maybe remove !this.tput.unicode check, however,
  1128. // this seems to be the way ncurses does it.
  1129. if (this.tput.strings.enter_alt_charset_mode
  1130. && !this.tput.brokenACS && (this.tput.acscr[ch] || acs)) {
  1131. // Fun fact: even if this.tput.brokenACS wasn't checked here,
  1132. // the linux console would still work fine because the acs
  1133. // table would fail the check of: this.tput.acscr[ch]
  1134. if (this.tput.acscr[ch]) {
  1135. if (acs) {
  1136. ch = this.tput.acscr[ch];
  1137. } else {
  1138. ch = this.tput.smacs()
  1139. + this.tput.acscr[ch];
  1140. acs = true;
  1141. }
  1142. } else if (acs) {
  1143. ch = this.tput.rmacs() + ch;
  1144. acs = false;
  1145. }
  1146. } else {
  1147. // U8 is not consistently correct. Some terminfo's
  1148. // terminals that do not declare it may actually
  1149. // support utf8 (e.g. urxvt), but if the terminal
  1150. // does not declare support for ACS (and U8), chances
  1151. // are it does not support UTF8. This is probably
  1152. // the "safest" way to do this. Should fix things
  1153. // like sun-color.
  1154. // NOTE: It could be the case that the $LANG
  1155. // is all that matters in some cases:
  1156. // if (!this.tput.unicode && ch > '~') {
  1157. if (!this.tput.unicode && this.tput.numbers.U8 !== 1 && ch > '~') {
  1158. ch = this.tput.utoa[ch] || '?';
  1159. }
  1160. }
  1161. out += ch;
  1162. attr = data;
  1163. }
  1164. if (attr !== this.dattr) {
  1165. out += '\x1b[m';
  1166. }
  1167. if (out) {
  1168. main += this.tput.cup(y, 0) + out;
  1169. }
  1170. }
  1171. if (acs) {
  1172. main += this.tput.rmacs();
  1173. acs = false;
  1174. }
  1175. if (main) {
  1176. pre = '';
  1177. post = '';
  1178. pre += this.tput.sc();
  1179. post += this.tput.rc();
  1180. if (!this.program.cursorHidden) {
  1181. pre += this.tput.civis();
  1182. post += this.tput.cnorm();
  1183. }
  1184. // this.program.flush();
  1185. // this.program._owrite(pre + main + post);
  1186. this.program._write(pre + main + post);
  1187. }
  1188. // this.emit('draw');
  1189. };
  1190. Screen.prototype._reduceColor = function(color) {
  1191. return colors.reduce(color, this.tput.colors);
  1192. };
  1193. // Convert an SGR string to our own attribute format.
  1194. Screen.prototype.attrCode = function(code, cur, def) {
  1195. var flags = (cur >> 18) & 0x1ff
  1196. , fg = (cur >> 9) & 0x1ff
  1197. , bg = cur & 0x1ff
  1198. , c
  1199. , i;
  1200. code = code.slice(2, -1).split(';');
  1201. if (!code[0]) code[0] = '0';
  1202. for (i = 0; i < code.length; i++) {
  1203. c = +code[i] || 0;
  1204. switch (c) {
  1205. case 0: // normal
  1206. bg = def & 0x1ff;
  1207. fg = (def >> 9) & 0x1ff;
  1208. flags = (def >> 18) & 0x1ff;
  1209. break;
  1210. case 1: // bold
  1211. flags |= 1;
  1212. break;
  1213. case 22:
  1214. flags = (def >> 18) & 0x1ff;
  1215. break;
  1216. case 4: // underline
  1217. flags |= 2;
  1218. break;
  1219. case 24:
  1220. flags = (def >> 18) & 0x1ff;
  1221. break;
  1222. case 5: // blink
  1223. flags |= 4;
  1224. break;
  1225. case 25:
  1226. flags = (def >> 18) & 0x1ff;
  1227. break;
  1228. case 7: // inverse
  1229. flags |= 8;
  1230. break;
  1231. case 27:
  1232. flags = (def >> 18) & 0x1ff;
  1233. break;
  1234. case 8: // invisible
  1235. flags |= 16;
  1236. break;
  1237. case 28:
  1238. flags = (def >> 18) & 0x1ff;
  1239. break;
  1240. case 39: // default fg
  1241. fg = (def >> 9) & 0x1ff;
  1242. break;
  1243. case 49: // default bg
  1244. bg = def & 0x1ff;
  1245. break;
  1246. case 100: // default fg/bg
  1247. fg = (def >> 9) & 0x1ff;
  1248. bg = def & 0x1ff;
  1249. break;
  1250. default: // color
  1251. if (c === 48 && +code[i+1] === 5) {
  1252. i += 2;
  1253. bg = +code[i];
  1254. break;
  1255. } else if (c === 48 && +code[i+1] === 2) {
  1256. i += 2;
  1257. bg = colors.match(+code[i], +code[i+1], +code[i+2]);
  1258. if (bg === -1) bg = def & 0x1ff;
  1259. i += 2;
  1260. break;
  1261. } else if (c === 38 && +code[i+1] === 5) {
  1262. i += 2;
  1263. fg = +code[i];
  1264. break;
  1265. } else if (c === 38 && +code[i+1] === 2) {
  1266. i += 2;
  1267. fg = colors.match(+code[i], +code[i+1], +code[i+2]);
  1268. if (fg === -1) fg = (def >> 9) & 0x1ff;
  1269. i += 2;
  1270. break;
  1271. }
  1272. if (c >= 40 && c <= 47) {
  1273. bg = c - 40;
  1274. } else if (c >= 100 && c <= 107) {
  1275. bg = c - 100;
  1276. bg += 8;
  1277. } else if (c === 49) {
  1278. bg = def & 0x1ff;
  1279. } else if (c >= 30 && c <= 37) {
  1280. fg = c - 30;
  1281. } else if (c >= 90 && c <= 97) {
  1282. fg = c - 90;
  1283. fg += 8;
  1284. } else if (c === 39) {
  1285. fg = (def >> 9) & 0x1ff;
  1286. } else if (c === 100) {
  1287. fg = (def >> 9) & 0x1ff;
  1288. bg = def & 0x1ff;
  1289. }
  1290. break;
  1291. }
  1292. }
  1293. return (flags << 18) | (fg << 9) | bg;
  1294. };
  1295. // Convert our own attribute format to an SGR string.
  1296. Screen.prototype.codeAttr = function(code) {
  1297. var flags = (code >> 18) & 0x1ff
  1298. , fg = (code >> 9) & 0x1ff
  1299. , bg = code & 0x1ff
  1300. , out = '';
  1301. // bold
  1302. if (flags & 1) {
  1303. out += '1;';
  1304. }
  1305. // underline
  1306. if (flags & 2) {
  1307. out += '4;';
  1308. }
  1309. // blink
  1310. if (flags & 4) {
  1311. out += '5;';
  1312. }
  1313. // inverse
  1314. if (flags & 8) {
  1315. out += '7;';
  1316. }
  1317. // invisible
  1318. if (flags & 16) {
  1319. out += '8;';
  1320. }
  1321. if (bg !== 0x1ff) {
  1322. bg = this._reduceColor(bg);
  1323. if (bg < 16) {
  1324. if (bg < 8) {
  1325. bg += 40;
  1326. } else if (bg < 16) {
  1327. bg -= 8;
  1328. bg += 100;
  1329. }
  1330. out += bg + ';';
  1331. } else {
  1332. out += '48;5;' + bg + ';';
  1333. }
  1334. }
  1335. if (fg !== 0x1ff) {
  1336. fg = this._reduceColor(fg);
  1337. if (fg < 16) {
  1338. if (fg < 8) {
  1339. fg += 30;
  1340. } else if (fg < 16) {
  1341. fg -= 8;
  1342. fg += 90;
  1343. }
  1344. out += fg + ';';
  1345. } else {
  1346. out += '38;5;' + fg + ';';
  1347. }
  1348. }
  1349. if (out[out.length - 1] === ';') out = out.slice(0, -1);
  1350. return '\x1b[' + out + 'm';
  1351. };
  1352. Screen.prototype.focusOffset = function(offset) {
  1353. var shown = this.keyable.filter(function(el) {
  1354. return !el.detached && el.visible;
  1355. }).length;
  1356. if (!shown || !offset) {
  1357. return;
  1358. }
  1359. var i = this.keyable.indexOf(this.focused);
  1360. if (!~i) return;
  1361. if (offset > 0) {
  1362. while (offset--) {
  1363. if (++i > this.keyable.length - 1) i = 0;
  1364. if (this.keyable[i].detached || !this.keyable[i].visible) offset++;
  1365. }
  1366. } else {
  1367. offset = -offset;
  1368. while (offset--) {
  1369. if (--i < 0) i = this.keyable.length - 1;
  1370. if (this.keyable[i].detached || !this.keyable[i].visible) offset++;
  1371. }
  1372. }
  1373. return this.keyable[i].focus();
  1374. };
  1375. Screen.prototype.focusPrev =
  1376. Screen.prototype.focusPrevious = function() {
  1377. return this.focusOffset(-1);
  1378. };
  1379. Screen.prototype.focusNext = function() {
  1380. return this.focusOffset(1);
  1381. };
  1382. Screen.prototype.focusPush = function(el) {
  1383. if (!el) return;
  1384. var old = this.history[this.history.length - 1];
  1385. if (this.history.length === 10) {
  1386. this.history.shift();
  1387. }
  1388. this.history.push(el);
  1389. this._focus(el, old);
  1390. };
  1391. Screen.prototype.focusPop = function() {
  1392. var old = this.history.pop();
  1393. if (this.history.length) {
  1394. this._focus(this.history[this.history.length - 1], old);
  1395. }
  1396. return old;
  1397. };
  1398. Screen.prototype.saveFocus = function() {
  1399. return this._savedFocus = this.focused;
  1400. };
  1401. Screen.prototype.restoreFocus = function() {
  1402. if (!this._savedFocus) return;
  1403. this._savedFocus.focus();
  1404. delete this._savedFocus;
  1405. return this.focused;
  1406. };
  1407. Screen.prototype.rewindFocus = function() {
  1408. var old = this.history.pop()
  1409. , el;
  1410. while (this.history.length) {
  1411. el = this.history.pop();
  1412. if (!el.detached && el.visible) {
  1413. this.history.push(el);
  1414. this._focus(el, old);
  1415. return el;
  1416. }
  1417. }
  1418. if (old) {
  1419. old.emit('blur');
  1420. }
  1421. };
  1422. Screen.prototype._focus = function(self, old) {
  1423. // Find a scrollable ancestor if we have one.
  1424. var el = self;
  1425. while (el = el.parent) {
  1426. if (el.scrollable) break;
  1427. }
  1428. // If we're in a scrollable element,
  1429. // automatically scroll to the focused element.
  1430. if (el && !el.detached) {
  1431. // NOTE: This is different from the other "visible" values - it needs the
  1432. // visible height of the scrolling element itself, not the element within
  1433. // it.
  1434. var visible = self.screen.height - el.atop - el.itop - el.abottom - el.ibottom;
  1435. if (self.rtop < el.childBase) {
  1436. el.scrollTo(self.rtop);
  1437. self.screen.render();
  1438. } else if (self.rtop + self.height - self.ibottom > el.childBase + visible) {
  1439. // Explanation for el.itop here: takes into account scrollable elements
  1440. // with borders otherwise the element gets covered by the bottom border:
  1441. el.scrollTo(self.rtop - (el.height - self.height) + el.itop, true);
  1442. self.screen.render();
  1443. }
  1444. }
  1445. if (old) {
  1446. old.emit('blur', self);
  1447. }
  1448. self.emit('focus', old);
  1449. };
  1450. Screen.prototype.__defineGetter__('focused', function() {
  1451. return this.history[this.history.length - 1];
  1452. });
  1453. Screen.prototype.__defineSetter__('focused', function(el) {
  1454. return this.focusPush(el);
  1455. });
  1456. Screen.prototype.clearRegion = function(xi, xl, yi, yl, override) {
  1457. return this.fillRegion(this.dattr, ' ', xi, xl, yi, yl, override);
  1458. };
  1459. Screen.prototype.fillRegion = function(attr, ch, xi, xl, yi, yl, override) {
  1460. var lines = this.lines
  1461. , cell
  1462. , xx;
  1463. if (xi < 0) xi = 0;
  1464. if (yi < 0) yi = 0;
  1465. for (; yi < yl; yi++) {
  1466. if (!lines[yi]) break;
  1467. for (xx = xi; xx < xl; xx++) {
  1468. cell = lines[yi][xx];
  1469. if (!cell) break;
  1470. if (override || attr !== cell[0] || ch !== cell[1]) {
  1471. lines[yi][xx][0] = attr;
  1472. lines[yi][xx][1] = ch;
  1473. lines[yi].dirty = true;
  1474. }
  1475. }
  1476. }
  1477. };
  1478. Screen.prototype.key = function() {
  1479. return this.program.key.apply(this, arguments);
  1480. };
  1481. Screen.prototype.onceKey = function() {
  1482. return this.program.onceKey.apply(this, arguments);
  1483. };
  1484. Screen.prototype.unkey =
  1485. Screen.prototype.removeKey = function() {
  1486. return this.program.unkey.apply(this, arguments);
  1487. };
  1488. Screen.prototype.spawn = function(file, args, options) {
  1489. if (!Array.isArray(args)) {
  1490. options = args;
  1491. args = [];
  1492. }
  1493. var screen = this
  1494. , program = screen.program
  1495. , spawn = require('child_process').spawn
  1496. , mouse = program.mouseEnabled
  1497. , ps;
  1498. options = options || {};
  1499. options.stdio = options.stdio || 'inherit';
  1500. program.lsaveCursor('spawn');
  1501. // program.csr(0, program.rows - 1);
  1502. program.normalBuffer();
  1503. program.showCursor();
  1504. if (mouse) program.disableMouse();
  1505. var write = program.output.write;
  1506. program.output.write = function() {};
  1507. program.input.pause();
  1508. if (program.input.setRawMode) {
  1509. program.input.setRawMode(false);
  1510. }
  1511. var resume = function() {
  1512. if (resume.done) return;
  1513. resume.done = true;
  1514. if (program.input.setRawMode) {
  1515. program.input.setRawMode(true);
  1516. }
  1517. program.input.resume();
  1518. program.output.write = write;
  1519. program.alternateBuffer();
  1520. // program.csr(0, program.rows - 1);
  1521. if (mouse) {
  1522. program.enableMouse();
  1523. if (screen.options.sendFocus) {
  1524. screen.program.setMouse({ sendFocus: true }, true);
  1525. }
  1526. }
  1527. screen.alloc();
  1528. screen.render();
  1529. screen.program.lrestoreCursor('spawn', true);
  1530. };
  1531. ps = spawn(file, args, options);
  1532. ps.on('error', resume);
  1533. ps.on('exit', resume);
  1534. return ps;
  1535. };
  1536. Screen.prototype.exec = function(file, args, options, callback) {
  1537. var ps = this.spawn(file, args, options);
  1538. ps.on('error', function(err) {
  1539. if (!callback) return;
  1540. return callback(err, false);
  1541. });
  1542. ps.on('exit', function(code) {
  1543. if (!callback) return;
  1544. return callback(null, code === 0);
  1545. });
  1546. return ps;
  1547. };
  1548. Screen.prototype.readEditor = function(options, callback) {
  1549. if (typeof options === 'string') {
  1550. options = { editor: options };
  1551. }
  1552. if (!callback) {
  1553. callback = options;
  1554. options = null;
  1555. }
  1556. if (!callback) {
  1557. callback = function() {};
  1558. }
  1559. options = options || {};
  1560. var self = this
  1561. , editor = options.editor || process.env.EDITOR || 'vi'
  1562. , name = options.name || process.title || 'blessed'
  1563. , rnd = Math.random().toString(36).split('.').pop()
  1564. , file = '/tmp/' + name + '.' + rnd
  1565. , args = [file]
  1566. , opt;
  1567. opt = {
  1568. stdio: 'inherit',
  1569. env: process.env,
  1570. cwd: process.env.HOME
  1571. };
  1572. function writeFile(callback) {
  1573. if (!options.value) return callback();
  1574. return fs.writeFile(file, options.value, callback);
  1575. }
  1576. return writeFile(function(err) {
  1577. if (err) return callback(err);
  1578. return self.exec(editor, args, opt, function(err, success) {
  1579. if (err) return callback(err);
  1580. return fs.readFile(file, 'utf8', function(err, data) {
  1581. return fs.unlink(file, function() {
  1582. if (!success) return callback(new Error('Unsuccessful.'));
  1583. if (err) return callback(err);
  1584. return callback(null, data);
  1585. });
  1586. });
  1587. });
  1588. });
  1589. };
  1590. Screen.prototype.displayImage = function(file, callback) {
  1591. if (!file) {
  1592. if (!callback) return;
  1593. return callback(new Error('No image.'));
  1594. }
  1595. file = path.resolve(process.cwd(), file);
  1596. if (!~file.indexOf('://')) {
  1597. file = 'file://' + file;
  1598. }
  1599. var args = ['w3m', '-T', 'text/html'];
  1600. var input = '<title>press q to exit</title>'
  1601. + '<img align="center" src="' + file + '">';
  1602. var opt = {
  1603. stdio: ['pipe', 1, 2],
  1604. env: process.env,
  1605. cwd: process.env.HOME
  1606. };
  1607. var ps = this.spawn(args[0], args.slice(1), opt);
  1608. ps.on('error', function(err) {
  1609. if (!callback) return;
  1610. return callback(err);
  1611. });
  1612. ps.on('exit', function(code) {
  1613. if (!callback) return;
  1614. if (code !== 0) return callback(new Error('Exit Code: ' + code));
  1615. return callback(null, code === 0);
  1616. });
  1617. ps.stdin.write(input + '\n');
  1618. ps.stdin.end();
  1619. };
  1620. Screen.prototype.setEffects = function(el, fel, over, out, effects, temp) {
  1621. if (!effects) return;
  1622. var tmp = {};
  1623. if (temp) el[temp] = tmp;
  1624. if (typeof el !== 'function') {
  1625. var _el = el;
  1626. el = function() { return _el; };
  1627. }
  1628. fel.on(over, function() {
  1629. var element = el();
  1630. Object.keys(effects).forEach(function(key) {
  1631. var val = effects[key];
  1632. if (val !== null && typeof val === 'object') {
  1633. tmp[key] = tmp[key] || {};
  1634. // element.style[key] = element.style[key] || {};
  1635. Object.keys(val).forEach(function(k) {
  1636. var v = val[k];
  1637. tmp[key][k] = element.style[key][k];
  1638. element.style[key][k] = v;
  1639. });
  1640. return;
  1641. }
  1642. tmp[key] = element.style[key];
  1643. element.style[key] = val;
  1644. });
  1645. element.screen.render();
  1646. });
  1647. fel.on(out, function() {
  1648. var element = el();
  1649. Object.keys(effects).forEach(function(key) {
  1650. var val = effects[key];
  1651. if (val !== null && typeof val === 'object') {
  1652. tmp[key] = tmp[key] || {};
  1653. // element.style[key] = element.style[key] || {};
  1654. Object.keys(val).forEach(function(k) {
  1655. if (tmp[key].hasOwnProperty(k)) {
  1656. element.style[key][k] = tmp[key][k];
  1657. }
  1658. });
  1659. return;
  1660. }
  1661. if (tmp.hasOwnProperty(key)) {
  1662. element.style[key] = tmp[key];
  1663. }
  1664. });
  1665. element.screen.render();
  1666. });
  1667. };
  1668. Screen.prototype.sigtstp = function(callback) {
  1669. var self = this;
  1670. this.program.sigtstp(function() {
  1671. self.alloc();
  1672. self.render();
  1673. self.program.lrestoreCursor('pause', true);
  1674. if (callback) callback();
  1675. });
  1676. };
  1677. Screen.prototype.copyToClipboard = function(text) {
  1678. return this.program.copyToClipboard(text);
  1679. };
  1680. Screen.prototype.cursorShape = function(shape, blink) {
  1681. var self = this;
  1682. this.cursor.shape = shape || 'block';
  1683. this.cursor.blink = blink || false;
  1684. this.cursor._set = true;
  1685. if (this.cursor.artificial) {
  1686. if (!this.program.hideCursor_old) {
  1687. var hideCursor = this.program.hideCursor;
  1688. this.program.hideCursor_old = this.program.hideCursor;
  1689. this.program.hideCursor = function() {
  1690. hideCursor.call(self.program);
  1691. self.cursor._hidden = true;
  1692. if (self.renders) self.render();
  1693. };
  1694. }
  1695. if (!this.program.showCursor_old) {
  1696. var showCursor = this.program.showCursor;
  1697. this.program.showCursor_old = this.program.showCursor;
  1698. this.program.showCursor = function() {
  1699. self.cursor._hidden = false;
  1700. if (self.program._exiting) showCursor.call(self.program);
  1701. if (self.renders) self.render();
  1702. };
  1703. }
  1704. if (!this._cursorBlink) {
  1705. this._cursorBlink = setInterval(function() {
  1706. if (!self.cursor.blink) return;
  1707. self.cursor._state ^= 1;
  1708. if (self.renders) self.render();
  1709. }, 500);
  1710. if (this._cursorBlink.unref) {
  1711. this._cursorBlink.unref();
  1712. }
  1713. }
  1714. return true;
  1715. }
  1716. return this.program.cursorShape(this.cursor.shape, this.cursor.blink);
  1717. };
  1718. Screen.prototype.cursorColor = function(color) {
  1719. this.cursor.color = color != null
  1720. ? colors.convert(color)
  1721. : null;
  1722. this.cursor._set = true;
  1723. if (this.cursor.artificial) {
  1724. return true;
  1725. }
  1726. return this.program.cursorColor(colors.ncolors[this.cursor.color]);
  1727. };
  1728. Screen.prototype.cursorReset =
  1729. Screen.prototype.resetCursor = function() {
  1730. this.cursor.shape = 'block';
  1731. this.cursor.blink = false;
  1732. this.cursor.color = null;
  1733. this.cursor._set = false;
  1734. if (this.cursor.artificial) {
  1735. this.cursor.artificial = false;
  1736. if (this.program.hideCursor_old) {
  1737. this.program.hideCursor = this.program.hideCursor_old;
  1738. delete this.program.hideCursor_old;
  1739. }
  1740. if (this.program.showCursor_old) {
  1741. this.program.showCursor = this.program.showCursor_old;
  1742. delete this.program.showCursor_old;
  1743. }
  1744. if (this._cursorBlink) {
  1745. clearInterval(this._cursorBlink);
  1746. delete this._cursorBlink;
  1747. }
  1748. return true;
  1749. }
  1750. return this.program.cursorReset();
  1751. };
  1752. Screen.prototype._cursorAttr = function(cursor, dattr) {
  1753. var attr = dattr || this.dattr
  1754. , cattr
  1755. , ch;
  1756. if (cursor.shape === 'line') {
  1757. attr &= ~(0x1ff << 9);
  1758. attr |= 7 << 9;
  1759. ch = '\u2502';
  1760. } else if (cursor.shape === 'underline') {
  1761. attr &= ~(0x1ff << 9);
  1762. attr |= 7 << 9;
  1763. attr |= 2 << 18;
  1764. } else if (cursor.shape === 'block') {
  1765. attr &= ~(0x1ff << 9);
  1766. attr |= 7 << 9;
  1767. attr |= 8 << 18;
  1768. } else if (typeof cursor.shape === 'object' && cursor.shape) {
  1769. cattr = Element.prototype.sattr.call(cursor, cursor.shape);
  1770. if (cursor.shape.bold || cursor.shape.underline
  1771. || cursor.shape.blink || cursor.shape.inverse
  1772. || cursor.shape.invisible) {
  1773. attr &= ~(0x1ff << 18);
  1774. attr |= ((cattr >> 18) & 0x1ff) << 18;
  1775. }
  1776. if (cursor.shape.fg) {
  1777. attr &= ~(0x1ff << 9);
  1778. attr |= ((cattr >> 9) & 0x1ff) << 9;
  1779. }
  1780. if (cursor.shape.bg) {
  1781. attr &= ~(0x1ff << 0);
  1782. attr |= cattr & 0x1ff;
  1783. }
  1784. if (cursor.shape.ch) {
  1785. ch = cursor.shape.ch;
  1786. }
  1787. }
  1788. if (cursor.color != null) {
  1789. attr &= ~(0x1ff << 9);
  1790. attr |= cursor.color << 9;
  1791. }
  1792. return {
  1793. ch: ch,
  1794. attr: attr
  1795. };
  1796. };
  1797. Screen.prototype.screenshot = function(xi, xl, yi, yl, term) {
  1798. if (xi == null) xi = 0;
  1799. if (xl == null) xl = this.cols;
  1800. if (yi == null) yi = 0;
  1801. if (yl == null) yl = this.rows;
  1802. if (xi < 0) xi = 0;
  1803. if (yi < 0) yi = 0;
  1804. var x
  1805. , y
  1806. , line
  1807. , out
  1808. , ch
  1809. , data
  1810. , attr;
  1811. var sdattr = this.dattr;
  1812. if (term) {
  1813. this.dattr = term.defAttr;
  1814. }
  1815. var main = '';
  1816. for (y = yi; y < yl; y++) {
  1817. line = term
  1818. ? term.lines[y]
  1819. : this.lines[y];
  1820. if (!line) break;
  1821. out = '';
  1822. attr = this.dattr;
  1823. for (x = xi; x < xl; x++) {
  1824. if (!line[x]) break;
  1825. data = line[x][0];
  1826. ch = line[x][1];
  1827. if (data !== attr) {
  1828. if (attr !== this.dattr) {
  1829. out += '\x1b[m';
  1830. }
  1831. if (data !== this.dattr) {
  1832. var _data = data;
  1833. if (term) {
  1834. if (((_data >> 9) & 0x1ff) === 257) _data |= 0x1ff << 9;
  1835. if ((_data & 0x1ff) === 256) _data |= 0x1ff;
  1836. }
  1837. out += this.codeAttr(_data);
  1838. }
  1839. }
  1840. if (this.fullUnicode) {
  1841. if (unicode.charWidth(line[x][1]) === 2) {
  1842. if (x === xl - 1) {
  1843. ch = ' ';
  1844. } else {
  1845. x++;
  1846. }
  1847. }
  1848. }
  1849. out += ch;
  1850. attr = data;
  1851. }
  1852. if (attr !== this.dattr) {
  1853. out += '\x1b[m';
  1854. }
  1855. if (out) {
  1856. main += (y > 0 ? '\n' : '') + out;
  1857. }
  1858. }
  1859. main = main.replace(/(?:\s*\x1b\[40m\s*\x1b\[m\s*)*$/, '') + '\n';
  1860. if (term) {
  1861. this.dattr = sdattr;
  1862. }
  1863. return main;
  1864. };
  1865. /**
  1866. * Positioning
  1867. */
  1868. Screen.prototype._getPos = function() {
  1869. return this;
  1870. };
  1871. /**
  1872. * Angle Table
  1873. */
  1874. var angles = {
  1875. '\u2518': true, // '┘'
  1876. '\u2510': true, // '┐'
  1877. '\u250c': true, // '┌'
  1878. '\u2514': true, // '└'
  1879. '\u253c': true, // '┼'
  1880. '\u251c': true, // '├'
  1881. '\u2524': true, // '┤'
  1882. '\u2534': true, // '┴'
  1883. '\u252c': true, // '┬'
  1884. '\u2502': true, // '│'
  1885. '\u2500': true // '─'
  1886. };
  1887. var langles = {
  1888. '\u250c': true, // '┌'
  1889. '\u2514': true, // '└'
  1890. '\u253c': true, // '┼'
  1891. '\u251c': true, // '├'
  1892. '\u2534': true, // '┴'
  1893. '\u252c': true, // '┬'
  1894. '\u2500': true // '─'
  1895. };
  1896. var uangles = {
  1897. '\u2510': true, // '┐'
  1898. '\u250c': true, // '┌'
  1899. '\u253c': true, // '┼'
  1900. '\u251c': true, // '├'
  1901. '\u2524': true, // '┤'
  1902. '\u252c': true, // '┬'
  1903. '\u2502': true // '│'
  1904. };
  1905. var rangles = {
  1906. '\u2518': true, // '┘'
  1907. '\u2510': true, // '┐'
  1908. '\u253c': true, // '┼'
  1909. '\u2524': true, // '┤'
  1910. '\u2534': true, // '┴'
  1911. '\u252c': true, // '┬'
  1912. '\u2500': true // '─'
  1913. };
  1914. var dangles = {
  1915. '\u2518': true, // '┘'
  1916. '\u2514': true, // '└'
  1917. '\u253c': true, // '┼'
  1918. '\u251c': true, // '├'
  1919. '\u2524': true, // '┤'
  1920. '\u2534': true, // '┴'
  1921. '\u2502': true // '│'
  1922. };
  1923. // var cdangles = {
  1924. // '\u250c': true // '┌'
  1925. // };
  1926. // Every ACS angle character can be
  1927. // represented by 4 bits ordered like this:
  1928. // [langle][uangle][rangle][dangle]
  1929. var angleTable = {
  1930. '0000': '', // ?
  1931. '0001': '\u2502', // '│' // ?
  1932. '0010': '\u2500', // '─' // ??
  1933. '0011': '\u250c', // '┌'
  1934. '0100': '\u2502', // '│' // ?
  1935. '0101': '\u2502', // '│'
  1936. '0110': '\u2514', // '└'
  1937. '0111': '\u251c', // '├'
  1938. '1000': '\u2500', // '─' // ??
  1939. '1001': '\u2510', // '┐'
  1940. '1010': '\u2500', // '─' // ??
  1941. '1011': '\u252c', // '┬'
  1942. '1100': '\u2518', // '┘'
  1943. '1101': '\u2524', // '┤'
  1944. '1110': '\u2534', // '┴'
  1945. '1111': '\u253c' // '┼'
  1946. };
  1947. Object.keys(angleTable).forEach(function(key) {
  1948. angleTable[parseInt(key, 2)] = angleTable[key];
  1949. delete angleTable[key];
  1950. });
  1951. /**
  1952. * Expose
  1953. */
  1954. module.exports = Screen;