-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.js
More file actions
153 lines (124 loc) · 3.38 KB
/
Copy pathbook.js
File metadata and controls
153 lines (124 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const os = require('os');
const colorCodes = {
red: '\x1b[31m',
blue: '\x1b[34m',
green: '\x1b[32m',
reset: '\x1b[0m',
};
function getBookmarksPath() {
return path.join(os.homedir(), '.bookmarks.json');
}
function getRepoName() {
return path.basename(process.cwd());
}
function readBookmarks() {
const bookmarksPath = getBookmarksPath();
try {
if (fs.existsSync(bookmarksPath)) {
const data = fs.readFileSync(bookmarksPath, 'utf-8');
return JSON.parse(data);
}
} catch (err) {
console.error('Error reading bookmarks:', err.message);
}
return {};
}
function writeBookmarks(bookmarks) {
const bookmarksPath = getBookmarksPath();
try {
fs.writeFileSync(bookmarksPath, JSON.stringify(bookmarks, null, 2), 'utf-8');
} catch (err) {
console.error('Error saving bookmarks:', err.message);
process.exit(1);
}
}
function addBookmark(args) {
if (args.length < 2) {
console.log("Usage: book '<file-path>' <title> [color]");
return;
}
const filePath = args[0];
const title = args[1];
let color = 'blue'; // default color
if (args.length > 2) {
color = args[2];
}
// Validate color
if (!colorCodes[color]) {
console.log(`Invalid color '${color}'. Use: red, blue, or green`);
return;
}
const repo = getRepoName();
const bookmarks = readBookmarks();
// Create repo entry if it doesn't exist
if (!bookmarks[repo]) {
bookmarks[repo] = [];
}
// Add new bookmark
bookmarks[repo].push({
file: filePath,
title: title,
color: color,
});
writeBookmarks(bookmarks);
console.log(`✓ Bookmark added to ${repo}`);
}
function listBookmarks(args) {
const repo = getRepoName();
const bookmarks = readBookmarks();
const repoBookmarks = bookmarks[repo];
if (!repoBookmarks || repoBookmarks.length === 0) {
console.log(`No bookmarks found for repo '${repo}'`);
return;
}
let filterColor = null;
if (args.length > 0) {
filterColor = args[0];
if (!colorCodes[filterColor]) {
console.log(`Invalid color '${filterColor}'. Use: red, blue, or green`);
return;
}
}
console.log(`\n📚 Bookmarks for '${repo}':`);
console.log('─'.repeat(80));
let count = 0;
repoBookmarks.forEach((bm) => {
if (filterColor && bm.color !== filterColor) {
return;
}
const colorCode = colorCodes[bm.color];
const reset = colorCodes.reset;
console.log(`${colorCode}[${bm.color}]${reset} ${bm.title}`);
console.log(` → ${bm.file}`);
console.log();
count++;
});
if (count === 0) {
console.log(`No bookmarks found with color '${filterColor}'`);
} else {
console.log(`Total: ${count} bookmark(s)`);
}
}
function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Usage:');
console.log(" mark <file-path> <title> [color] - Add bookmark");
console.log(' mark list [color] - List bookmarks');
console.log('\nColors: red, blue, green');
return;
}
const command = args[0];
if (command === 'list') {
listBookmarks(args.slice(1));
} else if (command.startsWith('/') || command.startsWith('.')) {
// It's a file path (add command)
addBookmark(args);
} else {
console.log("Unknown command. Use 'mark list' or 'mark <file-path> <title> [color]'");
}
}
main();