@dotrino/terminal-agent 0.13.0 → 0.15.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.
package/bin/terminal.js CHANGED
@@ -55,8 +55,8 @@ if (args.includes('-h') || args.includes('--help')) {
55
55
  dotrino-terminal rename <perfil> [nuevo] renombra un perfil (para su agente: cierra sus consolas)
56
56
 
57
57
  Dentro de una consola: Ctrl+] y luego d la suelta sin cerrarla; Ctrl+] n abre otra
58
- (y suelta la actual); Ctrl+] a <id> Enter pasa a esa consola; Ctrl+] r ajusta la consola al
59
- tamaño de esta ventana.
58
+ (y suelta la actual); Ctrl+] a <id> Enter pasa a esa consola; Ctrl+] p fija el tamaño de la
59
+ consola a esta ventana y Ctrl+] u lo suelta.
60
60
  Cerrar la ventana cierra la consola que abrió.`, `usage:
61
61
  dotrino-terminal [--name <n>] [--cwd <dir>] open a new console in this window, in the current folder
62
62
  dotrino-terminal attach <id> [--name <n>] attach to an open console
@@ -67,8 +67,8 @@ Cerrar la ventana cierra la consola que abrió.`, `usage:
67
67
  dotrino-terminal rename <profile> [new] rename a profile (stops its agent: closes its consoles)
68
68
 
69
69
  Inside a console: Ctrl+] then d detaches without closing it; Ctrl+] n opens another
