Skip to content

Commit

Permalink
Merge pull request #8 from Aman-zishan/feat/get-snippet-cli
Browse files Browse the repository at this point in the history
feat(CLI): CLI command to list and get snippets
  • Loading branch information
Aman-zishan authored Dec 28, 2023
2 parents bc5ad1b + d3d44cd commit 9cdd676
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 3 deletions.
18 changes: 18 additions & 0 deletions dist/CLI/get-snippet.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/CLI/get-snippet.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 60 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/CLI/get-snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import loadSnippets from '../core/get-snippets';

export default function getSnippet(title: string) {
const snippets = loadSnippets();

// Check if the snippet with the given name exists
if (!snippets.some((snippet: Snippet) => snippet.title === title)) {
return `Error: Snippet with name ${name} not found.`;
}

// Filter out the snippet with the given name
const snippet = snippets.filter(
(snippet: Snippet) => snippet.title === title,
);

return snippet;
}
40 changes: 40 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Command } from 'commander';
import figlet from 'figlet';
import listSnippets from './core/list-interface';
import { saveSnippet } from './core/save-snippet';
import loadSnippets from './core/get-snippets';
import highlight, { supportsLanguage } from 'cli-highlight';

const program = new Command();

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

Expand All @@ -41,3 +45,39 @@ if (options.save) {
if (options.list) {
listSnippets();
}

if (options.listAll) {
const snippets = loadSnippets();
const titles = snippets.map((snippet: Snippet) => snippet.title);
titles.forEach((title: string) => console.log(chalk.green(title)));
}

if (options.output) {
const snippets = loadSnippets();
console.log(options.output);
const title =
typeof options.output === 'string'
? options.output.split('_').join(' ').trim()
: '';
const snippet = snippets.filter(
(snippet: Snippet) =>
snippet.title.toLowerCase().trim() === title.toLowerCase(),
);
const supportLanguge = supportsLanguage(snippet[0].language.toLowerCase())
? snippet[0].language.toLowerCase()
: 'txt';
const highlightedCode = highlight(snippet[0].code, {
language: supportLanguge,
ignoreIllegals: true,
theme: {
keyword: chalk.hex('#8F00FF'),
literal: chalk.magenta,
function: chalk.blueBright,
string: chalk.greenBright,
number: chalk.cyan,
comment: chalk.green,
params: chalk.yellow,
},
});
console.log(highlightedCode);
}

0 comments on commit 9cdd676

Please sign in to comment.