Skip to content

Commit d3d44cd

Browse files
committed
feat(CLI): CLI command to list and get snippets
1 parent bc5ad1b commit d3d44cd

File tree

6 files changed

+137
-3
lines changed

6 files changed

+137
-3
lines changed

dist/CLI/get-snippet.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/CLI/get-snippet.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 60 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CLI/get-snippet.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import loadSnippets from '../core/get-snippets';
2+
3+
export default function getSnippet(title: string) {
4+
const snippets = loadSnippets();
5+
6+
// Check if the snippet with the given name exists
7+
if (!snippets.some((snippet: Snippet) => snippet.title === title)) {
8+
return `Error: Snippet with name ${name} not found.`;
9+
}
10+
11+
// Filter out the snippet with the given name
12+
const snippet = snippets.filter(
13+
(snippet: Snippet) => snippet.title === title,
14+
);
15+
16+
return snippet;
17+
}

src/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Command } from 'commander';
55
import figlet from 'figlet';
66
import listSnippets from './core/list-interface';
77
import { saveSnippet } from './core/save-snippet';
8+
import loadSnippets from './core/get-snippets';
9+
import highlight, { supportsLanguage } from 'cli-highlight';
810

911
const program = new Command();
1012

@@ -19,6 +21,8 @@ program
1921
),
2022
)
2123
.option('-s, --save <filepath>', 'Save a code snippet')
24+
.option('-ls, --list-all', 'List all snippets')
25+
.option('-o, --output <snippet_title>', 'Output a particular snippet')
2226
.option('-l, --list', 'Open TUI')
2327
.parse(process.argv);
2428

@@ -41,3 +45,39 @@ if (options.save) {
4145
if (options.list) {
4246
listSnippets();
4347
}
48+
49+
if (options.listAll) {
50+
const snippets = loadSnippets();
51+
const titles = snippets.map((snippet: Snippet) => snippet.title);
52+
titles.forEach((title: string) => console.log(chalk.green(title)));
53+
}
54+
55+
if (options.output) {
56+
const snippets = loadSnippets();
57+
console.log(options.output);
58+
const title =
59+
typeof options.output === 'string'
60+
? options.output.split('_').join(' ').trim()
61+
: '';
62+
const snippet = snippets.filter(
63+
(snippet: Snippet) =>
64+
snippet.title.toLowerCase().trim() === title.toLowerCase(),
65+
);
66+
const supportLanguge = supportsLanguage(snippet[0].language.toLowerCase())
67+
? snippet[0].language.toLowerCase()
68+
: 'txt';
69+
const highlightedCode = highlight(snippet[0].code, {
70+
language: supportLanguge,
71+
ignoreIllegals: true,
72+
theme: {
73+
keyword: chalk.hex('#8F00FF'),
74+
literal: chalk.magenta,
75+
function: chalk.blueBright,
76+
string: chalk.greenBright,
77+
number: chalk.cyan,
78+
comment: chalk.green,
79+
params: chalk.yellow,
80+
},
81+
});
82+
console.log(highlightedCode);
83+
}

0 commit comments

Comments
 (0)