@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,2570 @@
1
+ /**
2
+ * element.js - base element 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 assert = require('assert');
12
+
13
+ var colors = require('../colors')
14
+ , unicode = require('../unicode');
15
+
16
+ var nextTick = global.setImmediate || process.nextTick.bind(process);
17
+
18
+ var helpers = require('../helpers');
19
+
20
+ var Node = require('./node');
21
+
22
+ /**
23
+ * Element
24
+ */
25
+
26
+ function Element(options) {
27
+ var self = this;
28
+
29
+ if (!(this instanceof Node)) {
30
+ return new Element(options);
31
+ }
32
+
33
+ options = options || {};
34
+
35
+ // Workaround to get a `scrollable` option.
36
+ if (options.scrollable && !this._ignore && this.type !== 'scrollable-box') {
37
+ var ScrollableBox = require('./scrollablebox');
38
+ Object.getOwnPropertyNames(ScrollableBox.prototype).forEach(function(key) {
39
+ if (key === 'type') return;
40
+ Object.defineProperty(this, key,
41
+ Object.getOwnPropertyDescriptor(ScrollableBox.prototype, key));
42
+ }, this);
43
+ this._ignore = true;
44
+ ScrollableBox.call(this, options);
45
+ delete this._ignore;
46
+ return this;
47
+ }
48
+
49
+ Node.call(this, options);
50
+
51
+ this.name = options.name;
52
+
53
+ options.position = options.position || {
54
+ left: options.left,
55
+ right: options.right,
56
+ top: options.top,
57
+ bottom: options.bottom,
58
+ width: options.width,
59
+ height: options.height
60
+ };
61
+
62
+ if (options.position.width === 'shrink'
63
+ || options.position.height === 'shrink') {
64
+ if (options.position.width === 'shrink') {
65
+ delete options.position.width;
66
+ }
67
+ if (options.position.height === 'shrink') {
68
+ delete options.position.height;
69
+ }
70
+ options.shrink = true;
71
+ }
72
+
73
+ this.position = options.position;
74
+
75
+ this.noOverflow = options.noOverflow;
76
+ this.dockBorders = options.dockBorders;
77
+ this.shadow = options.shadow;
78
+
79
+ this.style = options.style;
80
+
81
+ if (!this.style) {
82
+ this.style = {};
83
+ this.style.fg = options.fg;
84
+ this.style.bg = options.bg;
85
+ this.style.bold = options.bold;
86
+ this.style.underline = options.underline;
87
+ this.style.blink = options.blink;
88
+ this.style.inverse = options.inverse;
89
+ this.style.invisible = options.invisible;
90
+ this.style.transparent = options.transparent;
91
+ }
92
+
93
+ this.hidden = options.hidden || false;
94
+ this.fixed = options.fixed || false;
95
+ this.align = options.align || 'left';
96
+ this.valign = options.valign || 'top';
97
+ this.wrap = options.wrap !== false;
98
+ this.shrink = options.shrink;
99
+ this.fixed = options.fixed;
100
+ this.ch = options.ch || ' ';
101
+
102
+ if (typeof options.padding === 'number' || !options.padding) {
103
+ options.padding = {
104
+ left: options.padding,
105
+ top: options.padding,
106
+ right: options.padding,
107
+ bottom: options.padding
108
+ };
109
+ }
110
+
111
+ this.padding = {
112
+ left: options.padding.left || 0,
113
+ top: options.padding.top || 0,
114
+ right: options.padding.right || 0,
115
+ bottom: options.padding.bottom || 0
116
+ };
117
+
118
+ this.border = options.border;
119
+ if (this.border) {
120
+ if (typeof this.border === 'string') {
121
+ this.border = { type: this.border };
122
+ }
123
+ this.border.type = this.border.type || 'bg';
124
+ if (this.border.type === 'ascii') this.border.type = 'line';
125
+ this.border.ch = this.border.ch || ' ';
126
+ this.style.border = this.style.border || this.border.style;
127
+ if (!this.style.border) {
128
+ this.style.border = {};
129
+ this.style.border.fg = this.border.fg;
130
+ this.style.border.bg = this.border.bg;
131
+ }
132
+ //this.border.style = this.style.border;
133
+ if (this.border.left == null) this.border.left = true;
134
+ if (this.border.top == null) this.border.top = true;
135
+ if (this.border.right == null) this.border.right = true;
136
+ if (this.border.bottom == null) this.border.bottom = true;
137
+ }
138
+
139
+ // if (options.mouse || options.clickable) {
140
+ if (options.clickable) {
141
+ this.screen._listenMouse(this);
142
+ }
143
+
144
+ if (options.input || options.keyable) {
145
+ this.screen._listenKeys(this);
146
+ }
147
+
148
+ this.parseTags = options.parseTags || options.tags;
149
+
150
+ this.setContent(options.content || '', true);
151
+
152
+ if (options.label) {
153
+ this.setLabel(options.label);
154
+ }
155
+
156
+ if (options.hoverText) {
157
+ this.setHover(options.hoverText);
158
+ }
159
+
160
+ // TODO: Possibly move this to Node for onScreenEvent('mouse', ...).
161
+ this.on('newListener', function fn(type) {
162
+ // type = type.split(' ').slice(1).join(' ');
163
+ if (type === 'mouse'
164
+ || type === 'click'
165
+ || type === 'mouseover'
166
+ || type === 'mouseout'
167
+ || type === 'mousedown'
168
+ || type === 'mouseup'
169
+ || type === 'mousewheel'
170
+ || type === 'wheeldown'
171
+ || type === 'wheelup'
172
+ || type === 'mousemove') {
173
+ self.screen._listenMouse(self);
174
+ } else if (type === 'keypress' || type.indexOf('key ') === 0) {
175
+ self.screen._listenKeys(self);
176
+ }
177
+ });
178
+
179
+ this.on('resize', function() {
180
+ self.parseContent();
181
+ });
182
+
183
+ this.on('attach', function() {
184
+ self.parseContent();
185
+ });
186
+
187
+ this.on('detach', function() {
188
+ delete self.lpos;
189
+ });
190
+
191
+ if (options.hoverBg != null) {
192
+ options.hoverEffects = options.hoverEffects || {};
193
+ options.hoverEffects.bg = options.hoverBg;
194
+ }
195
+
196
+ if (this.style.hover) {
197
+ options.hoverEffects = this.style.hover;
198
+ }
199
+
200
+ if (this.style.focus) {
201
+ options.focusEffects = this.style.focus;
202
+ }
203
+
204
+ if (options.effects) {
205
+ if (options.effects.hover) options.hoverEffects = options.effects.hover;
206
+ if (options.effects.focus) options.focusEffects = options.effects.focus;
207
+ }
208
+
209
+ [['hoverEffects', 'mouseover', 'mouseout', '_htemp'],
210
+ ['focusEffects', 'focus', 'blur', '_ftemp']].forEach(function(props) {
211
+ var pname = props[0], over = props[1], out = props[2], temp = props[3];
212
+ self.screen.setEffects(self, self, over, out, self.options[pname], temp);
213
+ });
214
+
215
+ if (this.options.draggable) {
216
+ this.draggable = true;
217
+ }
218
+
219
+ if (options.focused) {
220
+ this.focus();
221
+ }
222
+ }
223
+
224
+ Element.prototype.__proto__ = Node.prototype;
225
+
226
+ Element.prototype.type = 'element';
227
+
228
+ Element.prototype.__defineGetter__('focused', function() {
229
+ return this.screen.focused === this;
230
+ });
231
+
232
+ Element.prototype.sattr = function(style, fg, bg) {
233
+ var bold = style.bold
234
+ , underline = style.underline
235
+ , blink = style.blink
236
+ , inverse = style.inverse
237
+ , invisible = style.invisible;
238
+
239
+ // if (arguments.length === 1) {
240
+ if (fg == null && bg == null) {
241
+ fg = style.fg;
242
+ bg = style.bg;
243
+ }
244
+
245
+ // This used to be a loop, but I decided
246
+ // to unroll it for performance's sake.
247
+ if (typeof bold === 'function') bold = bold(this);
248
+ if (typeof underline === 'function') underline = underline(this);
249
+ if (typeof blink === 'function') blink = blink(this);
250
+ if (typeof inverse === 'function') inverse = inverse(this);
251
+ if (typeof invisible === 'function') invisible = invisible(this);
252
+
253
+ if (typeof fg === 'function') fg = fg(this);
254
+ if (typeof bg === 'function') bg = bg(this);
255
+
256
+ // return (this.uid << 24)
257
+ // | ((this.dockBorders ? 32 : 0) << 18)
258
+ return ((invisible ? 16 : 0) << 18)
259
+ | ((inverse ? 8 : 0) << 18)
260
+ | ((blink ? 4 : 0) << 18)
261
+ | ((underline ? 2 : 0) << 18)
262
+ | ((bold ? 1 : 0) << 18)
263
+ | (colors.convert(fg) << 9)
264
+ | colors.convert(bg);
265
+ };
266
+
267
+ Element.prototype.onScreenEvent = function(type, handler) {
268
+ var listeners = this._slisteners = this._slisteners || [];
269
+ listeners.push({ type: type, handler: handler });
270
+ this.screen.on(type, handler);
271
+ };
272
+
273
+ Element.prototype.onceScreenEvent = function(type, handler) {
274
+ var listeners = this._slisteners = this._slisteners || [];
275
+ var entry = { type: type, handler: handler };
276
+ listeners.push(entry);
277
+ this.screen.once(type, function() {
278
+ var i = listeners.indexOf(entry);
279
+ if (~i) listeners.splice(i, 1);
280
+ return handler.apply(this, arguments);
281
+ });
282
+ };
283
+
284
+ Element.prototype.removeScreenEvent = function(type, handler) {
285
+ var listeners = this._slisteners = this._slisteners || [];
286
+ for (var i = 0; i < listeners.length; i++) {
287
+ var listener = listeners[i];
288
+ if (listener.type === type && listener.handler === handler) {
289
+ listeners.splice(i, 1);
290
+ if (this._slisteners.length === 0) {
291
+ delete this._slisteners;
292
+ }
293
+ break;
294
+ }
295
+ }
296
+ this.screen.removeListener(type, handler);
297
+ };
298
+
299
+ Element.prototype.free = function() {
300
+ var listeners = this._slisteners = this._slisteners || [];
301
+ for (var i = 0; i < listeners.length; i++) {
302
+ var listener = listeners[i];
303
+ this.screen.removeListener(listener.type, listener.handler);
304
+ }
305
+ delete this._slisteners;
306
+ };
307
+
308
+ Element.prototype.hide = function() {
309
+ if (this.hidden) return;
310
+ this.clearPos();
311
+ this.hidden = true;
312
+ this.emit('hide');
313
+ if (this.screen.focused === this) {
314
+ this.screen.rewindFocus();
315
+ }
316
+ };
317
+
318
+ Element.prototype.show = function() {
319
+ if (!this.hidden) return;
320
+ this.hidden = false;
321
+ this.emit('show');
322
+ };
323
+
324
+ Element.prototype.toggle = function() {
325
+ return this.hidden ? this.show() : this.hide();
326
+ };
327
+
328
+ Element.prototype.focus = function() {
329
+ return this.screen.focused = this;
330
+ };
331
+
332
+ Element.prototype.setContent = function(content, noClear, noTags) {
333
+ if (!noClear) this.clearPos();
334
+ this.content = content || '';
335
+ this.parseContent(noTags);
336
+ this.emit('set content');
337
+ };
338
+
339
+ Element.prototype.getContent = function() {
340
+ if (!this._clines) return '';
341
+ return this._clines.fake.join('\n');
342
+ };
343
+
344
+ Element.prototype.setText = function(content, noClear) {
345
+ content = content || '';
346
+ content = content.replace(/\x1b\[[\d;]*m/g, '');
347
+ return this.setContent(content, noClear, true);
348
+ };
349
+
350
+ Element.prototype.getText = function() {
351
+ return this.getContent().replace(/\x1b\[[\d;]*m/g, '');
352
+ };
353
+
354
+ Element.prototype.parseContent = function(noTags) {
355
+ if (this.detached) return false;
356
+
357
+ var width = this.width - this.iwidth;
358
+ if (this._clines == null
359
+ || this._clines.width !== width
360
+ || this._clines.content !== this.content) {
361
+ var content = this.content;
362
+
363
+ content = content
364
+ .replace(/[\x00-\x08\x0b-\x0c\x0e-\x1a\x1c-\x1f\x7f]/g, '')
365
+ .replace(/\x1b(?!\[[\d;]*m)/g, '')
366
+ .replace(/\r\n|\r/g, '\n')
367
+ .replace(/\t/g, this.screen.tabc);
368
+
369
+ if (this.screen.fullUnicode) {
370
+ // double-width chars will eat the next char after render. create a
371
+ // blank character after it so it doesn't eat the real next char.
372
+ content = content.replace(unicode.chars.all, '$1\x03');
373
+ // iTerm2 cannot render combining characters properly.
374
+ if (this.screen.program.isiTerm2) {
375
+ content = content.replace(unicode.chars.combining, '');
376
+ }
377
+ } else {
378
+ // no double-width: replace them with question-marks.
379
+ content = content.replace(unicode.chars.all, '??');
380
+ // delete combining characters since they're 0-width anyway.
381
+ // NOTE: We could drop this, the non-surrogates would get changed to ? by
382
+ // the unicode filter, and surrogates changed to ? by the surrogate
383
+ // regex. however, the user might expect them to be 0-width.
384
+ // NOTE: Might be better for performance to drop!
385
+ content = content.replace(unicode.chars.combining, '');
386
+ // no surrogate pairs: replace them with question-marks.
387
+ content = content.replace(unicode.chars.surrogate, '?');
388
+ // XXX Deduplicate code here:
389
+ // content = helpers.dropUnicode(content);
390
+ }
391
+
392
+ if (!noTags) {
393
+ content = this._parseTags(content);
394
+ }
395
+
396
+ this._clines = this._wrapContent(content, width);
397
+ this._clines.width = width;
398
+ this._clines.content = this.content;
399
+ this._clines.attr = this._parseAttr(this._clines);
400
+ this._clines.ci = [];
401
+ this._clines.reduce(function(total, line) {
402
+ this._clines.ci.push(total);
403
+ return total + line.length + 1;
404
+ }.bind(this), 0);
405
+
406
+ this._pcontent = this._clines.join('\n');
407
+ this.emit('parsed content');
408
+
409
+ return true;
410
+ }
411
+
412
+ // Need to calculate this every time because the default fg/bg may change.
413
+ this._clines.attr = this._parseAttr(this._clines) || this._clines.attr;
414
+
415
+ return false;
416
+ };
417
+
418
+ // Convert `{red-fg}foo{/red-fg}` to `\x1b[31mfoo\x1b[39m`.
419
+ Element.prototype._parseTags = function(text) {
420
+ if (!this.parseTags) return text;
421
+ if (!/{\/?[\w\-,;!#]*}/.test(text)) return text;
422
+
423
+ var program = this.screen.program
424
+ , out = ''
425
+ , state
426
+ , bg = []
427
+ , fg = []
428
+ , flag = []
429
+ , cap
430
+ , slash
431
+ , param
432
+ , attr
433
+ , esc;
434
+
435
+ for (;;) {
436
+ if (!esc && (cap = /^{escape}/.exec(text))) {
437
+ text = text.substring(cap[0].length);
438
+ esc = true;
439
+ continue;
440
+ }
441
+
442
+ if (esc && (cap = /^([\s\S]+?){\/escape}/.exec(text))) {
443
+ text = text.substring(cap[0].length);
444
+ out += cap[1];
445
+ esc = false;
446
+ continue;
447
+ }
448
+
449
+ if (esc) {
450
+ // throw new Error('Unterminated escape tag.');
451
+ out += text;
452
+ break;
453
+ }
454
+
455
+ if (cap = /^{(\/?)([\w\-,;!#]*)}/.exec(text)) {
456
+ text = text.substring(cap[0].length);
457
+ slash = cap[1] === '/';
458
+ param = cap[2].replace(/-/g, ' ');
459
+
460
+ if (param === 'open') {
461
+ out += '{';
462
+ continue;
463
+ } else if (param === 'close') {
464
+ out += '}';
465
+ continue;
466
+ }
467
+
468
+ if (param.slice(-3) === ' bg') state = bg;
469
+ else if (param.slice(-3) === ' fg') state = fg;
470
+ else state = flag;
471
+
472
+ if (slash) {
473
+ if (!param) {
474
+ out += program._attr('normal');
475
+ bg.length = 0;
476
+ fg.length = 0;
477
+ flag.length = 0;
478
+ } else {
479
+ attr = program._attr(param, false);
480
+ if (attr == null) {
481
+ out += cap[0];
482
+ } else {
483
+ // if (param !== state[state.length - 1]) {
484
+ // throw new Error('Misnested tags.');
485
+ // }
486
+ state.pop();
487
+ if (state.length) {
488
+ out += program._attr(state[state.length - 1]);
489
+ } else {
490
+ out += attr;
491
+ }
492
+ }
493
+ }
494
+ } else {
495
+ if (!param) {
496
+ out += cap[0];
497
+ } else {
498
+ attr = program._attr(param);
499
+ if (attr == null) {
500
+ out += cap[0];
501
+ } else {
502
+ state.push(param);
503
+ out += attr;
504
+ }
505
+ }
506
+ }
507
+
508
+ continue;
509
+ }
510
+
511
+ if (cap = /^[\s\S]+?(?={\/?[\w\-,;!#]*})/.exec(text)) {
512
+ text = text.substring(cap[0].length);
513
+ out += cap[0];
514
+ continue;
515
+ }
516
+
517
+ out += text;
518
+ break;
519
+ }
520
+
521
+ return out;
522
+ };
523
+
524
+ Element.prototype._parseAttr = function(lines) {
525
+ var dattr = this.sattr(this.style)
526
+ , attr = dattr
527
+ , attrs = []
528
+ , line
529
+ , i
530
+ , j
531
+ , c;
532
+
533
+ if (lines[0].attr === attr) {
534
+ return;
535
+ }
536
+
537
+ for (j = 0; j < lines.length; j++) {
538
+ line = lines[j];
539
+ attrs[j] = attr;
540
+ for (i = 0; i < line.length; i++) {
541
+ if (line[i] === '\x1b') {
542
+ if (c = /^\x1b\[[\d;]*m/.exec(line.substring(i))) {
543
+ attr = this.screen.attrCode(c[0], attr, dattr);
544
+ i += c[0].length - 1;
545
+ }
546
+ }
547
+ }
548
+ }
549
+
550
+ return attrs;
551
+ };
552
+
553
+ Element.prototype._align = function(line, width, align) {
554
+ if (!align) return line;
555
+ //if (!align && !~line.indexOf('{|}')) return line;
556
+
557
+ var cline = line.replace(/\x1b\[[\d;]*m/g, '')
558
+ , len = cline.length
559
+ , s = width - len;
560
+
561
+ if (this.shrink) {
562
+ s = 0;
563
+ }
564
+
565
+ if (len === 0) return line;
566
+ if (s < 0) return line;
567
+
568
+ if (align === 'center') {
569
+ s = Array(((s / 2) | 0) + 1).join(' ');
570
+ return s + line + s;
571
+ } else if (align === 'right') {
572
+ s = Array(s + 1).join(' ');
573
+ return s + line;
574
+ } else if (this.parseTags && ~line.indexOf('{|}')) {
575
+ var parts = line.split('{|}');
576
+ var cparts = cline.split('{|}');
577
+ s = Math.max(width - cparts[0].length - cparts[1].length, 0);
578
+ s = Array(s + 1).join(' ');
579
+ return parts[0] + s + parts[1];
580
+ }
581
+
582
+ return line;
583
+ };
584
+
585
+ Element.prototype._wrapContent = function(content, width) {
586
+ var tags = this.parseTags
587
+ , state = this.align
588
+ , wrap = this.wrap
589
+ , margin = 0
590
+ , rtof = []
591
+ , ftor = []
592
+ , out = []
593
+ , no = 0
594
+ , line
595
+ , align
596
+ , cap
597
+ , total
598
+ , i
599
+ , part
600
+ , j
601
+ , lines
602
+ , rest;
603
+
604
+ lines = content.split('\n');
605
+
606
+ if (!content) {
607
+ out.push(content);
608
+ out.rtof = [0];
609
+ out.ftor = [[0]];
610
+ out.fake = lines;
611
+ out.real = out;
612
+ out.mwidth = 0;
613
+ return out;
614
+ }
615
+
616
+ if (this.scrollbar) margin++;
617
+ if (this.type === 'textarea') margin++;
618
+ if (width > margin) width -= margin;
619
+
620
+ main:
621
+ for (; no < lines.length; no++) {
622
+ line = lines[no];
623
+ align = state;
624
+
625
+ ftor.push([]);
626
+
627
+ // Handle alignment tags.
628
+ if (tags) {
629
+ if (cap = /^{(left|center|right)}/.exec(line)) {
630
+ line = line.substring(cap[0].length);
631
+ align = state = cap[1] !== 'left'
632
+ ? cap[1]
633
+ : null;
634
+ }
635
+ if (cap = /{\/(left|center|right)}$/.exec(line)) {
636
+ line = line.slice(0, -cap[0].length);
637
+ //state = null;
638
+ state = this.align;
639
+ }
640
+ }
641
+
642
+ // If the string is apparently too long, wrap it.
643
+ while (line.length > width) {
644
+ // Measure the real width of the string.
645
+ for (i = 0, total = 0; i < line.length; i++) {
646
+ while (line[i] === '\x1b') {
647
+ while (line[i] && line[i++] !== 'm');
648
+ }
649
+ if (!line[i]) break;
650
+ if (++total === width) {
651
+ // If we're not wrapping the text, we have to finish up the rest of
652
+ // the control sequences before cutting off the line.
653
+ i++;
654
+ if (!wrap) {
655
+ rest = line.substring(i).match(/\x1b\[[^m]*m/g);
656
+ rest = rest ? rest.join('') : '';
657
+ out.push(this._align(line.substring(0, i) + rest, width, align));
658
+ ftor[no].push(out.length - 1);
659
+ rtof.push(no);
660
+ continue main;
661
+ }
662
+ if (!this.screen.fullUnicode) {
663
+ // Try to find a space to break on.
664
+ if (i !== line.length) {
665
+ j = i;
666
+ while (j > i - 10 && j > 0 && line[--j] !== ' ');
667
+ if (line[j] === ' ') i = j + 1;
668
+ }
669
+ } else {
670
+ // Try to find a character to break on.
671
+ if (i !== line.length) {
672
+ // <XXX>
673
+ // Compensate for surrogate length
674
+ // counts on wrapping (experimental):
675
+ // NOTE: Could optimize this by putting
676
+ // it in the parent for loop.
677
+ if (unicode.isSurrogate(line, i)) i--;
678
+ for (var s = 0, n = 0; n < i; n++) {
679
+ if (unicode.isSurrogate(line, n)) s++, n++;
680
+ }
681
+ i += s;
682
+ // </XXX>
683
+ j = i;
684
+ // Break _past_ space.
685
+ // Break _past_ double-width chars.
686
+ // Break _past_ surrogate pairs.
687
+ // Break _past_ combining chars.
688
+ while (j > i - 10 && j > 0) {
689
+ j--;
690
+ if (line[j] === ' '
691
+ || line[j] === '\x03'
692
+ || (unicode.isSurrogate(line, j - 1) && line[j + 1] !== '\x03')
693
+ || unicode.isCombining(line, j)) {
694
+ break;
695
+ }
696
+ }
697
+ if (line[j] === ' '
698
+ || line[j] === '\x03'
699
+ || (unicode.isSurrogate(line, j - 1) && line[j + 1] !== '\x03')
700
+ || unicode.isCombining(line, j)) {
701
+ i = j + 1;
702
+ }
703
+ }
704
+ }
705
+ break;
706
+ }
707
+ }
708
+
709
+ part = line.substring(0, i);
710
+ line = line.substring(i);
711
+
712
+ out.push(this._align(part, width, align));
713
+ ftor[no].push(out.length - 1);
714
+ rtof.push(no);
715
+
716
+ // Make sure we didn't wrap the line to the very end, otherwise
717
+ // we get a pointless empty line after a newline.
718
+ if (line === '') continue main;
719
+
720
+ // If only an escape code got cut off, at it to `part`.
721
+ if (/^(?:\x1b[\[\d;]*m)+$/.test(line)) {
722
+ out[out.length - 1] += line;
723
+ continue main;
724
+ }
725
+ }
726
+
727
+ out.push(this._align(line, width, align));
728
+ ftor[no].push(out.length - 1);
729
+ rtof.push(no);
730
+ }
731
+
732
+ out.rtof = rtof;
733
+ out.ftor = ftor;
734
+ out.fake = lines;
735
+ out.real = out;
736
+
737
+ out.mwidth = out.reduce(function(current, line) {
738
+ line = line.replace(/\x1b\[[\d;]*m/g, '');
739
+ return line.length > current
740
+ ? line.length
741
+ : current;
742
+ }, 0);
743
+
744
+ return out;
745
+ };
746
+
747
+ Element.prototype.__defineGetter__('visible', function() {
748
+ var el = this;
749
+ do {
750
+ if (el.detached) return false;
751
+ if (el.hidden) return false;
752
+ // if (!el.lpos) return false;
753
+ // if (el.position.width === 0 || el.position.height === 0) return false;
754
+ } while (el = el.parent);
755
+ return true;
756
+ });
757
+
758
+ Element.prototype.__defineGetter__('_detached', function() {
759
+ var el = this;
760
+ do {
761
+ if (el.type === 'screen') return false;
762
+ if (!el.parent) return true;
763
+ } while (el = el.parent);
764
+ return false;
765
+ });
766
+
767
+ Element.prototype.enableMouse = function() {
768
+ this.screen._listenMouse(this);
769
+ };
770
+
771
+ Element.prototype.enableKeys = function() {
772
+ this.screen._listenKeys(this);
773
+ };
774
+
775
+ Element.prototype.enableInput = function() {
776
+ this.screen._listenMouse(this);
777
+ this.screen._listenKeys(this);
778
+ };
779
+
780
+ Element.prototype.__defineGetter__('draggable', function() {
781
+ return this._draggable === true;
782
+ });
783
+
784
+ Element.prototype.__defineSetter__('draggable', function(draggable) {
785
+ return draggable ? this.enableDrag(draggable) : this.disableDrag();
786
+ });
787
+
788
+ Element.prototype.enableDrag = function(verify) {
789
+ var self = this;
790
+
791
+ if (this._draggable) return true;
792
+
793
+ if (typeof verify !== 'function') {
794
+ verify = function() { return true; };
795
+ }
796
+
797
+ this.enableMouse();
798
+
799
+ this.on('mousedown', this._dragMD = function(data) {
800
+ if (self.screen._dragging) return;
801
+ if (!verify(data)) return;
802
+ self.screen._dragging = self;
803
+ self._drag = {
804
+ x: data.x - self.aleft,
805
+ y: data.y - self.atop
806
+ };
807
+ self.setFront();
808
+ });
809
+
810
+ this.onScreenEvent('mouse', this._dragM = function(data) {
811
+ if (self.screen._dragging !== self) return;
812
+
813
+ if (data.action !== 'mousedown' && data.action !== 'mousemove') {
814
+ delete self.screen._dragging;
815
+ delete self._drag;
816
+ return;
817
+ }
818
+
819
+ // This can happen in edge cases where the user is
820
+ // already dragging and element when it is detached.
821
+ if (!self.parent) return;
822
+
823
+ var ox = self._drag.x
824
+ , oy = self._drag.y
825
+ , px = self.parent.aleft
826
+ , py = self.parent.atop
827
+ , x = data.x - px - ox
828
+ , y = data.y - py - oy;
829
+
830
+ if (self.position.right != null) {
831
+ if (self.position.left != null) {
832
+ self.width = '100%-' + (self.parent.width - self.width);
833
+ }
834
+ self.position.right = null;
835
+ }
836
+
837
+ if (self.position.bottom != null) {
838
+ if (self.position.top != null) {
839
+ self.height = '100%-' + (self.parent.height - self.height);
840
+ }
841
+ self.position.bottom = null;
842
+ }
843
+
844
+ self.rleft = x;
845
+ self.rtop = y;
846
+
847
+ self.screen.render();
848
+ });
849
+
850
+ return this._draggable = true;
851
+ };
852
+
853
+ Element.prototype.disableDrag = function() {
854
+ if (!this._draggable) return false;
855
+ delete this.screen._dragging;
856
+ delete this._drag;
857
+ this.removeListener('mousedown', this._dragMD);
858
+ this.removeScreenEvent('mouse', this._dragM);
859
+ return this._draggable = false;
860
+ };
861
+
862
+ Element.prototype.key = function() {
863
+ return this.screen.program.key.apply(this, arguments);
864
+ };
865
+
866
+ Element.prototype.onceKey = function() {
867
+ return this.screen.program.onceKey.apply(this, arguments);
868
+ };
869
+
870
+ Element.prototype.unkey =
871
+ Element.prototype.removeKey = function() {
872
+ return this.screen.program.unkey.apply(this, arguments);
873
+ };
874
+
875
+ Element.prototype.setIndex = function(index) {
876
+ if (!this.parent) return;
877
+
878
+ if (index < 0) {
879
+ index = this.parent.children.length + index;
880
+ }
881
+
882
+ index = Math.max(index, 0);
883
+ index = Math.min(index, this.parent.children.length - 1);
884
+
885
+ var i = this.parent.children.indexOf(this);
886
+ if (!~i) return;
887
+
888
+ var item = this.parent.children.splice(i, 1)[0];
889
+ this.parent.children.splice(index, 0, item);
890
+ };
891
+
892
+ Element.prototype.setFront = function() {
893
+ return this.setIndex(-1);
894
+ };
895
+
896
+ Element.prototype.setBack = function() {
897
+ return this.setIndex(0);
898
+ };
899
+
900
+ Element.prototype.clearPos = function(get, override) {
901
+ if (this.detached) return;
902
+ var lpos = this._getCoords(get);
903
+ if (!lpos) return;
904
+ this.screen.clearRegion(
905
+ lpos.xi, lpos.xl,
906
+ lpos.yi, lpos.yl,
907
+ override);
908
+ };
909
+
910
+ Element.prototype.setLabel = function(options) {
911
+ var self = this;
912
+ var Box = require('./box');
913
+
914
+ if (typeof options === 'string') {
915
+ options = { text: options };
916
+ }
917
+
918
+ if (this._label) {
919
+ this._label.setContent(options.text);
920
+ if (options.side !== 'right') {
921
+ this._label.rleft = 2 + (this.border ? -1 : 0);
922
+ this._label.position.right = undefined;
923
+ if (!this.screen.autoPadding) {
924
+ this._label.rleft = 2;
925
+ }
926
+ } else {
927
+ this._label.rright = 2 + (this.border ? -1 : 0);
928
+ this._label.position.left = undefined;
929
+ if (!this.screen.autoPadding) {
930
+ this._label.rright = 2;
931
+ }
932
+ }
933
+ return;
934
+ }
935
+
936
+ this._label = new Box({
937
+ screen: this.screen,
938
+ parent: this,
939
+ content: options.text,
940
+ top: -this.itop,
941
+ tags: this.parseTags,
942
+ shrink: true,
943
+ style: this.style.label
944
+ });
945
+
946
+ if (options.side !== 'right') {
947
+ this._label.rleft = 2 - this.ileft;
948
+ } else {
949
+ this._label.rright = 2 - this.iright;
950
+ }
951
+
952
+ this._label._isLabel = true;
953
+
954
+ if (!this.screen.autoPadding) {
955
+ if (options.side !== 'right') {
956
+ this._label.rleft = 2;
957
+ } else {
958
+ this._label.rright = 2;
959
+ }
960
+ this._label.rtop = 0;
961
+ }
962
+
963
+ var reposition = function() {
964
+ self._label.rtop = (self.childBase || 0) - self.itop;
965
+ if (!self.screen.autoPadding) {
966
+ self._label.rtop = (self.childBase || 0);
967
+ }
968
+ self.screen.render();
969
+ };
970
+
971
+ this.on('scroll', this._labelScroll = function() {
972
+ reposition();
973
+ });
974
+
975
+ this.on('resize', this._labelResize = function() {
976
+ nextTick(function() {
977
+ reposition();
978
+ });
979
+ });
980
+ };
981
+
982
+ Element.prototype.removeLabel = function() {
983
+ if (!this._label) return;
984
+ this.removeListener('scroll', this._labelScroll);
985
+ this.removeListener('resize', this._labelResize);
986
+ this._label.detach();
987
+ delete this._labelScroll;
988
+ delete this._labelResize;
989
+ delete this._label;
990
+ };
991
+
992
+ Element.prototype.setHover = function(options) {
993
+ if (typeof options === 'string') {
994
+ options = { text: options };
995
+ }
996
+
997
+ this._hoverOptions = options;
998
+ this.enableMouse();
999
+ this.screen._initHover();
1000
+ };
1001
+
1002
+ Element.prototype.removeHover = function() {
1003
+ delete this._hoverOptions;
1004
+ if (!this.screen._hoverText || this.screen._hoverText.detached) return;
1005
+ this.screen._hoverText.detach();
1006
+ this.screen.render();
1007
+ };
1008
+
1009
+ /**
1010
+ * Positioning
1011
+ */
1012
+
1013
+ // The below methods are a bit confusing: basically
1014
+ // whenever Box.render is called `lpos` gets set on
1015
+ // the element, an object containing the rendered
1016
+ // coordinates. Since these don't update if the
1017
+ // element is moved somehow, they're unreliable in
1018
+ // that situation. However, if we can guarantee that
1019
+ // lpos is good and up to date, it can be more
1020
+ // accurate than the calculated positions below.
1021
+ // In this case, if the element is being rendered,
1022
+ // it's guaranteed that the parent will have been
1023
+ // rendered first, in which case we can use the
1024
+ // parant's lpos instead of recalculating it's
1025
+ // position (since that might be wrong because
1026
+ // it doesn't handle content shrinkage).
1027
+
1028
+ Element.prototype._getPos = function() {
1029
+ var pos = this.lpos;
1030
+
1031
+ assert.ok(pos);
1032
+
1033
+ if (pos.aleft != null) return pos;
1034
+
1035
+ pos.aleft = pos.xi;
1036
+ pos.atop = pos.yi;
1037
+ pos.aright = this.screen.cols - pos.xl;
1038
+ pos.abottom = this.screen.rows - pos.yl;
1039
+ pos.width = pos.xl - pos.xi;
1040
+ pos.height = pos.yl - pos.yi;
1041
+
1042
+ return pos;
1043
+ };
1044
+
1045
+ /**
1046
+ * Position Getters
1047
+ */
1048
+
1049
+ Element.prototype._getWidth = function(get) {
1050
+ var parent = get ? this.parent._getPos() : this.parent
1051
+ , width = this.position.width
1052
+ , left
1053
+ , expr;
1054
+
1055
+ if (typeof width === 'string') {
1056
+ if (width === 'half') width = '50%';
1057
+ expr = width.split(/(?=\+|-)/);
1058
+ width = expr[0];
1059
+ width = +width.slice(0, -1) / 100;
1060
+ width = parent.width * width | 0;
1061
+ width += +(expr[1] || 0);
1062
+ return width;
1063
+ }
1064
+
1065
+ // This is for if the element is being streched or shrunken.
1066
+ // Although the width for shrunken elements is calculated
1067
+ // in the render function, it may be calculated based on
1068
+ // the content width, and the content width is initially
1069
+ // decided by the width the element, so it needs to be
1070
+ // calculated here.
1071
+ if (width == null) {
1072
+ left = this.position.left || 0;
1073
+ if (typeof left === 'string') {
1074
+ if (left === 'center') left = '50%';
1075
+ expr = left.split(/(?=\+|-)/);
1076
+ left = expr[0];
1077
+ left = +left.slice(0, -1) / 100;
1078
+ left = parent.width * left | 0;
1079
+ left += +(expr[1] || 0);
1080
+ }
1081
+ width = parent.width - (this.position.right || 0) - left;
1082
+ if (this.screen.autoPadding) {
1083
+ if ((this.position.left != null || this.position.right == null)
1084
+ && this.position.left !== 'center') {
1085
+ width -= this.parent.ileft;
1086
+ }
1087
+ width -= this.parent.iright;
1088
+ }
1089
+ }
1090
+
1091
+ return width;
1092
+ };
1093
+
1094
+ Element.prototype.__defineGetter__('width', function() {
1095
+ return this._getWidth(false);
1096
+ });
1097
+
1098
+ Element.prototype._getHeight = function(get) {
1099
+ var parent = get ? this.parent._getPos() : this.parent
1100
+ , height = this.position.height
1101
+ , top
1102
+ , expr;
1103
+
1104
+ if (typeof height === 'string') {
1105
+ if (height === 'half') height = '50%';
1106
+ expr = height.split(/(?=\+|-)/);
1107
+ height = expr[0];
1108
+ height = +height.slice(0, -1) / 100;
1109
+ height = parent.height * height | 0;
1110
+ height += +(expr[1] || 0);
1111
+ return height;
1112
+ }
1113
+
1114
+ // This is for if the element is being streched or shrunken.
1115
+ // Although the width for shrunken elements is calculated
1116
+ // in the render function, it may be calculated based on
1117
+ // the content width, and the content width is initially
1118
+ // decided by the width the element, so it needs to be
1119
+ // calculated here.
1120
+ if (height == null) {
1121
+ top = this.position.top || 0;
1122
+ if (typeof top === 'string') {
1123
+ if (top === 'center') top = '50%';
1124
+ expr = top.split(/(?=\+|-)/);
1125
+ top = expr[0];
1126
+ top = +top.slice(0, -1) / 100;
1127
+ top = parent.height * top | 0;
1128
+ top += +(expr[1] || 0);
1129
+ }
1130
+ height = parent.height - (this.position.bottom || 0) - top;
1131
+ if (this.screen.autoPadding) {
1132
+ if ((this.position.top != null
1133
+ || this.position.bottom == null)
1134
+ && this.position.top !== 'center') {
1135
+ height -= this.parent.itop;
1136
+ }
1137
+ height -= this.parent.ibottom;
1138
+ }
1139
+ }
1140
+
1141
+ return height;
1142
+ };
1143
+
1144
+ Element.prototype.__defineGetter__('height', function() {
1145
+ return this._getHeight(false);
1146
+ });
1147
+
1148
+ Element.prototype._getLeft = function(get) {
1149
+ var parent = get ? this.parent._getPos() : this.parent
1150
+ , left = this.position.left || 0
1151
+ , expr;
1152
+
1153
+ if (typeof left === 'string') {
1154
+ if (left === 'center') left = '50%';
1155
+ expr = left.split(/(?=\+|-)/);
1156
+ left = expr[0];
1157
+ left = +left.slice(0, -1) / 100;
1158
+ left = parent.width * left | 0;
1159
+ left += +(expr[1] || 0);
1160
+ if (this.position.left === 'center') {
1161
+ left -= this._getWidth(get) / 2 | 0;
1162
+ }
1163
+ }
1164
+
1165
+ if (this.position.left == null && this.position.right != null) {
1166
+ return this.screen.cols - this._getWidth(get) - this._getRight(get);
1167
+ }
1168
+
1169
+ if (this.screen.autoPadding) {
1170
+ if ((this.position.left != null
1171
+ || this.position.right == null)
1172
+ && this.position.left !== 'center') {
1173
+ left += this.parent.ileft;
1174
+ }
1175
+ }
1176
+
1177
+ return (parent.aleft || 0) + left;
1178
+ };
1179
+
1180
+ Element.prototype.__defineGetter__('aleft', function() {
1181
+ return this._getLeft(false);
1182
+ });
1183
+
1184
+ Element.prototype._getRight = function(get) {
1185
+ var parent = get ? this.parent._getPos() : this.parent
1186
+ , right;
1187
+
1188
+ if (this.position.right == null && this.position.left != null) {
1189
+ right = this.screen.cols - (this._getLeft(get) + this._getWidth(get));
1190
+ if (this.screen.autoPadding) {
1191
+ right += this.parent.iright;
1192
+ }
1193
+ return right;
1194
+ }
1195
+
1196
+ right = (parent.aright || 0) + (this.position.right || 0);
1197
+
1198
+ if (this.screen.autoPadding) {
1199
+ right += this.parent.iright;
1200
+ }
1201
+
1202
+ return right;
1203
+ };
1204
+
1205
+ Element.prototype.__defineGetter__('aright', function() {
1206
+ return this._getRight(false);
1207
+ });
1208
+
1209
+ Element.prototype._getTop = function(get) {
1210
+ var parent = get ? this.parent._getPos() : this.parent
1211
+ , top = this.position.top || 0
1212
+ , expr;
1213
+
1214
+ if (typeof top === 'string') {
1215
+ if (top === 'center') top = '50%';
1216
+ expr = top.split(/(?=\+|-)/);
1217
+ top = expr[0];
1218
+ top = +top.slice(0, -1) / 100;
1219
+ top = parent.height * top | 0;
1220
+ top += +(expr[1] || 0);
1221
+ if (this.position.top === 'center') {
1222
+ top -= this._getHeight(get) / 2 | 0;
1223
+ }
1224
+ }
1225
+
1226
+ if (this.position.top == null && this.position.bottom != null) {
1227
+ return this.screen.rows - this._getHeight(get) - this._getBottom(get);
1228
+ }
1229
+
1230
+ if (this.screen.autoPadding) {
1231
+ if ((this.position.top != null
1232
+ || this.position.bottom == null)
1233
+ && this.position.top !== 'center') {
1234
+ top += this.parent.itop;
1235
+ }
1236
+ }
1237
+
1238
+ return (parent.atop || 0) + top;
1239
+ };
1240
+
1241
+ Element.prototype.__defineGetter__('atop', function() {
1242
+ return this._getTop(false);
1243
+ });
1244
+
1245
+ Element.prototype._getBottom = function(get) {
1246
+ var parent = get ? this.parent._getPos() : this.parent
1247
+ , bottom;
1248
+
1249
+ if (this.position.bottom == null && this.position.top != null) {
1250
+ bottom = this.screen.rows - (this._getTop(get) + this._getHeight(get));
1251
+ if (this.screen.autoPadding) {
1252
+ bottom += this.parent.ibottom;
1253
+ }
1254
+ return bottom;
1255
+ }
1256
+
1257
+ bottom = (parent.abottom || 0) + (this.position.bottom || 0);
1258
+
1259
+ if (this.screen.autoPadding) {
1260
+ bottom += this.parent.ibottom;
1261
+ }
1262
+
1263
+ return bottom;
1264
+ };
1265
+
1266
+ Element.prototype.__defineGetter__('abottom', function() {
1267
+ return this._getBottom(false);
1268
+ });
1269
+
1270
+ Element.prototype.__defineGetter__('rleft', function() {
1271
+ return this.aleft - this.parent.aleft;
1272
+ });
1273
+
1274
+ Element.prototype.__defineGetter__('rright', function() {
1275
+ return this.aright - this.parent.aright;
1276
+ });
1277
+
1278
+ Element.prototype.__defineGetter__('rtop', function() {
1279
+ return this.atop - this.parent.atop;
1280
+ });
1281
+
1282
+ Element.prototype.__defineGetter__('rbottom', function() {
1283
+ return this.abottom - this.parent.abottom;
1284
+ });
1285
+
1286
+ /**
1287
+ * Position Setters
1288
+ */
1289
+
1290
+ // NOTE:
1291
+ // For aright, abottom, right, and bottom:
1292
+ // If position.bottom is null, we could simply set top instead.
1293
+ // But it wouldn't replicate bottom behavior appropriately if
1294
+ // the parent was resized, etc.
1295
+ Element.prototype.__defineSetter__('width', function(val) {
1296
+ if (this.position.width === val) return;
1297
+ if (/^\d+$/.test(val)) val = +val;
1298
+ this.emit('resize');
1299
+ this.clearPos();
1300
+ return this.position.width = val;
1301
+ });
1302
+
1303
+ Element.prototype.__defineSetter__('height', function(val) {
1304
+ if (this.position.height === val) return;
1305
+ if (/^\d+$/.test(val)) val = +val;
1306
+ this.emit('resize');
1307
+ this.clearPos();
1308
+ return this.position.height = val;
1309
+ });
1310
+
1311
+ Element.prototype.__defineSetter__('aleft', function(val) {
1312
+ var expr;
1313
+ if (typeof val === 'string') {
1314
+ if (val === 'center') {
1315
+ val = this.screen.width / 2 | 0;
1316
+ val -= this.width / 2 | 0;
1317
+ } else {
1318
+ expr = val.split(/(?=\+|-)/);
1319
+ val = expr[0];
1320
+ val = +val.slice(0, -1) / 100;
1321
+ val = this.screen.width * val | 0;
1322
+ val += +(expr[1] || 0);
1323
+ }
1324
+ }
1325
+ val -= this.parent.aleft;
1326
+ if (this.position.left === val) return;
1327
+ this.emit('move');
1328
+ this.clearPos();
1329
+ return this.position.left = val;
1330
+ });
1331
+
1332
+ Element.prototype.__defineSetter__('aright', function(val) {
1333
+ val -= this.parent.aright;
1334
+ if (this.position.right === val) return;
1335
+ this.emit('move');
1336
+ this.clearPos();
1337
+ return this.position.right = val;
1338
+ });
1339
+
1340
+ Element.prototype.__defineSetter__('atop', function(val) {
1341
+ var expr;
1342
+ if (typeof val === 'string') {
1343
+ if (val === 'center') {
1344
+ val = this.screen.height / 2 | 0;
1345
+ val -= this.height / 2 | 0;
1346
+ } else {
1347
+ expr = val.split(/(?=\+|-)/);
1348
+ val = expr[0];
1349
+ val = +val.slice(0, -1) / 100;
1350
+ val = this.screen.height * val | 0;
1351
+ val += +(expr[1] || 0);
1352
+ }
1353
+ }
1354
+ val -= this.parent.atop;
1355
+ if (this.position.top === val) return;
1356
+ this.emit('move');
1357
+ this.clearPos();
1358
+ return this.position.top = val;
1359
+ });
1360
+
1361
+ Element.prototype.__defineSetter__('abottom', function(val) {
1362
+ val -= this.parent.abottom;
1363
+ if (this.position.bottom === val) return;
1364
+ this.emit('move');
1365
+ this.clearPos();
1366
+ return this.position.bottom = val;
1367
+ });
1368
+
1369
+ Element.prototype.__defineSetter__('rleft', function(val) {
1370
+ if (this.position.left === val) return;
1371
+ if (/^\d+$/.test(val)) val = +val;
1372
+ this.emit('move');
1373
+ this.clearPos();
1374
+ return this.position.left = val;
1375
+ });
1376
+
1377
+ Element.prototype.__defineSetter__('rright', function(val) {
1378
+ if (this.position.right === val) return;
1379
+ this.emit('move');
1380
+ this.clearPos();
1381
+ return this.position.right = val;
1382
+ });
1383
+
1384
+ Element.prototype.__defineSetter__('rtop', function(val) {
1385
+ if (this.position.top === val) return;
1386
+ if (/^\d+$/.test(val)) val = +val;
1387
+ this.emit('move');
1388
+ this.clearPos();
1389
+ return this.position.top = val;
1390
+ });
1391
+
1392
+ Element.prototype.__defineSetter__('rbottom', function(val) {
1393
+ if (this.position.bottom === val) return;
1394
+ this.emit('move');
1395
+ this.clearPos();
1396
+ return this.position.bottom = val;
1397
+ });
1398
+
1399
+ Element.prototype.__defineGetter__('ileft', function() {
1400
+ return (this.border ? 1 : 0) + this.padding.left;
1401
+ // return (this.border && this.border.left ? 1 : 0) + this.padding.left;
1402
+ });
1403
+
1404
+ Element.prototype.__defineGetter__('itop', function() {
1405
+ return (this.border ? 1 : 0) + this.padding.top;
1406
+ // return (this.border && this.border.top ? 1 : 0) + this.padding.top;
1407
+ });
1408
+
1409
+ Element.prototype.__defineGetter__('iright', function() {
1410
+ return (this.border ? 1 : 0) + this.padding.right;
1411
+ // return (this.border && this.border.right ? 1 : 0) + this.padding.right;
1412
+ });
1413
+
1414
+ Element.prototype.__defineGetter__('ibottom', function() {
1415
+ return (this.border ? 1 : 0) + this.padding.bottom;
1416
+ // return (this.border && this.border.bottom ? 1 : 0) + this.padding.bottom;
1417
+ });
1418
+
1419
+ Element.prototype.__defineGetter__('iwidth', function() {
1420
+ // return (this.border
1421
+ // ? ((this.border.left ? 1 : 0) + (this.border.right ? 1 : 0)) : 0)
1422
+ // + this.padding.left + this.padding.right;
1423
+ return (this.border ? 2 : 0) + this.padding.left + this.padding.right;
1424
+ });
1425
+
1426
+ Element.prototype.__defineGetter__('iheight', function() {
1427
+ // return (this.border
1428
+ // ? ((this.border.top ? 1 : 0) + (this.border.bottom ? 1 : 0)) : 0)
1429
+ // + this.padding.top + this.padding.bottom;
1430
+ return (this.border ? 2 : 0) + this.padding.top + this.padding.bottom;
1431
+ });
1432
+
1433
+ Element.prototype.__defineGetter__('tpadding', function() {
1434
+ return this.padding.left + this.padding.top
1435
+ + this.padding.right + this.padding.bottom;
1436
+ });
1437
+
1438
+ /**
1439
+ * Relative coordinates as default properties
1440
+ */
1441
+
1442
+ Element.prototype.__defineGetter__('left', function() {
1443
+ return this.rleft;
1444
+ });
1445
+
1446
+ Element.prototype.__defineGetter__('right', function() {
1447
+ return this.rright;
1448
+ });
1449
+
1450
+ Element.prototype.__defineGetter__('top', function() {
1451
+ return this.rtop;
1452
+ });
1453
+
1454
+ Element.prototype.__defineGetter__('bottom', function() {
1455
+ return this.rbottom;
1456
+ });
1457
+
1458
+ Element.prototype.__defineSetter__('left', function(val) {
1459
+ return this.rleft = val;
1460
+ });
1461
+
1462
+ Element.prototype.__defineSetter__('right', function(val) {
1463
+ return this.rright = val;
1464
+ });
1465
+
1466
+ Element.prototype.__defineSetter__('top', function(val) {
1467
+ return this.rtop = val;
1468
+ });
1469
+
1470
+ Element.prototype.__defineSetter__('bottom', function(val) {
1471
+ return this.rbottom = val;
1472
+ });
1473
+
1474
+ /**
1475
+ * Rendering - here be dragons
1476
+ */
1477
+
1478
+ Element.prototype._getShrinkBox = function(xi, xl, yi, yl, get) {
1479
+ if (!this.children.length) {
1480
+ return { xi: xi, xl: xi + 1, yi: yi, yl: yi + 1 };
1481
+ }
1482
+
1483
+ var i, el, ret, mxi = xi, mxl = xi + 1, myi = yi, myl = yi + 1;
1484
+
1485
+ // This is a chicken and egg problem. We need to determine how the children
1486
+ // will render in order to determine how this element renders, but it in
1487
+ // order to figure out how the children will render, they need to know
1488
+ // exactly how their parent renders, so, we can give them what we have so
1489
+ // far.
1490
+ var _lpos;
1491
+ if (get) {
1492
+ _lpos = this.lpos;
1493
+ this.lpos = { xi: xi, xl: xl, yi: yi, yl: yl };
1494
+ //this.shrink = false;
1495
+ }
1496
+
1497
+ for (i = 0; i < this.children.length; i++) {
1498
+ el = this.children[i];
1499
+
1500
+ ret = el._getCoords(get);
1501
+
1502
+ // Or just (seemed to work, but probably not good):
1503
+ // ret = el.lpos || this.lpos;
1504
+
1505
+ if (!ret) continue;
1506
+
1507
+ // Since the parent element is shrunk, and the child elements think it's
1508
+ // going to take up as much space as possible, an element anchored to the
1509
+ // right or bottom will inadvertantly make the parent's shrunken size as
1510
+ // large as possible. So, we can just use the height and/or width the of
1511
+ // element.
1512
+ // if (get) {
1513
+ if (el.position.left == null && el.position.right != null) {
1514
+ ret.xl = xi + (ret.xl - ret.xi);
1515
+ ret.xi = xi;
1516
+ if (this.screen.autoPadding) {
1517
+ // Maybe just do this no matter what.
1518
+ ret.xl += this.ileft;
1519
+ ret.xi += this.ileft;
1520
+ }
1521
+ }
1522
+ if (el.position.top == null && el.position.bottom != null) {
1523
+ ret.yl = yi + (ret.yl - ret.yi);
1524
+ ret.yi = yi;
1525
+ if (this.screen.autoPadding) {
1526
+ // Maybe just do this no matter what.
1527
+ ret.yl += this.itop;
1528
+ ret.yi += this.itop;
1529
+ }
1530
+ }
1531
+
1532
+ if (ret.xi < mxi) mxi = ret.xi;
1533
+ if (ret.xl > mxl) mxl = ret.xl;
1534
+ if (ret.yi < myi) myi = ret.yi;
1535
+ if (ret.yl > myl) myl = ret.yl;
1536
+ }
1537
+
1538
+ if (get) {
1539
+ this.lpos = _lpos;
1540
+ //this.shrink = true;
1541
+ }
1542
+
1543
+ if (this.position.width == null
1544
+ && (this.position.left == null
1545
+ || this.position.right == null)) {
1546
+ if (this.position.left == null && this.position.right != null) {
1547
+ xi = xl - (mxl - mxi);
1548
+ if (!this.screen.autoPadding) {
1549
+ xi -= this.padding.left + this.padding.right;
1550
+ } else {
1551
+ xi -= this.ileft;
1552
+ }
1553
+ } else {
1554
+ xl = mxl;
1555
+ if (!this.screen.autoPadding) {
1556
+ xl += this.padding.left + this.padding.right;
1557
+ // XXX Temporary workaround until we decide to make autoPadding default.
1558
+ // See widget-listtable.js for an example of why this is necessary.
1559
+ // XXX Maybe just to this for all this being that this would affect
1560
+ // width shrunken normal shrunken lists as well.
1561
+ // if (this._isList) {
1562
+ if (this.type === 'list-table') {
1563
+ xl -= this.padding.left + this.padding.right;
1564
+ xl += this.iright;
1565
+ }
1566
+ } else {
1567
+ //xl += this.padding.right;
1568
+ xl += this.iright;
1569
+ }
1570
+ }
1571
+ }
1572
+
1573
+ if (this.position.height == null
1574
+ && (this.position.top == null
1575
+ || this.position.bottom == null)
1576
+ && (!this.scrollable || this._isList)) {
1577
+ // NOTE: Lists get special treatment if they are shrunken - assume they
1578
+ // want all list items showing. This is one case we can calculate the
1579
+ // height based on items/boxes.
1580
+ if (this._isList) {
1581
+ myi = 0 - this.itop;
1582
+ myl = this.items.length + this.ibottom;
1583
+ }
1584
+ if (this.position.top == null && this.position.bottom != null) {
1585
+ yi = yl - (myl - myi);
1586
+ if (!this.screen.autoPadding) {
1587
+ yi -= this.padding.top + this.padding.bottom;
1588
+ } else {
1589
+ yi -= this.itop;
1590
+ }
1591
+ } else {
1592
+ yl = myl;
1593
+ if (!this.screen.autoPadding) {
1594
+ yl += this.padding.top + this.padding.bottom;
1595
+ } else {
1596
+ yl += this.ibottom;
1597
+ }
1598
+ }
1599
+ }
1600
+
1601
+ return { xi: xi, xl: xl, yi: yi, yl: yl };
1602
+ };
1603
+
1604
+ Element.prototype._getShrinkContent = function(xi, xl, yi, yl) {
1605
+ var h = this._clines.length
1606
+ , w = this._clines.mwidth || 1;
1607
+
1608
+ if (this.position.width == null
1609
+ && (this.position.left == null
1610
+ || this.position.right == null)) {
1611
+ if (this.position.left == null && this.position.right != null) {
1612
+ xi = xl - w - this.iwidth;
1613
+ } else {
1614
+ xl = xi + w + this.iwidth;
1615
+ }
1616
+ }
1617
+
1618
+ if (this.position.height == null
1619
+ && (this.position.top == null
1620
+ || this.position.bottom == null)
1621
+ && (!this.scrollable || this._isList)) {
1622
+ if (this.position.top == null && this.position.bottom != null) {
1623
+ yi = yl - h - this.iheight;
1624
+ } else {
1625
+ yl = yi + h + this.iheight;
1626
+ }
1627
+ }
1628
+
1629
+ return { xi: xi, xl: xl, yi: yi, yl: yl };
1630
+ };
1631
+
1632
+ Element.prototype._getShrink = function(xi, xl, yi, yl, get) {
1633
+ var shrinkBox = this._getShrinkBox(xi, xl, yi, yl, get)
1634
+ , shrinkContent = this._getShrinkContent(xi, xl, yi, yl, get)
1635
+ , xll = xl
1636
+ , yll = yl;
1637
+
1638
+ // Figure out which one is bigger and use it.
1639
+ if (shrinkBox.xl - shrinkBox.xi > shrinkContent.xl - shrinkContent.xi) {
1640
+ xi = shrinkBox.xi;
1641
+ xl = shrinkBox.xl;
1642
+ } else {
1643
+ xi = shrinkContent.xi;
1644
+ xl = shrinkContent.xl;
1645
+ }
1646
+
1647
+ if (shrinkBox.yl - shrinkBox.yi > shrinkContent.yl - shrinkContent.yi) {
1648
+ yi = shrinkBox.yi;
1649
+ yl = shrinkBox.yl;
1650
+ } else {
1651
+ yi = shrinkContent.yi;
1652
+ yl = shrinkContent.yl;
1653
+ }
1654
+
1655
+ // Recenter shrunken elements.
1656
+ if (xl < xll && this.position.left === 'center') {
1657
+ xll = (xll - xl) / 2 | 0;
1658
+ xi += xll;
1659
+ xl += xll;
1660
+ }
1661
+
1662
+ if (yl < yll && this.position.top === 'center') {
1663
+ yll = (yll - yl) / 2 | 0;
1664
+ yi += yll;
1665
+ yl += yll;
1666
+ }
1667
+
1668
+ return { xi: xi, xl: xl, yi: yi, yl: yl };
1669
+ };
1670
+
1671
+ Element.prototype._getCoords = function(get, noscroll) {
1672
+ if (this.hidden) return;
1673
+
1674
+ // if (this.parent._rendering) {
1675
+ // get = true;
1676
+ // }
1677
+
1678
+ var xi = this._getLeft(get)
1679
+ , xl = xi + this._getWidth(get)
1680
+ , yi = this._getTop(get)
1681
+ , yl = yi + this._getHeight(get)
1682
+ , base = this.childBase || 0
1683
+ , el = this
1684
+ , fixed = this.fixed
1685
+ , coords
1686
+ , v
1687
+ , noleft
1688
+ , noright
1689
+ , notop
1690
+ , nobot
1691
+ , ppos
1692
+ , b;
1693
+
1694
+ // Attempt to shrink the element base on the
1695
+ // size of the content and child elements.
1696
+ if (this.shrink) {
1697
+ coords = this._getShrink(xi, xl, yi, yl, get);
1698
+ xi = coords.xi, xl = coords.xl;
1699
+ yi = coords.yi, yl = coords.yl;
1700
+ }
1701
+
1702
+ // Find a scrollable ancestor if we have one.
1703
+ while (el = el.parent) {
1704
+ if (el.scrollable) {
1705
+ if (fixed) {
1706
+ fixed = false;
1707
+ continue;
1708
+ }
1709
+ break;
1710
+ }
1711
+ }
1712
+
1713
+ // Check to make sure we're visible and
1714
+ // inside of the visible scroll area.
1715
+ // NOTE: Lists have a property where only
1716
+ // the list items are obfuscated.
1717
+
1718
+ // Old way of doing things, this would not render right if a shrunken element
1719
+ // with lots of boxes in it was within a scrollable element.
1720
+ // See: $ node test/widget-shrink-fail.js
1721
+ // var thisparent = this.parent;
1722
+
1723
+ var thisparent = el;
1724
+ if (el && !noscroll) {
1725
+ ppos = thisparent.lpos;
1726
+
1727
+ // The shrink option can cause a stack overflow
1728
+ // by calling _getCoords on the child again.
1729
+ // if (!get && !thisparent.shrink) {
1730
+ // ppos = thisparent._getCoords();
1731
+ // }
1732
+
1733
+ if (!ppos) return;
1734
+
1735
+ // TODO: Figure out how to fix base (and cbase to only
1736
+ // take into account the *parent's* padding.
1737
+
1738
+ yi -= ppos.base;
1739
+ yl -= ppos.base;
1740
+
1741
+ b = thisparent.border ? 1 : 0;
1742
+
1743
+ // XXX
1744
+ // Fixes non-`fixed` labels to work with scrolling (they're ON the border):
1745
+ // if (this.position.left < 0
1746
+ // || this.position.right < 0
1747
+ // || this.position.top < 0
1748
+ // || this.position.bottom < 0) {
1749
+ if (this._isLabel) {
1750
+ b = 0;
1751
+ }
1752
+
1753
+ if (yi < ppos.yi + b) {
1754
+ if (yl - 1 < ppos.yi + b) {
1755
+ // Is above.
1756
+ return;
1757
+ } else {
1758
+ // Is partially covered above.
1759
+ notop = true;
1760
+ v = ppos.yi - yi;
1761
+ if (this.border) v--;
1762
+ if (thisparent.border) v++;
1763
+ base += v;
1764
+ yi += v;
1765
+ }
1766
+ } else if (yl > ppos.yl - b) {
1767
+ if (yi > ppos.yl - 1 - b) {
1768
+ // Is below.
1769
+ return;
1770
+ } else {
1771
+ // Is partially covered below.
1772
+ nobot = true;
1773
+ v = yl - ppos.yl;
1774
+ if (this.border) v--;
1775
+ if (thisparent.border) v++;
1776
+ yl -= v;
1777
+ }
1778
+ }
1779
+
1780
+ // Shouldn't be necessary.
1781
+ // assert.ok(yi < yl);
1782
+ if (yi >= yl) return;
1783
+
1784
+ // Could allow overlapping stuff in scrolling elements
1785
+ // if we cleared the pending buffer before every draw.
1786
+ if (xi < el.lpos.xi) {
1787
+ xi = el.lpos.xi;
1788
+ noleft = true;
1789
+ if (this.border) xi--;
1790
+ if (thisparent.border) xi++;
1791
+ }
1792
+ if (xl > el.lpos.xl) {
1793
+ xl = el.lpos.xl;
1794
+ noright = true;
1795
+ if (this.border) xl++;
1796
+ if (thisparent.border) xl--;
1797
+ }
1798
+ //if (xi > xl) return;
1799
+ if (xi >= xl) return;
1800
+ }
1801
+
1802
+ if (this.noOverflow && this.parent.lpos) {
1803
+ if (xi < this.parent.lpos.xi + this.parent.ileft) {
1804
+ xi = this.parent.lpos.xi + this.parent.ileft;
1805
+ }
1806
+ if (xl > this.parent.lpos.xl - this.parent.iright) {
1807
+ xl = this.parent.lpos.xl - this.parent.iright;
1808
+ }
1809
+ if (yi < this.parent.lpos.yi + this.parent.itop) {
1810
+ yi = this.parent.lpos.yi + this.parent.itop;
1811
+ }
1812
+ if (yl > this.parent.lpos.yl - this.parent.ibottom) {
1813
+ yl = this.parent.lpos.yl - this.parent.ibottom;
1814
+ }
1815
+ }
1816
+
1817
+ // if (this.parent.lpos) {
1818
+ // this.parent.lpos._scrollBottom = Math.max(
1819
+ // this.parent.lpos._scrollBottom, yl);
1820
+ // }
1821
+
1822
+ return {
1823
+ xi: xi,
1824
+ xl: xl,
1825
+ yi: yi,
1826
+ yl: yl,
1827
+ base: base,
1828
+ noleft: noleft,
1829
+ noright: noright,
1830
+ notop: notop,
1831
+ nobot: nobot,
1832
+ renders: this.screen.renders
1833
+ };
1834
+ };
1835
+
1836
+ Element.prototype.render = function() {
1837
+ this._emit('prerender');
1838
+
1839
+ this.parseContent();
1840
+
1841
+ var coords = this._getCoords(true);
1842
+ if (!coords) {
1843
+ delete this.lpos;
1844
+ return;
1845
+ }
1846
+
1847
+ if (coords.xl - coords.xi <= 0) {
1848
+ coords.xl = Math.max(coords.xl, coords.xi);
1849
+ return;
1850
+ }
1851
+
1852
+ if (coords.yl - coords.yi <= 0) {
1853
+ coords.yl = Math.max(coords.yl, coords.yi);
1854
+ return;
1855
+ }
1856
+
1857
+ var lines = this.screen.lines
1858
+ , xi = coords.xi
1859
+ , xl = coords.xl
1860
+ , yi = coords.yi
1861
+ , yl = coords.yl
1862
+ , x
1863
+ , y
1864
+ , cell
1865
+ , attr
1866
+ , ch
1867
+ , content = this._pcontent
1868
+ , ci = this._clines.ci[coords.base]
1869
+ , battr
1870
+ , dattr
1871
+ , c
1872
+ , visible
1873
+ , i
1874
+ , bch = this.ch;
1875
+
1876
+ // Clip content if it's off the edge of the screen
1877
+ // if (xi + this.ileft < 0 || yi + this.itop < 0) {
1878
+ // var clines = this._clines.slice();
1879
+ // if (xi + this.ileft < 0) {
1880
+ // for (var i = 0; i < clines.length; i++) {
1881
+ // var t = 0;
1882
+ // var csi = '';
1883
+ // var csis = '';
1884
+ // for (var j = 0; j < clines[i].length; j++) {
1885
+ // while (clines[i][j] === '\x1b') {
1886
+ // csi = '\x1b';
1887
+ // while (clines[i][j++] !== 'm') csi += clines[i][j];
1888
+ // csis += csi;
1889
+ // }
1890
+ // if (++t === -(xi + this.ileft) + 1) break;
1891
+ // }
1892
+ // clines[i] = csis + clines[i].substring(j);
1893
+ // }
1894
+ // }
1895
+ // if (yi + this.itop < 0) {
1896
+ // clines = clines.slice(-(yi + this.itop));
1897
+ // }
1898
+ // content = clines.join('\n');
1899
+ // }
1900
+
1901
+ if (coords.base >= this._clines.ci.length) {
1902
+ ci = this._pcontent.length;
1903
+ }
1904
+
1905
+ this.lpos = coords;
1906
+
1907
+ if (this.border && this.border.type === 'line') {
1908
+ this.screen._borderStops[coords.yi] = true;
1909
+ this.screen._borderStops[coords.yl - 1] = true;
1910
+ // if (!this.screen._borderStops[coords.yi]) {
1911
+ // this.screen._borderStops[coords.yi] = { xi: coords.xi, xl: coords.xl };
1912
+ // } else {
1913
+ // if (this.screen._borderStops[coords.yi].xi > coords.xi) {
1914
+ // this.screen._borderStops[coords.yi].xi = coords.xi;
1915
+ // }
1916
+ // if (this.screen._borderStops[coords.yi].xl < coords.xl) {
1917
+ // this.screen._borderStops[coords.yi].xl = coords.xl;
1918
+ // }
1919
+ // }
1920
+ // this.screen._borderStops[coords.yl - 1] = this.screen._borderStops[coords.yi];
1921
+ }
1922
+
1923
+ dattr = this.sattr(this.style);
1924
+ attr = dattr;
1925
+
1926
+ // If we're in a scrollable text box, check to
1927
+ // see which attributes this line starts with.
1928
+ if (ci > 0) {
1929
+ attr = this._clines.attr[Math.min(coords.base, this._clines.length - 1)];
1930
+ }
1931
+
1932
+ if (this.border) xi++, xl--, yi++, yl--;
1933
+
1934
+ // If we have padding/valign, that means the
1935
+ // content-drawing loop will skip a few cells/lines.
1936
+ // To deal with this, we can just fill the whole thing
1937
+ // ahead of time. This could be optimized.
1938
+ if (this.tpadding || (this.valign && this.valign !== 'top')) {
1939
+ if (this.style.transparent) {
1940
+ for (y = Math.max(yi, 0); y < yl; y++) {
1941
+ if (!lines[y]) break;
1942
+ for (x = Math.max(xi, 0); x < xl; x++) {
1943
+ if (!lines[y][x]) break;
1944
+ lines[y][x][0] = colors.blend(attr, lines[y][x][0]);
1945
+ // lines[y][x][1] = bch;
1946
+ lines[y].dirty = true;
1947
+ }
1948
+ }
1949
+ } else {
1950
+ this.screen.fillRegion(dattr, bch, xi, xl, yi, yl);
1951
+ }
1952
+ }
1953
+
1954
+ if (this.tpadding) {
1955
+ xi += this.padding.left, xl -= this.padding.right;
1956
+ yi += this.padding.top, yl -= this.padding.bottom;
1957
+ }
1958
+
1959
+ // Determine where to place the text if it's vertically aligned.
1960
+ if (this.valign === 'middle' || this.valign === 'bottom') {
1961
+ visible = yl - yi;
1962
+ if (this._clines.length < visible) {
1963
+ if (this.valign === 'middle') {
1964
+ visible = visible / 2 | 0;
1965
+ visible -= this._clines.length / 2 | 0;
1966
+ } else if (this.valign === 'bottom') {
1967
+ visible -= this._clines.length;
1968
+ }
1969
+ ci -= visible * (xl - xi);
1970
+ }
1971
+ }
1972
+
1973
+ // Draw the content and background.
1974
+ for (y = yi; y < yl; y++) {
1975
+ if (!lines[y]) {
1976
+ if (y >= this.screen.height || yl < this.ibottom) {
1977
+ break;
1978
+ } else {
1979
+ continue;
1980
+ }
1981
+ }
1982
+ for (x = xi; x < xl; x++) {
1983
+ cell = lines[y][x];
1984
+ if (!cell) {
1985
+ if (x >= this.screen.width || xl < this.iright) {
1986
+ break;
1987
+ } else {
1988
+ continue;
1989
+ }
1990
+ }
1991
+
1992
+ ch = content[ci++] || bch;
1993
+
1994
+ // if (!content[ci] && !coords._contentEnd) {
1995
+ // coords._contentEnd = { x: x - xi, y: y - yi };
1996
+ // }
1997
+
1998
+ // Handle escape codes.
1999
+ while (ch === '\x1b') {
2000
+ if (c = /^\x1b\[[\d;]*m/.exec(content.substring(ci - 1))) {
2001
+ ci += c[0].length - 1;
2002
+ attr = this.screen.attrCode(c[0], attr, dattr);
2003
+ // Ignore foreground changes for selected items.
2004
+ if (this.parent._isList && this.parent.interactive
2005
+ && this.parent.items[this.parent.selected] === this
2006
+ && this.parent.options.invertSelected !== false) {
2007
+ attr = (attr & ~(0x1ff << 9)) | (dattr & (0x1ff << 9));
2008
+ }
2009
+ ch = content[ci] || bch;
2010
+ ci++;
2011
+ } else {
2012
+ break;
2013
+ }
2014
+ }
2015
+
2016
+ // Handle newlines.
2017
+ if (ch === '\t') ch = bch;
2018
+ if (ch === '\n') {
2019
+ // If we're on the first cell and we find a newline and the last cell
2020
+ // of the last line was not a newline, let's just treat this like the
2021
+ // newline was already "counted".
2022
+ if (x === xi && y !== yi && content[ci - 2] !== '\n') {
2023
+ x--;
2024
+ continue;
2025
+ }
2026
+ // We could use fillRegion here, name the
2027
+ // outer loop, and continue to it instead.
2028
+ ch = bch;
2029
+ for (; x < xl; x++) {
2030
+ cell = lines[y][x];
2031
+ if (!cell) break;
2032
+ if (this.style.transparent) {
2033
+ lines[y][x][0] = colors.blend(attr, lines[y][x][0]);
2034
+ if (content[ci]) lines[y][x][1] = ch;
2035
+ lines[y].dirty = true;
2036
+ } else {
2037
+ if (attr !== cell[0] || ch !== cell[1]) {
2038
+ lines[y][x][0] = attr;
2039
+ lines[y][x][1] = ch;
2040
+ lines[y].dirty = true;
2041
+ }
2042
+ }
2043
+ }
2044
+ continue;
2045
+ }
2046
+
2047
+ if (this.screen.fullUnicode && content[ci - 1]) {
2048
+ var point = unicode.codePointAt(content, ci - 1);
2049
+ // Handle combining chars:
2050
+ // Make sure they get in the same cell and are counted as 0.
2051
+ if (unicode.combining[point]) {
2052
+ if (point > 0x00ffff) {
2053
+ ch = content[ci - 1] + content[ci];
2054
+ ci++;
2055
+ }
2056
+ if (x - 1 >= xi) {
2057
+ lines[y][x - 1][1] += ch;
2058
+ } else if (y - 1 >= yi) {
2059
+ lines[y - 1][xl - 1][1] += ch;
2060
+ }
2061
+ x--;
2062
+ continue;
2063
+ }
2064
+ // Handle surrogate pairs:
2065
+ // Make sure we put surrogate pair chars in one cell.
2066
+ if (point > 0x00ffff) {
2067
+ ch = content[ci - 1] + content[ci];
2068
+ ci++;
2069
+ }
2070
+ }
2071
+
2072
+ if (this._noFill) continue;
2073
+
2074
+ if (this.style.transparent) {
2075
+ lines[y][x][0] = colors.blend(attr, lines[y][x][0]);
2076
+ if (content[ci]) lines[y][x][1] = ch;
2077
+ lines[y].dirty = true;
2078
+ } else {
2079
+ if (attr !== cell[0] || ch !== cell[1]) {
2080
+ lines[y][x][0] = attr;
2081
+ lines[y][x][1] = ch;
2082
+ lines[y].dirty = true;
2083
+ }
2084
+ }
2085
+ }
2086
+ }
2087
+
2088
+ // Draw the scrollbar.
2089
+ // Could possibly draw this after all child elements.
2090
+ if (this.scrollbar) {
2091
+ // XXX
2092
+ // i = this.getScrollHeight();
2093
+ i = Math.max(this._clines.length, this._scrollBottom());
2094
+ }
2095
+ if (coords.notop || coords.nobot) i = -Infinity;
2096
+ if (this.scrollbar && (yl - yi) < i) {
2097
+ x = xl - 1;
2098
+ if (this.scrollbar.ignoreBorder && this.border) x++;
2099
+ if (this.alwaysScroll) {
2100
+ y = this.childBase / (i - (yl - yi));
2101
+ } else {
2102
+ y = (this.childBase + this.childOffset) / (i - 1);
2103
+ }
2104
+ y = yi + ((yl - yi) * y | 0);
2105
+ if (y >= yl) y = yl - 1;
2106
+ cell = lines[y] && lines[y][x];
2107
+ if (cell) {
2108
+ if (this.track) {
2109
+ ch = this.track.ch || ' ';
2110
+ attr = this.sattr(this.style.track,
2111
+ this.style.track.fg || this.style.fg,
2112
+ this.style.track.bg || this.style.bg);
2113
+ this.screen.fillRegion(attr, ch, x, x + 1, yi, yl);
2114
+ }
2115
+ ch = this.scrollbar.ch || ' ';
2116
+ attr = this.sattr(this.style.scrollbar,
2117
+ this.style.scrollbar.fg || this.style.fg,
2118
+ this.style.scrollbar.bg || this.style.bg);
2119
+ if (attr !== cell[0] || ch !== cell[1]) {
2120
+ lines[y][x][0] = attr;
2121
+ lines[y][x][1] = ch;
2122
+ lines[y].dirty = true;
2123
+ }
2124
+ }
2125
+ }
2126
+
2127
+ if (this.border) xi--, xl++, yi--, yl++;
2128
+
2129
+ if (this.tpadding) {
2130
+ xi -= this.padding.left, xl += this.padding.right;
2131
+ yi -= this.padding.top, yl += this.padding.bottom;
2132
+ }
2133
+
2134
+ // Draw the border.
2135
+ if (this.border) {
2136
+ battr = this.sattr(this.style.border);
2137
+ y = yi;
2138
+ if (coords.notop) y = -1;
2139
+ for (x = xi; x < xl; x++) {
2140
+ if (!lines[y]) break;
2141
+ if (coords.noleft && x === xi) continue;
2142
+ if (coords.noright && x === xl - 1) continue;
2143
+ cell = lines[y][x];
2144
+ if (!cell) continue;
2145
+ if (this.border.type === 'line') {
2146
+ if (x === xi) {
2147
+ ch = '\u250c'; // '┌'
2148
+ if (!this.border.left) {
2149
+ if (this.border.top) {
2150
+ ch = '\u2500'; // '─'
2151
+ } else {
2152
+ continue;
2153
+ }
2154
+ } else {
2155
+ if (!this.border.top) {
2156
+ ch = '\u2502'; // '│'
2157
+ }
2158
+ }
2159
+ } else if (x === xl - 1) {
2160
+ ch = '\u2510'; // '┐'
2161
+ if (!this.border.right) {
2162
+ if (this.border.top) {
2163
+ ch = '\u2500'; // '─'
2164
+ } else {
2165
+ continue;
2166
+ }
2167
+ } else {
2168
+ if (!this.border.top) {
2169
+ ch = '\u2502'; // '│'
2170
+ }
2171
+ }
2172
+ } else {
2173
+ ch = '\u2500'; // '─'
2174
+ }
2175
+ } else if (this.border.type === 'bg') {
2176
+ ch = this.border.ch;
2177
+ }
2178
+ if (!this.border.top && x !== xi && x !== xl - 1) {
2179
+ ch = ' ';
2180
+ if (dattr !== cell[0] || ch !== cell[1]) {
2181
+ lines[y][x][0] = dattr;
2182
+ lines[y][x][1] = ch;
2183
+ lines[y].dirty = true;
2184
+ continue;
2185
+ }
2186
+ }
2187
+ if (battr !== cell[0] || ch !== cell[1]) {
2188
+ lines[y][x][0] = battr;
2189
+ lines[y][x][1] = ch;
2190
+ lines[y].dirty = true;
2191
+ }
2192
+ }
2193
+ y = yi + 1;
2194
+ for (; y < yl - 1; y++) {
2195
+ if (!lines[y]) continue;
2196
+ cell = lines[y][xi];
2197
+ if (cell) {
2198
+ if (this.border.left) {
2199
+ if (this.border.type === 'line') {
2200
+ ch = '\u2502'; // '│'
2201
+ } else if (this.border.type === 'bg') {
2202
+ ch = this.border.ch;
2203
+ }
2204
+ if (!coords.noleft)
2205
+ if (battr !== cell[0] || ch !== cell[1]) {
2206
+ lines[y][xi][0] = battr;
2207
+ lines[y][xi][1] = ch;
2208
+ lines[y].dirty = true;
2209
+ }
2210
+ } else {
2211
+ ch = ' ';
2212
+ if (dattr !== cell[0] || ch !== cell[1]) {
2213
+ lines[y][xi][0] = dattr;
2214
+ lines[y][xi][1] = ch;
2215
+ lines[y].dirty = true;
2216
+ }
2217
+ }
2218
+ }
2219
+ cell = lines[y][xl - 1];
2220
+ if (cell) {
2221
+ if (this.border.right) {
2222
+ if (this.border.type === 'line') {
2223
+ ch = '\u2502'; // '│'
2224
+ } else if (this.border.type === 'bg') {
2225
+ ch = this.border.ch;
2226
+ }
2227
+ if (!coords.noright)
2228
+ if (battr !== cell[0] || ch !== cell[1]) {
2229
+ lines[y][xl - 1][0] = battr;
2230
+ lines[y][xl - 1][1] = ch;
2231
+ lines[y].dirty = true;
2232
+ }
2233
+ } else {
2234
+ ch = ' ';
2235
+ if (dattr !== cell[0] || ch !== cell[1]) {
2236
+ lines[y][xl - 1][0] = dattr;
2237
+ lines[y][xl - 1][1] = ch;
2238
+ lines[y].dirty = true;
2239
+ }
2240
+ }
2241
+ }
2242
+ }
2243
+ y = yl - 1;
2244
+ if (coords.nobot) y = -1;
2245
+ for (x = xi; x < xl; x++) {
2246
+ if (!lines[y]) break;
2247
+ if (coords.noleft && x === xi) continue;
2248
+ if (coords.noright && x === xl - 1) continue;
2249
+ cell = lines[y][x];
2250
+ if (!cell) continue;
2251
+ if (this.border.type === 'line') {
2252
+ if (x === xi) {
2253
+ ch = '\u2514'; // '└'
2254
+ if (!this.border.left) {
2255
+ if (this.border.bottom) {
2256
+ ch = '\u2500'; // '─'
2257
+ } else {
2258
+ continue;
2259
+ }
2260
+ } else {
2261
+ if (!this.border.bottom) {
2262
+ ch = '\u2502'; // '│'
2263
+ }
2264
+ }
2265
+ } else if (x === xl - 1) {
2266
+ ch = '\u2518'; // '┘'
2267
+ if (!this.border.right) {
2268
+ if (this.border.bottom) {
2269
+ ch = '\u2500'; // '─'
2270
+ } else {
2271
+ continue;
2272
+ }
2273
+ } else {
2274
+ if (!this.border.bottom) {
2275
+ ch = '\u2502'; // '│'
2276
+ }
2277
+ }
2278
+ } else {
2279
+ ch = '\u2500'; // '─'
2280
+ }
2281
+ } else if (this.border.type === 'bg') {
2282
+ ch = this.border.ch;
2283
+ }
2284
+ if (!this.border.bottom && x !== xi && x !== xl - 1) {
2285
+ ch = ' ';
2286
+ if (dattr !== cell[0] || ch !== cell[1]) {
2287
+ lines[y][x][0] = dattr;
2288
+ lines[y][x][1] = ch;
2289
+ lines[y].dirty = true;
2290
+ }
2291
+ continue;
2292
+ }
2293
+ if (battr !== cell[0] || ch !== cell[1]) {
2294
+ lines[y][x][0] = battr;
2295
+ lines[y][x][1] = ch;
2296
+ lines[y].dirty = true;
2297
+ }
2298
+ }
2299
+ }
2300
+
2301
+ if (this.shadow) {
2302
+ // right
2303
+ y = Math.max(yi + 1, 0);
2304
+ for (; y < yl + 1; y++) {
2305
+ if (!lines[y]) break;
2306
+ x = xl;
2307
+ for (; x < xl + 2; x++) {
2308
+ if (!lines[y][x]) break;
2309
+ // lines[y][x][0] = colors.blend(this.dattr, lines[y][x][0]);
2310
+ lines[y][x][0] = colors.blend(lines[y][x][0]);
2311
+ lines[y].dirty = true;
2312
+ }
2313
+ }
2314
+ // bottom
2315
+ y = yl;
2316
+ for (; y < yl + 1; y++) {
2317
+ if (!lines[y]) break;
2318
+ for (x = Math.max(xi + 1, 0); x < xl; x++) {
2319
+ if (!lines[y][x]) break;
2320
+ // lines[y][x][0] = colors.blend(this.dattr, lines[y][x][0]);
2321
+ lines[y][x][0] = colors.blend(lines[y][x][0]);
2322
+ lines[y].dirty = true;
2323
+ }
2324
+ }
2325
+ }
2326
+
2327
+ this.children.forEach(function(el) {
2328
+ if (el.screen._ci !== -1) {
2329
+ el.index = el.screen._ci++;
2330
+ }
2331
+ // if (el.screen._rendering) {
2332
+ // el._rendering = true;
2333
+ // }
2334
+ el.render();
2335
+ // if (el.screen._rendering) {
2336
+ // el._rendering = false;
2337
+ // }
2338
+ });
2339
+
2340
+ this._emit('render', [coords]);
2341
+
2342
+ return coords;
2343
+ };
2344
+
2345
+ Element.prototype._render = Element.prototype.render;
2346
+
2347
+ /**
2348
+ * Content Methods
2349
+ */
2350
+
2351
+ Element.prototype.insertLine = function(i, line) {
2352
+ if (typeof line === 'string') line = line.split('\n');
2353
+
2354
+ if (i !== i || i == null) {
2355
+ i = this._clines.ftor.length;
2356
+ }
2357
+
2358
+ i = Math.max(i, 0);
2359
+
2360
+ while (this._clines.fake.length < i) {
2361
+ this._clines.fake.push('');
2362
+ this._clines.ftor.push([this._clines.push('') - 1]);
2363
+ this._clines.rtof(this._clines.fake.length - 1);
2364
+ }
2365
+
2366
+ // NOTE: Could possibly compare the first and last ftor line numbers to see
2367
+ // if they're the same, or if they fit in the visible region entirely.
2368
+ var start = this._clines.length
2369
+ , diff
2370
+ , real;
2371
+
2372
+ if (i >= this._clines.ftor.length) {
2373
+ real = this._clines.ftor[this._clines.ftor.length - 1];
2374
+ real = real[real.length - 1] + 1;
2375
+ } else {
2376
+ real = this._clines.ftor[i][0];
2377
+ }
2378
+
2379
+ for (var j = 0; j < line.length; j++) {
2380
+ this._clines.fake.splice(i + j, 0, line[j]);
2381
+ }
2382
+
2383
+ this.setContent(this._clines.fake.join('\n'), true);
2384
+
2385
+ diff = this._clines.length - start;
2386
+
2387
+ if (diff > 0) {
2388
+ var pos = this._getCoords();
2389
+ if (!pos) return;
2390
+
2391
+ var height = pos.yl - pos.yi - this.iheight
2392
+ , base = this.childBase || 0
2393
+ , visible = real >= base && real - base < height;
2394
+
2395
+ if (pos && visible && this.screen.cleanSides(this)) {
2396
+ this.screen.insertLine(diff,
2397
+ pos.yi + this.itop + real - base,
2398
+ pos.yi,
2399
+ pos.yl - this.ibottom - 1);
2400
+ }
2401
+ }
2402
+ };
2403
+
2404
+ Element.prototype.deleteLine = function(i, n) {
2405
+ n = n || 1;
2406
+
2407
+ if (i !== i || i == null) {
2408
+ i = this._clines.ftor.length - 1;
2409
+ }
2410
+
2411
+ i = Math.max(i, 0);
2412
+ i = Math.min(i, this._clines.ftor.length - 1);
2413
+
2414
+ // NOTE: Could possibly compare the first and last ftor line numbers to see
2415
+ // if they're the same, or if they fit in the visible region entirely.
2416
+ var start = this._clines.length
2417
+ , diff
2418
+ , real = this._clines.ftor[i][0];
2419
+
2420
+ while (n--) {
2421
+ this._clines.fake.splice(i, 1);
2422
+ }
2423
+
2424
+ this.setContent(this._clines.fake.join('\n'), true);
2425
+
2426
+ diff = start - this._clines.length;
2427
+
2428
+ // XXX clearPos() without diff statement?
2429
+ var height = 0;
2430
+
2431
+ if (diff > 0) {
2432
+ var pos = this._getCoords();
2433
+ if (!pos) return;
2434
+
2435
+ height = pos.yl - pos.yi - this.iheight;
2436
+
2437
+ var base = this.childBase || 0
2438
+ , visible = real >= base && real - base < height;
2439
+
2440
+ if (pos && visible && this.screen.cleanSides(this)) {
2441
+ this.screen.deleteLine(diff,
2442
+ pos.yi + this.itop + real - base,
2443
+ pos.yi,
2444
+ pos.yl - this.ibottom - 1);
2445
+ }
2446
+ }
2447
+
2448
+ if (this._clines.length < height) {
2449
+ this.clearPos();
2450
+ }
2451
+ };
2452
+
2453
+ Element.prototype.insertTop = function(line) {
2454
+ var fake = this._clines.rtof[this.childBase || 0];
2455
+ return this.insertLine(fake, line);
2456
+ };
2457
+
2458
+ Element.prototype.insertBottom = function(line) {
2459
+ var h = (this.childBase || 0) + this.height - this.iheight
2460
+ , i = Math.min(h, this._clines.length)
2461
+ , fake = this._clines.rtof[i - 1] + 1;
2462
+
2463
+ return this.insertLine(fake, line);
2464
+ };
2465
+
2466
+ Element.prototype.deleteTop = function(n) {
2467
+ var fake = this._clines.rtof[this.childBase || 0];
2468
+ return this.deleteLine(fake, n);
2469
+ };
2470
+
2471
+ Element.prototype.deleteBottom = function(n) {
2472
+ var h = (this.childBase || 0) + this.height - 1 - this.iheight
2473
+ , i = Math.min(h, this._clines.length - 1)
2474
+ , fake = this._clines.rtof[i];
2475
+
2476
+ n = n || 1;
2477
+
2478
+ return this.deleteLine(fake - (n - 1), n);
2479
+ };
2480
+
2481
+ Element.prototype.setLine = function(i, line) {
2482
+ i = Math.max(i, 0);
2483
+ while (this._clines.fake.length < i) {
2484
+ this._clines.fake.push('');
2485
+ }
2486
+ this._clines.fake[i] = line;
2487
+ return this.setContent(this._clines.fake.join('\n'), true);
2488
+ };
2489
+
2490
+ Element.prototype.setBaseLine = function(i, line) {
2491
+ var fake = this._clines.rtof[this.childBase || 0];
2492
+ return this.setLine(fake + i, line);
2493
+ };
2494
+
2495
+ Element.prototype.getLine = function(i) {
2496
+ i = Math.max(i, 0);
2497
+ i = Math.min(i, this._clines.fake.length - 1);
2498
+ return this._clines.fake[i];
2499
+ };
2500
+
2501
+ Element.prototype.getBaseLine = function(i) {
2502
+ var fake = this._clines.rtof[this.childBase || 0];
2503
+ return this.getLine(fake + i);
2504
+ };
2505
+
2506
+ Element.prototype.clearLine = function(i) {
2507
+ i = Math.min(i, this._clines.fake.length - 1);
2508
+ return this.setLine(i, '');
2509
+ };
2510
+
2511
+ Element.prototype.clearBaseLine = function(i) {
2512
+ var fake = this._clines.rtof[this.childBase || 0];
2513
+ return this.clearLine(fake + i);
2514
+ };
2515
+
2516
+ Element.prototype.unshiftLine = function(line) {
2517
+ return this.insertLine(0, line);
2518
+ };
2519
+
2520
+ Element.prototype.shiftLine = function(n) {
2521
+ return this.deleteLine(0, n);
2522
+ };
2523
+
2524
+ Element.prototype.pushLine = function(line) {
2525
+ if (!this.content) return this.setLine(0, line);
2526
+ return this.insertLine(this._clines.fake.length, line);
2527
+ };
2528
+
2529
+ Element.prototype.popLine = function(n) {
2530
+ return this.deleteLine(this._clines.fake.length - 1, n);
2531
+ };
2532
+
2533
+ Element.prototype.getLines = function() {
2534
+ return this._clines.fake.slice();
2535
+ };
2536
+
2537
+ Element.prototype.getScreenLines = function() {
2538
+ return this._clines.slice();
2539
+ };
2540
+
2541
+ Element.prototype.strWidth = function(text) {
2542
+ text = this.parseTags
2543
+ ? helpers.stripTags(text)
2544
+ : text;
2545
+ return this.screen.fullUnicode
2546
+ ? unicode.strWidth(text)
2547
+ : helpers.dropUnicode(text).length;
2548
+ };
2549
+
2550
+ Element.prototype.screenshot = function(xi, xl, yi, yl) {
2551
+ xi = this.lpos.xi + this.ileft + (xi || 0);
2552
+ if (xl != null) {
2553
+ xl = this.lpos.xi + this.ileft + (xl || 0);
2554
+ } else {
2555
+ xl = this.lpos.xl - this.iright;
2556
+ }
2557
+ yi = this.lpos.yi + this.itop + (yi || 0);
2558
+ if (yl != null) {
2559
+ yl = this.lpos.yi + this.itop + (yl || 0);
2560
+ } else {
2561
+ yl = this.lpos.yl - this.ibottom;
2562
+ }
2563
+ return this.screen.screenshot(xi, xl, yi, yl);
2564
+ };
2565
+
2566
+ /**
2567
+ * Expose
2568
+ */
2569
+
2570
+ module.exports = Element;