Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CLI): CLI command to list and get snippets #8

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}