-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli_snippet.py
More file actions
185 lines (150 loc) Β· 5.12 KB
/
Copy pathcli_snippet.py
File metadata and controls
185 lines (150 loc) Β· 5.12 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# CLI snippet commands
snippet_commands = '''import fs from 'fs-extra';
import path from 'path';
import os from 'os';
import chalk from 'chalk';
import ora from 'ora';
import clipboardy from 'clipboardy';
import inquirer from 'inquirer';
/**
* Get snippets storage path
* @returns {string}
*/
function getSnippetsPath() {
return path.join(os.homedir(), '.devsuite', 'snippets.json');
}
/**
* Load snippets from storage
* @returns {Promise<Object>}
*/
async function loadSnippets() {
const snippetsPath = getSnippetsPath();
if (!await fs.pathExists(snippetsPath)) {
return {};
}
return await fs.readJson(snippetsPath);
}
/**
* Save snippets to storage
* @param {Object} snippets
*/
async function saveSnippets(snippets) {
const snippetsPath = getSnippetsPath();
await fs.ensureDir(path.dirname(snippetsPath));
await fs.writeJson(snippetsPath, snippets, { spaces: 2 });
}
/**
* Add a new snippet from clipboard
* @param {string} name - Snippet name
* @param {Object} options - Command options
*/
export async function snippetAddCommand(name, options) {
const spinner = ora('Reading clipboard...').start();
try {
const content = await clipboardy.read();
if (!content || content.trim().length === 0) {
spinner.fail(chalk.red('Clipboard is empty!'));
return;
}
spinner.text = 'Saving snippet...';
const snippets = await loadSnippets();
snippets[name] = {
content,
language: options.language || 'text',
createdAt: new Date().toISOString(),
size: content.length
};
await saveSnippets(snippets);
spinner.succeed(chalk.green(`β
Snippet "${name}" saved!`));
// Show preview
const preview = content.slice(0, 100).replace(/\\n/g, ' ');
console.log(chalk.dim(`\\nPreview: ${preview}${content.length > 100 ? '...' : ''}`));
console.log(chalk.gray(`Size: ${content.length} characters`));
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
console.log(chalk.yellow('\\nTip: Make sure you have copied content to clipboard'));
}
}
/**
* List all saved snippets
* @param {Object} options - Command options
*/
export async function snippetListCommand(options) {
const spinner = ora('Loading snippets...').start();
try {
const snippets = await loadSnippets();
const names = Object.keys(snippets);
spinner.stop();
if (names.length === 0) {
console.log(chalk.yellow('π No snippets saved yet.'));
console.log(chalk.dim('\\nTo add a snippet:'));
console.log(chalk.white(' devsuite snippet add <name>'));
return;
}
// Filter by language if specified
let filteredSnippets = names;
if (options.language) {
filteredSnippets = names.filter(name =>
snippets[name].language === options.language
);
}
console.log(chalk.bold(`\\nπ Saved Snippets (${filteredSnippets.length})\\n`));
// Group by language
const byLanguage = {};
filteredSnippets.forEach(name => {
const lang = snippets[name].language || 'text';
if (!byLanguage[lang]) byLanguage[lang] = [];
byLanguage[lang].push(name);
});
Object.entries(byLanguage).forEach(([lang, items]) => {
console.log(chalk.cyan(`${lang.toUpperCase()}:`));
items.forEach(name => {
const snippet = snippets[name];
const date = new Date(snippet.createdAt).toLocaleDateString();
const size = snippet.size || snippet.content.length;
console.log(chalk.white(` β’ ${name}`));
console.log(chalk.gray(` ${size} chars β’ ${date}`));
});
console.log();
});
console.log(chalk.dim('π‘ Use: devsuite snippet use <name> to copy to clipboard'));
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
}
/**
* Copy a snippet to clipboard
* @param {string} name - Snippet name
*/
export async function snippetUseCommand(name) {
const spinner = ora('Loading snippet...').start();
try {
const snippets = await loadSnippets();
if (!snippets[name]) {
spinner.fail(chalk.red(`Snippet "${name}" not found!`));
// Suggest similar names
const names = Object.keys(snippets);
const similar = names.filter(n =>
n.toLowerCase().includes(name.toLowerCase()) ||
name.toLowerCase().includes(n.toLowerCase())
);
if (similar.length > 0) {
console.log(chalk.yellow('\\nDid you mean:'));
similar.forEach(n => console.log(chalk.cyan(` β’ ${n}`)));
}
return;
}
const snippet = snippets[name];
await clipboardy.write(snippet.content);
spinner.succeed(chalk.green(`β
Copied "${name}" to clipboard!`));
// Show preview
const preview = snippet.content.slice(0, 150).replace(/\\n/g, ' ');
console.log(chalk.dim(`\\n${preview}${snippet.content.length > 150 ? '...' : ''}`));
} catch (error) {
spinner.fail(chalk.red(`Error: ${error.message}`));
}
}
'''
with open(f"{base_path}/packages/cli/src/commands/snippet.js", "w") as f:
f.write(snippet_commands)
print("β
CLI snippet commands created")