70
- (detaching the current one); Ctrl+] a <id> Enter switches to that console; Ctrl+] r fits the
71
- console to this window's size.
70
+ (detaching the current one); Ctrl+] a <id> Enter switches to that console; Ctrl+] p pins the
71
+ console's size to this window and Ctrl+] u unpins it.
72
72
  Closing the window closes the console it opened.`))
73
73
  process.exit(0)
74
74
  }
@@ -380,6 +380,8 @@ function interactive (conn, first, dir) {
380
380
  // «r»: esta ventana manda en el tamaño de la consola (la app la usa al ganar el foco: con
381
381
  // varias ventanas de tamaños distintos en la misma consola, se ajusta a la que miras).
382
382
  if (ch === 'r') { conn.send({ type: 'resize', ...size() }); continue }
383
+ // «p» / «u»: esta ventana fija (⤢) o suelta el tamaño de la consola.
384
+ if (ch === 'p' || ch === 'u') { conn.send({ type: 'pin', on: ch === 'p' }); if (ch === 'p') conn.send({ type: 'resize', ...size() }); continue }
383
385
  if (ch === 'n') {
384
386
  if (send) { input(send); send = '' }
385
387
  switchTo({ type: 'open', cwd: first.cwd || path.resolve(process.cwd()) })
package/consoles.js CHANGED
@@ -37,7 +37,14 @@ class Console {
37
37
  this.rows = rows
38
38
  this.createdAt = Date.now()
39
39
  this.lastActive = Date.now()
40
- this.viewers = new Set() // sesiones mirando: { onOut(data), onExit(code), onMeta?(info), origin, device? }
40
+ this.viewers = new Set() // sesiones mirando: { onOut(data), onExit(code), onMeta?(info), origin, device?, tag?, size? }
41
+ // QUIÉN MANDA EN EL TAMAÑO. `pinKey`: la pantalla que lo eligió a propósito (⤢ en la app o el teléfono);
42
+ // si nadie, `holder`: el último que se enganchó. Solo ese lo cambia, y se sigue su pantalla
43
+ // (redimensionar la ventana, girar el teléfono). Escribir o dar foco no cambia nada.
44
+ // `pinKey` recuerda QUÉ PANTALLA lo eligió (ventana o aparato), no su conexión: si esa pantalla
45
+ // pasa a otra consola y vuelve, lo recupera. Mientras no está, manda el último que llegó.
46
+ this.pinKey = null
47
+ this.holder = null
41
48
  this.exited = false
42
49
  this.screen = new Terminal({ cols, rows, scrollback: SCROLLBACK, allowProposedApi: true })
43
50
  this.serializer = new SerializeAddon()
@@ -94,7 +101,46 @@ class Console {
94
101
  })
95
102
  }
96
103
 
97
- detach (viewer) { if (this.viewers.delete(viewer)) this._meta() }
104
+ detach (viewer) {
105
+ if (!this.viewers.has(viewer)) return
106
+ // Quién decidía, ANTES de quitarlo: si era él, hay que pasar el tamaño a otro.
107
+ const before = this.decider()
108
+ this.viewers.delete(viewer)
109
+ // Si se fue el último que llegó, lo es el último de los que quedan.
110
+ if (this.holder === viewer) this.holder = [...this.viewers].filter((v) => v.size).pop() || null
111
+ // Si cambió quién decide (se fue el que lo tenía, fijado o no), manda su tamaño.
112
+ const d = this.decider()
113
+ if (d !== before && d?.size) this.resize(d.size.cols, d.size.rows)
114
+ this._meta()
115
+ }
116
+
117
+ /** Quién decide el tamaño ahora. */
118
+ decider () { return this.pinnedViewer() || this.holder }
119
+
120
+ /** La pantalla que eligió el tamaño, si está mirando ahora. */
121
+ pinnedViewer () {
122
+ if (!this.pinKey) return null
123
+ return [...this.viewers].filter((v) => viewerKey(v) === this.pinKey).pop() || null
124
+ }
125
+
126
+ /**
127
+ * `viewer` dice su tamaño (al engancharse, `attaching`, o porque cambió su pantalla). Se aplica
128
+ * solo si es quien decide; engancharse lo hace decidir si nadie lo tiene fijado.
129
+ */
130
+ sizeFrom (viewer, { attaching = false } = {}) {
131
+ if (attaching) this.holder = viewer
132
+ if (this.decider() === viewer && viewer.size) this.resize(viewer.size.cols, viewer.size.rows)
133
+ else this._meta()
134
+ }
135
+
136
+ /** ⤢: `viewer` fija (o suelta) el tamaño a su pantalla. */
137
+ pin (viewer, on) {
138
+ if (on) this.pinKey = viewerKey(viewer)
139
+ else if (this.pinKey === viewerKey(viewer)) this.pinKey = null
140
+ const d = this.decider()
141
+ if (d?.size) this.resize(d.size.cols, d.size.rows)
142
+ this._meta()
143
+ }
98
144
 
99
145
  /**
100
146
  * `watchers` dice QUIÉN mira: una ventana de esta máquina o un aparato de la cuenta (con
@@ -103,10 +149,15 @@ class Console {
103
149
  */
104
150
  info () {
105
151
  const watchers = [...this.viewers].filter((v) => v.origin).map((v) => ({ origin: v.origin, device: v.device || null, tag: v.tag || null }))
106
- return { id: this.id, n: this.n, origin: this.origin, title: this.title, cols: this.cols, rows: this.rows, createdAt: this.createdAt, lastActive: this.lastActive, viewers: this.viewers.size, watchers }
152
+ const d = this.decider()
153
+ const sizeBy = d ? { origin: d.origin || null, device: d.device || null, tag: d.tag || null, pinned: !!d && d === this.pinnedViewer() } : null
154
+ return { id: this.id, n: this.n, sizeBy, origin: this.origin, title: this.title, cols: this.cols, rows: this.rows, createdAt: this.createdAt, lastActive: this.lastActive, viewers: this.viewers.size, watchers }
107
155
  }
108
156
  }
109
157
 
158
+ /** Qué pantalla es un `viewer`: su ventana (`tag`) o su aparato, sin importar la conexión. */
159
+ function viewerKey (v) { return `${v.origin || ''}|${v.device || ''}|${v.tag || ''}` }
160
+
110
161
  /** Las consolas de este agente. */
111
162
  export class ConsoleHub {
112
163
  /**
package/index.js CHANGED
@@ -12,7 +12,7 @@
12
12
  * Payloads de dominio (van cifrados dentro de la sesión, el proxio no los ve):
13
13
  * cliente → agente: { type:'list' } · { type:'open', cols, rows, cwd?, tag? } ·
14
14
  * { type:'attach', id, cols, rows } · { type:'detach' } ·
15
- * { type:'input', data } · { type:'resize', cols, rows } ·
15
+ * { type:'input', data } · { type:'resize', cols, rows } · { type:'pin', on } ·
16
16
  * { type:'close' } (mata la consola enganchada) · { type:'kill', id }
17
17
  * agente → cliente: { type:'consoles', list } · { type:'attached', id, fresh } ·
18
18
  * { type:'replay', id, data, last } · { type:'out', data } ·
@@ -70,9 +70,9 @@ export function makeHub (pty, opts = {}) {
70
70
  * ella: cerrar la ventana mata su shell, como en cualquier terminal. Una ventana que solo
71
71
  * se enganchó a una consola ajena, o que la soltó a propósito (`detach`), no mata nada.
72
72
  *
73
- * Tamaño con varios mirando: manda el último que se enganchó (o abrió). Escribir lo cambia solo
74
- * desde una ventana de esta máquina; desde otro aparato (la PWA), solo al engancharse o con su
75
- * botón «Ajustar a esta pantalla» (un `resize` explícito).
73
+ * Tamaño con varios mirando: lo decide quien lo FIJÓ (`pin`, el ⤢ de la app o el teléfono) y, si
74
+ * nadie, el último que se enganchó. Solo esa pantalla lo cambia (sus `resize` se siguen: girar el
75
+ * teléfono, redimensionar la ventana). Escribir o dar foco no cambia nada.
76
76
  * Exportada para las pruebas.
77
77
  *
78
78
  * @param {object} session
@@ -92,7 +92,7 @@ export function serveSession (session, hub, { origin = 'remote' } = {}) {
92
92
  }
93
93
  const release = () => { if (current) { current.detach(viewer); current = null } }
94
94
  const fail = (code, message) => session.send({ type: 'fail', code, message })
95
- const takeSize = (msg) => { if (msg.cols && msg.rows) size = { cols: msg.cols, rows: msg.rows } }
95
+ const takeSize = (msg) => { if (msg.cols && msg.rows) { size = { cols: msg.cols, rows: msg.rows }; viewer.size = size } }
96
96
  // La etiqueta con la que se presenta quien mira (una ventana de la app de escritorio).
97
97
  const takeTag = (msg) => { if (typeof msg.tag === 'string') viewer.tag = msg.tag.slice(0, 64) }
98
98
 
@@ -100,6 +100,7 @@ export function serveSession (session, hub, { origin = 'remote' } = {}) {
100
100
  release()
101
101
  current = c
102
102
  const snapshot = await c.attach(viewer)
103
+ c.sizeFrom(viewer, { attaching: true })
103
104
  // La pantalla va en trozos: el proxio corta los mensajes a 1 MB.
104
105
  for (let i = 0; i < snapshot.length || i === 0; i += REPLAY_CHUNK) {
105
106
  await session.send({ type: 'replay', id: c.id, data: snapshot.slice(i, i + REPLAY_CHUNK), last: i + REPLAY_CHUNK >= snapshot.length })
@@ -126,18 +127,18 @@ export function serveSession (session, hub, { origin = 'remote' } = {}) {
126
127
  if (!c) return fail('no-console', 'that console no longer exists')
127
128
  takeSize(msg)
128
129
  takeTag(msg)
129
- c.resize(size.cols, size.rows)
130
130
  attachTo(c, { fresh: false })
131
131
  return
132
132
  }
133
133
  if (msg.type === 'detach') { owned = null; release(); return }
134
134
  if (msg.type === 'input') {
135
135
  if (!current) return
136
- if (origin === 'local' && (current.cols !== size.cols || current.rows !== size.rows)) current.resize(size.cols, size.rows)
137
136
  current.write(String(msg.data ?? ''))
138
137
  return
139
138
  }
140
- if (msg.type === 'resize') { takeSize(msg); current?.resize(size.cols, size.rows); return }
139
+ if (msg.type === 'resize') { takeSize(msg); current?.sizeFrom(viewer); return }
140
+ // ⤢ «Esta pantalla manda en el tamaño» (on) o soltarlo (off).
141
+ if (msg.type === 'pin') { current?.pin(viewer, !!msg.on); return }
141
142
  if (msg.type === 'close') { if (current) hub.kill(current.id); return }
142
143
  if (msg.type === 'kill') { if (!hub.kill(msg.id)) fail('no-console', 'that console no longer exists') }
143
144
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/terminal-agent",
3
- "version": "0.13.0",
3
+ "version": "0.15.0",
4
4
  "description": "Dotrino Terminal: las ventanas de esta máquina (dotrino-terminal) y su agente, que deja abrir esas mismas consolas desde los otros aparatos de tu cuenta, cifrado punto a punto.",
5
5
  "type": "module",
6
6
  "bin": {