diffwatch 1.1.3 → 2.0.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/src/index.ts DELETED
@@ -1,643 +0,0 @@
1
- #!/usr/bin/env node
2
- const blessed = require('neo-neo-blessed');
3
- import chalk from 'chalk';
4
- import { spawn } from 'child_process';
5
- import { GitHandler, FileStatus } from './utils/git';
6
- import { formatDiffWithDiff2Html } from './utils/diff-formatter';
7
- import { readFileSync } from 'fs';
8
- import { dirname, join } from 'path';
9
-
10
- const packageJson = JSON.parse(readFileSync(join(dirname(__filename), '..', 'package.json'), 'utf-8'));
11
-
12
- async function main() {
13
- const args = process.argv.slice(2);
14
- let repoPath = process.cwd();
15
-
16
- const showHelp = () => {
17
- console.log(`
18
- Usage: diffwatch [path]
19
-
20
- Arguments:
21
- path Path to the git repository (default: current directory)
22
-
23
- Options:
24
- -h, --help Show help information
25
- -v, --version Show version information
26
- `);
27
- process.exit(0);
28
- };
29
-
30
- const showVersion = () => {
31
- console.log(`diffwatch v${packageJson.version}`);
32
- process.exit(0);
33
- };
34
-
35
- const positionalArgs = [];
36
- for (let i = 0; i < args.length; i++) {
37
- if (args[i] === '-h' || args[i] === '--help') {
38
- showHelp();
39
- } else if (args[i] === '-v' || args[i] === '--version') {
40
- showVersion();
41
- } else if (args[i].startsWith('-')) {
42
- // Ignore unknown flags or handle them if needed
43
- } else {
44
- positionalArgs.push(args[i]);
45
- }
46
- }
47
-
48
- if (positionalArgs.length > 0) {
49
- repoPath = positionalArgs[0];
50
- }
51
-
52
- try {
53
- process.chdir(repoPath);
54
- } catch (err) {
55
- console.error(`Error: Could not change directory to ${repoPath}`);
56
- process.exit(1);
57
- }
58
-
59
- const gitHandler = new GitHandler(repoPath);
60
-
61
- if (!(await gitHandler.isRepo())) {
62
- console.log(chalk.red(`Error: ${repoPath} is not a git repository.`));
63
- process.exit(1);
64
- }
65
-
66
- // Force xterm-256color to encourage better mouse support on Windows terminals
67
- if (process.platform === 'win32' && !process.env.TERM) {
68
- process.env.TERM = 'xterm-256color';
69
- }
70
-
71
- const screen = blessed.screen({
72
- smartCSR: true,
73
- title: 'diffwatch',
74
- mouse: true,
75
- });
76
-
77
- // Explicitly enable mouse tracking
78
- screen.program.enableMouse();
79
-
80
- let fileList: any = blessed.list({
81
- top: 0,
82
- left: 0,
83
- width: '30%',
84
- height: '99%',
85
- label: 'Files (0)',
86
- keys: true,
87
- vi: true,
88
- mouse: true,
89
- tags: true,
90
- padding: { left: 0, right: 0 },
91
- scrollbar: {
92
- ch: ' ',
93
- track: { bg: 'white' },
94
- style: { bg: 'blue' },
95
- },
96
- style: {
97
- selected: { fg: 'black', bg: 'white' },
98
- border: { fg: 'white' },
99
- },
100
- border: { type: 'line' },
101
- });
102
-
103
- const createFileList = () => {
104
- const newList = blessed.list({
105
- top: 0,
106
- left: 0,
107
- width: '30%',
108
- height: '99%',
109
- label: 'Files (0)',
110
- keys: true,
111
- vi: true,
112
- mouse: true,
113
- tags: true,
114
- padding: { left: 0, right: 0 },
115
- scrollbar: {
116
- ch: ' ',
117
- track: { bg: 'white' },
118
- style: { bg: 'blue' },
119
- },
120
- style: {
121
- selected: { fg: 'black', bg: 'white' },
122
- border: { fg: 'white' },
123
- },
124
- border: { type: 'line' },
125
- });
126
-
127
- newList.on('select item', () => {
128
- scheduleDiffUpdate();
129
- });
130
-
131
- newList.key(['up', 'down'], () => {
132
- scheduleDiffUpdate();
133
- });
134
-
135
- newList.on('wheeldown', () => handleScroll('down'));
136
- newList.on('wheelup', () => handleScroll('up'));
137
-
138
- return newList;
139
- };
140
-
141
- const diffView = blessed.scrollabletext({
142
- top: 0,
143
- left: '30%',
144
- width: '70%',
145
- height: '99%',
146
- label: ' Diff () ',
147
- keys: true,
148
- vi: true,
149
- mouse: true,
150
- padding: { left: 1, right: 0 },
151
- scrollbar: {
152
- ch: ' ',
153
- track: { bg: 'white' },
154
- style: { bg: 'blue' },
155
- },
156
- style: {
157
- border: { fg: 'white' },
158
- },
159
- border: { type: 'line' },
160
- tags: false,
161
- });
162
-
163
- const searchBox = blessed.box({
164
- top: 'center',
165
- left: '40%', // Center of right pane (30% + 70%/2 = 65%)
166
- width: '50%',
167
- height: 3,
168
- label: ' Search ',
169
- border: { type: 'line' },
170
- style: { border: { fg: 'yellow' } },
171
- hidden: true,
172
- });
173
-
174
- const searchInput = blessed.textbox({
175
- parent: searchBox,
176
- top: 0,
177
- left: 0,
178
- width: '100%-2',
179
- height: 1,
180
- keys: true,
181
- inputOnFocus: true,
182
- style: { fg: 'white', bg: 'black' },
183
- });
184
-
185
- // Confirmation dialog for revert
186
- const confirmDialog = blessed.box({
187
- top: 'center',
188
- left: '45%', // Center of right pane (30% + 70%/2 = 65%)
189
- width: '38%',
190
- label: ' Confirm Revert ',
191
- height: 3,
192
- content: 'Press SPACE key to confirm revert or ESC to cancel.',
193
- border: { type: 'line' },
194
- style: {
195
- fg: 'yellow',
196
- bg: 'black',
197
- border: { fg: 'yellow' }
198
- },
199
- hidden: true,
200
- });
201
-
202
- const notificationBox = blessed.box({
203
- top: 'center',
204
- left: '45%', // Center of right pane (30% + 70%/2 = 65%)
205
- width: '38%',
206
- height: 3,
207
- label: ' Notification ',
208
- border: { type: 'line' },
209
- style: {
210
- fg: 'green',
211
- bg: 'black',
212
- border: { fg: 'green' }
213
- },
214
- hidden: true,
215
- });
216
-
217
- const showNotification = (message: string, color: 'green' | 'red' = 'green') => {
218
- const previouslyFocused = screen.focused;
219
- notificationBox.setContent(message);
220
- notificationBox.style = {
221
- fg: color,
222
- bg: 'black',
223
- border: { fg: color }
224
- };
225
- notificationBox.show();
226
- notificationBox.setFront();
227
- screen.render();
228
-
229
- setTimeout(() => {
230
- notificationBox.hide();
231
- if (previouslyFocused && previouslyFocused !== notificationBox) {
232
- previouslyFocused.focus();
233
- } else {
234
- fileList.focus();
235
- }
236
- updateBorders();
237
- screen.render();
238
- }, 3000);
239
- };
240
-
241
- // Branch display in bottom right
242
- const branchBox = blessed.box({
243
- bottom: 0,
244
- right: 0,
245
- width: 'shrink',
246
- height: 1,
247
- align: 'left',
248
- content: chalk.cyan('Branch: '),
249
- style: {
250
- fg: 'white',
251
- },
252
- });
253
-
254
- // Footer box to show shortcuts - aligned with the panes
255
- const footer = blessed.box({
256
- bottom: 0,
257
- left: 0,
258
- width: '100%',
259
- height: 1,
260
- content: chalk.green('←→') + ' Switch |' + chalk.green(' ⏎') + ' Open | ' + chalk.green('S') + ' Search | ' + chalk.green('R') + ' Revert | ' + chalk.green('Q') + ' Quit ',
261
- });
262
-
263
- screen.append(fileList);
264
- screen.append(diffView);
265
- screen.append(searchBox);
266
- screen.append(notificationBox);
267
- screen.append(confirmDialog);
268
- screen.append(footer);
269
- screen.append(branchBox);
270
-
271
- const updateBorders = () => {
272
- fileList.style.border.fg = screen.focused === fileList ? 'yellow' : 'white';
273
- diffView.style.border.fg = screen.focused === diffView ? 'yellow' : 'white';
274
- screen.render();
275
- };
276
-
277
- let currentFiles: FileStatus[] = [];
278
- let lastSelectedPath: string | null = null;
279
- let diffUpdateTimeout: NodeJS.Timeout | null = null;
280
- let currentSearchTerm: string = '';
281
- let currentBranch: string = '';
282
- let lastFilePaths: string[] = [];
283
- let isUpdatingList = false;
284
-
285
- const scheduleDiffUpdate = () => {
286
- if (diffUpdateTimeout) clearTimeout(diffUpdateTimeout);
287
- diffUpdateTimeout = setTimeout(async () => {
288
- await updateDiff();
289
- }, 150); // 150ms debounce
290
- };
291
-
292
- const handleScroll = (direction: 'up' | 'down') => {
293
- const focused = screen.focused;
294
- if (focused === fileList) {
295
- if (direction === 'up') {
296
- focused.up(1);
297
- } else {
298
- focused.down(1);
299
- }
300
- scheduleDiffUpdate();
301
- screen.render();
302
- } else if (focused === diffView) {
303
- const scrollAmount = direction === 'up' ? -2 : 2;
304
- diffView.scroll(scrollAmount);
305
- screen.render();
306
- }
307
- };
308
-
309
- // Remove default wheel listeners to enforce "scroll focused only" behavior
310
- fileList.removeAllListeners('wheeldown');
311
- fileList.removeAllListeners('wheelup');
312
- diffView.removeAllListeners('wheeldown');
313
- diffView.removeAllListeners('wheelup');
314
-
315
- // Attach custom scroll handlers to widgets (captures wheel even if hovering specific widget)
316
- // We use widget-level listeners now that screen.mouse is true.
317
- // We attach to both to ensure the event is caught regardless of where the mouse is.
318
- // We use widget-level listeners now that screen.mouse is true.
319
- // We attach to both to ensure the event is caught regardless of where the mouse is.
320
- // The handleScroll function will then decide WHAT to scroll based on focus.
321
-
322
- diffView.on('wheeldown', () => handleScroll('down'));
323
- diffView.on('wheelup', () => handleScroll('up'));
324
-
325
- // Also listen on screen for events that might miss the widgets (margins, borders)
326
- screen.on('wheeldown', () => handleScroll('down'));
327
- screen.on('wheelup', () => handleScroll('up'));
328
-
329
- const openInEditor = (filePath: string) => {
330
- try {
331
- if (process.platform === 'win32') {
332
- // On Windows, use 'start' to open with default program
333
- spawn('cmd', ['/c', 'start', '', filePath], { stdio: 'ignore', detached: true }).unref();
334
- } else {
335
- // On Unix-like systems, try EDITOR, fallback to xdg-open
336
- const editor = process.env.EDITOR || process.env.VISUAL || 'xdg-open';
337
- spawn(editor, [filePath], { stdio: 'ignore', detached: true }).unref();
338
- }
339
- } catch (error) {
340
- console.error(`Failed to open ${filePath}: ${error}`);
341
- }
342
- };
343
-
344
- const updateBranch = async () => {
345
- const branch = await gitHandler.getBranch();
346
- if (branch !== currentBranch) {
347
- currentBranch = branch;
348
- branchBox.setContent(chalk.cyan('Branch: ') + chalk.yellow(branch));
349
- screen.render();
350
- }
351
- };
352
-
353
- const updateDiff = async () => {
354
- const selectedIndex = fileList.selected;
355
- const selectedFile = currentFiles[selectedIndex];
356
- if (!selectedFile) {
357
- // No valid file selected, clear diff view
358
- diffView.setContent('Select a file to view diff.');
359
- diffView.setLabel(' Diff () ');
360
- lastSelectedPath = null;
361
- screen.render();
362
- return;
363
- }
364
- if (selectedFile) {
365
- let content = '';
366
- let label = ` Diff (${selectedFile.path}) `;
367
-
368
- if (selectedFile.status !== 'unchanged' && selectedFile.status !== 'deleted') {
369
- const diff = await gitHandler.getDiff(selectedFile.path);
370
- content = formatDiffWithDiff2Html(diff, currentSearchTerm);
371
- }
372
-
373
- if (!content && selectedFile.status !== 'deleted') {
374
- content = await gitHandler.getFileContent(selectedFile.path);
375
- // Highlight search term in full content
376
- if (currentSearchTerm) {
377
- const regex = new RegExp(`(${currentSearchTerm.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
378
- content = content.replace(regex, `\x1b[43m\x1b[30m$1\x1b[0m`);
379
- }
380
- label = ` File (${selectedFile.path}) `;
381
- } else if (!content && selectedFile.status === 'deleted') {
382
- content = chalk.red('File was deleted.');
383
- label = ` Diff (${selectedFile.path}) `;
384
- }
385
-
386
- const currentContent = diffView.content;
387
- const currentLabel = diffView.label;
388
-
389
- // Only update if content or label changed to reduce flickering
390
- if (content !== currentContent || label !== currentLabel) {
391
- const savedScroll = diffView.scrollTop;
392
- const isNewFile = selectedFile.path !== lastSelectedPath;
393
-
394
- diffView.setContent(content);
395
- diffView.setLabel(label);
396
-
397
- if (isNewFile) {
398
- diffView.scrollTo(0);
399
- } else {
400
- diffView.scrollTop = savedScroll;
401
- }
402
- }
403
- lastSelectedPath = selectedFile.path;
404
- } else {
405
- const newContent = 'Select a file to view diff.';
406
- const newLabel = ' Diff () ';
407
- if (diffView.content !== newContent || diffView.label !== newLabel) {
408
- diffView.setContent(newContent);
409
- diffView.setLabel(newLabel);
410
- diffView.scrollTo(0);
411
- }
412
- lastSelectedPath = null;
413
- }
414
- screen.render();
415
- };
416
-
417
- const updateFileList = async () => {
418
- if (isUpdatingList) return;
419
- isUpdatingList = true;
420
-
421
- const selectedPath = currentFiles[fileList.selected]?.path;
422
-
423
- let files: FileStatus[];
424
-
425
- if (currentSearchTerm) {
426
- files = await gitHandler.searchFiles(currentSearchTerm);
427
- } else {
428
- files = await gitHandler.getStatus();
429
- }
430
-
431
- currentFiles = files;
432
-
433
- const items = files.map((f) => {
434
- let color = '{white-fg}';
435
- if (f.status === 'added') color = '{green-fg}';
436
- else if (f.status === 'deleted') color = '{red-fg}';
437
- else if (f.status === 'modified') color = '{blue-fg}';
438
- else if (f.status === 'unstaged') color = '{green-fg}';
439
- else if (f.status === 'unchanged') color = '{grey-fg}';
440
-
441
- let statusSymbol = '';
442
- if (f.status === 'added') statusSymbol = 'A ';
443
- else if (f.status === 'deleted') statusSymbol = 'D ';
444
- else if (f.status === 'modified') statusSymbol = 'M ';
445
- else if (f.status === 'unstaged') statusSymbol = '+ ';
446
- else if (f.status === 'unchanged') statusSymbol = ' ';
447
-
448
- return `${color}${statusSymbol}${f.path}{/}`;
449
- });
450
-
451
- const labelTitle = currentSearchTerm ? `Files (${files.length}) - Searching: "${currentSearchTerm}"` : `Files (${files.length})`;
452
-
453
- const newFilePaths = files.map(f => f.path);
454
- const selectedFileStillExists = selectedPath ? newFilePaths.includes(selectedPath) : false;
455
- const listChanged = lastFilePaths.length !== newFilePaths.length ||
456
- lastFilePaths.some(path => !newFilePaths.includes(path)) ||
457
- newFilePaths.some(path => !lastFilePaths.includes(path));
458
-
459
- if (items.length > 0) {
460
- const newSelectedIndex = selectedPath ? currentFiles.findIndex(f => f.path === selectedPath) : -1;
461
-
462
- const oldFileList = fileList;
463
- const newFileList = createFileList();
464
- newFileList.setLabel(labelTitle);
465
- newFileList.setItems(items);
466
- newFileList.select(newSelectedIndex >= 0 ? newSelectedIndex : 0);
467
-
468
- if (!selectedFileStillExists || listChanged) {
469
- lastSelectedPath = null;
470
- }
471
-
472
- lastFilePaths = newFilePaths;
473
-
474
- if (diffUpdateTimeout) {
475
- clearTimeout(diffUpdateTimeout);
476
- diffUpdateTimeout = null;
477
- }
478
- await updateDiff();
479
-
480
- oldFileList.destroy();
481
- screen.remove(oldFileList);
482
- fileList = newFileList;
483
- screen.append(fileList);
484
- } else {
485
- const oldFileList = fileList;
486
- const newFileList = createFileList();
487
- newFileList.setLabel(labelTitle);
488
- newFileList.setItems([]);
489
-
490
- diffView.setContent(currentSearchTerm ? `No files match "${currentSearchTerm}".` : 'No changes detected.');
491
- diffView.setLabel(' Diff () ');
492
- lastSelectedPath = null;
493
- lastFilePaths = [];
494
-
495
- oldFileList.destroy();
496
- screen.remove(oldFileList);
497
- fileList = newFileList;
498
- screen.append(fileList);
499
- }
500
-
501
- screen.render();
502
- isUpdatingList = false;
503
- };
504
-
505
-
506
-
507
- screen.key(['s'], () => {
508
- searchBox.show();
509
- searchBox.setFront();
510
- searchInput.setValue(currentSearchTerm);
511
- searchInput.focus();
512
- screen.render();
513
- });
514
-
515
- searchInput.on('submit', async (value: string) => {
516
- currentSearchTerm = (value || '').trim();
517
- searchBox.hide();
518
- fileList.focus();
519
- // Force immediate update
520
- await updateFileList();
521
- });
522
-
523
- searchInput.on('cancel', () => {
524
- searchBox.hide();
525
- fileList.focus();
526
- screen.render();
527
- });
528
-
529
- screen.key(['tab'], () => {
530
- if (screen.focused === fileList) {
531
- diffView.focus();
532
- } else {
533
- fileList.focus();
534
- }
535
- updateBorders();
536
- });
537
-
538
- screen.key(['left'], () => {
539
- if (screen.focused !== fileList) {
540
- fileList.focus();
541
- updateBorders();
542
- }
543
- });
544
-
545
- screen.key(['right'], () => {
546
- if (screen.focused !== diffView) {
547
- diffView.focus();
548
- updateBorders();
549
- }
550
- });
551
-
552
- screen.key(['enter'], () => {
553
- const selectedIndex = fileList.selected;
554
- const selectedFile = currentFiles[selectedIndex];
555
- if (selectedFile) {
556
- openInEditor(selectedFile.path);
557
- }
558
- });
559
-
560
- // Handle revert key press
561
- screen.key(['r'], async () => {
562
- const selectedIndex = fileList.selected;
563
- const selectedFile = currentFiles[selectedIndex];
564
- if (selectedFile) {
565
- confirmDialog.show();
566
- confirmDialog.setFront();
567
- confirmDialog.focus();
568
- screen.render();
569
- }
570
- });
571
-
572
- // Handle dialog keys
573
- confirmDialog.key(['space'], async () => {
574
- const selectedIndex = fileList.selected;
575
- const selectedFile = currentFiles[selectedIndex];
576
- if (selectedFile) {
577
- try {
578
- await gitHandler.revertFile(selectedFile.path);
579
- showNotification(`File ${selectedFile.path} reverted successfully.`, 'green');
580
- await updateFileList();
581
- } catch (error) {
582
- const errorMessage = error instanceof Error ? error.message : String(error);
583
- showNotification(`Error reverting file: ${errorMessage}`, 'red');
584
- }
585
- }
586
- confirmDialog.hide();
587
- fileList.focus();
588
- screen.render();
589
- });
590
-
591
- confirmDialog.key(['escape'], () => {
592
- confirmDialog.hide();
593
- fileList.focus();
594
- screen.render();
595
- });
596
-
597
- await updateBranch();
598
- await updateFileList();
599
- fileList.focus();
600
- updateBorders();
601
-
602
- // Set up periodic updates to detect new git changes
603
- const updateInterval = setInterval(async () => {
604
- const currentlyFocused = screen.focused;
605
- const wasFileListFocused = currentlyFocused === fileList;
606
-
607
- await updateBranch();
608
- await updateFileList();
609
-
610
- // Restore focus to the same pane it was on before the update
611
- if (wasFileListFocused) {
612
- fileList.focus();
613
- } else if (currentlyFocused === diffView) {
614
- diffView.focus();
615
- }
616
- updateBorders();
617
- }, 2000); // Check for changes every 2 seconds
618
-
619
- // Clean up interval on exit
620
- screen.key(['escape', 'q', 'C-c'], () => {
621
- // Don't handle ESC if confirmation dialog has focus or is visible
622
- if (screen.focused === confirmDialog || !confirmDialog.hidden) {
623
- return false;
624
- }
625
-
626
- if (!searchBox.hidden) {
627
- searchBox.hide();
628
- screen.render();
629
- fileList.focus();
630
- return false;
631
- } else {
632
- // Clear interval only when actually exiting
633
- clearInterval(updateInterval);
634
- screen.destroy();
635
- process.exit(0);
636
- }
637
- });
638
- }
639
-
640
- main().catch(err => {
641
- console.error(err);
642
- process.exit(1);
643
- });