@leosdc/exploreio 0.7.0 → 0.9.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/dist/tui/app.js CHANGED
@@ -10,12 +10,14 @@ import { VimEngine } from '../editor/vimEngine.js';
10
10
  import { buildEditorFrame } from '../editor/vimView.js';
11
11
  import { getGitSummaryBadge, gitCheckoutBranch, gitCommit, gitCreateBranch, gitDiscardFile, gitFetch, gitPull, gitPush, gitStageAll, gitStageFile, gitStashPop, gitStashPush, gitUnstageAll, gitUnstageFile, loadCommitDetails, loadFileDiff, loadGitHubData, loadGitPanelSnapshot, } from '../git/gitService.js';
12
12
  import { buildGitHubListItems, buildGitPanelFrame, buildQuickActions, buildStatusListItems, formatGitHeaderBadge, } from '../git/gitView.js';
13
- import { createFileOrDir, deleteEntry, formatBytes, readDirectoryEntries, renameEntry, safeReadFilePreview, safeWriteTextFile, } from '../fs/explorer.js';
13
+ import { createFileOrDir, createFileSafe, createFolderSafe, deleteEntry, duplicateEntrySafe, formatBytes, readDirectoryEntries, renameEntry, safeReadFilePreview, safeWriteTextFile, suggestDuplicateName, } from '../fs/explorer.js';
14
+ import { DirectoryWatcher, resolveExistingDirectory, resolveNextSelectedIndex, } from '../fs/watcher.js';
14
15
  import { collectAllCollapsiblePaths } from '../renderers/json.js';
15
16
  import { renderDirectoryPreview, renderDocument, } from '../renderers/index.js';
16
17
  import { bg, fg, getTheme, nextTheme } from '../themes/palette.js';
17
18
  import { BOLD, DIM, ITALIC, RESET, fitAnsi, highlightSearch, stripUnsafeAnsi, truncateAnsi, visibleLength } from './ansi.js';
18
19
  import { copyTextToClipboard } from './clipboard.js';
20
+ import { applyModalOverlay, createInitialModalState, cycleModalExtension, renderCreateEntryModalOverlay, resolveModalTargetName, } from './createEntryModal.js';
19
21
  import { openInFullscreenShell } from './fullscreenLauncher.js';
20
22
  import { parseInputChunk } from './input.js';
21
23
  import { KEYBINDS_TABS, buildKeybindsPanelFrame, getFilteredKeybindDefinitions, } from './keybindsView.js';
