diff --git a/dist/CLI/get-snippet.js b/dist/CLI/get-snippet.js new file mode 100644 index 0000000..9ddca22 --- /dev/null +++ b/dist/CLI/get-snippet.js @@ -0,0 +1,18 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const get_snippets_1 = __importDefault(require("../core/get-snippets")); +function getSnippet(title) { + const snippets = (0, get_snippets_1.default)(); + // Check if the snippet with the given name exists + if (!snippets.some((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.title === title); + return snippet; +} +exports.default = getSnippet; +//# sourceMappingURL=get-snippet.js.map \ No newline at end of file diff --git a/dist/CLI/get-snippet.js.map b/dist/CLI/get-snippet.js.map new file mode 100644 index 0000000..ebbe9f5 --- /dev/null +++ b/dist/CLI/get-snippet.js.map @@ -0,0 +1 @@ +{"version":3,"file":"get-snippet.js","sourceRoot":"","sources":["../../src/CLI/get-snippet.ts"],"names":[],"mappings":";;;;;AAAA,wEAAgD;AAEhD,SAAwB,UAAU,CAAC,KAAa;IAC/C,MAAM,QAAQ,GAAG,IAAA,sBAAY,GAAE,CAAC;IAEhC,kDAAkD;IAClD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAgB,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,KAAK,KAAK,CAAC,EAAE,CAAC;QACnE,OAAO,4BAA4B,IAAI,aAAa,CAAC;IACtD,CAAC;IAED,6CAA6C;IAC7C,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAC9B,CAAC,OAAgB,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,KAAK,KAAK,CAC7C,CAAC;IAEF,OAAO,OAAO,CAAC;AAChB,CAAC;AAdD,6BAcC"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 5adec0e..8ad09af 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,28 @@ #! /usr/bin/env node "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; @@ -9,12 +32,17 @@ const commander_1 = require("commander"); const figlet_1 = __importDefault(require("figlet")); const list_interface_1 = __importDefault(require("./core/list-interface")); const save_snippet_1 = require("./core/save-snippet"); +const get_snippets_1 = __importDefault(require("./core/get-snippets")); +const cli_highlight_1 = __importStar(require("cli-highlight")); const program = new commander_1.Command(); +// CSM logo in CLI console.log(chalk_1.default.red(figlet_1.default.textSync('CSM', '3D Diagonal'))); program - .version('1.0.0') - .description(chalk_1.default.green('A CLI Code Snippet Manager tool for managing code snippets directly from your terminal currently supports JavaScript, TypeScript and Python')) + .version('1.3.0') + .description(chalk_1.default.green('A CLI Code Snippet Manager tool for managing code snippets directly from your terminal')) .option('-s, --save ', 'Save a code snippet') + .option('-ls, --list-all', 'List all snippets') + .option('-o, --output ', 'Output a particular snippet') .option('-l, --list', 'Open TUI') .parse(process.argv); const options = program.opts(); @@ -31,4 +59,34 @@ if (options.save) { if (options.list) { (0, list_interface_1.default)(); } +if (options.listAll) { + const snippets = (0, get_snippets_1.default)(); + const titles = snippets.map((snippet) => snippet.title); + titles.forEach((title) => console.log(chalk_1.default.green(title))); +} +if (options.output) { + const snippets = (0, get_snippets_1.default)(); + console.log(options.output); + const title = typeof options.output === 'string' + ? options.output.split('_').join(' ').trim() + : ''; + const snippet = snippets.filter((snippet) => snippet.title.toLowerCase().trim() === title.toLowerCase()); + const supportLanguge = (0, cli_highlight_1.supportsLanguage)(snippet[0].language.toLowerCase()) + ? snippet[0].language.toLowerCase() + : 'txt'; + const highlightedCode = (0, cli_highlight_1.default)(snippet[0].code, { + language: supportLanguge, + ignoreIllegals: true, + theme: { + keyword: chalk_1.default.hex('#8F00FF'), + literal: chalk_1.default.magenta, + function: chalk_1.default.blueBright, + string: chalk_1.default.greenBright, + number: chalk_1.default.cyan, + comment: chalk_1.default.green, + params: chalk_1.default.yellow, + }, + }); + console.log(highlightedCode); +} //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/index.js.map b/dist/index.js.map index 2ed29de..ba1747c 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAEA,kDAA0B;AAC1B,yCAAoC;AACpC,oDAA4B;AAC5B,2EAAiD;AACjD,sDAAkD;AAElD,MAAM,OAAO,GAAG,IAAI,mBAAO,EAAE,CAAC;AAE9B,OAAO,CAAC,GAAG,CAAC,eAAK,CAAC,GAAG,CAAC,gBAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC;AAE9D,OAAO;KACL,OAAO,CAAC,OAAO,CAAC;KAChB,WAAW,CACX,eAAK,CAAC,KAAK,CACV,6IAA6I,CAC7I,CACD;KACA,MAAM,CAAC,uBAAuB,EAAE,qBAAqB,CAAC;KACtD,MAAM,CAAC,YAAY,EAAE,UAAU,CAAC;KAChC,KAAK,CAAC,OAAO,CAAC,IA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AC,MAAM;SACpB;KACD,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;AAC9B,CAAC"} \ No newline at end of file diff --git a/src/CLI/get-snippet.ts b/src/CLI/get-snippet.ts new file mode 100644 index 0000000..6efa3eb --- /dev/null +++ b/src/CLI/get-snippet.ts @@ -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; +} diff --git a/src/index.ts b/src/index.ts index 036b35c..70c3411 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); @@ -19,6 +21,8 @@ program ), ) .option('-s, --save ', 'Save a code snippet') + .option('-ls, --list-all', 'List all snippets') + .option('-o, --output ', 'Output a particular snippet') .option('-l, --list', 'Open TUI') .parse(process.argv); @@ -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); +}