feat(truncate): add truncate command

This commit is contained in:
Nan72 2025-02-15 16:00:09 +07:00
parent 0a3bab20d8
commit 9198643934
No known key found for this signature in database
3 changed files with 68 additions and 1 deletions

View File

@ -14,6 +14,7 @@ const entrypoints = [
"./src/trim.ts",
"./src/trim-end.ts",
"./src/trim-start.ts",
"./src/uis/truncate.ts",
];
async function build() {

View File

@ -3,10 +3,11 @@
"name": "kunkun-ext-string-utils",
"version": "1.0.2",
"license": "MIT",
"author": "NaN72dev",
"type": "module",
"repository": "https://github.com/NaN72dev/kunkun-ext-string-utils",
"kunkun": {
"name": "String Utils",
"name": "String Utilities",
"shortDescription": "A string utility headless Extension",
"longDescription": "A string utility headless Extension",
"identifier": "kunkun-ext-string-utils",
@ -70,6 +71,13 @@
"main": "dist/trim-start.js",
"cmds": []
}
],
"templateUiCmds": [
{
"main": "dist/truncate.js",
"name": "Truncate a string to a maximum length",
"cmds": []
}
]
},
"scripts": {

58
src/uis/truncate.ts Normal file
View File

@ -0,0 +1,58 @@
import { Action, expose, Form, Icon, IconEnum, List, TemplateUiCommand, ui, clipboard, toast } from "@kksh/api/ui/template";
import { isEmpty } from "lodash";
import truncate from "lodash/truncate";
class TruncateExt extends TemplateUiCommand {
async onFormSubmit(value: Record<string, any>): Promise<void> {
const options: Parameters<typeof truncate>[1] = { omission: value.omission }
if (value.maxLength > 0)
// cannot pass `length: undefined` directly to options, lodash treats it as 0
options.length = value.maxLength + value.omission.length;
if (!isEmpty(value.separator))
// cannot pass `length: undefined` directly to options, lodash treats it as " " (space)
options.separator = value.separator;
const truncatedText = truncate(value.string, options);
await clipboard.writeText(truncatedText);
toast.success(`Copied "${truncatedText}"`);
}
async load() {
const clipboardText = await clipboard.readText();
return ui.render(
new Form.Form({
key: "truncateForm",
fields: [
new Form.InputField({
key: "string",
label: "Text",
placeholder: "The text to truncate",
default: clipboardText,
component: "textarea",
}),
new Form.NumberField({
key: "maxLength",
label: "Maximum length (not including omission, non-positive will be treated as 30)",
placeholder: "Maximum length to be truncated",
}),
new Form.InputField({
key: "separator",
label: "Separator (leave blank for none)",
placeholder: "The separator (or regex pattern) to truncate to",
}),
new Form.InputField({
key: "omission",
label: "Omission (the text to show when the text is truncated)",
placeholder: "The string to indicate text is omitted",
default: "..."
}),
],
})
);
}
}
expose(new TruncateExt())