[READ-ONLY] Mirror of https://github.com/bombshell-dev/clack. Effortlessly build beautiful command-line apps bomb.sh/docs/clack/basics/getting-started/
cli command-line command-line-app node prompt prompts
5

Configure Feed

Select the types of activity you want to include in your feed.

add utils (wrap, strip, width) to core

Nate Moore (Feb 25, 2023, 6:09 PM -0600) fae41714 2be262bf

+332 -27
+46
examples/dev-server/index.ts
··· 1 + import { wrap } from '@clack/core'; 2 + import { stdin, stdout } from 'node:process'; 3 + import * as readline from 'readline'; 4 + import { cursor, erase } from 'sisteransi'; 5 + import color from 'picocolors'; 6 + 7 + function createLogger({ input = stdin, output = stdout } = {}) { 8 + let previousLineCount = 0; 9 + let previousWidth = output.columns ?? 80; 10 + let previousFormatted = ''; 11 + 12 + function log(message = '') { 13 + const width = output.columns ?? 80; 14 + let formatted = wrap(message, width) + '\n' 15 + let currLineCount = formatted.split('\n').length; 16 + if (previousLineCount > 0 && currLineCount > previousLineCount) { 17 + previousLineCount = wrap(previousFormatted, width).split('\n').length; 18 + } 19 + if (formatted === previousFormatted && previousWidth === width) { 20 + return; 21 + } 22 + previousFormatted = formatted; 23 + previousWidth = width; 24 + 25 + output.write(erase.lines(previousLineCount) + formatted); 26 + previousLineCount = formatted.split('\n').length; 27 + } 28 + 29 + return { log }; 30 + } 31 + 32 + const paragraph = `Lorem ipsum dolor sit ${color.red('amet')}, ${color.green('consectetur')} adipiscing elit. Fusce sed urna a ${color.inverse(' sem ')} aliquet efficitur sit amet quis turpis.\n\nDonec consectetur neque eget consequat ultricies. Integer ornare ipsum quis maximus tempor. Fusce quis sem eget dolor maximus sollicitudin quis sodales enim. Mauris a finibus risus, vitae porta dolor. Aliquam nec dui vitae dui fermentum dictum eget mattis mauris. Suspendisse condimentum sodales nisl, quis vestibulum dolor pretium vel. Etiam dignissim maximus leo, eget auctor felis congue non. Vestibulum venenatis mi sapien, nec sagittis dui finibus quis. Ut neque lacus, vestibulum nec congue non, finibus in lorem. Cras consectetur, neque non pretium luctus, orci massa auctor orci, id scelerisque velit mi quis erat. Proin a nibh placerat, sollicitudin dui ut, pretium nisi.`; 33 + 34 + const rl = readline.createInterface({ 35 + input: process.stdin, 36 + output: process.stdout, 37 + prompt: '' 38 + }); 39 + 40 + async function main() { 41 + const logger = createLogger(); 42 + logger.log(paragraph) 43 + process.stdout.on('resize', () => logger.log(paragraph)) 44 + } 45 + 46 + main();
+19
examples/dev-server/package.json
··· 1 + { 2 + "name": "@example/dev-server", 3 + "private": true, 4 + "version": "0.0.0", 5 + "type": "module", 6 + "dependencies": { 7 + "@clack/core": "^0.3.0", 8 + "@clack/logger": "workspace:*", 9 + "picocolors": "^1.0.0", 10 + "sisteransi": "^1.0.5", 11 + "string-width": "^5.1.2" 12 + }, 13 + "scripts": { 14 + "start": "jiti ./index.ts" 15 + }, 16 + "devDependencies": { 17 + "jiti": "^1.17.0" 18 + } 19 + }
+3
examples/dev-server/tsconfig.json
··· 1 + { 2 + "extends": "../../tsconfig.json" 3 + }
+1 -1
package.json
··· 27 27 }, 28 28 "packageManager": "pnpm@7.6.0", 29 29 "volta": { 30 - "node": "16.19.0" 30 + "node": "18.14.2" 31 31 } 32 32 }
+23
packages/core/LICENSE
··· 1 + MIT License 2 + 3 + Copyright (c) Nate Moore 4 + 5 + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 + 7 + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 + 9 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 + 11 + --- 12 + 13 + `ansi-regex` is adapted from https://github.com/chalk/ansi-regex 14 + 15 + MIT License 16 + 17 + Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) 18 + 19 + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 20 + 21 + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 22 + 23 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+1
packages/core/package.json
··· 52 52 "build": "unbuild" 53 53 }, 54 54 "dependencies": { 55 + "eastasianwidth": "^0.2.0", 55 56 "picocolors": "^1.0.0", 56 57 "sisteransi": "^1.0.5" 57 58 },
+1 -1
packages/core/src/index.ts
··· 7 7 export { default as SelectPrompt } from './prompts/select'; 8 8 export { default as SelectKeyPrompt } from './prompts/select-key'; 9 9 export { default as TextPrompt } from './prompts/text'; 10 - export { block } from './utils'; 10 + export * from './utils';
+81
packages/core/src/utils.ts
··· 3 3 import { stdin, stdout } from 'node:process'; 4 4 import * as readline from 'node:readline'; 5 5 import { cursor } from 'sisteransi'; 6 + // @ts-expect-error no types 7 + import ea from 'eastasianwidth'; 6 8 7 9 export function block({ 8 10 input = stdin, ··· 46 48 rl.close(); 47 49 }; 48 50 } 51 + 52 + // Adapted from https://github.com/chalk/ansi-regex 53 + // @see LICENSE 54 + function ansiRegex() { 55 + const pattern = [ 56 + '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', 57 + '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))', 58 + ].join('|'); 59 + 60 + return new RegExp(pattern, 'g'); 61 + } 62 + 63 + export function strip(str: string) { 64 + return str.replace(ansiRegex(), ''); 65 + } 66 + 67 + export function width(str: string): number { 68 + return Array.from(graphemes.segment(str)).map(({ segment: c }) => ea.characterLength(c)).reduce((a, b) => a + b, 0); 69 + } 70 + 71 + const words = new Intl.Segmenter([], { granularity: 'word' }); 72 + const graphemes = new Intl.Segmenter([], { granularity: 'grapheme' }); 73 + 74 + export function wrap(str: string, cols = process.stdout.columns): string { 75 + const parts: string[] = []; 76 + let part = ''; 77 + let i = 0; 78 + const handle = (chunk: string, { prefix = "", suffix = '' } = {}) => { 79 + for (const word of words.segment(chunk)) { 80 + if (word.segment === '\n') { 81 + parts.push(part) 82 + i = 0 83 + part = '' 84 + continue; 85 + } 86 + const len = width(word.segment); 87 + // Gracefully handle trailing punctuation by ensuring it is always joined with the previous word 88 + if (len === 1 && word.segment.trim() && /\W/.test(word.segment)) { 89 + part += word.segment; 90 + i += 1 91 + continue; 92 + } 93 + if ((i + len) > cols - 1) { 94 + parts.push(part.trim()) 95 + i = 0 96 + part = '' 97 + } 98 + part += `${prefix}${word.segment}${suffix}` 99 + i += len; 100 + } 101 + } 102 + 103 + const re = ansiRegex(); 104 + let lastIndex = 0; 105 + let lastPart = ""; 106 + let m; 107 + while (m = re.exec(str)) { 108 + const chunk = m.input.slice(lastIndex, m.index); 109 + const [prefix, suffix] = [lastPart, m[0]] 110 + if (prefix) { 111 + const [pCode, sCode] = [Number(prefix.slice(2, -1)), Number(suffix.slice(2, -1))] 112 + if (sCode > pCode) { 113 + handle(chunk, { prefix, suffix }); 114 + } else { 115 + handle(chunk) 116 + } 117 + } else { 118 + handle(chunk) 119 + } 120 + lastPart = m[0]; 121 + lastIndex = m.index + m[0].length; 122 + } 123 + const chunk = str.slice(lastIndex); 124 + handle(chunk); 125 + parts.push(part); 126 + 127 + return parts.map(p => /^\s{2,}/.test(p) ? p : p.trimStart()).join('\n'); 128 + } 129 +
+7
packages/logger/build.config.ts
··· 1 + import { defineBuildConfig } from 'unbuild'; 2 + 3 + // @see https://github.com/unjs/unbuild 4 + export default defineBuildConfig({ 5 + preset: '../../build.preset', 6 + entries: ['src/index'], 7 + });
+62
packages/logger/package.json
··· 1 + { 2 + "name": "@clack/logger", 3 + "version": "0.0.0", 4 + "type": "module", 5 + "main": "./dist/index.cjs", 6 + "module": "./dist/index.mjs", 7 + "exports": { 8 + ".": { 9 + "types": "./dist/index.d.ts", 10 + "import": "./dist/index.mjs", 11 + "require": "./dist/index.cjs" 12 + }, 13 + "./package.json": "./package.json" 14 + }, 15 + "types": "./dist/index.d.ts", 16 + "repository": { 17 + "type": "git", 18 + "url": "https://github.com/natemoo-re/clack", 19 + "directory": "packages/logger" 20 + }, 21 + "bugs": { 22 + "url": "https://github.com/natemoo-re/clack/issues" 23 + }, 24 + "homepage": "https://github.com/natemoo-re/clack/tree/main/packages/logger#readme", 25 + "files": [ 26 + "dist", 27 + "CHANGELOG.md" 28 + ], 29 + "keywords": [ 30 + "ask", 31 + "clack", 32 + "cli", 33 + "command-line", 34 + "command", 35 + "input", 36 + "interact", 37 + "interface", 38 + "menu", 39 + "prompt", 40 + "prompts", 41 + "stdin", 42 + "ui" 43 + ], 44 + "author": { 45 + "name": "Nate Moore", 46 + "email": "nate@natemoo.re", 47 + "url": "https://twitter.com/n_moore" 48 + }, 49 + "license": "MIT", 50 + "packageManager": "pnpm@7.6.0", 51 + "scripts": { 52 + "build": "unbuild" 53 + }, 54 + "dependencies": { 55 + "@clack/core": "workspace:*", 56 + "easta": "^7.0.0", 57 + "eastasianwidth": "^0.2.0" 58 + }, 59 + "devDependencies": { 60 + "unbuild": "^1.1.1" 61 + } 62 + }
+19
packages/logger/src/index.ts
··· 1 + import color from 'picocolors'; 2 + export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; 3 + const levels: Record<LogLevel, number> = { 4 + debug: 20, 5 + info: 30, 6 + warn: 40, 7 + error: 50, 8 + silent: 90, 9 + } 10 + 11 + // export interface LogOptions { 12 + // dest: LogWritable<LogMessage>; 13 + // level: LoggerLevel; 14 + // } 15 + 16 + // export const log = (chunk) => { 17 + // process.stdout.write('' + chunk + "\n"); 18 + // } 19 +
+3
packages/logger/tsconfig.json
··· 1 + { 2 + "extends": "../../tsconfig.json" 3 + }
-14
packages/prompts/LICENSE
··· 7 7 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 8 9 9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 - 11 - --- 12 - 13 - `ansi-regex` is adapted from https://github.com/chalk/ansi-regex 14 - 15 - MIT License 16 - 17 - Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) 18 - 19 - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 20 - 21 - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 22 - 23 - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-11
packages/prompts/src/index.ts
··· 640 640 }; 641 641 }; 642 642 643 - // Adapted from https://github.com/chalk/ansi-regex 644 - // @see LICENSE 645 - function ansiRegex() { 646 - const pattern = [ 647 - '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', 648 - '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))', 649 - ].join('|'); 650 - 651 - return new RegExp(pattern, 'g'); 652 - } 653 - 654 643 export type PromptGroupAwaitedReturn<T> = { 655 644 [P in keyof T]: Exclude<Awaited<T[P]>, symbol>; 656 645 };
+66
pnpm-lock.yaml
··· 46 46 devDependencies: 47 47 jiti: 1.17.0 48 48 49 + examples/dev-server: 50 + specifiers: 51 + '@clack/core': ^0.3.0 52 + '@clack/logger': workspace:* 53 + jiti: ^1.17.0 54 + picocolors: ^1.0.0 55 + sisteransi: ^1.0.5 56 + string-width: ^5.1.2 57 + dependencies: 58 + '@clack/core': link:../../packages/core 59 + '@clack/logger': link:../../packages/logger 60 + picocolors: 1.0.0 61 + sisteransi: 1.0.5 62 + string-width: 5.1.2 63 + devDependencies: 64 + jiti: 1.17.0 65 + 49 66 packages/core: 50 67 specifiers: 68 + eastasianwidth: ^0.2.0 51 69 picocolors: ^1.0.0 52 70 sisteransi: ^1.0.5 53 71 unbuild: ^1.1.1 54 72 dependencies: 73 + eastasianwidth: 0.2.0 55 74 picocolors: 1.0.0 56 75 sisteransi: 1.0.5 76 + devDependencies: 77 + unbuild: 1.1.1 78 + 79 + packages/logger: 80 + specifiers: 81 + '@clack/core': workspace:* 82 + easta: ^7.0.0 83 + eastasianwidth: ^0.2.0 84 + unbuild: ^1.1.1 85 + dependencies: 86 + '@clack/core': link:../core 87 + easta: 7.0.0 88 + eastasianwidth: 0.2.0 57 89 devDependencies: 58 90 unbuild: 1.1.1 59 91 ··· 1105 1137 engines: {node: '>=8'} 1106 1138 dev: true 1107 1139 1140 + /ansi-regex/6.0.1: 1141 + resolution: {integrity: sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==} 1142 + engines: {node: '>=12'} 1143 + dev: false 1144 + 1108 1145 /ansi-styles/3.2.1: 1109 1146 resolution: {integrity: sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==} 1110 1147 engines: {node: '>=4'} ··· 1405 1442 path-type: 4.0.0 1406 1443 dev: true 1407 1444 1445 + /easta/7.0.0: 1446 + resolution: {integrity: sha512-caC2nQJKBc21OP3tsltT2eQPntubPtPdios67ZOQseyw+29UfdIM5aFVZ7Dh74+ZEhTJx8RLOckp8mY+NI+CTw==} 1447 + engines: {node: '>=14'} 1448 + dev: false 1449 + 1450 + /eastasianwidth/0.2.0: 1451 + resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} 1452 + dev: false 1453 + 1408 1454 /editorconfig/0.15.3: 1409 1455 resolution: {integrity: sha512-M9wIMFx96vq0R4F+gRpY3o2exzb8hEj/n9S8unZtHSvYjibBp/iMufSzvmOcV/laG0ZtuTVGtiJggPOSW2r93g==} 1410 1456 hasBin: true ··· 1422 1468 /emoji-regex/8.0.0: 1423 1469 resolution: {integrity: sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==} 1424 1470 dev: true 1471 + 1472 + /emoji-regex/9.2.2: 1473 + resolution: {integrity: sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==} 1474 + dev: false 1425 1475 1426 1476 /enquirer/2.3.6: 1427 1477 resolution: {integrity: sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==} ··· 2726 2776 strip-ansi: 6.0.1 2727 2777 dev: true 2728 2778 2779 + /string-width/5.1.2: 2780 + resolution: {integrity: sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA==} 2781 + engines: {node: '>=12'} 2782 + dependencies: 2783 + eastasianwidth: 0.2.0 2784 + emoji-regex: 9.2.2 2785 + strip-ansi: 7.0.1 2786 + dev: false 2787 + 2729 2788 /string.prototype.trimend/1.0.6: 2730 2789 resolution: {integrity: sha512-JySq+4mrPf9EsDBEDYMOb/lM7XQLulwg5R/m1r0PXEFqrV0qHvl58sdTilSXtKOflCsK2E8jxf+GKC0T07RWwQ==} 2731 2790 dependencies: ··· 2748 2807 dependencies: 2749 2808 ansi-regex: 5.0.1 2750 2809 dev: true 2810 + 2811 + /strip-ansi/7.0.1: 2812 + resolution: {integrity: sha512-cXNxvT8dFNRVfhVME3JAe98mkXDYN2O1l7jmcwMnOslDeESg1rF/OZMtK0nRAhiari1unG5cD4jG3rapUAkLbw==} 2813 + engines: {node: '>=12'} 2814 + dependencies: 2815 + ansi-regex: 6.0.1 2816 + dev: false 2751 2817 2752 2818 /strip-bom/3.0.0: 2753 2819 resolution: {integrity: sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==}