@muwi/herdr-app 0.0.0-stage → 0.7.1
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/LICENSE +21 -0
- package/README.md +149 -2
- package/app/muwi/arc.svg +3 -0
- package/app/muwi/main.js +1744 -0
- package/app/muwi/mod.json +1 -0
- package/bin/muwi.js +10 -0
- package/package.json +27 -4
- package/src/commands.js +286 -0
- package/src/hc.js +55 -0
- package/src/host.js +54 -0
- package/src/install.js +61 -0
- package/src/layout.js +171 -0
- package/src/live.js +71 -0
- package/src/preview.js +106 -0
- package/src/resume.js +68 -0
- package/src/screen.js +545 -0
package/src/screen.js
ADDED
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
// Herdr 앱 화면(레이아웃)에 muwi 칸을 넣고 빼는 곳 (muwi layout).
|
|
2
|
+
// 사이드바의 Agents 칸 자리 → muwi 작업 목록 (variant list)
|
|
3
|
+
// 터미널 영역 아래 → muwi 그래프 (variant graph. 목록을 넣지 못했으면 목록까지 있는 full)
|
|
4
|
+
// 사람이 쓰던 레이아웃은 고치지 않는다. 지금 레이아웃을 복제(layout.clone)하고 복제본만 바꾼다.
|
|
5
|
+
// 되돌리면 원래 레이아웃으로 바꾸고 복제본을 지운다. 사람이 복제본을 손으로 고쳤으면 지우지 않는다.
|
|
6
|
+
import crypto from 'node:crypto';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { herdrApp } from './hc.js';
|
|
11
|
+
import { layoutPreview } from './preview.js';
|
|
12
|
+
import { readJson, writeJson } from '@muwi/core/store';
|
|
13
|
+
|
|
14
|
+
export const LAYOUT_NAME = 'muwi';
|
|
15
|
+
export const GRAPH_ID = 'muwi-graph';
|
|
16
|
+
/** 터미널 영역과 그래프를 담는 세로 묶음. 이 묶음 안의 경계는 끌어서 높이를 바꿀 수 있다 */
|
|
17
|
+
export const SPLIT_ID = 'muwi-split';
|
|
18
|
+
/**
|
|
19
|
+
* muwi 칸을 넣는 배치의 판. 배치를 바꾸면 올린다. 옛 판으로 만든 복제본은 muwi layout이 다시 만든다
|
|
20
|
+
* 1: 그래프를 터미널 영역 아래에 끼워 넣음 (높이 고정)
|
|
21
|
+
* 2: 터미널 영역과 그래프를 끌 수 있는 경계로 나눔
|
|
22
|
+
*/
|
|
23
|
+
export const PLACE_VERSION = 2;
|
|
24
|
+
const GRAPH_PX = 300;
|
|
25
|
+
const GRAPH_SIDE_PX = 480;
|
|
26
|
+
|
|
27
|
+
/** 그래프 칸의 자리: 터미널 영역의 아래, 위, 오른쪽 */
|
|
28
|
+
|
|
29
|
+
export const GRAPH_AT = ['bottom', 'top', 'right'];
|
|
30
|
+
/** Herdr가 직접 내는 레이아웃. 업데이트되면 그 모양을 따라 다시 복제할 수 있다 */
|
|
31
|
+
const SHIPPED = new Set(['main', 'terminals', 'win98']);
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/** muwi layout이 바꾼 것. 되돌리기와 다시 맞추기에 쓴다 */
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
const recordFile = (dir ) => path.join(dir, 'layout.json');
|
|
65
|
+
export const readLayoutRecord = (dir ) => readJson (recordFile(dir));
|
|
66
|
+
|
|
67
|
+
const hashOf = (text ) => crypto.createHash('sha256').update(text).digest('hex').slice(0, 16);
|
|
68
|
+
|
|
69
|
+
/** 키 순서와 상관없이 같은 나무면 같은 글이 되게 (Herdr가 layout.set으로 파일을 다시 쓰면 키 순서가 바뀐다) */
|
|
70
|
+
function stable(v ) {
|
|
71
|
+
if (Array.isArray(v)) return v.map(stable);
|
|
72
|
+
if (v && typeof v === 'object') return Object.fromEntries(Object.keys(v ).sort().map(k => [k, stable((v )[k])]));
|
|
73
|
+
return v;
|
|
74
|
+
}
|
|
75
|
+
export const treeHash = (tree ) => hashOf(JSON.stringify(stable(tree)));
|
|
76
|
+
|
|
77
|
+
/** 복제본이 우리가 마지막으로 쓴 그대로인가. 예전 기록(hashKind 없음)은 파일 글자로 비교한다 */
|
|
78
|
+
function untouched(record , file ) {
|
|
79
|
+
return record.hashKind === 'tree' ? treeHash(file.tree) === record.hash : hashOf(file.text) === record.hash;
|
|
80
|
+
}
|
|
81
|
+
/** Herdr 앱의 설정 폴더 (setup.ts의 places와 같은 규칙) */
|
|
82
|
+
const herdrHome = (env ) => env.HERDR_APP_HOME || path.join(env.HOME || os.homedir(), '.config', 'herdr-app');
|
|
83
|
+
const layoutsDir = (env ) => path.join(herdrHome(env), 'layouts');
|
|
84
|
+
const layoutPath = (env , name ) => path.join(layoutsDir(env), `${name}.json`);
|
|
85
|
+
/** Herdr 설정 화면이 이 레이아웃의 그림으로 보여 줄 파일 (레이아웃 파일 옆) */
|
|
86
|
+
const previewPath = (env , name ) => path.join(layoutsDir(env), `${name}.svg`);
|
|
87
|
+
|
|
88
|
+
/** 설정 화면에 보일 그림을 그래프 칸의 자리대로 그려 둔다. 못 그렸으면 false */
|
|
89
|
+
function drawPreview(env , name , where ) {
|
|
90
|
+
try {
|
|
91
|
+
fs.writeFileSync(previewPath(env, name), layoutPreview(where));
|
|
92
|
+
return true;
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 그림을 그려 두고 레이아웃 나무에 그 그림을 적는다 ("preview": "layouts/<이름>.svg", Herdr 폴더에서 본 경로).
|
|
100
|
+
* 그림이 없으면 설정 화면은 빈 틀만 그린다. 그림을 못 그려도 레이아웃은 그대로 쓴다
|
|
101
|
+
*/
|
|
102
|
+
function withPreview(env , name , tree , where ) {
|
|
103
|
+
const { preview: _old, ...rest } = tree;
|
|
104
|
+
return drawPreview(env, name, where) ? { ...rest, preview: `layouts/${name}.svg` } : rest;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function walk(node , visit ) {
|
|
108
|
+
visit(node);
|
|
109
|
+
for (const child of node.children ?? []) walk(child, visit);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 조건에 맞는 첫 노드까지의 길 (뿌리부터) */
|
|
113
|
+
function trailTo(node , match , trail = []) {
|
|
114
|
+
const here = [...trail, node];
|
|
115
|
+
if (match(node)) return here;
|
|
116
|
+
for (const child of node.children ?? []) {
|
|
117
|
+
const found = trailTo(child, match, here);
|
|
118
|
+
if (found) return found;
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 레이아웃에 muwi 칸이 들어 있는가 */
|
|
124
|
+
export function hasMuwi(tree ) {
|
|
125
|
+
let found = false;
|
|
126
|
+
walk(tree, n => {
|
|
127
|
+
if (n.content?.mod === 'muwi') found = true;
|
|
128
|
+
});
|
|
129
|
+
return found;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 레이아웃 나무에 muwi 칸을 넣은 새 나무를 돌려준다. 넣을 자리를 찾지 못한 쪽은 false.
|
|
134
|
+
* list: Agents 칸(content.mod "agents")의 내용을 muwi 작업 목록으로 바꿨다. 칸의 자리·크기는 그대로다
|
|
135
|
+
* graph: 터미널 영역(content.tab "active")을 담은 칸 아래에 그래프 칸을 넣었다. 둘을 새 세로 묶음으로 감싸서
|
|
136
|
+
* 그 사이 경계를 끌어 높이를 바꿀 수 있게 한다 (Herdr의 기본 레이아웃은 작업 영역의 경계를 끌 수 없게 해 둔다)
|
|
137
|
+
*/
|
|
138
|
+
export function placeMuwi(tree , where = 'bottom') {
|
|
139
|
+
const root = structuredClone(tree);
|
|
140
|
+
let list = false;
|
|
141
|
+
walk(root, n => {
|
|
142
|
+
if (!list && n.type === 'block' && n.content?.mod === 'agents') {
|
|
143
|
+
n.content = { mod: 'muwi', props: { variant: 'list' } };
|
|
144
|
+
list = true;
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const trail = trailTo(root, n => n.type === 'block' && n.content?.tab === 'active');
|
|
149
|
+
let graph = false;
|
|
150
|
+
if (trail && trail.length > 1) {
|
|
151
|
+
const side = where === 'right';
|
|
152
|
+
const block = {
|
|
153
|
+
id: GRAPH_ID,
|
|
154
|
+
type: 'block',
|
|
155
|
+
size: { px: side ? GRAPH_SIDE_PX : GRAPH_PX },
|
|
156
|
+
min: side ? 280 : 140,
|
|
157
|
+
max: side ? 1400 : 900,
|
|
158
|
+
content: { mod: 'muwi', props: { variant: list ? 'graph' : 'full' } },
|
|
159
|
+
};
|
|
160
|
+
// 터미널 영역을 담은 가장 가까운 세로 묶음에서 터미널 영역 쪽 칸을 고른다 (오른쪽 패널이 있으면 그것까지).
|
|
161
|
+
// 세로 묶음이 없으면 터미널 영역 칸 자체를 고른다
|
|
162
|
+
let parent = trail[trail.length - 2];
|
|
163
|
+
let target = trail[trail.length - 1];
|
|
164
|
+
for (let i = trail.length - 2; i >= 0; i--) {
|
|
165
|
+
if (trail[i].type !== 'column') continue;
|
|
166
|
+
parent = trail[i];
|
|
167
|
+
target = trail[i + 1];
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
// 고른 칸과 그래프를 끌 수 있는 경계로 나눈 묶음으로 감싼다 (아래·위는 세로, 오른쪽은 가로).
|
|
171
|
+
// 묶음은 고른 칸의 자리와 크기를 물려받는다
|
|
172
|
+
const at = parent.children .indexOf(target);
|
|
173
|
+
const { size, min, max, ...rest } = target;
|
|
174
|
+
const main = { ...rest, size: { weight: 1 }, min: side ? 320 : 120 };
|
|
175
|
+
parent.children [at] = {
|
|
176
|
+
id: SPLIT_ID,
|
|
177
|
+
type: side ? 'row' : 'column',
|
|
178
|
+
resizable: true,
|
|
179
|
+
divider: { width: 1, color: 'border' },
|
|
180
|
+
...(size !== undefined ? { size } : {}),
|
|
181
|
+
...(min !== undefined ? { min } : {}),
|
|
182
|
+
...(max !== undefined ? { max } : {}),
|
|
183
|
+
children: where === 'top' ? [block, main] : [main, block],
|
|
184
|
+
};
|
|
185
|
+
graph = true;
|
|
186
|
+
}
|
|
187
|
+
return { tree: root, list, graph };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
function appVersion(env ) {
|
|
211
|
+
const r = herdrApp(['--version'], env, 5000);
|
|
212
|
+
return r.ok ? r.out.trim().split(/\s+/).pop() : undefined;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function readTree(env , name ) {
|
|
216
|
+
try {
|
|
217
|
+
const text = fs.readFileSync(layoutPath(env, name), 'utf8');
|
|
218
|
+
return { tree: JSON.parse(text) , text };
|
|
219
|
+
} catch {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function shownLayout(env ) {
|
|
225
|
+
const r = herdrApp(['status'], env, 5000);
|
|
226
|
+
if (!r.ok) return null;
|
|
227
|
+
try {
|
|
228
|
+
const name = JSON.parse(r.out)?.layout;
|
|
229
|
+
return typeof name === 'string' && name ? name : null;
|
|
230
|
+
} catch {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 지금 화면 상태. Herdr 앱이 닫혀 있으면 app이 false다 */
|
|
236
|
+
export function screenState(dir , env = process.env) {
|
|
237
|
+
const record = readLayoutRecord(dir);
|
|
238
|
+
const shown = shownLayout(env);
|
|
239
|
+
const base = { app: Boolean(shown), muwi: false, canList: false, canGraph: false, record, outdated: false, edited: false };
|
|
240
|
+
if (record) {
|
|
241
|
+
const file = readTree(env, record.name);
|
|
242
|
+
base.edited = Boolean(file && !untouched(record, file));
|
|
243
|
+
}
|
|
244
|
+
if (!shown) return base;
|
|
245
|
+
const version = appVersion(env);
|
|
246
|
+
const file = readTree(env, shown);
|
|
247
|
+
if (!file) return { ...base, shown, version };
|
|
248
|
+
const placed = placeMuwi(file.tree);
|
|
249
|
+
const ours = Boolean(record && record.name === shown);
|
|
250
|
+
const herdrMoved = ours && SHIPPED.has(record .base) && Boolean(version && record .version && version !== record .version);
|
|
251
|
+
const muwiMoved = ours && (record .shape ?? 1) < PLACE_VERSION;
|
|
252
|
+
return {
|
|
253
|
+
...base,
|
|
254
|
+
shown,
|
|
255
|
+
version,
|
|
256
|
+
muwi: hasMuwi(file.tree),
|
|
257
|
+
canList: placed.list,
|
|
258
|
+
canGraph: placed.graph,
|
|
259
|
+
outdated: herdrMoved || muwiMoved,
|
|
260
|
+
...(herdrMoved ? { outdatedBy: 'herdr' } : muwiMoved ? { outdatedBy: 'muwi' } : {}),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export class ScreenError extends Error {}
|
|
265
|
+
|
|
266
|
+
const pause = (ms ) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
267
|
+
|
|
268
|
+
/** 앱이 새 레이아웃 파일을 다 쓸 때까지 잠깐 기다린다 (쓰는 도중이면 JSON이 아직 깨져 있다) */
|
|
269
|
+
function waitForTree(env , name , ms = 3000) {
|
|
270
|
+
for (let waited = 0; waited <= ms; waited += 100) {
|
|
271
|
+
const got = readTree(env, name);
|
|
272
|
+
if (got) return got;
|
|
273
|
+
pause(100);
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** 쓴 레이아웃을 앱이 받아들였는지 본다. 받아들이지 않으면 <이름>.error에 이유를 적는다 */
|
|
279
|
+
function layoutError(env , name , since ) {
|
|
280
|
+
for (const file of [path.join(herdrHome(env), 'layout.error'), path.join(layoutsDir(env), `${name}.error`)]) {
|
|
281
|
+
try {
|
|
282
|
+
if (fs.statSync(file).mtimeMs < since) continue;
|
|
283
|
+
return fs.readFileSync(file, 'utf8').trim() || '이유 없음';
|
|
284
|
+
} catch {
|
|
285
|
+
// 파일이 없으면 받아들인 것이다
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* 레이아웃 파일을 그 자리에서 고친다. 앱이 바로 다시 읽으므로 레이아웃을 바꾸지 않아도 된다.
|
|
293
|
+
* layout.set은 쓰지 않는다: 그 레이아웃에서 사람이 고른 것(테마, 칸 크기, 고른 탭. layouts/.state)을 모두 지운다.
|
|
294
|
+
* 파일을 고치면 고친 칸의 값만 파일을 따르고 나머지는 그대로 남는다.
|
|
295
|
+
* 앱이 받아들이지 않으면 before(전 내용)로 되돌리고 그 이유를 돌려준다
|
|
296
|
+
*/
|
|
297
|
+
function writeLayout(env , name , tree , before ) {
|
|
298
|
+
const file = layoutPath(env, name);
|
|
299
|
+
const put = (text ) => {
|
|
300
|
+
const tmp = `${file}.muwi-tmp`;
|
|
301
|
+
fs.writeFileSync(tmp, text);
|
|
302
|
+
fs.renameSync(tmp, file);
|
|
303
|
+
};
|
|
304
|
+
const since = Date.now();
|
|
305
|
+
put(JSON.stringify(tree, null, 2) + '\n');
|
|
306
|
+
pause(1200);
|
|
307
|
+
const problem = layoutError(env, name, since);
|
|
308
|
+
if (problem && before !== undefined) put(before);
|
|
309
|
+
return problem;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** 그래프 칸이 가로·세로를 바꿨으면 그 방향의 기본 크기로 맞춘다 */
|
|
313
|
+
function fitGraph(env , was , where ) {
|
|
314
|
+
if ((was === 'right') === (where === 'right')) return;
|
|
315
|
+
herdrApp(['do', 'layout.resize', JSON.stringify({ id: GRAPH_ID, px: where === 'right' ? GRAPH_SIDE_PX : GRAPH_PX })], env);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function freeName(env , record ) {
|
|
319
|
+
const ours = (name ) => {
|
|
320
|
+
if (record?.name !== name) return false;
|
|
321
|
+
const file = readTree(env, name);
|
|
322
|
+
return !file || untouched(record, file);
|
|
323
|
+
};
|
|
324
|
+
for (let i = 1; i < 50; i++) {
|
|
325
|
+
const name = i === 1 ? LAYOUT_NAME : `${LAYOUT_NAME}-${i}`;
|
|
326
|
+
if (!fs.existsSync(layoutPath(env, name)) || ours(name)) return name;
|
|
327
|
+
}
|
|
328
|
+
throw new ScreenError('muwi 레이아웃 이름을 정하지 못했어요. layouts 폴더의 muwi-* 파일을 정리해 주세요.');
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* 지금 레이아웃을 복제해 muwi 칸을 넣고 그 레이아웃으로 바꾼다.
|
|
335
|
+
* 이미 muwi 칸이 있으면 그대로 둔다. 우리 복제본이 옛 모양이면(Herdr 업데이트, muwi 배치 변경) 원래 레이아웃의
|
|
336
|
+
* 새 모양으로, 그래프 자리를 바꾸라고 하면(graphAt) 그 자리로, 복제본 파일을 그 자리에서 고친다.
|
|
337
|
+
*/
|
|
338
|
+
export function applyLayout(dir , env = process.env, o = {}) {
|
|
339
|
+
const state = screenState(dir, env);
|
|
340
|
+
if (!state.app || !state.shown) throw new ScreenError('Herdr 앱이 열려 있지 않거나 herdr-app 명령이 없어요. Herdr 앱을 켠 뒤에 다시 실행해요.');
|
|
341
|
+
const ours = Boolean(state.record && state.record.name === state.shown);
|
|
342
|
+
const was = state.record?.graphAt ?? 'bottom';
|
|
343
|
+
// 자리를 말하지 않으면 전에 고른 자리를 쓴다
|
|
344
|
+
const where = o.graphAt ?? state.record?.graphAt ?? 'bottom';
|
|
345
|
+
const base = state.shown;
|
|
346
|
+
// 그래프 자리만 바꾸는 것이면 지금 레이아웃에서 그 칸만 옮긴다. 레이아웃을 바꾸지 않으니 화면이 번쩍이지 않는다
|
|
347
|
+
if (state.muwi && ours && where !== was && !state.outdated && !state.edited) return moveGraph(dir, env, state.record , where);
|
|
348
|
+
if (state.muwi) {
|
|
349
|
+
const moved = ours && where !== was;
|
|
350
|
+
if (!state.outdated && !moved) {
|
|
351
|
+
if (o.graphAt && !ours) throw new ScreenError(`${state.shown} 레이아웃은 muwi layout이 만든 것이 아니라 그래프 자리를 바꾸지 못해요. 레이아웃 파일에서 muwi 칸을 옮기거나, muwi 칸이 없는 레이아웃에서 muwi layout --graph ${where}을 실행해요.`);
|
|
352
|
+
return { name: state.shown, base: state.record?.base ?? state.shown, list: true, graph: true, graphAt: was, already: true };
|
|
353
|
+
}
|
|
354
|
+
// Herdr가 업데이트됐거나 muwi의 배치가 바뀌었다. 우리 복제본을 그 자리에서 새 모양으로 고친다 (사람이 손으로 고쳤으면 그대로 둔다)
|
|
355
|
+
if (state.edited) {
|
|
356
|
+
const why = moved ? '그래프 자리를 바꾸려고 했지만' : state.outdatedBy === 'herdr' ? 'Herdr가 업데이트됐지만' : 'muwi 화면 배치가 바뀌었지만';
|
|
357
|
+
throw new ScreenError(`${why} ${state.shown} 레이아웃을 손으로 고친 것 같아 다시 만들지 않았어요. 고친 복제본은 남겨 두고 새로 만들려면 muwi layout --remove 뒤에 muwi layout을 실행해요.`);
|
|
358
|
+
}
|
|
359
|
+
return rebuild(dir, env, state, where);
|
|
360
|
+
}
|
|
361
|
+
const from = readTree(env, base);
|
|
362
|
+
if (!from) throw new ScreenError(`${base} 레이아웃 파일을 읽지 못했어요.`);
|
|
363
|
+
const check = placeMuwi(from.tree, where);
|
|
364
|
+
if (!check.list && !check.graph) {
|
|
365
|
+
throw new ScreenError(`${base} 레이아웃에는 Agents 칸도 터미널 영역도 없어서 muwi 칸을 넣을 자리를 못 찾았어요. 그래프는 muwi open으로 터미널 옆에 열어요.`);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const name = freeName(env, readLayoutRecord(dir));
|
|
369
|
+
fs.rmSync(layoutPath(env, name), { force: true });
|
|
370
|
+
const cloned = herdrApp(['do', 'layout.clone', JSON.stringify({ name: base, as: name })], env);
|
|
371
|
+
if (!cloned.ok) throw new ScreenError(`레이아웃을 복제하지 못했어요: ${(cloned.err || cloned.out).trim()}`);
|
|
372
|
+
const file = layoutPath(env, name);
|
|
373
|
+
const copy = waitForTree(env, name);
|
|
374
|
+
if (!copy) throw new ScreenError(`복제한 레이아웃을 읽지 못했어요: ${file}`);
|
|
375
|
+
|
|
376
|
+
// 복제하면 원래 레이아웃에서 사람이 고른 것(테마, 칸 크기)도 따라온다. 파일을 고쳐도 그것은 남는다
|
|
377
|
+
const placed = placeMuwi(copy.tree, where);
|
|
378
|
+
const tree = withPreview(env, name, placed.tree, where);
|
|
379
|
+
// 앱이 받아들이지 않았으면 원래대로 돌린다
|
|
380
|
+
const problem = writeLayout(env, name, tree);
|
|
381
|
+
if (problem) {
|
|
382
|
+
switchTo(env, base);
|
|
383
|
+
fs.rmSync(file, { force: true });
|
|
384
|
+
fs.rmSync(previewPath(env, name), { force: true });
|
|
385
|
+
throw new ScreenError(`Herdr 앱이 바꾼 레이아웃을 받아들이지 않아 원래대로 돌렸어요: ${problem}`);
|
|
386
|
+
}
|
|
387
|
+
// 복제하면 앱이 그 레이아웃으로 바꾼다. 혹시 바꾸지 않았으면 바꾼다
|
|
388
|
+
if (shownLayout(env) !== name) switchTo(env, name);
|
|
389
|
+
|
|
390
|
+
const record = {
|
|
391
|
+
name,
|
|
392
|
+
base,
|
|
393
|
+
...(copy.tree.cloned_from?.version ? { version: copy.tree.cloned_from.version } : state.version ? { version: state.version } : {}),
|
|
394
|
+
hash: treeHash(tree),
|
|
395
|
+
hashKind: 'tree',
|
|
396
|
+
shape: PLACE_VERSION,
|
|
397
|
+
list: placed.list,
|
|
398
|
+
graph: placed.graph,
|
|
399
|
+
graphAt: where,
|
|
400
|
+
at: new Date().toISOString(),
|
|
401
|
+
};
|
|
402
|
+
writeJson(recordFile(dir), record);
|
|
403
|
+
return { name, base, list: placed.list, graph: placed.graph, graphAt: where };
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Herdr가 업데이트됐거나 muwi의 배치가 바뀌었다: 원래 레이아웃의 새 모양으로 우리 복제본을 그 자리에서 다시 쓴다.
|
|
408
|
+
* 복제본을 지우고 다시 복제하면 이 레이아웃에서 사람이 고른 것(테마, 칸 크기, 고른 탭)이 사라지므로 그러지 않는다.
|
|
409
|
+
* 레이아웃을 바꾸지 않으니 화면도 번쩍이지 않는다
|
|
410
|
+
*/
|
|
411
|
+
function rebuild(dir , env , state , where ) {
|
|
412
|
+
const record = state.record ;
|
|
413
|
+
const was = record.graphAt ?? 'bottom';
|
|
414
|
+
const from = readTree(env, record.base);
|
|
415
|
+
if (!from) throw new ScreenError(`${record.base} 레이아웃 파일을 읽지 못했어요.`);
|
|
416
|
+
const placed = placeMuwi(from.tree, where);
|
|
417
|
+
if (!placed.list && !placed.graph) {
|
|
418
|
+
throw new ScreenError(`${record.base} 레이아웃에 muwi 칸을 넣을 자리가 없어졌어요. muwi layout --remove 뒤에 muwi open으로 터미널 옆에 열어요.`);
|
|
419
|
+
}
|
|
420
|
+
const before = readTree(env, record.name);
|
|
421
|
+
const version = state.version ?? before?.tree.cloned_from?.version ?? record.version;
|
|
422
|
+
// Herdr가 복제할 때처럼 어디서 복제했는지 적는다 (다음 업데이트를 알아보는 데 쓴다)
|
|
423
|
+
const tree = withPreview(env, record.name, { ...placed.tree, cloned_from: { name: record.base, ...(version ? { version } : {}) } }, where);
|
|
424
|
+
const problem = writeLayout(env, record.name, tree, before?.text);
|
|
425
|
+
if (problem) {
|
|
426
|
+
drawPreview(env, record.name, was);
|
|
427
|
+
throw new ScreenError(`Herdr 앱이 새 모양을 받아들이지 않아 원래대로 돌렸어요: ${problem}`);
|
|
428
|
+
}
|
|
429
|
+
fitGraph(env, was, where);
|
|
430
|
+
const next = {
|
|
431
|
+
...record,
|
|
432
|
+
...(version ? { version } : {}),
|
|
433
|
+
hash: treeHash(tree),
|
|
434
|
+
hashKind: 'tree',
|
|
435
|
+
shape: PLACE_VERSION,
|
|
436
|
+
list: placed.list,
|
|
437
|
+
graph: placed.graph,
|
|
438
|
+
graphAt: where,
|
|
439
|
+
at: new Date().toISOString(),
|
|
440
|
+
};
|
|
441
|
+
writeJson(recordFile(dir), next);
|
|
442
|
+
return { name: record.name, base: record.base, list: placed.list, graph: placed.graph, graphAt: where, ...(where !== was ? { moved: true } : { refreshed: true }) };
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** 레이아웃 나무에서 muwi가 넣은 묶음(터미널 영역 + 그래프)을 풀어 터미널 영역만 그 자리에 돌려놓는다 */
|
|
446
|
+
export function unplaceGraph(tree ) {
|
|
447
|
+
const root = structuredClone(tree);
|
|
448
|
+
const trail = trailTo(root, n => n.id === SPLIT_ID);
|
|
449
|
+
if (!trail || trail.length < 2) return root;
|
|
450
|
+
const split = trail[trail.length - 1];
|
|
451
|
+
const parent = trail[trail.length - 2];
|
|
452
|
+
const main = (split.children ?? []).find(c => c.id !== GRAPH_ID);
|
|
453
|
+
if (!main) return root;
|
|
454
|
+
// 감쌀 때 터미널 영역에 붙인 크기는 떼고, 묶음이 물려받았던 원래 크기를 돌려준다
|
|
455
|
+
const { size: _size, min: _min, max: _max, ...rest } = main;
|
|
456
|
+
const back = { ...rest };
|
|
457
|
+
if (split.size !== undefined) back.size = split.size;
|
|
458
|
+
if (split.min !== undefined) back.min = split.min;
|
|
459
|
+
if (split.max !== undefined) back.max = split.max;
|
|
460
|
+
parent.children [parent.children .indexOf(split)] = back;
|
|
461
|
+
return root;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* 지금 보이는 muwi 레이아웃에서 그래프 칸만 다른 자리로 옮긴다. 레이아웃 파일을 그 자리에서 고쳐서(writeLayout)
|
|
466
|
+
* 레이아웃을 바꾸지 않고, 사람이 고른 테마와 칸 크기도 지킨다. 가로·세로가 바뀌면 그 방향의 기본 크기로 맞춘다
|
|
467
|
+
*/
|
|
468
|
+
function moveGraph(dir , env , record , where ) {
|
|
469
|
+
const file = readTree(env, record.name);
|
|
470
|
+
if (!file) throw new ScreenError(`${record.name} 레이아웃 파일을 읽지 못했어요.`);
|
|
471
|
+
const content = trailTo(file.tree, n => n.id === GRAPH_ID)?.at(-1)?.content;
|
|
472
|
+
const placed = placeMuwi(unplaceGraph(file.tree), where);
|
|
473
|
+
const graph = trailTo(placed.tree, n => n.id === GRAPH_ID)?.at(-1);
|
|
474
|
+
if (!graph) throw new ScreenError('그래프 칸을 옮길 자리를 찾지 못했어요.');
|
|
475
|
+
if (content) graph.content = content; // 사이드바 목록이 있으니 그래프만 그리는 모양 그대로
|
|
476
|
+
// 설정 화면의 그림도 새 자리대로
|
|
477
|
+
const tree = withPreview(env, record.name, placed.tree, where);
|
|
478
|
+
const problem = writeLayout(env, record.name, tree, file.text);
|
|
479
|
+
if (problem) {
|
|
480
|
+
drawPreview(env, record.name, record.graphAt ?? 'bottom');
|
|
481
|
+
throw new ScreenError(`Herdr 앱이 옮긴 레이아웃을 받아들이지 않아 원래대로 돌렸어요: ${problem}`);
|
|
482
|
+
}
|
|
483
|
+
fitGraph(env, record.graphAt ?? 'bottom', where);
|
|
484
|
+
writeJson(recordFile(dir), { ...record, graphAt: where, hash: treeHash(tree), hashKind: 'tree', at: new Date().toISOString() });
|
|
485
|
+
return { name: record.name, base: record.base, list: record.list, graph: true, graphAt: where, moved: true };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function switchTo(env , name ) {
|
|
489
|
+
const r = herdrApp(['do', 'layout.switch', JSON.stringify({ name })], env);
|
|
490
|
+
if (!r.ok) throw new ScreenError(`${name} 레이아웃으로 바꾸지 못했어요: ${(r.err || r.out).trim()}`);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
/** muwi layout으로 바꾼 것을 되돌린다: 원래 레이아웃으로 바꾸고, 손대지 않은 복제본은 지운다 */
|
|
496
|
+
export function removeLayout(dir , env = process.env) {
|
|
497
|
+
const record = readLayoutRecord(dir);
|
|
498
|
+
if (!record) return { deleted: false, none: true };
|
|
499
|
+
const shown = shownLayout(env);
|
|
500
|
+
const result = { deleted: false };
|
|
501
|
+
const file = layoutPath(env, record.name);
|
|
502
|
+
const current = readTree(env, record.name);
|
|
503
|
+
if (shown === record.name) {
|
|
504
|
+
const back = fs.existsSync(layoutPath(env, record.base)) ? record.base : 'main';
|
|
505
|
+
switchTo(env, back);
|
|
506
|
+
result.switchedTo = back;
|
|
507
|
+
}
|
|
508
|
+
// 앱이 닫혀 있고 그 레이아웃이 마지막으로 보던 것이면 지우지 않는다 (다음에 켤 때 보여 줄 것이 없어진다)
|
|
509
|
+
const stillShown = !shown && sharedLayout(env) === record.name;
|
|
510
|
+
if (current && !untouched(record, current)) result.kept = file;
|
|
511
|
+
else if (current && !stillShown) {
|
|
512
|
+
fs.rmSync(file, { force: true });
|
|
513
|
+
fs.rmSync(previewPath(env, record.name), { force: true });
|
|
514
|
+
result.deleted = true;
|
|
515
|
+
} else if (stillShown) result.kept = file;
|
|
516
|
+
fs.rmSync(recordFile(dir), { force: true });
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* 그림 없이 만든 muwi 레이아웃(0.5.24까지)에 설정 화면의 그림을 넣고, 이미 있으면 지금 판의 그림으로 맞춘다. muwi setup이 부른다.
|
|
522
|
+
* 우리가 쓴 그대로인 복제본에만 넣고, 사람이 손으로 고친 복제본은 그대로 둔다. 그래프 자리는 기록대로. 넣었으면 true
|
|
523
|
+
*/
|
|
524
|
+
export function ensurePreview(dir , env = process.env) {
|
|
525
|
+
const record = readLayoutRecord(dir);
|
|
526
|
+
if (!record) return false;
|
|
527
|
+
const file = readTree(env, record.name);
|
|
528
|
+
if (!file || !untouched(record, file)) return false;
|
|
529
|
+
if (file.tree.preview && fs.existsSync(previewPath(env, record.name))) {
|
|
530
|
+
// 그림이 이미 있으면 그림 파일만 지금 판의 그림으로 맞춘다 (색 같은 것이 바뀌었을 때). 레이아웃 파일은 건드리지 않는다
|
|
531
|
+
const want = layoutPreview(record.graphAt ?? 'bottom');
|
|
532
|
+
if (fs.readFileSync(previewPath(env, record.name), 'utf8') === want) return false;
|
|
533
|
+
return drawPreview(env, record.name, record.graphAt ?? 'bottom');
|
|
534
|
+
}
|
|
535
|
+
const tree = withPreview(env, record.name, file.tree, record.graphAt ?? 'bottom');
|
|
536
|
+
if (!tree.preview || writeLayout(env, record.name, tree, file.text)) return false;
|
|
537
|
+
writeJson(recordFile(dir), { ...record, hash: treeHash(tree), hashKind: 'tree' });
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** 앱이 닫혀 있을 때 마지막으로 보던 레이아웃 (shared.json) */
|
|
542
|
+
function sharedLayout(env ) {
|
|
543
|
+
const shared = readJson (path.join(herdrHome(env), 'shared.json'));
|
|
544
|
+
return typeof shared?.layout === 'string' ? shared.layout : null;
|
|
545
|
+
}
|