@@ -112,17 +114,26 @@ export class ExploreioApp {
112
114
  keybindsCaptureMode = null;
113
115
  keybindsDirty = false;
114
116
  keybindsReturnView = 'browser';
115
- // Prompt modal inline (barra inferior)
117
+ // Prompt modal inline (barra inferior) & Modal de criação arquivo/pasta (SPEC-007)
116
118
  promptKind = null;
117
119
  promptInput = '';
120
+ createModal = null;
118
121
  toastMessage = '';
119
122
  running = false;
123
+ // Observador de arquivos em tempo real (SPEC-008)
124
+ dirWatcher;
125
+ externalRefreshRunning = false;
126
+ queuedExternalScopes = new Set();
127
+ deferredExternalScopes = new Set();
120
128
  constructor(options) {
121
129
  this.currentDir = path.resolve(options.startDir);
122
130
  this.initialMode = options.initialMode;
123
131
  this.singleFile = Boolean(options.singleFile);
124
132
  this.themeName = options.themeName ?? 'midnight';
125
133
  this.showHidden = Boolean(options.showHidden);
134
+ this.dirWatcher = new DirectoryWatcher({
135
+ onChange: (batch) => this.handleWatcherChange(batch),
136
+ });
126
137
  const loadedBinds = loadKeybindsRegistry(options.configPath);
127
138
  this.keybinds = loadedBinds.registry;
128
139
  this.configPath = loadedBinds.configPath;
@@ -141,6 +152,7 @@ export class ExploreioApp {
141
152
  this.screen.enter();
142
153
  const cleanup = () => {
143
154
  this.running = false;
155
+ this.dirWatcher.close();
144
156
  this.screen.leave();
145
157
  };
146
158
  process.once('SIGINT', () => {
@@ -171,6 +183,7 @@ export class ExploreioApp {
171
183
  }
172
184
  shutdown() {
173
185
  this.running = false;
186
+ this.dirWatcher.close();
174
187
  process.stdout.off('resize', this.onResize);
175
188
  process.stdin.off('data', this.onStdinData);
176
189
  this.screen.leave();
@@ -209,9 +222,14 @@ export class ExploreioApp {
209
222
  if (ev.name === 'escape' || this.keybinds.matches('global.help', ev) || this.keybinds.matches('global.quit', ev)) {
210
223
  this.showHelp = false;
211
224
  this.draw();
225
+ this.flushDeferredExternalRefresh();
212
226
  }
213
227
  continue;
214
228
  }
229
+ if (this.createModal) {
230
+ await this.handleCreateModalKey(ev);
231
+ continue;
232
+ }
215
233
  if (this.promptKind) {
216
234
  await this.handlePromptKey(ev);
217
235
  continue;
@@ -247,6 +265,7 @@ export class ExploreioApp {
247
265
  this.promptKind = null;
248
266
  this.promptInput = '';
249
267
  this.draw();
268
+ this.flushDeferredExternalRefresh();
250
269
  return;
251
270
  }
252
271
  if (ev.name === 'enter') {
@@ -272,14 +291,27 @@ export class ExploreioApp {
272
291
  }
273
292
  else if (kind === 'create' && value.trim()) {
274
293
  try {
275
- await createFileOrDir(this.currentDir, value);
294
+ const created = await createFileOrDir(this.currentDir, value);
276
295
  this.toastMessage = `Criado: ${value.trim()}`;
277
- await this.reloadDirectory();
296
+ await this.reloadDirectory(path.basename(created));
278
297
  }
279
298
  catch (err) {
280
299
  this.toastMessage = err instanceof Error ? err.message : String(err);
281
300
  }
282
301
  }
302
+ else if (kind === 'duplicate' && value.trim()) {
303
+ const current = this.currentEntries[this.selectedIndex];
304
+ if (current) {
305
+ try {
306
+ const res = await duplicateEntrySafe(current.fullPath, value);
307
+ this.toastMessage = `Duplicado: ${value.trim()}`;
308
+ await this.reloadDirectory(res.focusName);
309
+ }
310
+ catch (err) {
311
+ this.toastMessage = err instanceof Error ? err.message : String(err);
312
+ }
313
+ }
314
+ }
283
315
  else if (kind === 'rename' && value.trim()) {
284
316
  const current = this.currentEntries[this.selectedIndex];
285
317
  if (current) {
@@ -371,6 +403,72 @@ export class ExploreioApp {
371
403
  this.draw();
372
404
  }
373
405
  }
406
+ async handleCreateModalKey(ev) {
407
+ const modal = this.createModal;
408
+ if (!modal)
409
+ return;
410
+ if (ev.name === 'escape') {
411
+ this.createModal = null;
412
+ this.draw();
413
+ this.flushDeferredExternalRefresh();
414
+ return;
415
+ }
416
+ if (modal.mode === 'file') {
417
+ if (ev.name === 'tab') {
418
+ cycleModalExtension(modal, ev.shift ? -1 : 1);
419
+ this.draw();
420
+ return;
421
+ }
422
+ if (ev.name === 'up' || ev.name === 'down') {
423
+ cycleModalExtension(modal, ev.name === 'up' ? -1 : 1);
424
+ this.draw();
425
+ return;
426
+ }
427
+ if (ev.ctrl && ev.name === 'e') {
428
+ modal.openInEditor = !modal.openInEditor;
429
+ this.draw();
430
+ return;
431
+ }
432
+ }
433
+ if (ev.name === 'enter') {
434
+ const targetName = resolveModalTargetName(modal);
435
+ if (!targetName) {
436
+ modal.errorMessage = 'Informe um nome antes de confirmar.';
437
+ this.draw();
438
+ return;
439
+ }
440
+ try {
441
+ const shouldOpenEditor = modal.mode === 'file' && modal.openInEditor;
442
+ const res = modal.mode === 'file'
443
+ ? await createFileSafe(this.currentDir, targetName)
444
+ : await createFolderSafe(this.currentDir, targetName);
445
+ this.createModal = null;
446
+ this.toastMessage =
447
+ modal.mode === 'file' ? `Arquivo criado: ${targetName}` : `Pasta criada: ${targetName}`;
448
+ await this.reloadDirectory(res.focusName);
449
+ if (shouldOpenEditor) {
450
+ await this.openFileInEditor(res.targetPath, 'browser');
451
+ return;
452
+ }
453
+ }
454
+ catch (err) {
455
+ modal.errorMessage = err instanceof Error ? err.message : String(err);
456
+ }
457
+ this.draw();
458
+ return;
459
+ }
460
+ if (ev.name === 'backspace') {
461
+ modal.input = modal.input.slice(0, -1);
462
+ modal.errorMessage = '';
463
+ this.draw();
464
+ return;
465
+ }
466
+ if (ev.char && !ev.ctrl) {
467
+ modal.input += ev.char;
468
+ modal.errorMessage = '';
469
+ this.draw();
470
+ }
471
+ }
374
472
  async handleBrowserKey(ev) {
375
473
  this.toastMessage = '';
376
474
  const maxIdx = Math.max(0, this.currentEntries.length - 1);
@@ -407,6 +505,24 @@ export class ExploreioApp {
407
505
  this.draw();
408
506
  return;
409
507
  }
508
+ if (this.keybinds.matches('browser.createFile', ev)) {
509
+ this.createModal = createInitialModalState('file');
510
+ this.draw();
511
+ return;
512
+ }
513
+ if (this.keybinds.matches('browser.createFolder', ev)) {
514
+ this.createModal = createInitialModalState('folder');
515
+ this.draw();
516
+ return;
517
+ }
518
+ if (this.keybinds.matches('browser.duplicateEntry', ev) && this.currentEntries[this.selectedIndex]) {
519
+ const current = this.currentEntries[this.selectedIndex];
520
+ const existingNames = new Set(this.currentEntries.map((e) => e.name));
521
+ this.promptKind = 'duplicate';
522
+ this.promptInput = suggestDuplicateName(existingNames, current.name, current.isDirectory);
523
+ this.draw();
524
+ return;
525
+ }
410
526
  if (this.keybinds.matches('browser.createEntry', ev)) {
411
527
  this.promptKind = 'create';
412
528
  this.promptInput = '';
@@ -531,6 +647,7 @@ export class ExploreioApp {
531
647
  this.readerSearchMatches = [];
532
648
  await this.refreshSelectedPreview();
533
649
  this.draw();
650
+ this.flushDeferredExternalRefresh();
534
651
  return;
535
652
  }
536
653
  if (this.keybinds.matches('global.help', ev)) {
@@ -706,7 +823,7 @@ export class ExploreioApp {
706
823
  }
707
824
  this.draw();
708
825
  }
709
- async reloadDirectory(focusEntryName) {
826
+ async reloadDirectory(focusEntryName, fallbackIndex) {
710
827
  const parentDir = path.dirname(this.currentDir);
711
828
  const [parentRes, currentRes] = await Promise.all([
712
829
  parentDir !== this.currentDir
@@ -716,13 +833,8 @@ export class ExploreioApp {
716
833
  ]);
717
834
  this.parentEntries = parentRes.entries;
718
835
  this.currentEntries = currentRes.entries;
719
- if (focusEntryName) {
720
- const idx = this.currentEntries.findIndex((e) => e.name === focusEntryName);
721
- this.selectedIndex = idx >= 0 ? idx : 0;
722
- }
723
- else {
724
- this.selectedIndex = Math.min(this.selectedIndex, Math.max(0, this.currentEntries.length - 1));
725
- }
836
+ const effectiveFallback = fallbackIndex ?? (focusEntryName ? 0 : this.selectedIndex);
837
+ this.selectedIndex = resolveNextSelectedIndex(this.currentEntries, focusEntryName, effectiveFallback);
726
838
  await this.refreshSelectedPreview();
727
839
  this.refreshGitHeaderSummary();
728
840
  }
@@ -731,6 +843,7 @@ export class ExploreioApp {
731
843
  const theme = getTheme(this.themeName);
732
844
  const { width, height } = this.screen.getSize();
733
845
  const previewWidth = Math.max(24, Math.floor(width * 0.45) - 4);
846
+ this.syncDirectoryWatcher();
734
847
  if (!entry) {
735
848
  this.previewBadge = 'VAZIO';
736
849
  this.previewLines = [`${fg(theme.muted)}${ITALIC}(nenhum item selecionado)${RESET}`];
@@ -751,6 +864,88 @@ export class ExploreioApp {
751
864
  this.previewBadge = doc.badge;
752
865
  this.previewLines = doc.lines;
753
866
  }
867
+ isBrowserInteractiveIdle() {
868
+ return (this.running &&
869
+ this.view === 'browser' &&
870
+ !this.promptKind &&
871
+ !this.createModal &&
872
+ !this.showHelp);
873
+ }
874
+ syncDirectoryWatcher() {
875
+ if (!this.running)
876
+ return;
877
+ const parentDir = path.dirname(this.currentDir);
878
+ const selected = this.currentEntries[this.selectedIndex];
879
+ const previewDir = selected?.isDirectory ? selected.fullPath : null;
880
+ this.dirWatcher.updateTargets({
881
+ currentDir: this.currentDir,
882
+ parentDir: parentDir !== this.currentDir ? parentDir : null,
883
+ previewDir,
884
+ });
885
+ }
886
+ handleWatcherChange(batch) {
887
+ if (!this.running)
888
+ return;
889
+ if (!this.isBrowserInteractiveIdle()) {
890
+ for (const scope of batch.scopes) {
891
+ this.deferredExternalScopes.add(scope);
892
+ }
893
+ return;
894
+ }
895
+ for (const scope of batch.scopes) {
896
+ this.queuedExternalScopes.add(scope);
897
+ }
898
+ void this.drainExternalRefreshQueue();
899
+ }
900
+ flushDeferredExternalRefresh() {
901
+ if (this.deferredExternalScopes.size === 0 || !this.isBrowserInteractiveIdle())
902
+ return;
903
+ for (const scope of this.deferredExternalScopes) {
904
+ this.queuedExternalScopes.add(scope);
905
+ }
906
+ this.deferredExternalScopes.clear();
907
+ void this.drainExternalRefreshQueue();
908
+ }
909
+ async drainExternalRefreshQueue() {
910
+ if (this.externalRefreshRunning)
911
+ return;
912
+ this.externalRefreshRunning = true;
913
+ try {
914
+ while (this.queuedExternalScopes.size > 0 && this.isBrowserInteractiveIdle()) {
915
+ const scopes = new Set(this.queuedExternalScopes);
916
+ this.queuedExternalScopes.clear();
917
+ await this.applyExternalScopes(scopes);
918
+ }
919
+ }
920
+ finally {
921
+ this.externalRefreshRunning = false;
922
+ }
923
+ }
924
+ async applyExternalScopes(scopes) {
925
+ if (scopes.has('current') || scopes.has('parent')) {
926
+ const resolved = await resolveExistingDirectory(this.currentDir);
927
+ if (!this.isBrowserInteractiveIdle())
928
+ return;
929
+ if (resolved.fellBack) {
930
+ this.currentDir = resolved.dirPath;
931
+ this.filterQuery = '';
932
+ this.selectedIndex = 0;
933
+ this.listScroll = 0;
934
+ this.toastMessage = `Diretório anterior removido; voltando para ${stripUnsafeAnsi(resolved.dirPath)}`;
935
+ await this.reloadDirectory();
936
+ }
937
+ else {
938
+ const previousName = this.currentEntries[this.selectedIndex]?.name;
939
+ await this.reloadDirectory(previousName, this.selectedIndex);
940
+ }
941
+ }
942
+ else if (scopes.has('preview')) {
943
+ await this.refreshSelectedPreview();
944
+ }
945
+ if (this.isBrowserInteractiveIdle()) {
946
+ this.draw();
947
+ }
948
+ }
754
949
  async openInFullscreenTerminal(filePath, viewMode) {
755
950
  const result = await openInFullscreenShell(filePath, {
756
951
  themeName: this.themeName,
@@ -895,6 +1090,7 @@ export class ExploreioApp {
895
1090
  else {
896
1091
  await this.refreshSelectedPreview();
897
1092
  }
1093
+ this.flushDeferredExternalRefresh();
898
1094
  }
899
1095
  this.draw();
900
1096
  }
@@ -948,6 +1144,10 @@ export class ExploreioApp {
948
1144
  : this.view === 'reader'
949
1145
  ? this.buildReaderFrame(width, height, theme)
950
1146
  : this.buildBrowserFrame(width, height, theme);
1147
+ if (this.createModal) {
1148
+ const modalLines = renderCreateEntryModalOverlay(this.createModal, this.currentDir, width, height, theme);
1149
+ applyModalOverlay(lines, modalLines, height);
1150
+ }
951
1151
  if (this.showHelp) {
952
1152
  this.overlayHelpModal(lines, width, height, theme);
953
1153
  }
@@ -1072,6 +1272,7 @@ export class ExploreioApp {
1072
1272
  filter: 'Filtrar arquivos: ',
1073
1273
  search: 'Buscar no arquivo: ',
1074
1274
  create: 'Criar arquivo ou pasta/ : ',
1275
+ duplicate: 'Duplicar para: ',
1075
1276
  rename: 'Renomear para: ',
1076
1277
  delete: `Excluir "${this.currentEntries[this.selectedIndex]?.name ?? ''}"? (y/N): `,
1077
1278
  'git-commit': 'git commit -m : ',
@@ -1133,7 +1334,8 @@ export class ExploreioApp {
1133
1334
  row(k('browser.filter'), 'Filtrar arquivos (fuzzy)'),
1134
1335
  row(k('browser.toggleHidden'), 'Mostrar/ocultar arquivos ocultos'),
1135
1336
  row(k('browser.copyPath'), 'Copiar caminho selecionado (arquivo ou pasta)'),
1136
- row(`${k('browser.createEntry')} / ${k('browser.renameEntry')} / ${k('browser.deleteEntry')}`, 'Criar / Renomear / Excluir (com confirmação)'),
1337
+ row(`${k('browser.createFile')} / ${k('browser.createFolder')}`, 'Novo arquivo (com extensão) / Nova pasta cross-OS'),
1338
+ row(`${k('browser.duplicateEntry')} / ${k('browser.renameEntry')} / ${k('browser.deleteEntry')}`, 'Duplicar / Renomear / Excluir (com confirmação)'),
1137
1339
  row(k('browser.openGitPanel'), 'Abrir PAINEL GIT / GITHUB (status, árvore, branches, PRs)'),
1138
1340
  '',
1139
1341
  ' PAINEL GIT / GITHUB',
@@ -1911,6 +2113,7 @@ export class ExploreioApp {
1911
2113
  }
1912
2114
  this.view = this.keybindsReturnView;
1913
2115
  this.draw();
2116
+ this.flushDeferredExternalRefresh();
1914
2117
  return;
1915
2118
  }
1916
2119
  if (ev.char === 't') {