tput.js 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  1. /**
  2. * tput.js - parse and compile terminfo caps to javascript.
  3. * Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
  4. * https://github.com/chjj/blessed
  5. */
  6. // Resources:
  7. // $ man term
  8. // $ man terminfo
  9. // http://invisible-island.net/ncurses/man/term.5.html
  10. // https://en.wikipedia.org/wiki/Terminfo
  11. // Todo:
  12. // - xterm's XT (set-title capability?) value should
  13. // be true (at least tmux thinks it should).
  14. // It's not parsed as true. Investigate.
  15. // - Possibly switch to other method of finding the
  16. // extended data string table: i += h.symOffsetCount * 2;
  17. /**
  18. * Modules
  19. */
  20. var assert = require('assert')
  21. , path = require('path')
  22. , fs = require('fs')
  23. , cp = require('child_process');
  24. /**
  25. * Tput
  26. */
  27. function Tput(options) {
  28. if (!(this instanceof Tput)) {
  29. return new Tput(options);
  30. }
  31. options = options || {};
  32. if (typeof options === 'string') {
  33. options = { terminal: options };
  34. }
  35. this.options = options;
  36. this.terminal = options.terminal
  37. || options.term
  38. || process.env.TERM
  39. || (process.platform === 'win32' ? 'windows-ansi' : 'xterm');
  40. this.terminal = this.terminal.toLowerCase();
  41. this.debug = options.debug;
  42. this.padding = options.padding;
  43. this.extended = options.extended;
  44. this.printf = options.printf;
  45. this.termcap = options.termcap;
  46. this.error = null;
  47. this.terminfoPrefix = options.terminfoPrefix;
  48. this.terminfoFile = options.terminfoFile;
  49. this.termcapFile = options.termcapFile;
  50. if (options.terminal || options.term) {
  51. this.setup();
  52. }
  53. }
  54. Tput.prototype.setup = function() {
  55. this.error = null;
  56. try {
  57. if (this.termcap) {
  58. try {
  59. this.injectTermcap();
  60. } catch (e) {
  61. if (this.debug) throw e;
  62. this.error = new Error('Termcap parse error.');
  63. this._useInternalCap(this.terminal);
  64. }
  65. } else {
  66. try {
  67. this.injectTerminfo();
  68. } catch (e) {
  69. if (this.debug) throw e;
  70. this.error = new Error('Terminfo parse error.');
  71. this._useInternalInfo(this.terminal);
  72. }
  73. }
  74. } catch (e) {
  75. // If there was an error, fallback
  76. // to an internally stored terminfo/cap.
  77. if (this.debug) throw e;
  78. this.error = new Error('Terminfo not found.');
  79. this._useXtermInfo();
  80. }
  81. };
  82. Tput.prototype.term = function(is) {
  83. return this.terminal.indexOf(is) === 0;
  84. };
  85. Tput.prototype._debug = function() {
  86. if (!this.debug) return;
  87. return console.log.apply(console, arguments);
  88. };
  89. /**
  90. * Fallback
  91. */
  92. Tput.prototype._useVt102Cap = function() {
  93. return this.injectTermcap('vt102');
  94. };
  95. Tput.prototype._useXtermCap = function() {
  96. return this.injectTermcap(__dirname + '/../usr/xterm.termcap');
  97. };
  98. Tput.prototype._useXtermInfo = function() {
  99. return this.injectTerminfo(__dirname + '/../usr/xterm');
  100. };
  101. Tput.prototype._useInternalInfo = function(name) {
  102. name = path.basename(name);
  103. return this.injectTerminfo(__dirname + '/../usr/' + name);
  104. };
  105. Tput.prototype._useInternalCap = function(name) {
  106. name = path.basename(name);
  107. return this.injectTermcap(__dirname + '/../usr/' + name + '.termcap');
  108. };
  109. /**
  110. * Terminfo
  111. */
  112. Tput.ipaths = [
  113. process.env.TERMINFO || '',
  114. (process.env.TERMINFO_DIRS || '').split(':'),
  115. (process.env.HOME || '') + '/.terminfo',
  116. '/usr/share/terminfo',
  117. '/usr/share/lib/terminfo',
  118. '/usr/lib/terminfo',
  119. '/usr/local/share/terminfo',
  120. '/usr/local/share/lib/terminfo',
  121. '/usr/local/lib/terminfo',
  122. '/usr/local/ncurses/lib/terminfo',
  123. '/lib/terminfo'
  124. ];
  125. Tput.prototype.readTerminfo = function(term) {
  126. var data
  127. , file
  128. , info;
  129. term = term || this.terminal;
  130. file = path.normalize(this._prefix(term));
  131. data = fs.readFileSync(file);
  132. info = this.parseTerminfo(data, file);
  133. if (this.debug) {
  134. this._terminfo = info;
  135. }
  136. return info;
  137. };
  138. Tput._prefix =
  139. Tput.prototype._prefix = function(term) {
  140. // If we have a terminfoFile, or our
  141. // term looks like a filename, use it.
  142. if (term) {
  143. if (~term.indexOf(path.sep)) {
  144. return term;
  145. }
  146. if (this.terminfoFile) {
  147. return this.terminfoFile;
  148. }
  149. }
  150. var paths = Tput.ipaths.slice()
  151. , file;
  152. if (this.terminfoPrefix) {
  153. paths.unshift(this.terminfoPrefix);
  154. }
  155. // Try exact matches.
  156. file = this._tprefix(paths, term);
  157. if (file) return file;
  158. // Try similar matches.
  159. file = this._tprefix(paths, term, true);
  160. if (file) return file;
  161. // Not found.
  162. throw new Error('Terminfo directory not found.');
  163. };
  164. Tput._tprefix =
  165. Tput.prototype._tprefix = function(prefix, term, soft) {
  166. if (!prefix) return;
  167. var file
  168. , dir
  169. , i
  170. , sdiff
  171. , sfile
  172. , list;
  173. if (Array.isArray(prefix)) {
  174. for (i = 0; i < prefix.length; i++) {
  175. file = this._tprefix(prefix[i], term, soft);
  176. if (file) return file;
  177. }
  178. return;
  179. }
  180. var find = function(word) {
  181. var file, ch;
  182. file = path.resolve(prefix, word[0]);
  183. try {
  184. fs.statSync(file);
  185. return file;
  186. } catch (e) {
  187. ;
  188. }
  189. ch = word[0].charCodeAt(0).toString(16);
  190. if (ch.length < 2) ch = '0' + ch;
  191. file = path.resolve(prefix, ch);
  192. try {
  193. fs.statSync(file);
  194. return file;
  195. } catch (e) {
  196. ;
  197. }
  198. };
  199. if (!term) {
  200. // Make sure the directory's sub-directories
  201. // are all one-letter, or hex digits.
  202. // return find('x') ? prefix : null;
  203. try {
  204. dir = fs.readdirSync(prefix).filter(function(file) {
  205. return file.length !== 1 && !/^[0-9a-fA-F]{2}$/.test(file);
  206. });
  207. if (!dir.length) {
  208. return prefix;
  209. }
  210. } catch (e) {
  211. ;
  212. }
  213. return;
  214. }
  215. term = path.basename(term);
  216. dir = find(term);
  217. if (!dir) return;
  218. if (soft) {
  219. try {
  220. list = fs.readdirSync(dir);
  221. } catch (e) {
  222. return;
  223. }
  224. list.forEach(function(file) {
  225. if (file.indexOf(term) === 0) {
  226. var diff = file.length - term.length;
  227. if (!sfile || diff < sdiff) {
  228. sdiff = diff;
  229. sfile = file;
  230. }
  231. }
  232. });
  233. return sfile && (soft || sdiff === 0)
  234. ? path.resolve(dir, sfile)
  235. : null;
  236. }
  237. file = path.resolve(dir, term);
  238. try {
  239. fs.statSync(file);
  240. return file;
  241. } catch (e) {
  242. ;
  243. }
  244. };
  245. /**
  246. * Terminfo Parser
  247. * All shorts are little-endian
  248. */
  249. Tput.prototype.parseTerminfo = function(data, file) {
  250. var info = {}
  251. , extended
  252. , l = data.length
  253. , i = 0
  254. , v
  255. , o;
  256. var h = info.header = {
  257. dataSize: data.length,
  258. headerSize: 12,
  259. magicNumber: (data[1] << 8) | data[0],
  260. namesSize: (data[3] << 8) | data[2],
  261. boolCount: (data[5] << 8) | data[4],
  262. numCount: (data[7] << 8) | data[6],
  263. strCount: (data[9] << 8) | data[8],
  264. strTableSize: (data[11] << 8) | data[10]
  265. };
  266. h.total = h.headerSize
  267. + h.namesSize
  268. + h.boolCount
  269. + h.numCount * 2
  270. + h.strCount * 2
  271. + h.strTableSize;
  272. i += h.headerSize;
  273. // Names Section
  274. var names = data.toString('ascii', i, i + h.namesSize - 1)
  275. , parts = names.split('|')
  276. , name = parts[0]
  277. , desc = parts.pop();
  278. info.name = name;
  279. info.names = parts;
  280. info.desc = desc;
  281. info.dir = path.resolve(file, '..', '..');
  282. info.file = file;
  283. i += h.namesSize - 1;
  284. // Names is nul-terminated.
  285. assert.equal(data[i], 0);
  286. i++;
  287. // Booleans Section
  288. // One byte for each flag
  289. // Same order as <term.h>
  290. info.bools = {};
  291. l = i + h.boolCount;
  292. o = 0;
  293. for (; i < l; i++) {
  294. v = Tput.bools[o++];
  295. info.bools[v] = data[i] === 1;
  296. }
  297. // Null byte in between to make sure numbers begin on an even byte.
  298. if (i % 2) {
  299. assert.equal(data[i], 0);
  300. i++;
  301. }
  302. // Numbers Section
  303. info.numbers = {};
  304. l = i + h.numCount * 2;
  305. o = 0;
  306. for (; i < l; i += 2) {
  307. v = Tput.numbers[o++];
  308. if (data[i + 1] === 0377 && data[i] === 0377) {
  309. info.numbers[v] = -1;
  310. } else {
  311. info.numbers[v] = (data[i + 1] << 8) | data[i];
  312. }
  313. }
  314. // Strings Section
  315. info.strings = {};
  316. l = i + h.strCount * 2;
  317. o = 0;
  318. for (; i < l; i += 2) {
  319. v = Tput.strings[o++];
  320. if (data[i + 1] === 0377 && data[i] === 0377) {
  321. info.strings[v] = -1;
  322. } else {
  323. info.strings[v] = (data[i + 1] << 8) | data[i];
  324. }
  325. }
  326. // String Table
  327. Object.keys(info.strings).forEach(function(key) {
  328. if (info.strings[key] === -1) {
  329. delete info.strings[key];
  330. return;
  331. }
  332. // Workaround: fix an odd bug in the screen-256color terminfo where it tries
  333. // to set -1, but it appears to have {0xfe, 0xff} instead of {0xff, 0xff}.
  334. // TODO: Possibly handle errors gracefully below, as well as in the
  335. // extended info. Also possibly do: `if (info.strings[key] >= data.length)`.
  336. if (info.strings[key] === 65534) {
  337. delete info.strings[key];
  338. return;
  339. }
  340. var s = i + info.strings[key]
  341. , j = s;
  342. while (data[j]) j++;
  343. assert(j < data.length);
  344. info.strings[key] = data.toString('ascii', s, j);
  345. });
  346. // Extended Header
  347. if (this.extended !== false) {
  348. i--;
  349. i += h.strTableSize;
  350. if (i % 2) {
  351. assert.equal(data[i], 0);
  352. i++;
  353. }
  354. l = data.length;
  355. if (i < l - 1) {
  356. try {
  357. extended = this.parseExtended(data.slice(i));
  358. } catch (e) {
  359. if (this.debug) {
  360. throw e;
  361. }
  362. return info;
  363. }
  364. info.header.extended = extended.header;
  365. ['bools', 'numbers', 'strings'].forEach(function(key) {
  366. merge(info[key], extended[key]);
  367. });
  368. }
  369. }
  370. return info;
  371. };
  372. /**
  373. * Extended Parsing
  374. */
  375. // Some data to help understand:
  376. // For xterm, non-extended header:
  377. // { dataSize: 3270,
  378. // headerSize: 12,
  379. // magicNumber: 282,
  380. // namesSize: 48,
  381. // boolCount: 38,
  382. // numCount: 15,
  383. // strCount: 413,
  384. // strTableSize: 1388,
  385. // total: 2342 }
  386. // For xterm, header:
  387. // Offset: 2342
  388. // { header:
  389. // { dataSize: 928,
  390. // headerSize: 10,
  391. // boolCount: 2,
  392. // numCount: 1,
  393. // strCount: 57,
  394. // strTableSize: 117,
  395. // lastStrTableOffset: 680,
  396. // total: 245 },
  397. // For xterm, layout:
  398. // { header: '0 - 10', // length: 10
  399. // bools: '10 - 12', // length: 2
  400. // numbers: '12 - 14', // length: 2
  401. // strings: '14 - 128', // length: 114 (57 short)
  402. // symoffsets: '128 - 248', // length: 120 (60 short)
  403. // stringtable: '248 - 612', // length: 364
  404. // sym: '612 - 928' } // length: 316
  405. //
  406. // How lastStrTableOffset works:
  407. // data.length - h.lastStrTableOffset === 248
  408. // (sym-offset end, string-table start)
  409. // 364 + 316 === 680 (lastStrTableOffset)
  410. // How strTableSize works:
  411. // h.strCount + [symOffsetCount] === h.strTableSize
  412. // 57 + 60 === 117 (strTableSize)
  413. // symOffsetCount doesn't actually exist in the header. it's just implied.
  414. // Getting the number of sym offsets:
  415. // h.symOffsetCount = h.strTableSize - h.strCount;
  416. // h.symOffsetSize = (h.strTableSize - h.strCount) * 2;
  417. Tput.prototype.parseExtended = function(data) {
  418. var info = {}
  419. , l = data.length
  420. , i = 0;
  421. var h = info.header = {
  422. dataSize: data.length,
  423. headerSize: 10,
  424. boolCount: (data[i + 1] << 8) | data[i + 0],
  425. numCount: (data[i + 3] << 8) | data[i + 2],
  426. strCount: (data[i + 5] << 8) | data[i + 4],
  427. strTableSize: (data[i + 7] << 8) | data[i + 6],
  428. lastStrTableOffset: (data[i + 9] << 8) | data[i + 8]
  429. };
  430. // h.symOffsetCount = h.strTableSize - h.strCount;
  431. h.total = h.headerSize
  432. + h.boolCount
  433. + h.numCount * 2
  434. + h.strCount * 2
  435. + h.strTableSize;
  436. i += h.headerSize;
  437. // Booleans Section
  438. // One byte for each flag
  439. var _bools = [];
  440. l = i + h.boolCount;
  441. for (; i < l; i++) {
  442. _bools.push(data[i] === 1);
  443. }
  444. // Null byte in between to make sure numbers begin on an even byte.
  445. if (i % 2) {
  446. assert.equal(data[i], 0);
  447. i++;
  448. }
  449. // Numbers Section
  450. var _numbers = [];
  451. l = i + h.numCount * 2;
  452. for (; i < l; i += 2) {
  453. if (data[i + 1] === 0377 && data[i] === 0377) {
  454. _numbers.push(-1);
  455. } else {
  456. _numbers.push((data[i + 1] << 8) | data[i]);
  457. }
  458. }
  459. // Strings Section
  460. var _strings = [];
  461. l = i + h.strCount * 2;
  462. for (; i < l; i += 2) {
  463. if (data[i + 1] === 0377 && data[i] === 0377) {
  464. _strings.push(-1);
  465. } else {
  466. _strings.push((data[i + 1] << 8) | data[i]);
  467. }
  468. }
  469. // Pass over the sym offsets and get to the string table.
  470. i = data.length - h.lastStrTableOffset;
  471. // Might be better to do this instead if the file has trailing bytes:
  472. // i += h.symOffsetCount * 2;
  473. // String Table
  474. var high = 0;
  475. _strings.forEach(function(offset, k) {
  476. if (offset === -1) {
  477. _strings[k] = '';
  478. return;
  479. }
  480. var s = i + offset
  481. , j = s;
  482. while (data[j]) j++;
  483. assert(j < data.length);
  484. // Find out where the string table ends by
  485. // getting the highest string length.
  486. if (high < j - i) {
  487. high = j - i;
  488. }
  489. _strings[k] = data.toString('ascii', s, j);
  490. });
  491. // Symbol Table
  492. // Add one to the highest string length because we didn't count \0.
  493. i += high + 1;
  494. l = data.length;
  495. var sym = []
  496. , j;
  497. for (; i < l; i++) {
  498. j = i;
  499. while (data[j]) j++;
  500. sym.push(data.toString('ascii', i, j));
  501. i = j;
  502. }
  503. // Identify by name
  504. j = 0;
  505. info.bools = {};
  506. _bools.forEach(function(bool) {
  507. info.bools[sym[j++]] = bool;
  508. });
  509. info.numbers = {};
  510. _numbers.forEach(function(number) {
  511. info.numbers[sym[j++]] = number;
  512. });
  513. info.strings = {};
  514. _strings.forEach(function(string) {
  515. info.strings[sym[j++]] = string;
  516. });
  517. // Should be the very last bit of data.
  518. assert.equal(i, data.length);
  519. return info;
  520. };
  521. Tput.prototype.compileTerminfo = function(term) {
  522. return this.compile(this.readTerminfo(term));
  523. };
  524. Tput.prototype.injectTerminfo = function(term) {
  525. return this.inject(this.compileTerminfo(term));
  526. };
  527. /**
  528. * Compiler - terminfo cap->javascript
  529. */
  530. Tput.prototype.compile = function(info) {
  531. var self = this;
  532. if (!info) {
  533. throw new Error('Terminal not found.');
  534. }
  535. this.detectFeatures(info);
  536. this._debug(info);
  537. info.all = {};
  538. info.methods = {};
  539. ['bools', 'numbers', 'strings'].forEach(function(type) {
  540. Object.keys(info[type]).forEach(function(key) {
  541. info.all[key] = info[type][key];
  542. info.methods[key] = self._compile(info, key, info.all[key]);
  543. });
  544. });
  545. Tput.bools.forEach(function(key) {
  546. if (info.methods[key] == null) info.methods[key] = false;
  547. });
  548. Tput.numbers.forEach(function(key) {
  549. if (info.methods[key] == null) info.methods[key] = -1;
  550. });
  551. Tput.strings.forEach(function(key) {
  552. if (!info.methods[key]) info.methods[key] = noop;
  553. });
  554. Object.keys(info.methods).forEach(function(key) {
  555. if (!Tput.alias[key]) return;
  556. Tput.alias[key].forEach(function(alias) {
  557. info.methods[alias] = info.methods[key];
  558. });
  559. // Could just use:
  560. // Object.keys(Tput.aliasMap).forEach(function(key) {
  561. // info.methods[key] = info.methods[Tput.aliasMap[key]];
  562. // });
  563. });
  564. return info;
  565. };
  566. Tput.prototype.inject = function(info) {
  567. var self = this
  568. , methods = info.methods || info;
  569. Object.keys(methods).forEach(function(key) {
  570. if (typeof methods[key] !== 'function') {
  571. self[key] = methods[key];
  572. return;
  573. }
  574. self[key] = function() {
  575. var args = Array.prototype.slice.call(arguments);
  576. return methods[key].call(self, args);
  577. };
  578. });
  579. this.info = info;
  580. this.all = info.all;
  581. this.methods = info.methods;
  582. this.bools = info.bools;
  583. this.numbers = info.numbers;
  584. this.strings = info.strings;
  585. if (!~info.names.indexOf(this.terminal)) {
  586. this.terminal = info.name;
  587. }
  588. this.features = info.features;
  589. Object.keys(info.features).forEach(function(key) {
  590. if (key === 'padding') {
  591. if (!info.features.padding && self.options.padding !== true) {
  592. self.padding = false;
  593. }
  594. return;
  595. }
  596. self[key] = info.features[key];
  597. });
  598. };
  599. // See:
  600. // ~/ncurses/ncurses/tinfo/lib_tparm.c
  601. // ~/ncurses/ncurses/tinfo/comp_scan.c
  602. Tput.prototype._compile = function(info, key, str) {
  603. var v;
  604. this._debug('Compiling %s: %s', key, JSON.stringify(str));
  605. switch (typeof str) {
  606. case 'boolean':
  607. return str;
  608. case 'number':
  609. return str;
  610. case 'string':
  611. break;
  612. default:
  613. return noop;
  614. }
  615. if (!str) {
  616. return noop;
  617. }
  618. // See:
  619. // ~/ncurses/progs/tput.c - tput() - L149
  620. // ~/ncurses/progs/tset.c - set_init() - L992
  621. if (key === 'init_file' || key === 'reset_file') {
  622. try {
  623. str = fs.readFileSync(str, 'utf8');
  624. if (this.debug) {
  625. v = ('return ' + JSON.stringify(str) + ';')
  626. .replace(/\x1b/g, '\\x1b')
  627. .replace(/\r/g, '\\r')
  628. .replace(/\n/g, '\\n');
  629. process.stdout.write(v + '\n');
  630. }
  631. return function() { return str; };
  632. } catch (e) {
  633. return noop;
  634. }
  635. }
  636. var tkey = info.name + '.' + key
  637. , header = 'var v, dyn = {}, stat = {}, stack = [], out = [];'
  638. , footer = ';return out.join("");'
  639. , code = header
  640. , val = str
  641. , buff = ''
  642. , cap
  643. , ch
  644. , fi
  645. , then
  646. , els
  647. , end;
  648. function read(regex, no) {
  649. cap = regex.exec(val);
  650. if (!cap) return;
  651. val = val.substring(cap[0].length);
  652. ch = cap[1];
  653. if (!no) clear();
  654. return cap;
  655. }
  656. function stmt(c) {
  657. if (code[code.length - 1] === ',') {
  658. code = code.slice(0, -1);
  659. }
  660. code += c;
  661. }
  662. function expr(c) {
  663. code += c + ',';
  664. }
  665. function echo(c) {
  666. if (c === '""') return;
  667. expr('out.push(' + c + ')');
  668. }
  669. function print(c) {
  670. buff += c;
  671. }
  672. function clear() {
  673. if (buff) {
  674. echo(JSON.stringify(buff).replace(/\\u00([0-9a-fA-F]{2})/g, '\\x$1'));
  675. buff = '';
  676. }
  677. }
  678. while (val) {
  679. // Ignore newlines
  680. if (read(/^\n /, true)) {
  681. continue;
  682. }
  683. // '^A' -> ^A
  684. if (read(/^\^(.)/i, true)) {
  685. if (!(ch >= ' ' && ch <= '~')) {
  686. this._debug('%s: bad caret char.', tkey);
  687. // NOTE: ncurses appears to simply
  688. // continue in this situation, but
  689. // I could be wrong.
  690. print(cap[0]);
  691. continue;
  692. }
  693. if (ch === '?') {
  694. ch = '\x7f';
  695. } else {
  696. ch = ch.charCodeAt(0) & 31;
  697. if (ch === 0) ch = 128;
  698. ch = String.fromCharCode(ch);
  699. }
  700. print(ch);
  701. continue;
  702. }
  703. // 3 octal digits -> character
  704. if (read(/^\\([0-7]{3})/, true)) {
  705. print(String.fromCharCode(parseInt(ch, 8)));
  706. continue;
  707. }
  708. // '\e' -> ^[
  709. // '\n' -> \n
  710. // '\r' -> \r
  711. // '\0' -> \200 (special case)
  712. if (read(/^\\([eEnlrtbfs\^\\,:0]|.)/, true)) {
  713. switch (ch) {
  714. case 'e':
  715. case 'E':
  716. ch = '\x1b';
  717. break;
  718. case 'n':
  719. ch = '\n';
  720. break;
  721. case 'l':
  722. ch = '\x85';
  723. break;
  724. case 'r':
  725. ch = '\r';
  726. break;
  727. case 't':
  728. ch = '\t';
  729. break;
  730. case 'b':
  731. ch = '\x08';
  732. break;
  733. case 'f':
  734. ch = '\x0c';
  735. break;
  736. case 's':
  737. ch = ' ';
  738. break;
  739. case '^':
  740. ch = '^';
  741. break;
  742. case '\\':
  743. ch = '\\';
  744. break;
  745. case ',':
  746. ch = ',';
  747. break;
  748. case ':':
  749. ch = ':';
  750. break;
  751. case '0':
  752. ch = '\200';
  753. break;
  754. case 'a':
  755. ch = '\x07';
  756. break;
  757. default:
  758. this._debug('%s: bad backslash char.', tkey);
  759. ch = cap[0];
  760. break;
  761. }
  762. print(ch);
  763. continue;
  764. }
  765. // $<5> -> padding
  766. // e.g. flash_screen: '\u001b[?5h$<100/>\u001b[?5l',
  767. if (read(/^\$<(\d+)([*\/]{0,2})>/, true)) {
  768. if (this.padding) print(cap[0]);
  769. continue;
  770. }
  771. // %% outputs `%'
  772. if (read(/^%%/, true)) {
  773. print('%');
  774. continue;
  775. }
  776. // %[[:]flags][width[.precision]][doxXs]
  777. // as in printf, flags are [-+#] and space. Use a `:' to allow the
  778. // next character to be a `-' flag, avoiding interpreting "%-" as an
  779. // operator.
  780. // %c print pop() like %c in printf
  781. // Example from screen terminfo:
  782. // S0: "\u001b(%p1%c"
  783. // %d print pop()
  784. // "Print (e.g., "%d") is a special case."
  785. // %s print pop() like %s in printf
  786. if (read(/^%((?::-|[+# ]){1,4})?(\d+(?:\.\d+)?)?([doxXsc])/)) {
  787. if (this.printf || cap[1] || cap[2] || ~'oxX'.indexOf(cap[3])) {
  788. echo('sprintf("'+ cap[0].replace(':-', '-') + '", stack.pop())');
  789. } else if (cap[3] === 'c') {
  790. echo('(v = stack.pop(), isFinite(v) '
  791. + '? String.fromCharCode(v || 0200) : "")');
  792. } else {
  793. echo('stack.pop()');
  794. }
  795. continue;
  796. }
  797. // %p[1-9]
  798. // push i'th parameter
  799. if (read(/^%p([1-9])/)) {
  800. expr('(stack.push(v = params[' + (ch - 1) + ']), v)');
  801. continue;
  802. }
  803. // %P[a-z]
  804. // set dynamic variable [a-z] to pop()
  805. if (read(/^%P([a-z])/)) {
  806. expr('dyn.' + ch + ' = stack.pop()');
  807. continue;
  808. }
  809. // %g[a-z]
  810. // get dynamic variable [a-z] and push it
  811. if (read(/^%g([a-z])/)) {
  812. expr('(stack.push(dyn.' + ch + '), dyn.' + ch + ')');
  813. continue;
  814. }
  815. // %P[A-Z]
  816. // set static variable [a-z] to pop()
  817. if (read(/^%P([A-Z])/)) {
  818. expr('stat.' + ch + ' = stack.pop()');
  819. continue;
  820. }
  821. // %g[A-Z]
  822. // get static variable [a-z] and push it
  823. // The terms "static" and "dynamic" are misleading. Historically,
  824. // these are simply two different sets of variables, whose values are
  825. // not reset between calls to tparm. However, that fact is not
  826. // documented in other implementations. Relying on it will adversely
  827. // impact portability to other implementations.
  828. if (read(/^%g([A-Z])/)) {
  829. expr('(stack.push(v = stat.' + ch + '), v)');
  830. continue;
  831. }
  832. // %'c' char constant c
  833. // NOTE: These are stored as c chars, exemplified by:
  834. // cursor_address: "\u001b=%p1%' '%+%c%p2%' '%+%c"
  835. if (read(/^%'(.)'/)) {
  836. expr('(stack.push(v = ' + ch.charCodeAt(0) + '), v)');
  837. continue;
  838. }
  839. // %{nn}
  840. // integer constant nn
  841. if (read(/^%\{(\d+)\}/)) {
  842. expr('(stack.push(v = ' + ch + '), v)');
  843. continue;
  844. }
  845. // %l push strlen(pop)
  846. if (read(/^%l/)) {
  847. expr('(stack.push(v = (stack.pop() || "").length || 0), v)');
  848. continue;
  849. }
  850. // %+ %- %* %/ %m
  851. // arithmetic (%m is mod): push(pop() op pop())
  852. // %& %| %^
  853. // bit operations (AND, OR and exclusive-OR): push(pop() op pop())
  854. // %= %> %<
  855. // logical operations: push(pop() op pop())
  856. if (read(/^%([+\-*\/m&|\^=><])/)) {
  857. if (ch === '=') ch = '===';
  858. else if (ch === 'm') ch = '%';
  859. expr('(v = stack.pop(),'
  860. + ' stack.push(v = (stack.pop() ' + ch + ' v) || 0),'
  861. + ' v)');
  862. continue;
  863. }
  864. // %A, %O
  865. // logical AND and OR operations (for conditionals)
  866. if (read(/^%([AO])/)) {
  867. // Are we supposed to store the result on the stack?
  868. expr('(stack.push(v = (stack.pop() '
  869. + (ch === 'A' ? '&&' : '||')
  870. + ' stack.pop())), v)');
  871. continue;
  872. }
  873. // %! %~
  874. // unary operations (logical and bit complement): push(op pop())
  875. if (read(/^%([!~])/)) {
  876. expr('(stack.push(v = ' + ch + 'stack.pop()), v)');
  877. continue;
  878. }
  879. // %i add 1 to first two parameters (for ANSI terminals)
  880. if (read(/^%i/)) {
  881. // Are these supposed to go on the stack in certain situations?
  882. // ncurses doesn't seem to put them on the stack, but xterm.user6
  883. // seems to assume they're on the stack for some reason. Could
  884. // just be a bad terminfo string.
  885. // user6: "\u001b[%i%d;%dR" - possibly a termcap-style string.
  886. // expr('(params[0] |= 0, params[1] |= 0, params[0]++, params[1]++)');
  887. expr('(params[0]++, params[1]++)');
  888. continue;
  889. }
  890. // %? expr %t thenpart %e elsepart %;
  891. // This forms an if-then-else. The %e elsepart is optional. Usually
  892. // the %? expr part pushes a value onto the stack, and %t pops it from
  893. // the stack, testing if it is nonzero (true). If it is zero (false),
  894. // control passes to the %e (else) part.
  895. // It is possible to form else-if's a la Algol 68:
  896. // %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e %;
  897. // where ci are conditions, bi are bodies.
  898. if (read(/^%\?/)) {
  899. end = -1;
  900. stmt(';if (');
  901. continue;
  902. }
  903. if (read(/^%t/)) {
  904. end = -1;
  905. // Technically this is supposed to pop everything off the stack that was
  906. // pushed onto the stack after the if statement, see man terminfo.
  907. // Right now, we don't pop anything off. This could cause compat issues.
  908. // Perhaps implement a "pushed" counter from the time the if statement
  909. // is added, to the time the then statement is added, and pop off
  910. // the appropriate number of elements.
  911. // while (pushed--) expr('stack.pop()');
  912. stmt(') {');
  913. continue;
  914. }
  915. // Terminfo does elseif's like
  916. // this: %?[expr]%t...%e[expr]%t...%;
  917. if (read(/^%e/)) {
  918. fi = val.indexOf('%?');
  919. then = val.indexOf('%t');
  920. els = val.indexOf('%e');
  921. end = val.indexOf('%;');
  922. if (end === -1) end = Infinity;
  923. if (then !== -1 && then < end
  924. && (fi === -1 || then < fi)
  925. && (els === -1 || then < els)) {
  926. stmt('} else if (');
  927. } else {
  928. stmt('} else {');
  929. }
  930. continue;
  931. }
  932. if (read(/^%;/)) {
  933. end = null;
  934. stmt('}');
  935. continue;
  936. }
  937. buff += val[0];
  938. val = val.substring(1);
  939. }
  940. // Clear the buffer of any remaining text.
  941. clear();
  942. // Some terminfos (I'm looking at you, atari-color), don't end an if
  943. // statement. It's assumed terminfo will automatically end it for
  944. // them, because they are a bunch of lazy bastards.
  945. if (end != null) {
  946. stmt('}');
  947. }
  948. // Add the footer.
  949. stmt(footer);
  950. // Optimize and cleanup generated code.
  951. v = code.slice(header.length, -footer.length);
  952. if (!v.length) {
  953. code = 'return "";';
  954. } else if (v = /^out\.push\(("(?:[^"]|\\")+")\)$/.exec(v)) {
  955. code = 'return ' + v[1] + ';';
  956. } else {
  957. // Turn `(stack.push(v = params[0]), v),out.push(stack.pop())`
  958. // into `out.push(params[0])`.
  959. code = code.replace(
  960. /\(stack\.push\(v = params\[(\d+)\]\), v\),out\.push\(stack\.pop\(\)\)/g,
  961. 'out.push(params[$1])');
  962. // Remove unnecessary variable initializations.
  963. v = code.slice(header.length, -footer.length);
  964. if (!~v.indexOf('v = ')) code = code.replace('v, ', '');
  965. if (!~v.indexOf('dyn')) code = code.replace('dyn = {}, ', '');
  966. if (!~v.indexOf('stat')) code = code.replace('stat = {}, ', '');
  967. if (!~v.indexOf('stack')) code = code.replace('stack = [], ', '');
  968. // Turn `var out = [];out.push("foo"),` into `var out = ["foo"];`.
  969. code = code.replace(
  970. /out = \[\];out\.push\(("(?:[^"]|\\")+")\),/,
  971. 'out = [$1];');
  972. }
  973. // Terminfos `wyse350-vb`, and `wy350-w`
  974. // seem to have a few broken strings.
  975. if (str === '\u001b%?') {
  976. code = 'return "\\x1b";';
  977. }
  978. if (this.debug) {
  979. v = code
  980. .replace(/\x1b/g, '\\x1b')
  981. .replace(/\r/g, '\\r')
  982. .replace(/\n/g, '\\n');
  983. process.stdout.write(v + '\n');
  984. }
  985. try {
  986. if (this.options.stringify && code.indexOf('return ') === 0) {
  987. return new Function('', code)();
  988. }
  989. return this.printf || ~code.indexOf('sprintf(')
  990. ? new Function('sprintf, params', code).bind(null, sprintf)
  991. : new Function('params', code);
  992. } catch (e) {
  993. console.error('');
  994. console.error('Error on %s:', tkey);
  995. console.error(JSON.stringify(str));
  996. console.error('');
  997. console.error(code.replace(/(,|;)/g, '$1\n'));
  998. e.stack = e.stack.replace(/\x1b/g, '\\x1b');
  999. throw e;
  1000. }
  1001. };
  1002. // See: ~/ncurses/ncurses/tinfo/lib_tputs.c
  1003. Tput.prototype._print = function(code, print, done) {
  1004. var xon = !this.bools.needs_xon_xoff || this.bools.xon_xoff;
  1005. print = print || write;
  1006. done = done || noop;
  1007. if (!this.padding) {
  1008. print(code);
  1009. return done();
  1010. }
  1011. var parts = code.split(/(?=\$<[\d.]+[*\/]{0,2}>)/)
  1012. , i = 0;
  1013. (function next() {
  1014. if (i === parts.length) {
  1015. return done();
  1016. }
  1017. var part = parts[i++]
  1018. , padding = /^\$<([\d.]+)([*\/]{0,2})>/.exec(part)
  1019. , amount
  1020. , suffix;
  1021. // , affect;
  1022. if (!padding) {
  1023. print(part);
  1024. return next();
  1025. }
  1026. part = part.substring(padding[0].length);
  1027. amount = +padding[1];
  1028. suffix = padding[2];
  1029. // A `/' suffix indicates that the padding is mandatory and forces a
  1030. // delay of the given number of milliseconds even on devices for which xon
  1031. // is present to indicate flow control.
  1032. if (xon && !~suffix.indexOf('/')) {
  1033. print(part);
  1034. return next();
  1035. }
  1036. // A `*' indicates that the padding required is proportional to the number
  1037. // of lines affected by the operation, and the amount given is the
  1038. // per-affected-unit padding required. (In the case of insert character,
  1039. // the factor is still the number of lines affected.) Normally, padding is
  1040. // advisory if the device has the xon capability; it is used for cost
  1041. // computation but does not trigger delays.
  1042. if (~suffix.indexOf('*')) {
  1043. // XXX Disable this for now.
  1044. amount = amount;
  1045. // if (affect = /\x1b\[(\d+)[LM]/.exec(part)) {
  1046. // amount *= +affect[1];
  1047. // }
  1048. // The above is a huge workaround. In reality, we need to compile
  1049. // `_print` into the string functions and check the cap name and
  1050. // params.
  1051. // if (cap === 'insert_line' || cap === 'delete_line') {
  1052. // amount *= params[0];
  1053. // }
  1054. // if (cap === 'clear_screen') {
  1055. // amount *= process.stdout.rows;
  1056. // }
  1057. }
  1058. return setTimeout(function() {
  1059. print(part);
  1060. return next();
  1061. }, amount);
  1062. })();
  1063. };
  1064. // A small helper function if we want
  1065. // to easily output text with setTimeouts.
  1066. Tput.print = function() {
  1067. var fake = {
  1068. padding: true,
  1069. bools: { needs_xon_xoff: true, xon_xoff: false }
  1070. };
  1071. return Tput.prototype._print.apply(fake, arguments);
  1072. };
  1073. /**
  1074. * Termcap
  1075. */
  1076. Tput.cpaths = [
  1077. process.env.TERMCAP || '',
  1078. (process.env.TERMPATH || '').split(/[: ]/),
  1079. (process.env.HOME || '') + '/.termcap',
  1080. '/usr/share/misc/termcap',
  1081. '/etc/termcap'
  1082. ];
  1083. Tput.prototype.readTermcap = function(term) {
  1084. var self = this
  1085. , terms
  1086. , term_
  1087. , root
  1088. , paths;
  1089. term = term || this.terminal;
  1090. // Termcap has a bunch of terminals usually stored in one file/string,
  1091. // so we need to find the one containing our desired terminal.
  1092. if (~term.indexOf(path.sep) && (terms = this._tryCap(path.resolve(term)))) {
  1093. term_ = path.basename(term).split('.')[0];
  1094. if (terms[process.env.TERM]) {
  1095. term = process.env.TERM;
  1096. } else if (terms[term_]) {
  1097. term = term_;
  1098. } else {
  1099. term = Object.keys(terms)[0];
  1100. }
  1101. } else {
  1102. paths = Tput.cpaths.slice();
  1103. if (this.termcapFile) {
  1104. paths.unshift(this.termcapFile);
  1105. }
  1106. paths.push(Tput.termcap);
  1107. terms = this._tryCap(paths, term);
  1108. }
  1109. if (!terms) {
  1110. throw new Error('Cannot find termcap for: ' + term);
  1111. }
  1112. root = terms[term];
  1113. if (this.debug) {
  1114. this._termcap = terms;
  1115. }
  1116. (function tc(term) {
  1117. if (term && term.strings.tc) {
  1118. root.inherits = root.inherits || [];
  1119. root.inherits.push(term.strings.tc);
  1120. var names = terms[term.strings.tc]
  1121. ? terms[term.strings.tc].names
  1122. : [term.strings.tc];
  1123. self._debug('%s inherits from %s.',
  1124. term.names.join('/'), names.join('/'));
  1125. var inherit = tc(terms[term.strings.tc]);
  1126. if (inherit) {
  1127. ['bools', 'numbers', 'strings'].forEach(function(type) {
  1128. merge(term[type], inherit[type]);
  1129. });
  1130. }
  1131. }
  1132. return term;
  1133. })(root);
  1134. // Translate termcap names to terminfo-style names.
  1135. root = this.translateTermcap(root);
  1136. return root;
  1137. };
  1138. Tput.prototype._tryCap = function(file, term) {
  1139. if (!file) return;
  1140. var terms
  1141. , data
  1142. , i;
  1143. if (Array.isArray(file)) {
  1144. for (i = 0; i < file.length; i++) {
  1145. data = this._tryCap(file[i], term);
  1146. if (data) return data;
  1147. }
  1148. return;
  1149. }
  1150. // If the termcap string starts with `/`,
  1151. // ncurses considers it a filename.
  1152. data = file[0] === '/'
  1153. ? tryRead(file)
  1154. : file;
  1155. if (!data) return;
  1156. terms = this.parseTermcap(data, file);
  1157. if (term && !terms[term]) {
  1158. return;
  1159. }
  1160. return terms;
  1161. };
  1162. /**
  1163. * Termcap Parser
  1164. * http://en.wikipedia.org/wiki/Termcap
  1165. * http://www.gnu.org/software
  1166. * /termutils/manual/termcap-1.3/html_mono/termcap.html
  1167. * http://www.gnu.org/software
  1168. * /termutils/manual/termcap-1.3/html_mono/termcap.html#SEC17
  1169. * http://tldp.org/HOWTO/Text-Terminal-HOWTO.html#toc16
  1170. * man termcap
  1171. */
  1172. // Example:
  1173. // vt102|dec vt102:\
  1174. // :do=^J:co#80:li#24:cl=50\E[;H\E[2J:\
  1175. // :le=^H:bs:cm=5\E[%i%d;%dH:nd=2\E[C:up=2\E[A:\
  1176. // :ce=3\E[K:cd=50\E[J:so=2\E[7m:se=2\E[m:us=2\E[4m:ue=2\E[m:\
  1177. // :md=2\E[1m:mr=2\E[7m:mb=2\E[5m:me=2\E[m:is=\E[1;24r\E[24;1H:\
  1178. // :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:ks=\E[?1h\E=:ke=\E[?1l\E>:\
  1179. // :ku=\EOA:kd=\EOB:kr=\EOC:kl=\EOD:kb=^H:\
  1180. // :ho=\E[H:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:pt:sr=5\EM:vt#3:\
  1181. // :sc=\E7:rc=\E8:cs=\E[%i%d;%dr:vs=\E[?7l:ve=\E[?7h:\
  1182. // :mi:al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:
  1183. Tput.prototype.parseTermcap = function(data, file) {
  1184. var terms = {}
  1185. , parts
  1186. , term
  1187. , entries
  1188. , fields
  1189. , field
  1190. , names
  1191. , i
  1192. , j
  1193. , k;
  1194. // remove escaped newlines
  1195. data = data.replace(/\\\n[ \t]*/g, '');
  1196. // remove comments
  1197. data = data.replace(/^#[^\n]+/gm, '');
  1198. // split entries
  1199. entries = data.trim().split(/\n+/);
  1200. for (i = 0; i < entries.length; i++) {
  1201. fields = entries[i].split(/:+/);
  1202. for (j = 0; j < fields.length; j++) {
  1203. field = fields[j].trim();
  1204. if (!field) continue;
  1205. if (j === 0) {
  1206. names = field.split('|');
  1207. term = {
  1208. name: names[0],
  1209. names: names,
  1210. desc: names.pop(),
  1211. file: ~file.indexOf(path.sep)
  1212. ? path.resolve(file)
  1213. : file,
  1214. termcap: true
  1215. };
  1216. for (k = 0; k < names.length; k++) {
  1217. terms[names[k]] = term;
  1218. }
  1219. term.bools = {};
  1220. term.numbers = {};
  1221. term.strings = {};
  1222. continue;
  1223. }
  1224. if (~field.indexOf('=')) {
  1225. parts = field.split('=');
  1226. term.strings[parts[0]] = parts.slice(1).join('=');
  1227. } else if (~field.indexOf('#')) {
  1228. parts = field.split('#');
  1229. term.numbers[parts[0]] = +parts.slice(1).join('#');
  1230. } else {
  1231. term.bools[field] = true;
  1232. }
  1233. }
  1234. }
  1235. return terms;
  1236. };
  1237. /**
  1238. * Termcap Compiler
  1239. * man termcap
  1240. */
  1241. Tput.prototype.translateTermcap = function(info) {
  1242. var self = this
  1243. , out = {};
  1244. if (!info) return;
  1245. this._debug(info);
  1246. ['name', 'names', 'desc', 'file', 'termcap'].forEach(function(key) {
  1247. out[key] = info[key];
  1248. });
  1249. // Separate aliases for termcap
  1250. var map = (function() {
  1251. var out = {};
  1252. Object.keys(Tput.alias).forEach(function(key) {
  1253. var aliases = Tput.alias[key];
  1254. out[aliases.termcap] = key;
  1255. });
  1256. return out;
  1257. })();
  1258. // Translate termcap cap names to terminfo cap names.
  1259. // e.g. `up` -> `cursor_up`
  1260. ['bools', 'numbers', 'strings'].forEach(function(key) {
  1261. out[key] = {};
  1262. Object.keys(info[key]).forEach(function(cap) {
  1263. if (key === 'strings') {
  1264. info.strings[cap] = self._captoinfo(cap, info.strings[cap], 1);
  1265. }
  1266. if (map[cap]) {
  1267. out[key][map[cap]] = info[key][cap];
  1268. } else {
  1269. // NOTE: Possibly include all termcap names
  1270. // in a separate alias.js file. Some are
  1271. // missing from the terminfo alias.js file
  1272. // which is why we have to do this:
  1273. // See: $ man termcap
  1274. out[key][cap] = info[key][cap];
  1275. }
  1276. });
  1277. });
  1278. return out;
  1279. };
  1280. Tput.prototype.compileTermcap = function(term) {
  1281. return this.compile(this.readTermcap(term));
  1282. };
  1283. Tput.prototype.injectTermcap = function(term) {
  1284. return this.inject(this.compileTermcap(term));
  1285. };
  1286. /**
  1287. * _nc_captoinfo - ported to javascript directly from ncurses.
  1288. * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.
  1289. * See: ~/ncurses/ncurses/tinfo/captoinfo.c
  1290. *
  1291. * Convert a termcap string to terminfo format.
  1292. * 'cap' is the relevant terminfo capability index.
  1293. * 's' is the string value of the capability.
  1294. * 'parameterized' tells what type of translations to do:
  1295. * % translations if 1
  1296. * pad translations if >=0
  1297. */
  1298. Tput.prototype._captoinfo = function(cap, s, parameterized) {
  1299. var self = this;
  1300. var capstart;
  1301. if (parameterized == null) {
  1302. parameterized = 0;
  1303. }
  1304. var MAX_PUSHED = 16
  1305. , stack = [];
  1306. var stackptr = 0
  1307. , onstack = 0
  1308. , seenm = 0
  1309. , seenn = 0
  1310. , seenr = 0
  1311. , param = 1
  1312. , i = 0
  1313. , out = '';
  1314. function warn() {
  1315. var args = Array.prototype.slice.call(arguments);
  1316. args[0] = 'captoinfo: ' + (args[0] || '');
  1317. return self._debug.apply(self, args);
  1318. }
  1319. function isdigit(ch) {
  1320. return ch >= '0' && ch <= '9';
  1321. }
  1322. function isgraph(ch) {
  1323. return ch > ' ' && ch <= '~';
  1324. }
  1325. // convert a character to a terminfo push
  1326. function cvtchar(sp) {
  1327. var c = '\0'
  1328. , len;
  1329. var j = i;
  1330. switch (sp[j]) {
  1331. case '\\':
  1332. switch (sp[++j]) {
  1333. case '\'':
  1334. case '$':
  1335. case '\\':
  1336. case '%':
  1337. c = sp[j];
  1338. len = 2;
  1339. break;
  1340. case '\0':
  1341. c = '\\';
  1342. len = 1;
  1343. break;
  1344. case '0':
  1345. case '1':
  1346. case '2':
  1347. case '3':
  1348. len = 1;
  1349. while (isdigit(sp[j])) {
  1350. c = String.fromCharCode(8 * c.charCodeAt(0)
  1351. + (sp[j++].charCodeAt(0) - '0'.charCodeAt(0)));
  1352. len++;
  1353. }
  1354. break;
  1355. default:
  1356. c = sp[j];
  1357. len = 2;
  1358. break;
  1359. }
  1360. break;
  1361. case '^':
  1362. c = String.fromCharCode(sp[++j].charCodeAt(0) & 0x1f);
  1363. len = 2;
  1364. break;
  1365. default:
  1366. c = sp[j];
  1367. len = 1;
  1368. }
  1369. if (isgraph(c) && c !== ',' && c !== '\'' && c !== '\\' && c !== ':') {
  1370. out += '%\'';
  1371. out += c;
  1372. out += '\'';
  1373. } else {
  1374. out += '%{';
  1375. if (c.charCodeAt(0) > 99) {
  1376. out += String.fromCharCode(
  1377. (c.charCodeAt(0) / 100 | 0) + '0'.charCodeAt(0));
  1378. }
  1379. if (c.charCodeAt(0) > 9) {
  1380. out += String.fromCharCode(
  1381. (c.charCodeAt(0) / 10 | 0) % 10 + '0'.charCodeAt(0));
  1382. }
  1383. out += String.fromCharCode(
  1384. c.charCodeAt(0) % 10 + '0'.charCodeAt(0));
  1385. out += '}';
  1386. }
  1387. return len;
  1388. }
  1389. // push n copies of param on the terminfo stack if not already there
  1390. function getparm(parm, n) {
  1391. if (seenr) {
  1392. if (parm === 1) {
  1393. parm = 2;
  1394. } else if (parm === 2) {
  1395. parm = 1;
  1396. }
  1397. }
  1398. if (onstack === parm) {
  1399. if (n > 1) {
  1400. warn('string may not be optimal');
  1401. out += '%Pa';
  1402. while (n--) {
  1403. out += '%ga';
  1404. }
  1405. }
  1406. return;
  1407. }
  1408. if (onstack !== 0) {
  1409. push();
  1410. }
  1411. onstack = parm;
  1412. while (n--) {
  1413. out += '%p';
  1414. out += String.fromCharCode('0'.charCodeAt(0) + parm);
  1415. }
  1416. if (seenn && parm < 3) {
  1417. out += '%{96}%^';
  1418. }
  1419. if (seenm && parm < 3) {
  1420. out += '%{127}%^';
  1421. }
  1422. }
  1423. // push onstack on to the stack
  1424. function push() {
  1425. if (stackptr >= MAX_PUSHED) {
  1426. warn('string too complex to convert');
  1427. } else {
  1428. stack[stackptr++] = onstack;
  1429. }
  1430. }
  1431. // pop the top of the stack into onstack
  1432. function pop() {
  1433. if (stackptr === 0) {
  1434. if (onstack === 0) {
  1435. warn('I\'m confused');
  1436. } else {
  1437. onstack = 0;
  1438. }
  1439. } else {
  1440. onstack = stack[--stackptr];
  1441. }
  1442. param++;
  1443. }
  1444. function see03() {
  1445. getparm(param, 1);
  1446. out += '%3d';
  1447. pop();
  1448. }
  1449. function invalid() {
  1450. out += '%';
  1451. i--;
  1452. warn('unknown %% code %s (%#x) in %s',
  1453. JSON.stringify(s[i]), s[i].charCodeAt(0), cap);
  1454. }
  1455. // skip the initial padding (if we haven't been told not to)
  1456. capstart = null;
  1457. if (s == null) s = '';
  1458. if (parameterized >= 0 && isdigit(s[i])) {
  1459. for (capstart = i;; i++) {
  1460. if (!(isdigit(s[i]) || s[i] === '*' || s[i] === '.')) {
  1461. break;
  1462. }
  1463. }
  1464. }
  1465. while (s[i]) {
  1466. switch (s[i]) {
  1467. case '%':
  1468. i++;
  1469. if (parameterized < 1) {
  1470. out += '%';
  1471. break;
  1472. }
  1473. switch (s[i++]) {
  1474. case '%':
  1475. out += '%';
  1476. break;
  1477. case 'r':
  1478. if (seenr++ === 1) {
  1479. warn('saw %%r twice in %s', cap);
  1480. }
  1481. break;
  1482. case 'm':
  1483. if (seenm++ === 1) {
  1484. warn('saw %%m twice in %s', cap);
  1485. }
  1486. break;
  1487. case 'n':
  1488. if (seenn++ === 1) {
  1489. warn('saw %%n twice in %s', cap);
  1490. }
  1491. break;
  1492. case 'i':
  1493. out += '%i';
  1494. break;
  1495. case '6':
  1496. case 'B':
  1497. getparm(param, 1);
  1498. out += '%{10}%/%{16}%*';
  1499. getparm(param, 1);
  1500. out += '%{10}%m%+';
  1501. break;
  1502. case '8':
  1503. case 'D':
  1504. getparm(param, 2);
  1505. out += '%{2}%*%-';
  1506. break;
  1507. case '>':
  1508. getparm(param, 2);
  1509. // %?%{x}%>%t%{y}%+%;
  1510. out += '%?';
  1511. i += cvtchar(s);
  1512. out += '%>%t';
  1513. i += cvtchar(s);
  1514. out += '%+%;';
  1515. break;
  1516. case 'a':
  1517. if ((s[i] === '=' || s[i] === '+' || s[i] === '-'
  1518. || s[i] === '*' || s[i] === '/')
  1519. && (s[i + 1] === 'p' || s[i + 1] === 'c')
  1520. && s[i + 2] !== '\0' && s[i + 2]) {
  1521. var l;
  1522. l = 2;
  1523. if (s[i] !== '=') {
  1524. getparm(param, 1);
  1525. }
  1526. if (s[i + 1] === 'p') {
  1527. getparm(param + s[i + 2].charCodeAt(0) - '@'.charCodeAt(0), 1);
  1528. if (param !== onstack) {
  1529. pop();
  1530. param--;
  1531. }
  1532. l++;
  1533. } else {
  1534. i += 2, l += cvtchar(s), i -= 2;
  1535. }
  1536. switch (s[i]) {
  1537. case '+':
  1538. out += '%+';
  1539. break;
  1540. case '-':
  1541. out += '%-';
  1542. break;
  1543. case '*':
  1544. out += '%*';
  1545. break;
  1546. case '/':
  1547. out += '%/';
  1548. break;
  1549. case '=':
  1550. if (seenr) {
  1551. if (param === 1) {
  1552. onstack = 2;
  1553. } else if (param === 2) {
  1554. onstack = 1;
  1555. } else {
  1556. onstack = param;
  1557. }
  1558. } else {
  1559. onstack = param;
  1560. }
  1561. break;
  1562. }
  1563. i += l;
  1564. break;
  1565. }
  1566. getparm(param, 1);
  1567. i += cvtchar(s);
  1568. out += '%+';
  1569. break;
  1570. case '+':
  1571. getparm(param, 1);
  1572. i += cvtchar(s);
  1573. out += '%+%c';
  1574. pop();
  1575. break;
  1576. case 's':
  1577. // #ifdef WATERLOO
  1578. // i += cvtchar(s);
  1579. // getparm(param, 1);
  1580. // out += '%-';
  1581. // #else
  1582. getparm(param, 1);
  1583. out += '%s';
  1584. pop();
  1585. // #endif /* WATERLOO */
  1586. break;
  1587. case '-':
  1588. i += cvtchar(s);
  1589. getparm(param, 1);
  1590. out += '%-%c';
  1591. pop();
  1592. break;
  1593. case '.':
  1594. getparm(param, 1);
  1595. out += '%c';
  1596. pop();
  1597. break;
  1598. case '0': // not clear any of the historical termcaps did this
  1599. if (s[i] === '3') {
  1600. see03(); // goto
  1601. break;
  1602. } else if (s[i] !== '2') {
  1603. invalid(); // goto
  1604. break;
  1605. }
  1606. // FALLTHRU
  1607. case '2':
  1608. getparm(param, 1);
  1609. out += '%2d';
  1610. pop();
  1611. break;
  1612. case '3':
  1613. see03();
  1614. break;
  1615. case 'd':
  1616. getparm(param, 1);
  1617. out += '%d';
  1618. pop();
  1619. break;
  1620. case 'f':
  1621. param++;
  1622. break;
  1623. case 'b':
  1624. param--;
  1625. break;
  1626. case '\\':
  1627. out += '%\\';
  1628. break;
  1629. default:
  1630. invalid();
  1631. break;
  1632. }
  1633. break;
  1634. // #ifdef REVISIBILIZE
  1635. // case '\\':
  1636. // out += s[i++];
  1637. // out += s[i++];
  1638. // break;
  1639. // case '\n':
  1640. // out += '\\n';
  1641. // i++;
  1642. // break;
  1643. // case '\t':
  1644. // out += '\\t';
  1645. // i++;
  1646. // break;
  1647. // case '\r':
  1648. // out += '\\r';
  1649. // i++;
  1650. // break;
  1651. // case '\200':
  1652. // out += '\\0';
  1653. // i++;
  1654. // break;
  1655. // case '\f':
  1656. // out += '\\f';
  1657. // i++;
  1658. // break;
  1659. // case '\b':
  1660. // out += '\\b';
  1661. // i++;
  1662. // break;
  1663. // case ' ':
  1664. // out += '\\s';
  1665. // i++;
  1666. // break;
  1667. // case '^':
  1668. // out += '\\^';
  1669. // i++;
  1670. // break;
  1671. // case ':':
  1672. // out += '\\:';
  1673. // i++;
  1674. // break;
  1675. // case ',':
  1676. // out += '\\,';
  1677. // i++;
  1678. // break;
  1679. // default:
  1680. // if (s[i] === '\033') {
  1681. // out += '\\E';
  1682. // i++;
  1683. // } else if (s[i].charCodeAt(0) > 0 && s[i].charCodeAt(0) < 32) {
  1684. // out += '^';
  1685. // out += String.fromCharCode(s[i].charCodeAt(0) + '@'.charCodeAt(0));
  1686. // i++;
  1687. // } else if (s[i].charCodeAt(0) <= 0 || s[i].charCodeAt(0) >= 127) {
  1688. // out += '\\';
  1689. // out += String.fromCharCode(
  1690. // ((s[i].charCodeAt(0) & 0300) >> 6) + '0'.charCodeAt(0));
  1691. // out += String.fromCharCode(
  1692. // ((s[i].charCodeAt(0) & 0070) >> 3) + '0'.charCodeAt(0));
  1693. // out += String.fromCharCode(
  1694. // (s[i].charCodeAt(0) & 0007) + '0'.charCodeAt(0));
  1695. // i++;
  1696. // } else {
  1697. // out += s[i++];
  1698. // }
  1699. // break;
  1700. // #else
  1701. default:
  1702. out += s[i++];
  1703. break;
  1704. // #endif
  1705. }
  1706. }
  1707. // Now, if we stripped off some leading padding, add it at the end
  1708. // of the string as mandatory padding.
  1709. if (capstart != null) {
  1710. out += '$<';
  1711. for (i = capstart;; i++) {
  1712. if (isdigit(s[i]) || s[i] === '*' || s[i] === '.') {
  1713. out += s[i];
  1714. } else {
  1715. break;
  1716. }
  1717. }
  1718. out += '/>';
  1719. }
  1720. if (s !== out) {
  1721. warn('Translating %s from %s to %s.',
  1722. cap, JSON.stringify(s), JSON.stringify(out));
  1723. }
  1724. return out;
  1725. };
  1726. /**
  1727. * Compile All Terminfo
  1728. */
  1729. Tput.prototype.getAll = function() {
  1730. var dir = this._prefix()
  1731. , list = asort(fs.readdirSync(dir))
  1732. , infos = [];
  1733. list.forEach(function(letter) {
  1734. var terms = asort(fs.readdirSync(path.resolve(dir, letter)));
  1735. infos.push.apply(infos, terms);
  1736. });
  1737. function asort(obj) {
  1738. return obj.sort(function(a, b) {
  1739. a = a.toLowerCase().charCodeAt(0);
  1740. b = b.toLowerCase().charCodeAt(0);
  1741. return a - b;
  1742. });
  1743. }
  1744. return infos;
  1745. };
  1746. Tput.prototype.compileAll = function(start) {
  1747. var self = this
  1748. , all = {};
  1749. this.getAll().forEach(function(name) {
  1750. if (start && name !== start) {
  1751. return;
  1752. } else {
  1753. start = null;
  1754. }
  1755. all[name] = self.compileTerminfo(name);
  1756. });
  1757. return all;
  1758. };
  1759. /**
  1760. * Detect Features / Quirks
  1761. */
  1762. Tput.prototype.detectFeatures = function(info) {
  1763. var data = this.parseACS(info);
  1764. info.features = {
  1765. unicode: this.detectUnicode(info),
  1766. brokenACS: this.detectBrokenACS(info),
  1767. PCRomSet: this.detectPCRomSet(info),
  1768. magicCookie: this.detectMagicCookie(info),
  1769. padding: this.detectPadding(info),
  1770. setbuf: this.detectSetbuf(info),
  1771. acsc: data.acsc,
  1772. acscr: data.acscr
  1773. };
  1774. return info.features;
  1775. };
  1776. Tput.prototype.detectUnicode = function() {
  1777. if (this.options.forceUnicode != null) {
  1778. return this.options.forceUnicode;
  1779. }
  1780. var LANG = process.env.LANG
  1781. + ':' + process.env.LANGUAGE
  1782. + ':' + process.env.LC_ALL
  1783. + ':' + process.env.LC_CTYPE;
  1784. return /utf-?8/i.test(LANG) || (this.GetConsoleCP() === 65001);
  1785. };
  1786. // For some reason TERM=linux has smacs/rmacs, but it maps to `^[[11m`
  1787. // and it does not switch to the DEC SCLD character set. What the hell?
  1788. // xterm: \x1b(0, screen: \x0e, linux: \x1b[11m (doesn't work)
  1789. // `man console_codes` says:
  1790. // 11 select null mapping, set display control flag, reset tog‐
  1791. // gle meta flag (ECMA-48 says "first alternate font").
  1792. // See ncurses:
  1793. // ~/ncurses/ncurses/base/lib_set_term.c
  1794. // ~/ncurses/ncurses/tinfo/lib_acs.c
  1795. // ~/ncurses/ncurses/tinfo/tinfo_driver.c
  1796. // ~/ncurses/ncurses/tinfo/lib_setup.c
  1797. Tput.prototype.detectBrokenACS = function(info) {
  1798. // ncurses-compatible env variable.
  1799. if (process.env.NCURSES_NO_UTF8_ACS != null) {
  1800. return !!+process.env.NCURSES_NO_UTF8_ACS;
  1801. }
  1802. // If the terminal supports unicode, we don't need ACS.
  1803. if (info.numbers.U8 >= 0) {
  1804. return !!info.numbers.U8;
  1805. }
  1806. // The linux console is just broken for some reason.
  1807. // Apparently the Linux console does not support ACS,
  1808. // but it does support the PC ROM character set.
  1809. if (info.name === 'linux') {
  1810. return true;
  1811. }
  1812. // PC alternate charset
  1813. // if (acsc.indexOf('+\x10,\x11-\x18.\x190') === 0) {
  1814. if (this.detectPCRomSet(info)) {
  1815. return true;
  1816. }
  1817. // screen termcap is bugged?
  1818. if (this.termcap
  1819. && info.name.indexOf('screen') === 0
  1820. && process.env.TERMCAP
  1821. && ~process.env.TERMCAP.indexOf('screen')
  1822. && ~process.env.TERMCAP.indexOf('hhII00')) {
  1823. if (~info.strings.enter_alt_charset_mode.indexOf('\016')
  1824. || ~info.strings.enter_alt_charset_mode.indexOf('\017')
  1825. || ~info.strings.set_attributes.indexOf('\016')
  1826. || ~info.strings.set_attributes.indexOf('\017')) {
  1827. return true;
  1828. }
  1829. }
  1830. return false;
  1831. };
  1832. // If enter_pc_charset is the same as enter_alt_charset,
  1833. // the terminal does not support SCLD as ACS.
  1834. // See: ~/ncurses/ncurses/tinfo/lib_acs.c
  1835. Tput.prototype.detectPCRomSet = function(info) {
  1836. var s = info.strings;
  1837. if (s.enter_pc_charset_mode && s.enter_alt_charset_mode
  1838. && s.enter_pc_charset_mode === s.enter_alt_charset_mode
  1839. && s.exit_pc_charset_mode === s.exit_alt_charset_mode) {
  1840. return true;
  1841. }
  1842. return false;
  1843. };
  1844. Tput.prototype.detectMagicCookie = function() {
  1845. return process.env.NCURSES_NO_MAGIC_COOKIE == null;
  1846. };
  1847. Tput.prototype.detectPadding = function() {
  1848. return process.env.NCURSES_NO_PADDING == null;
  1849. };
  1850. Tput.prototype.detectSetbuf = function() {
  1851. return process.env.NCURSES_NO_SETBUF == null;
  1852. };
  1853. Tput.prototype.parseACS = function(info) {
  1854. var data = {};
  1855. data.acsc = {};
  1856. data.acscr = {};
  1857. // Possibly just return an empty object, as done here, instead of
  1858. // specifically saying ACS is "broken" above. This would be more
  1859. // accurate to ncurses logic. But it doesn't really matter.
  1860. if (this.detectPCRomSet(info)) {
  1861. return data;
  1862. }
  1863. // See: ~/ncurses/ncurses/tinfo/lib_acs.c: L208
  1864. Object.keys(Tput.acsc).forEach(function(ch) {
  1865. var acs_chars = info.strings.acs_chars || ''
  1866. , i = acs_chars.indexOf(ch)
  1867. , next = acs_chars[i + 1];
  1868. if (!next || i === -1 || !Tput.acsc[next]) {
  1869. return;
  1870. }
  1871. data.acsc[ch] = Tput.acsc[next];
  1872. data.acscr[Tput.acsc[next]] = ch;
  1873. });
  1874. return data;
  1875. };
  1876. Tput.prototype.GetConsoleCP = function() {
  1877. var ccp;
  1878. if (process.platform !== 'win32') {
  1879. return -1;
  1880. }
  1881. // Allow unicode on all windows consoles for now:
  1882. if (+process.env.NCURSES_UNICODE !== 0) {
  1883. return 65001;
  1884. }
  1885. // cp.execSync('chcp 65001', { stdio: 'ignore', timeout: 1500 });
  1886. try {
  1887. // Produces something like: 'Active code page: 437\n\n'
  1888. ccp = cp.execFileSync(process.env.WINDIR + '\\system32\\chcp.com', [], {
  1889. stdio: ['ignore', 'pipe', 'ignore'],
  1890. encoding: 'ascii',
  1891. timeout: 1500
  1892. });
  1893. // ccp = cp.execSync('chcp', {
  1894. // stdio: ['ignore', 'pipe', 'ignore'],
  1895. // encoding: 'ascii',
  1896. // timeout: 1500
  1897. // });
  1898. } catch (e) {
  1899. ;
  1900. }
  1901. ccp = /\d+/.exec(ccp);
  1902. if (!ccp) {
  1903. return -1;
  1904. }
  1905. ccp = +ccp[0];
  1906. return ccp;
  1907. };
  1908. /**
  1909. * Helpers
  1910. */
  1911. function noop() {
  1912. return '';
  1913. }
  1914. noop.unsupported = true;
  1915. function merge(a, b) {
  1916. Object.keys(b).forEach(function(key) {
  1917. a[key] = b[key];
  1918. });
  1919. return a;
  1920. }
  1921. function write(data) {
  1922. return process.stdout.write(data);
  1923. }
  1924. function tryRead(file) {
  1925. if (Array.isArray(file)) {
  1926. for (var i = 0; i < file.length; i++) {
  1927. var data = tryRead(file[i]);
  1928. if (data) return data;
  1929. }
  1930. return '';
  1931. }
  1932. if (!file) return '';
  1933. file = path.resolve.apply(path, arguments);
  1934. try {
  1935. return fs.readFileSync(file, 'utf8');
  1936. } catch (e) {
  1937. return '';
  1938. }
  1939. }
  1940. /**
  1941. * sprintf
  1942. * http://www.cplusplus.com/reference/cstdio/printf/
  1943. */
  1944. function sprintf(src) {
  1945. var params = Array.prototype.slice.call(arguments, 1)
  1946. , rule = /%([\-+# ]{1,4})?(\d+(?:\.\d+)?)?([doxXsc])/g
  1947. , i = 0;
  1948. return src.replace(rule, function(_, flag, width, type) {
  1949. var flags = (flag || '').split('')
  1950. , param = params[i] != null ? params[i] : ''
  1951. , initial = param
  1952. // , width = +width
  1953. , opt = {}
  1954. , pre = '';
  1955. i++;
  1956. switch (type) {
  1957. case 'd': // signed int
  1958. param = (+param).toString(10);
  1959. break;
  1960. case 'o': // unsigned octal
  1961. param = (+param).toString(8);
  1962. break;
  1963. case 'x': // unsigned hex int
  1964. param = (+param).toString(16);
  1965. break;
  1966. case 'X': // unsigned hex int uppercase
  1967. param = (+param).toString(16).toUppercase();
  1968. break;
  1969. case 's': // string
  1970. break;
  1971. case 'c': // char
  1972. param = isFinite(param)
  1973. ? String.fromCharCode(param || 0200)
  1974. : '';
  1975. break;
  1976. }
  1977. flags.forEach(function(flag) {
  1978. switch (flag) {
  1979. // left-justify by width
  1980. case '-':
  1981. opt.left = true;
  1982. break;
  1983. // always precede numbers with their signs
  1984. case '+':
  1985. opt.signs = true;
  1986. break;
  1987. // used with o, x, X - value is preceded with 0, 0x, or 0X respectively.
  1988. // used with a, A, e, E, f, F, g, G - forces written output to contain
  1989. // a decimal point even if no more digits follow
  1990. case '#':
  1991. opt.hexpoint = true;
  1992. break;
  1993. // if no sign is going to be written, black space in front of the value
  1994. case ' ':
  1995. opt.space = true;
  1996. break;
  1997. }
  1998. });
  1999. width = +width.split('.')[0];
  2000. // Should this be for opt.left too?
  2001. // Example: %2.2X - turns 0 into 00
  2002. if (width && !opt.left) {
  2003. param = param + '';
  2004. while (param.length < width) {
  2005. param = '0' + param;
  2006. }
  2007. }
  2008. if (opt.signs) {
  2009. if (+initial >= 0) {
  2010. pre += '+';
  2011. }
  2012. }
  2013. if (opt.space) {
  2014. if (!opt.signs && +initial >= 0) {
  2015. pre += ' ';
  2016. }
  2017. }
  2018. if (opt.hexpoint) {
  2019. switch (type) {
  2020. case 'o': // unsigned octal
  2021. pre += '0';
  2022. break;
  2023. case 'x': // unsigned hex int
  2024. pre += '0x';
  2025. break;
  2026. case 'X': // unsigned hex int uppercase
  2027. pre += '0X';
  2028. break;
  2029. }
  2030. }
  2031. if (opt.left) {
  2032. if (width > (pre.length + param.length)) {
  2033. width -= pre.length + param.length;
  2034. pre = Array(width + 1).join(' ') + pre;
  2035. }
  2036. }
  2037. return pre + param;
  2038. });
  2039. }
  2040. /**
  2041. * Aliases
  2042. */
  2043. Tput._alias = require('./alias');
  2044. Tput.alias = {};
  2045. ['bools', 'numbers', 'strings'].forEach(function(type) {
  2046. Object.keys(Tput._alias[type]).forEach(function(key) {
  2047. var aliases = Tput._alias[type][key];
  2048. Tput.alias[key] = [aliases[0]];
  2049. Tput.alias[key].terminfo = aliases[0];
  2050. Tput.alias[key].termcap = aliases[1];
  2051. });
  2052. });
  2053. // Bools
  2054. Tput.alias.no_esc_ctlc.push('beehive_glitch');
  2055. Tput.alias.dest_tabs_magic_smso.push('teleray_glitch');
  2056. // Numbers
  2057. Tput.alias.micro_col_size.push('micro_char_size');
  2058. /**
  2059. * Feature Checking
  2060. */
  2061. Tput.aliasMap = {};
  2062. Object.keys(Tput.alias).forEach(function(key) {
  2063. Tput.aliasMap[key] = key;
  2064. Tput.alias[key].forEach(function(k) {
  2065. Tput.aliasMap[k] = key;
  2066. });
  2067. });
  2068. Tput.prototype.has = function(name) {
  2069. name = Tput.aliasMap[name];
  2070. var val = this.all[name];
  2071. if (!name) return false;
  2072. if (typeof val === 'number') {
  2073. return val !== -1;
  2074. }
  2075. return !!val;
  2076. };
  2077. /**
  2078. * Fallback Termcap Entry
  2079. */
  2080. Tput.termcap = ''
  2081. + 'vt102|dec vt102:'
  2082. + ':do=^J:co#80:li#24:cl=50\\E[;H\\E[2J:'
  2083. + ':le=^H:bs:cm=5\\E[%i%d;%dH:nd=2\\E[C:up=2\\E[A:'
  2084. + ':ce=3\\E[K:cd=50\\E[J:so=2\\E[7m:se=2\\E[m:us=2\\E[4m:ue=2\\E[m:'
  2085. + ':md=2\\E[1m:mr=2\\E[7m:mb=2\\E[5m:me=2\\E[m:is=\\E[1;24r\\E[24;1H:'
  2086. + ':rs=\\E>\\E[?3l\\E[?4l\\E[?5l\\E[?7h\\E[?8h:ks=\\E[?1h\\E=:ke=\\E[?1l\\E>:'
  2087. + ':ku=\\EOA:kd=\\EOB:kr=\\EOC:kl=\\EOD:kb=^H:\\\n'
  2088. + ':ho=\\E[H:k1=\\EOP:k2=\\EOQ:k3=\\EOR:k4=\\EOS:pt:sr=5\\EM:vt#3:'
  2089. + ':sc=\\E7:rc=\\E8:cs=\\E[%i%d;%dr:vs=\\E[?7l:ve=\\E[?7h:'
  2090. + ':mi:al=\\E[L:dc=\\E[P:dl=\\E[M:ei=\\E[4l:im=\\E[4h:';
  2091. /**
  2092. * Terminfo Data
  2093. */
  2094. Tput.bools = [
  2095. 'auto_left_margin',
  2096. 'auto_right_margin',
  2097. 'no_esc_ctlc',
  2098. 'ceol_standout_glitch',
  2099. 'eat_newline_glitch',
  2100. 'erase_overstrike',
  2101. 'generic_type',
  2102. 'hard_copy',
  2103. 'has_meta_key',
  2104. 'has_status_line',
  2105. 'insert_null_glitch',
  2106. 'memory_above',
  2107. 'memory_below',
  2108. 'move_insert_mode',
  2109. 'move_standout_mode',
  2110. 'over_strike',
  2111. 'status_line_esc_ok',
  2112. 'dest_tabs_magic_smso',
  2113. 'tilde_glitch',
  2114. 'transparent_underline',
  2115. 'xon_xoff',
  2116. 'needs_xon_xoff',
  2117. 'prtr_silent',
  2118. 'hard_cursor',
  2119. 'non_rev_rmcup',
  2120. 'no_pad_char',
  2121. 'non_dest_scroll_region',
  2122. 'can_change',
  2123. 'back_color_erase',
  2124. 'hue_lightness_saturation',
  2125. 'col_addr_glitch',
  2126. 'cr_cancels_micro_mode',
  2127. 'has_print_wheel',
  2128. 'row_addr_glitch',
  2129. 'semi_auto_right_margin',
  2130. 'cpi_changes_res',
  2131. 'lpi_changes_res',
  2132. // #ifdef __INTERNAL_CAPS_VISIBLE
  2133. 'backspaces_with_bs',
  2134. 'crt_no_scrolling',
  2135. 'no_correctly_working_cr',
  2136. 'gnu_has_meta_key',
  2137. 'linefeed_is_newline',
  2138. 'has_hardware_tabs',
  2139. 'return_does_clr_eol'
  2140. ];
  2141. Tput.numbers = [
  2142. 'columns',
  2143. 'init_tabs',
  2144. 'lines',
  2145. 'lines_of_memory',
  2146. 'magic_cookie_glitch',
  2147. 'padding_baud_rate',
  2148. 'virtual_terminal',
  2149. 'width_status_line',
  2150. 'num_labels',
  2151. 'label_height',
  2152. 'label_width',
  2153. 'max_attributes',
  2154. 'maximum_windows',
  2155. 'max_colors',
  2156. 'max_pairs',
  2157. 'no_color_video',
  2158. 'buffer_capacity',
  2159. 'dot_vert_spacing',
  2160. 'dot_horz_spacing',
  2161. 'max_micro_address',
  2162. 'max_micro_jump',
  2163. 'micro_col_size',
  2164. 'micro_line_size',
  2165. 'number_of_pins',
  2166. 'output_res_char',
  2167. 'output_res_line',
  2168. 'output_res_horz_inch',
  2169. 'output_res_vert_inch',
  2170. 'print_rate',
  2171. 'wide_char_size',
  2172. 'buttons',
  2173. 'bit_image_entwining',
  2174. 'bit_image_type',
  2175. // #ifdef __INTERNAL_CAPS_VISIBLE
  2176. 'magic_cookie_glitch_ul',
  2177. 'carriage_return_delay',
  2178. 'new_line_delay',
  2179. 'backspace_delay',
  2180. 'horizontal_tab_delay',
  2181. 'number_of_function_keys'
  2182. ];
  2183. Tput.strings = [
  2184. 'back_tab',
  2185. 'bell',
  2186. 'carriage_return',
  2187. 'change_scroll_region',
  2188. 'clear_all_tabs',
  2189. 'clear_screen',
  2190. 'clr_eol',
  2191. 'clr_eos',
  2192. 'column_address',
  2193. 'command_character',
  2194. 'cursor_address',
  2195. 'cursor_down',
  2196. 'cursor_home',
  2197. 'cursor_invisible',
  2198. 'cursor_left',
  2199. 'cursor_mem_address',
  2200. 'cursor_normal',
  2201. 'cursor_right',
  2202. 'cursor_to_ll',
  2203. 'cursor_up',
  2204. 'cursor_visible',
  2205. 'delete_character',
  2206. 'delete_line',
  2207. 'dis_status_line',
  2208. 'down_half_line',
  2209. 'enter_alt_charset_mode',
  2210. 'enter_blink_mode',
  2211. 'enter_bold_mode',
  2212. 'enter_ca_mode',
  2213. 'enter_delete_mode',
  2214. 'enter_dim_mode',
  2215. 'enter_insert_mode',
  2216. 'enter_secure_mode',
  2217. 'enter_protected_mode',
  2218. 'enter_reverse_mode',
  2219. 'enter_standout_mode',
  2220. 'enter_underline_mode',
  2221. 'erase_chars',
  2222. 'exit_alt_charset_mode',
  2223. 'exit_attribute_mode',
  2224. 'exit_ca_mode',
  2225. 'exit_delete_mode',
  2226. 'exit_insert_mode',
  2227. 'exit_standout_mode',
  2228. 'exit_underline_mode',
  2229. 'flash_screen',
  2230. 'form_feed',
  2231. 'from_status_line',
  2232. 'init_1string',
  2233. 'init_2string',
  2234. 'init_3string',
  2235. 'init_file',
  2236. 'insert_character',
  2237. 'insert_line',
  2238. 'insert_padding',
  2239. 'key_backspace',
  2240. 'key_catab',
  2241. 'key_clear',
  2242. 'key_ctab',
  2243. 'key_dc',
  2244. 'key_dl',
  2245. 'key_down',
  2246. 'key_eic',
  2247. 'key_eol',
  2248. 'key_eos',
  2249. 'key_f0',
  2250. 'key_f1',
  2251. 'key_f10',
  2252. 'key_f2',
  2253. 'key_f3',
  2254. 'key_f4',
  2255. 'key_f5',
  2256. 'key_f6',
  2257. 'key_f7',
  2258. 'key_f8',
  2259. 'key_f9',
  2260. 'key_home',
  2261. 'key_ic',
  2262. 'key_il',
  2263. 'key_left',
  2264. 'key_ll',
  2265. 'key_npage',
  2266. 'key_ppage',
  2267. 'key_right',
  2268. 'key_sf',
  2269. 'key_sr',
  2270. 'key_stab',
  2271. 'key_up',
  2272. 'keypad_local',
  2273. 'keypad_xmit',
  2274. 'lab_f0',
  2275. 'lab_f1',
  2276. 'lab_f10',
  2277. 'lab_f2',
  2278. 'lab_f3',
  2279. 'lab_f4',
  2280. 'lab_f5',
  2281. 'lab_f6',
  2282. 'lab_f7',
  2283. 'lab_f8',
  2284. 'lab_f9',
  2285. 'meta_off',
  2286. 'meta_on',
  2287. 'newline',
  2288. 'pad_char',
  2289. 'parm_dch',
  2290. 'parm_delete_line',
  2291. 'parm_down_cursor',
  2292. 'parm_ich',
  2293. 'parm_index',
  2294. 'parm_insert_line',
  2295. 'parm_left_cursor',
  2296. 'parm_right_cursor',
  2297. 'parm_rindex',
  2298. 'parm_up_cursor',
  2299. 'pkey_key',
  2300. 'pkey_local',
  2301. 'pkey_xmit',
  2302. 'print_screen',
  2303. 'prtr_off',
  2304. 'prtr_on',
  2305. 'repeat_char',
  2306. 'reset_1string',
  2307. 'reset_2string',
  2308. 'reset_3string',
  2309. 'reset_file',
  2310. 'restore_cursor',
  2311. 'row_address',
  2312. 'save_cursor',
  2313. 'scroll_forward',
  2314. 'scroll_reverse',
  2315. 'set_attributes',
  2316. 'set_tab',
  2317. 'set_window',
  2318. 'tab',
  2319. 'to_status_line',
  2320. 'underline_char',
  2321. 'up_half_line',
  2322. 'init_prog',
  2323. 'key_a1',
  2324. 'key_a3',
  2325. 'key_b2',
  2326. 'key_c1',
  2327. 'key_c3',
  2328. 'prtr_non',
  2329. 'char_padding',
  2330. 'acs_chars',
  2331. 'plab_norm',
  2332. 'key_btab',
  2333. 'enter_xon_mode',
  2334. 'exit_xon_mode',
  2335. 'enter_am_mode',
  2336. 'exit_am_mode',
  2337. 'xon_character',
  2338. 'xoff_character',
  2339. 'ena_acs',
  2340. 'label_on',
  2341. 'label_off',
  2342. 'key_beg',
  2343. 'key_cancel',
  2344. 'key_close',
  2345. 'key_command',
  2346. 'key_copy',
  2347. 'key_create',
  2348. 'key_end',
  2349. 'key_enter',
  2350. 'key_exit',
  2351. 'key_find',
  2352. 'key_help',
  2353. 'key_mark',
  2354. 'key_message',
  2355. 'key_move',
  2356. 'key_next',
  2357. 'key_open',
  2358. 'key_options',
  2359. 'key_previous',
  2360. 'key_print',
  2361. 'key_redo',
  2362. 'key_reference',
  2363. 'key_refresh',
  2364. 'key_replace',
  2365. 'key_restart',
  2366. 'key_resume',
  2367. 'key_save',
  2368. 'key_suspend',
  2369. 'key_undo',
  2370. 'key_sbeg',
  2371. 'key_scancel',
  2372. 'key_scommand',
  2373. 'key_scopy',
  2374. 'key_screate',
  2375. 'key_sdc',
  2376. 'key_sdl',
  2377. 'key_select',
  2378. 'key_send',
  2379. 'key_seol',
  2380. 'key_sexit',
  2381. 'key_sfind',
  2382. 'key_shelp',
  2383. 'key_shome',
  2384. 'key_sic',
  2385. 'key_sleft',
  2386. 'key_smessage',
  2387. 'key_smove',
  2388. 'key_snext',
  2389. 'key_soptions',
  2390. 'key_sprevious',
  2391. 'key_sprint',
  2392. 'key_sredo',
  2393. 'key_sreplace',
  2394. 'key_sright',
  2395. 'key_srsume',
  2396. 'key_ssave',
  2397. 'key_ssuspend',
  2398. 'key_sundo',
  2399. 'req_for_input',
  2400. 'key_f11',
  2401. 'key_f12',
  2402. 'key_f13',
  2403. 'key_f14',
  2404. 'key_f15',
  2405. 'key_f16',
  2406. 'key_f17',
  2407. 'key_f18',
  2408. 'key_f19',
  2409. 'key_f20',
  2410. 'key_f21',
  2411. 'key_f22',
  2412. 'key_f23',
  2413. 'key_f24',
  2414. 'key_f25',
  2415. 'key_f26',
  2416. 'key_f27',
  2417. 'key_f28',
  2418. 'key_f29',
  2419. 'key_f30',
  2420. 'key_f31',
  2421. 'key_f32',
  2422. 'key_f33',
  2423. 'key_f34',
  2424. 'key_f35',
  2425. 'key_f36',
  2426. 'key_f37',
  2427. 'key_f38',
  2428. 'key_f39',
  2429. 'key_f40',
  2430. 'key_f41',
  2431. 'key_f42',
  2432. 'key_f43',
  2433. 'key_f44',
  2434. 'key_f45',
  2435. 'key_f46',
  2436. 'key_f47',
  2437. 'key_f48',
  2438. 'key_f49',
  2439. 'key_f50',
  2440. 'key_f51',
  2441. 'key_f52',
  2442. 'key_f53',
  2443. 'key_f54',
  2444. 'key_f55',
  2445. 'key_f56',
  2446. 'key_f57',
  2447. 'key_f58',
  2448. 'key_f59',
  2449. 'key_f60',
  2450. 'key_f61',
  2451. 'key_f62',
  2452. 'key_f63',
  2453. 'clr_bol',
  2454. 'clear_margins',
  2455. 'set_left_margin',
  2456. 'set_right_margin',
  2457. 'label_format',
  2458. 'set_clock',
  2459. 'display_clock',
  2460. 'remove_clock',
  2461. 'create_window',
  2462. 'goto_window',
  2463. 'hangup',
  2464. 'dial_phone',
  2465. 'quick_dial',
  2466. 'tone',
  2467. 'pulse',
  2468. 'flash_hook',
  2469. 'fixed_pause',
  2470. 'wait_tone',
  2471. 'user0',
  2472. 'user1',
  2473. 'user2',
  2474. 'user3',
  2475. 'user4',
  2476. 'user5',
  2477. 'user6',
  2478. 'user7',
  2479. 'user8',
  2480. 'user9',
  2481. 'orig_pair',
  2482. 'orig_colors',
  2483. 'initialize_color',
  2484. 'initialize_pair',
  2485. 'set_color_pair',
  2486. 'set_foreground',
  2487. 'set_background',
  2488. 'change_char_pitch',
  2489. 'change_line_pitch',
  2490. 'change_res_horz',
  2491. 'change_res_vert',
  2492. 'define_char',
  2493. 'enter_doublewide_mode',
  2494. 'enter_draft_quality',
  2495. 'enter_italics_mode',
  2496. 'enter_leftward_mode',
  2497. 'enter_micro_mode',
  2498. 'enter_near_letter_quality',
  2499. 'enter_normal_quality',
  2500. 'enter_shadow_mode',
  2501. 'enter_subscript_mode',
  2502. 'enter_superscript_mode',
  2503. 'enter_upward_mode',
  2504. 'exit_doublewide_mode',
  2505. 'exit_italics_mode',
  2506. 'exit_leftward_mode',
  2507. 'exit_micro_mode',
  2508. 'exit_shadow_mode',
  2509. 'exit_subscript_mode',
  2510. 'exit_superscript_mode',
  2511. 'exit_upward_mode',
  2512. 'micro_column_address',
  2513. 'micro_down',
  2514. 'micro_left',
  2515. 'micro_right',
  2516. 'micro_row_address',
  2517. 'micro_up',
  2518. 'order_of_pins',
  2519. 'parm_down_micro',
  2520. 'parm_left_micro',
  2521. 'parm_right_micro',
  2522. 'parm_up_micro',
  2523. 'select_char_set',
  2524. 'set_bottom_margin',
  2525. 'set_bottom_margin_parm',
  2526. 'set_left_margin_parm',
  2527. 'set_right_margin_parm',
  2528. 'set_top_margin',
  2529. 'set_top_margin_parm',
  2530. 'start_bit_image',
  2531. 'start_char_set_def',
  2532. 'stop_bit_image',
  2533. 'stop_char_set_def',
  2534. 'subscript_characters',
  2535. 'superscript_characters',
  2536. 'these_cause_cr',
  2537. 'zero_motion',
  2538. 'char_set_names',
  2539. 'key_mouse',
  2540. 'mouse_info',
  2541. 'req_mouse_pos',
  2542. 'get_mouse',
  2543. 'set_a_foreground',
  2544. 'set_a_background',
  2545. 'pkey_plab',
  2546. 'device_type',
  2547. 'code_set_init',
  2548. 'set0_des_seq',
  2549. 'set1_des_seq',
  2550. 'set2_des_seq',
  2551. 'set3_des_seq',
  2552. 'set_lr_margin',
  2553. 'set_tb_margin',
  2554. 'bit_image_repeat',
  2555. 'bit_image_newline',
  2556. 'bit_image_carriage_return',
  2557. 'color_names',
  2558. 'define_bit_image_region',
  2559. 'end_bit_image_region',
  2560. 'set_color_band',
  2561. 'set_page_length',
  2562. 'display_pc_char',
  2563. 'enter_pc_charset_mode',
  2564. 'exit_pc_charset_mode',
  2565. 'enter_scancode_mode',
  2566. 'exit_scancode_mode',
  2567. 'pc_term_options',
  2568. 'scancode_escape',
  2569. 'alt_scancode_esc',
  2570. 'enter_horizontal_hl_mode',
  2571. 'enter_left_hl_mode',
  2572. 'enter_low_hl_mode',
  2573. 'enter_right_hl_mode',
  2574. 'enter_top_hl_mode',
  2575. 'enter_vertical_hl_mode',
  2576. 'set_a_attributes',
  2577. 'set_pglen_inch',
  2578. // #ifdef __INTERNAL_CAPS_VISIBLE
  2579. 'termcap_init2',
  2580. 'termcap_reset',
  2581. 'linefeed_if_not_lf',
  2582. 'backspace_if_not_bs',
  2583. 'other_non_function_keys',
  2584. 'arrow_key_map',
  2585. 'acs_ulcorner',
  2586. 'acs_llcorner',
  2587. 'acs_urcorner',
  2588. 'acs_lrcorner',
  2589. 'acs_ltee',
  2590. 'acs_rtee',
  2591. 'acs_btee',
  2592. 'acs_ttee',
  2593. 'acs_hline',
  2594. 'acs_vline',
  2595. 'acs_plus',
  2596. 'memory_lock',
  2597. 'memory_unlock',
  2598. 'box_chars_1'
  2599. ];
  2600. // DEC Special Character and Line Drawing Set.
  2601. // Taken from tty.js.
  2602. Tput.acsc = { // (0
  2603. '`': '\u25c6', // '◆'
  2604. 'a': '\u2592', // '▒'
  2605. 'b': '\u0009', // '\t'
  2606. 'c': '\u000c', // '\f'
  2607. 'd': '\u000d', // '\r'
  2608. 'e': '\u000a', // '\n'
  2609. 'f': '\u00b0', // '°'
  2610. 'g': '\u00b1', // '±'
  2611. 'h': '\u2424', // '\u2424' (NL)
  2612. 'i': '\u000b', // '\v'
  2613. 'j': '\u2518', // '┘'
  2614. 'k': '\u2510', // '┐'
  2615. 'l': '\u250c', // '┌'
  2616. 'm': '\u2514', // '└'
  2617. 'n': '\u253c', // '┼'
  2618. 'o': '\u23ba', // '⎺'
  2619. 'p': '\u23bb', // '⎻'
  2620. 'q': '\u2500', // '─'
  2621. 'r': '\u23bc', // '⎼'
  2622. 's': '\u23bd', // '⎽'
  2623. 't': '\u251c', // '├'
  2624. 'u': '\u2524', // '┤'
  2625. 'v': '\u2534', // '┴'
  2626. 'w': '\u252c', // '┬'
  2627. 'x': '\u2502', // '│'
  2628. 'y': '\u2264', // '≤'
  2629. 'z': '\u2265', // '≥'
  2630. '{': '\u03c0', // 'π'
  2631. '|': '\u2260', // '≠'
  2632. '}': '\u00a3', // '£'
  2633. '~': '\u00b7' // '·'
  2634. };
  2635. // Convert ACS unicode characters to the
  2636. // most similar-looking ascii characters.
  2637. Tput.utoa = Tput.prototype.utoa = {
  2638. '\u25c6': '*', // '◆'
  2639. '\u2592': ' ', // '▒'
  2640. // '\u0009': '\t', // '\t'
  2641. // '\u000c': '\f', // '\f'
  2642. // '\u000d': '\r', // '\r'
  2643. // '\u000a': '\n', // '\n'
  2644. '\u00b0': '*', // '°'
  2645. '\u00b1': '+', // '±'
  2646. '\u2424': '\n', // '\u2424' (NL)
  2647. // '\u000b': '\v', // '\v'
  2648. '\u2518': '+', // '┘'
  2649. '\u2510': '+', // '┐'
  2650. '\u250c': '+', // '┌'
  2651. '\u2514': '+', // '└'
  2652. '\u253c': '+', // '┼'
  2653. '\u23ba': '-', // '⎺'
  2654. '\u23bb': '-', // '⎻'
  2655. '\u2500': '-', // '─'
  2656. '\u23bc': '-', // '⎼'
  2657. '\u23bd': '_', // '⎽'
  2658. '\u251c': '+', // '├'
  2659. '\u2524': '+', // '┤'
  2660. '\u2534': '+', // '┴'
  2661. '\u252c': '+', // '┬'
  2662. '\u2502': '|', // '│'
  2663. '\u2264': '<', // '≤'
  2664. '\u2265': '>', // '≥'
  2665. '\u03c0': '?', // 'π'
  2666. '\u2260': '=', // '≠'
  2667. '\u00a3': '?', // '£'
  2668. '\u00b7': '*' // '·'
  2669. };
  2670. /**
  2671. * Expose
  2672. */
  2673. exports = Tput;
  2674. exports.sprintf = sprintf;
  2675. exports.tryRead = tryRead;
  2676. module.exports = exports;