1 | import { cursor } from '@arpel/escape'
|
2 | import { Escape } from '../util/Escape.js'
|
3 | import { IO } from '../util/IO.js'
|
4 | import { Config } from './Config.js'
|
5 | import { Layout } from './Layout.js'
|
6 | import { State } from './State.js'
|
7 |
|
8 |
|
9 | export class Baro {
|
10 | config
|
11 | format
|
12 | states = []
|
13 | escape = Escape.build({
|
14 | fn: this.#renderStates,
|
15 | ctx: this,
|
16 | arg: this.states
|
17 | })
|
18 | #locker = null
|
19 | offset = 0
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 | constructor(config, layout) {
|
26 | this.config = config
|
27 | this.layout = layout
|
28 | this.io = IO.build(this.config)
|
29 | }
|
30 |
|
31 | static build(config, layout) {
|
32 | config = Config.build(config)
|
33 | layout = Layout.build(layout)
|
34 | return new Baro(config, layout)
|
35 | }
|
36 |
|
37 | get active() { return this.escape.active }
|
38 |
|
39 | get forceRedraw() {
|
40 | return this.config.forceRedraw || ( this.config.noTTYOutput && !this.io.isTTY )
|
41 | }
|
42 |
|
43 | async start() {
|
44 | const { io } = this
|
45 | io.input.resume()
|
46 | if (this.config.hideCursor) io.showCursor(false)
|
47 | if (!this.config.lineWrap) io.setLineWrap(false)
|
48 |
|
49 | const height = this.io.height
|
50 | this.io.output.write(cursor.nextLine(height) + cursor.prevLine(height))
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | this.escape.loop(this.config.throttle)
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 | async append(state) {
|
70 | if (this.#locker) await this.#locker // console.debug('>>', state.agent, 'waiting for occupy')
|
71 | const taskPromise = Promise
|
72 | .resolve()
|
73 | .then(async () => {
|
74 | state.last = Number.NEGATIVE_INFINITY
|
75 | this.states.push(state)
|
76 | if (!this.escape.active && this.states.length) await this.start()
|
77 | })
|
78 | this.#locker = taskPromise.then(() => this.#locker = null)
|
79 | return state
|
80 | }
|
81 |
|
82 |
|
83 | remove(state) {
|
84 | const index = this.states.indexOf(state)
|
85 | if (index < 0) return false
|
86 | this.states.splice(index, 1)
|
87 | this.io.nextLine()
|
88 | this.io.clearDown()
|
89 | return true
|
90 | }
|
91 |
|
92 | async stop() {
|
93 | this.escape.stop()
|
94 | if (this.config.hideCursor) { this.io.showCursor(true) }
|
95 | if (!this.config.lineWrap) { this.io.setLineWrap(true) }
|
96 | if (this.config.autoClear) {
|
97 | this.io.resetCursor()
|
98 | this.io.clearDown()
|
99 | }
|
100 | else {
|
101 |
|
102 | await this.#renderStates(this.states)
|
103 | }
|
104 |
|
105 | this.io.input.pause()
|
106 | }
|
107 |
|
108 | async #renderStates(states) {
|
109 | const { io } = this
|
110 | const height = io.height - 1
|
111 | const [ x, y ] = await io.asyncCursorPos()
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | io.offsetLines(-Math.min(this.offset, height))
|
122 | this.offset = 0
|
123 | if (height) {
|
124 | for (const state of states.slice(-( height ))) {
|
125 | if (this.forceRedraw || ( state.value !== state.last )) {
|
126 | io.writeOff(`CURSOR (${x}, ${y}) OFFSET (${this.offset}) TERM (${io.size}) ` + this.layout.format(state))
|
127 | state.last = state.value
|
128 | }
|
129 | io.nextLine()
|
130 | this.offset++
|
131 | }
|
132 | }
|
133 | if (this.config.autoStop && states.every(state => state.reachLimit)) {
|
134 | await this.stop()
|
135 | }
|
136 | }
|
137 | }
|