@nsshunt/stsui 1.9.21 → 1.9.23

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.
package/src/stsuiframe.ts DELETED
@@ -1,620 +0,0 @@
1
- import EventEmitter from 'events'
2
-
3
- // https://github.com/yaronn/blessed-contrib
4
- //var contrib = require('blessed-contrib');
5
- import blessed from 'blessed';
6
-
7
- import 'colors';
8
-
9
- import { v4 as uuidv4 } from 'uuid';
10
-
11
- import * as stsBlessed from './index'
12
- import * as MenuBar from './menubar'
13
-
14
- export type menuOptions = MenuBar.MenuBarOptions[]
15
-
16
- export interface widget {
17
- widget: string // Name of the blessed widget
18
- options: stsBlessed.elementOptions // blessed widget options - used in the constructor of the blessed widget
19
- }
20
-
21
- export interface widgets {
22
- [widgetName: string]: widget
23
- }
24
-
25
- export interface panel {
26
- id: string,
27
- pos?: {
28
- row: number,
29
- col: number,
30
- rowSpan: number,
31
- colSpan: number
32
- },
33
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
- data: any
35
- panelHeader: string,
36
- widgets: widgets
37
- }
38
-
39
- export interface panels {
40
- [panelId: string]: panel
41
- }
42
-
43
- export interface gridData {
44
- title: string
45
- grid: { rows: number, cols: number },
46
- menu: menuOptions,
47
- panels: panels
48
- }
49
-
50
- export interface renderPanelCb {
51
- (box: stsBlessed.box, panel: panel): void
52
- }
53
-
54
- export interface boxPos {
55
- top: number,
56
- left: number,
57
- width: number,
58
- height: number
59
- }
60
-
61
- export interface uiBox {
62
- box: stsBlessed.box, // blessed box
63
- boxHeader: stsBlessed.listbar // blessed box
64
- }
65
-
66
- export interface uiBoxes {
67
- [key: string]: uiBox
68
- }
69
-
70
- export class STSUIFrame extends EventEmitter {
71
- private uiBoxes:uiBoxes = { }; // { box: <<blessed box>>, boxHeader: <<blessed box>> }
72
- private screen: stsBlessed.screen | null = null;
73
- #cursor = 0; // panel index with the complete model data.
74
- #renderPanelFn: renderPanelCb | null = null;
75
- private screenHeaderText = '...';
76
-
77
- private minRows = 1;
78
- private minCols = 1;
79
- private minWidth = 40;
80
- private minHeight = 7;
81
-
82
- #cursorInfo: stsBlessed.box | null = null;
83
- private sortInfo: stsBlessed.box | null = null; // Current sort mode UI box control
84
- private screenHeader: stsBlessed.box | null = null;
85
-
86
- private uidata: gridData;
87
-
88
- /**
89
- *
90
- * @param {*} data The screen layout data. Schema: { title: <str>,
91
- * grid: { rows: <int>, cols: <int> },
92
- * panels: [ { id: <str>, pos: { row: <int>, col: <int>, rowSpan: <int>, colSpan: <int> },
93
- * template: <handlebars func>, widgets: { [ { <prop_name>: <blessed widget> } ] } } ] }
94
- */
95
- constructor(data: gridData, renderPanelFn: renderPanelCb | null = null) {
96
- super();
97
- this.uidata = data;
98
- this.#renderPanelFn = renderPanelFn;
99
- this.SetupUI();
100
- }
101
-
102
- /**
103
- *
104
- * @param {number} position The index (0 based) for the panel item to be calculated
105
- * @returns object with top, left, width and height correctly set
106
- */
107
- #CalcBoxPos = (panel: panel): boxPos | null => {
108
- // Calculate the position and dimensions for this UI element
109
-
110
- if (!this.screen) return null;
111
- if (!panel.pos) return null;
112
-
113
- let screenHeight = this.screen.height;
114
- if (this.uidata.menu !== null) {
115
- screenHeight -= this.uidata.menu.length; // Allow for any menu items
116
- }
117
- const screenWidth = this.screen.width;
118
-
119
- const rows = this.uidata.grid.rows;
120
- const cols = this.uidata.grid.cols;
121
-
122
- const pos = panel.pos;
123
-
124
- const colWidth = Math.floor(screenWidth / cols);
125
- const rowHeight = Math.floor(screenHeight / rows);
126
-
127
- // Allow for a top offset if a top menu is present
128
- let topoffset = 0;
129
- if (this.uidata.menu !== null) {
130
- for (const [, value] of Object.entries(this.uidata.menu)) {
131
- if (value.top === true) {
132
- topoffset = 1;
133
- break;
134
- }
135
- }
136
- }
137
-
138
- const boxPos: boxPos = {
139
- top: (pos.row * rowHeight) + topoffset,
140
- left: pos.col * colWidth,
141
- width: colWidth * pos.colSpan,
142
- height: rowHeight * pos.rowSpan
143
- }
144
-
145
- // Now check if this panel is on the far right and if so adjust for a perfect fit
146
-
147
- if ((pos.col + pos.colSpan) === cols) {
148
- //if (pos.col === (cols - 1)) {
149
- boxPos.width = screenWidth - (pos.col * colWidth);
150
- }
151
-
152
- // Now check if this panel is on the bottom and if so adjust for a perfect fit
153
- if ((pos.row + pos.rowSpan) === rows) {
154
- boxPos.height = screenHeight - (pos.row * rowHeight);
155
- }
156
-
157
- return boxPos;
158
- }
159
-
160
- #UpdateUIBoxPos = (panel: panel, uiBox: uiBox): void => {
161
- const pos: boxPos | null = this.#CalcBoxPos(panel);
162
- if (!pos) {
163
- return;
164
- }
165
-
166
- uiBox.box.top = pos.top;
167
- uiBox.box.left = pos.left;
168
- uiBox.box.width = pos.width;
169
- uiBox.box.height = pos.height;
170
-
171
- uiBox.boxHeader.top = pos.top;
172
- uiBox.boxHeader.left = pos.left + 2;
173
-
174
- if (typeof panel.widgets !== 'undefined' && panel.widgets !== null) {
175
- for (const [, value] of Object.entries(panel.widgets)) {
176
- if (value.widget === 'log') {
177
- const widget = uiBox.box.get(value.widget) as stsBlessed.element;
178
- if (widget.position.top >= (uiBox.box.height - 4)) {
179
- widget.position.top = uiBox.box.height - 4;
180
- } else {
181
- if (value.options.top) {
182
- widget.position.top = value.options.top;
183
- }
184
- }
185
- }
186
- }
187
- }
188
- };
189
-
190
- /**
191
- * ReSize the grid layout
192
- */
193
- #ReSize = (): void => {
194
- for (const [, value] of Object.entries(this.uiBoxes)) {
195
- this.#UpdateUIBoxPos(value.box.get('panel') as panel, value);
196
- }
197
- }
198
-
199
- #UpdatePanelPosition = (panel: panel, row: number, col: number, uiBox: uiBox): void => {
200
- // Update the panel position within the current screen view
201
- panel.pos = { row: row, col: col, rowSpan: 1, colSpan: 1 };
202
- this.#UpdateUIBoxPos(panel, uiBox);
203
- uiBox.boxHeader.setContent(panel.panelHeader);
204
- }
205
-
206
- /**
207
- *
208
- * Setup all panels
209
- */
210
- #SetupPanels(): void {
211
- if (!this.screen) {
212
- return;
213
- }
214
-
215
- const panelKeys = Object.keys(this.uidata.panels);
216
-
217
- // Check for deleted panels
218
- const uiBoxIds = Object.keys(this.uiBoxes);
219
- for (let i=0; i < uiBoxIds.length; i++) {
220
- if (typeof this.uidata.panels[uiBoxIds[i]] === 'undefined') {
221
- let uiBox: uiBox | null = this.uiBoxes[uiBoxIds[i]];
222
- if (typeof uiBox !== 'undefined') {
223
- this.screen.remove(uiBox.boxHeader);
224
- this.screen.remove(uiBox.box);
225
- uiBox = null;
226
- delete this.uiBoxes[uiBoxIds[i]];
227
- }
228
- }
229
- }
230
-
231
- const touched: Record<string, boolean> = { };
232
-
233
- for (let i=0; i < (this.uidata.grid.cols * this.uidata.grid.rows); i++) {
234
- const gridPos = this.#cursor + i;
235
- const col = i % this.uidata.grid.cols;
236
- const row = Math.floor(i / this.uidata.grid.cols);
237
-
238
- // Get the panel for this position within the grid
239
- const panel: panel = this.uidata.panels[panelKeys[gridPos]];
240
- if (typeof panel !== 'undefined') {
241
- if (typeof this.uiBoxes[panel.id] === 'undefined') {
242
- // Update the panel position within the current screen view
243
- panel.pos = { row: row, col: col, rowSpan: 1, colSpan: 1 };
244
-
245
- const calculatedBoxPos = this.#CalcBoxPos(panel);
246
- if (!calculatedBoxPos) {
247
- return
248
- }
249
- const { top, left, width, height } = this.#CalcBoxPos(panel) as boxPos;
250
-
251
- // Create the UI box
252
- const box: stsBlessed.box = blessed.box({
253
- parent: this.screen,
254
- top: top,
255
- left: left,
256
- width: width,
257
- height: height,
258
- style:
259
- {
260
- bg: '#101010' // was gray
261
- ,fg: 'white'
262
- },
263
- border:
264
- {
265
- type: 'line'
266
- },
267
- keys: false, // Do not allow default key handling for this box
268
- tags: true // Allow style in-line tags such as bolt, italics, etc.
269
- });
270
-
271
- const boxHeader: stsBlessed.listbar = blessed.box({
272
- parent: this.screen,
273
- top: top,
274
- left: left+2,
275
- width: 'shrink',
276
- height: 1,
277
- style:
278
- {
279
- bg: 'gray'
280
- , fg: 'white'
281
- },
282
- keys: false, // Do not allow default key handling for this box
283
- mouse: false,
284
- content: panel.panelHeader,
285
- tags: true // Allow style in-line tags such as bolt, italics, etc.
286
- });
287
-
288
- this.uiBoxes[panel.id] = { box: box, boxHeader: boxHeader };
289
-
290
- boxHeader.setIndex(-1);
291
-
292
- // https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback
293
- box.on('click', () => {
294
- //this.emit('click', box.data.panel);
295
- this.emit('click', box.get('panel') as panel);
296
- });
297
-
298
- if (typeof panel.widgets !== 'undefined' && panel.widgets !== null) {
299
- for (const [, value] of Object.entries(panel.widgets)) {
300
- // Create a fresh copy of the options for this widget instance.
301
- // If this is not done, all widgets will use the same options object and will not be able
302
- // to update screen elements individually.
303
- const widgetoptions = {...value.options};
304
-
305
- // Ensure that we always show at least 2 lines of log data even if this means we position over the top of
306
- // elements above this position.
307
- if (widgetoptions.top && (widgetoptions.top >= (box.height - 3))) {
308
- widgetoptions.top = box.height - 4;
309
- }
310
- const widget: stsBlessed.element = blessed[value.widget].call(this, widgetoptions);
311
- widget.set('id', uuidv4());
312
- box.append(widget);
313
- box.set(value.widget, widget); // Set the user data for this widget. This allows the handlebars template access via the name from the box object.
314
- }
315
- }
316
-
317
- // The entire panel is accessible using the _panel property.
318
- // This is so that the model is accessible from within the handlebars templates.
319
- // Access to a named widget can be done using box._panel.widgets[name] or box._panel.widgets.name
320
- box.set('panel', panel);
321
-
322
- this.uiBoxes[panel.id].box = box;
323
-
324
- touched[panel.id] = true;
325
- } else {
326
- //@@ need to hide previous positions
327
- //@@ and show ones that are visible
328
- this.#UpdatePanelPosition(panel, row, col, this.uiBoxes[panel.id]);
329
-
330
- touched[panel.id] = true;
331
- //this.#uiBox[panel.id].box.show();
332
- }
333
-
334
- // Render the uiBox with the renderPanelFn using the panel as the utility including the model
335
- if (this.#renderPanelFn) {
336
- this.#renderPanelFn(this.uiBoxes[panel.id].box, panel);
337
- }
338
- }
339
- }
340
-
341
- for (const [key, ] of Object.entries(this.uiBoxes)) {
342
- if (typeof touched[key] === 'undefined') {
343
- this.uiBoxes[key].box.hide();
344
- this.uiBoxes[key].boxHeader.hide();
345
- } else {
346
- this.uiBoxes[key].box.show();
347
- this.uiBoxes[key].boxHeader.show();
348
- }
349
- }
350
-
351
- }
352
-
353
- get gridData(): gridData {
354
- return this.uidata;
355
- }
356
-
357
- set gridData(gridData: gridData) {
358
- this.uidata = gridData;
359
- this.#SetupPanels();
360
- this.UpdateCursorInfo();
361
- this.Render();
362
- }
363
-
364
- get gridDataPanels(): panels {
365
- return this.uidata.panels;
366
- }
367
-
368
- set gridDataPanels(gridDataPanels: panels) {
369
- this.uidata.panels = gridDataPanels;
370
- this.#SetupPanels();
371
- this.UpdateCursorInfo();
372
- this.Render();
373
- }
374
-
375
- // https://www.npmjs.com/package/blessed
376
-
377
- // data:= { title: <str>,
378
- // grid: { rows: <int>, cols: <int> },
379
- // menu: [ { top: <bool>, menuitems: [ { text: <string>, key: <string>, cb: <func> } ] } ]
380
- // panels: [ { id: <str>, pos: { row: <int>, col: <int>, rowSpan: <int>, colSpan: <int> }, template: <str> } ] }
381
- /**
382
- *
383
- * @param {*} data UI Data
384
- */
385
- SetupUI = (): void => {
386
- this.DestroyUI();
387
-
388
- if (this.screen === null) {
389
- // Create a screen object.
390
- this.screen = blessed.screen({
391
- smartCSR: true
392
- });
393
- }
394
-
395
- if (!this.screen) {
396
- return;
397
- }
398
-
399
- const data = this.uidata;
400
- this.screen.title = data.title;
401
- this.screen.on('resize', () => {
402
- this.#ReSize();
403
- this.Render();
404
- });
405
-
406
- if (data.menu !== null) {
407
- for (let i = 0; i < data.menu.length; i++) {
408
- new MenuBar.MenuBar(this.screen, data.menu[i]);
409
- }
410
- }
411
-
412
- // Allow the serrvice to terminate by pressing Control-C.
413
- this.screen.key(['C-c'], (): void => {
414
- this.DestroyUI();
415
- this.emit('exit');
416
- // Cleanly shutdown the main process. Exit code success (> 0 is exit in error state)
417
- //setTimeout(() => process.kill(process.pid), 0);
418
- });
419
-
420
- this.screen.key(['escape'], (): void => {
421
- this.emit('escape');
422
- // Cleanly shutdown the main process. Exit code success (> 0 is exit in error state)
423
- //setTimeout(() => process.kill(process.pid), 0);
424
- });
425
-
426
- this.#SetupPanels();
427
-
428
- this.#cursorInfo = blessed.box({
429
- parent: this.screen,
430
- bottom: 0,
431
- right: 0,
432
- width: 'shrink',
433
- height: 1,
434
- style:
435
- {
436
- bg: 'gray'
437
- , fg: 'white'
438
- },
439
- keys: false, // Do not allow default key handling for this box
440
- mouse: false,
441
- content: '',
442
- tags: true // Allow style in-line tags such as bolt, italics, etc.
443
- });
444
-
445
- this.sortInfo = blessed.box({
446
- parent: this.screen,
447
- top: 0,
448
- right: 0,
449
- width: 'shrink',
450
- height: 1,
451
- style:
452
- {
453
- bg: 'gray'
454
- , fg: 'white'
455
- },
456
- keys: false, // Do not allow default key handling for this box
457
- mouse: false,
458
- content: '',
459
- tags: true // Allow style in-line tags such as bolt, italics, etc.
460
- });
461
-
462
-
463
- this.screenHeader = blessed.box({
464
- parent: this.screen,
465
- top: 0,
466
- left: 'center',
467
- width: 'shrink',
468
- height: 1,
469
- style:
470
- {
471
- bg: 'gray'
472
- , fg: 'white'
473
- },
474
- keys: false, // Do not allow default key handling for this box
475
- mouse: false,
476
- content: `[ ... ]`,
477
- tags: true // Allow style in-line tags such as bolt, italics, etc.
478
- });
479
-
480
- this.UpdateCursorInfo();
481
- this.UpdateSortInfo('Default');
482
- this.UpdateScreenHeader(this.screenHeaderText);
483
-
484
- // Render the screen.
485
- this.Render();
486
- }
487
-
488
- UpdateCursorInfo = (): void => {
489
- if (this.#cursorInfo) {
490
- this.#cursorInfo.setContent(`Cursor: ${this.#cursor} / ${this.TotalPanelNumber}`);
491
- }
492
- }
493
-
494
- UpdateSortInfo = (sortMode: string): void => {
495
- if (this.sortInfo) {
496
- this.sortInfo.setContent(`Sort: ${sortMode}`);
497
- }
498
- }
499
-
500
- UpdateScreenHeader = (screenHeaderText: string): void => {
501
- this.screenHeaderText = screenHeaderText;
502
- if (this.screenHeader) {
503
- this.screenHeader.setContent(`[ ${screenHeaderText} ]`);
504
- }
505
- }
506
-
507
- get rows(): number {
508
- return this.uidata.grid.rows;
509
- }
510
-
511
- get cols(): number {
512
- return this.uidata.grid.cols;
513
- }
514
-
515
- IncRows = (): void => {
516
- if (this.screen && (this.screen.height / (this.uidata.grid.rows+1) > this.minHeight)) {
517
- this.uidata.grid.rows++;
518
- this.CheckCursor();
519
- this.#UpdateUI();
520
- }
521
- }
522
-
523
- DecRows = (): void => {
524
- if (this.uidata.grid.rows > this.minRows) {
525
- this.uidata.grid.rows--;
526
- this.CheckCursor();
527
- this.#UpdateUI();
528
- }
529
- }
530
-
531
- IncCols = (): void => {
532
- if (this.screen && (this.screen.width / (this.uidata.grid.cols+1) > this.minWidth)) {
533
- this.uidata.grid.cols++;
534
- this.CheckCursor();
535
- this.#UpdateUI();
536
- }
537
- }
538
-
539
- DecCols = (): void => {
540
- if (this.uidata.grid.cols > this.minCols) {
541
- this.uidata.grid.cols--;
542
- this.CheckCursor();
543
- this.#UpdateUI();
544
- }
545
- }
546
-
547
- get ScreenCells(): number {
548
- return this.uidata.grid.cols * this.uidata.grid.rows;
549
- }
550
-
551
- get TotalPanelNumber(): number {
552
- return Object.keys(this.uidata.panels).length;
553
- }
554
-
555
- CheckCursor = (): void => {
556
- if (this.#cursor > (this.TotalPanelNumber - this.ScreenCells)) {
557
- this.#cursor = this.TotalPanelNumber - this.ScreenCells;
558
- if (this.#cursor < 0) {
559
- this.#cursor = 0;
560
- }
561
- }
562
- this.UpdateCursorInfo();
563
- }
564
-
565
- NextPage = (): void => {
566
- const endpos = this.TotalPanelNumber - this.ScreenCells;
567
- if (this.#cursor < endpos) {
568
- this.#cursor += this.ScreenCells;
569
- if (this.#cursor > endpos) {
570
- this.#cursor = endpos;
571
- }
572
- this.#UpdateUI();
573
- }
574
- }
575
-
576
- PrevPage = (): void => {
577
- if (this.#cursor > 0) {
578
- this.#cursor -= this.ScreenCells;
579
- if (this.#cursor < 0) {
580
- this.#cursor = 0;
581
- }
582
- this.#UpdateUI();
583
- }
584
- }
585
-
586
- #UpdateUI(): void {
587
- this.SetupUI();
588
-
589
- //@@ fix screen render - should not need to blow entire screen away ...
590
- //this.#SetupPanels();
591
- //this.UpdateCursorInfo();
592
- //this.Render();
593
- }
594
-
595
- Exit = (): void => {
596
- this.DestroyUI();
597
- this.emit('exit');
598
- }
599
-
600
- /**
601
- *
602
- */
603
- DestroyUI = (): void => {
604
- // Destroy the screen and disable UI log processing
605
- if (this.screen !== null) {
606
- this.uiBoxes = { };
607
- this.screen.destroy();
608
- this.screen = null;
609
- }
610
- }
611
-
612
- /**
613
- * Render the screen.
614
- */
615
- Render = (): void => {
616
- if (this.screen) {
617
- this.screen.render();
618
- }
619
- }
620
- }