@ohos-ports/pm2 0.0.0-stage → 7.1.2-beta.0

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