From eecbd369d6c4d64803bea41defaf1894ac1c6d67 Mon Sep 17 00:00:00 2001 From: Dan Remollino Date: Mon, 27 Mar 2023 15:46:04 -0400 Subject: [PATCH] updated to v.1.10 --- FIGlet Text.novaextension/CHANGELOG.md | 5 + FIGlet Text.novaextension/README.md | 7 +- FIGlet Text.novaextension/Scripts/main.js | 121 ++++++++++++++++++++-- FIGlet Text.novaextension/extension.json | 115 ++++++++++++++++++-- 4 files changed, 228 insertions(+), 20 deletions(-) diff --git a/FIGlet Text.novaextension/CHANGELOG.md b/FIGlet Text.novaextension/CHANGELOG.md index 8c37462..db1845c 100644 --- a/FIGlet Text.novaextension/CHANGELOG.md +++ b/FIGlet Text.novaextension/CHANGELOG.md @@ -1,3 +1,8 @@ +## Version 1.10 +- added Borders option to add custom borders +- added Padding option to adjust spacing between outputted text and borders +- changed New Lines option to Margins + ## Version 1.9 - changed `Preview All Installed FIGlet Fonts` to `Generate FIGlet Font Collection Preview` - added font collection selection for `Generate FIGlet Font Collection Preview` diff --git a/FIGlet Text.novaextension/README.md b/FIGlet Text.novaextension/README.md index 07c6b06..aeecb34 100644 --- a/FIGlet Text.novaextension/README.md +++ b/FIGlet Text.novaextension/README.md @@ -5,8 +5,8 @@ - select FIGlet font - configure FIGlet text output options - auto comment converted text; supported syntaxes: CSS, HTML, Javascript, PHP, Typescript, and SCSS -- prepend/append new lines to converted text -- generate a custom preview of all installed FIGlet fonts +- add padding, borders, and margins +- generate a custom previews of all Homebrew installed font collections # Requirements @@ -15,6 +15,3 @@ FIGlet Text requires [FIGlet](http://www.figlet.org) to be installed locally. Th # How to Use FIGlet Text FIGlet Text will add a 'Convert Selection to FIGlet' menu item to the Editor menu. Make one or more selections in the editor and run the command. - -# Planned Features -- borders diff --git a/FIGlet Text.novaextension/Scripts/main.js b/FIGlet Text.novaextension/Scripts/main.js index e735e21..6982c34 100644 --- a/FIGlet Text.novaextension/Scripts/main.js +++ b/FIGlet Text.novaextension/Scripts/main.js @@ -66,7 +66,31 @@ nova.commands.register('figletTextEditor', editor => { justification: nova.config.get('figlet_text.justification', 'string'), } - let comment = nova.config.get('figlet_text.comment', 'boolean') + let bordersEnabled = nova.config.get('figlet_text.borders', 'boolean') + let borders = { + top: { + width: nova.config.get('figlet_text.borderTopWidth', 'number'), + padding: nova.config.get('figlet_text.borderTopPadding', 'number'), + char: nova.config.get('figlet_text.borderTopChar', 'string'), + }, + right: { + width: nova.config.get('figlet_text.borderRightWidth', 'number'), + padding: nova.config.get('figlet_text.borderRightPadding', 'number'), + char: nova.config.get('figlet_text.borderRightChar', 'string'), + }, + bottom: { + width: nova.config.get('figlet_text.borderBottomWidth', 'number'), + padding: nova.config.get('figlet_text.borderBottomPadding', 'number'), + char: nova.config.get('figlet_text.borderBottomChar', 'string'), + }, + left: { + width: nova.config.get('figlet_text.borderLeftWidth', 'number'), + padding: nova.config.get('figlet_text.borderLeftPadding', 'number'), + char: nova.config.get('figlet_text.borderLeftChar', 'string'), + } + } + + let commentsEnabled = nova.config.get('figlet_text.comment', 'boolean') let commentPadding = nova.config.get('figlet_text.commentPadding', 'number') let commentPaddingStr = nova.config.get('figlet_text.commentPaddingStr', 'string') const getCommentChars = () => { @@ -108,9 +132,93 @@ nova.commands.register('figletTextEditor', editor => { // easier to modify line by line; order of transformations matter let figletTextArr = figletText.split('\n') + // add borders if the option is enabled + if (bordersEnabled) { + let longestLine = 0 + figletTextArr.map(line => { if (line.length > longestLine) longestLine = line.length }) + + let additionalWidth = ((borders.left.width * borders.left.char.length) + borders.left.padding) + ((borders.right.width * borders.right.char.length) + borders.right.padding) + + // top/bottom transformations need to be buffered and applied + // after left/right transformations which are done line by and + // are affected by top/bottom transformations if done in place + let borderBuffer = { paddingTop: [], paddingBottom: [], widthTop: [], widthBottom: [] } + + for (const border in borders) { + if (borders[border].padding > 0) { + switch (border) { + case 'left': + figletTextArr = figletTextArr.map(line => { + if (!/^\s+$/.test(line)) return `${' '.repeat(borders[border].padding)}${line}` + }) + break + case 'right': + figletTextArr = figletTextArr.map(line => { + if (!/^\s+$/.test(line)) return `${line}${' '.repeat(borders[border].padding)}` + }) + break + case 'top': + if (borders.left.width === 0 && borders.right.width === 0) { + // subtract one; will Array.prototype.join('\n') before final editor output + borderBuffer.paddingTop.push([`${'\n'.repeat(borders[border].padding - 1)}`]) + } else { + for (let count = borders.top.padding; count; count--) { + borderBuffer.paddingTop.push( + borders.left.char.repeat(borders.left.width) + ' '.repeat(longestLine + borders.left.padding + borders.right.padding) + borders.left.char.repeat(borders.right.width) + ) + } + } + break + case 'bottom': + if (borders.left.width === 0 && borders.right.width === 0) { + // subtract one; will Array.prototype.join('\n') before final editor output + borderBuffer.paddingBottom.push([`${'\n'.repeat(borders[border].padding - 1)}`]) + } else { + for (let count = borders.bottom.padding; count; count--) { + borderBuffer.paddingBottom.push( + borders.left.char.repeat(borders.left.width) + ' '.repeat(longestLine + borders.left.padding + borders.right.padding) + borders.left.char.repeat(borders.right.width) + ) + } + } + break + } + } + + if (borders[border].width > 0) { + switch (border) { + case 'left': + figletTextArr = figletTextArr.map(line => { + if (!/^\s+$/.test(line)) { return `${borders[border].char.repeat(borders[border].width)}${line}` } + }) + break + case 'right': + figletTextArr = figletTextArr.map(line => { + if (!/^\s+$/.test(line)) { return `${line}${borders[border].char.repeat(borders[border].width)}` } + }) + break + case 'top': + for (let count = 0; count < borders[border].width; count++) { + borderBuffer.widthTop.push(`${borders[border].char.repeat(longestLine + additionalWidth)}`) + } + break + case 'bottom': + for (let count = 0; count < borders[border].width; count++) { + borderBuffer.widthBottom.push(`${borders[border].char.repeat(longestLine + additionalWidth)}`) + } + break + } + } + } + + if (!borderBuffer.paddingTop.empty) figletTextArr = borderBuffer.paddingTop.concat(figletTextArr) + if (!borderBuffer.paddingBottom.empty) figletTextArr = figletTextArr.concat(borderBuffer.paddingBottom) + if (!borderBuffer.widthTop.empty) figletTextArr = borderBuffer.widthTop.concat(figletTextArr) + if (!borderBuffer.widthBottom.empty) figletTextArr = figletTextArr.concat(borderBuffer.widthBottom) + } + // comment each line if the option is selected and a // comment structure is defined for the current syntax - if (comment && getCommentChars() !== null) { + if (commentsEnabled && getCommentChars() !== null) { // find the longest line so we can add whitespace to shorter // lines so closing comments line up if the syntax uses them let longestLine = 0 @@ -122,20 +230,21 @@ nova.commands.register('figletTextEditor', editor => { let linePadding = 0 if (line.length < longestLine && (getCommentChars().end !== '')) linePadding = longestLine - line.length - // return the fully commented and formatted array of strings + // return the commented line if not whitespace + if (/^\s+$/.test(line)) return '\n' return `${getCommentChars().start}${commentPaddingStr.repeat(commentPadding)}${line}${' '.repeat(linePadding)}${commentPaddingStr.repeat(commentPadding)}${getCommentChars().end}`.trimEnd() }) } // prepend/append new lines - if (prependNewLines > 0) figletTextArr = Array.of(`${'\n'.repeat(prependNewLines)}`).concat(figletTextArr) - if (appendNewLines > 0) figletTextArr = figletTextArr.concat(Array.of(`${'\n'.repeat(appendNewLines)}`)) + if (prependNewLines > 0) figletTextArr = [`${'\n'.repeat(prependNewLines)}`].concat(figletTextArr) + if (appendNewLines > 0) figletTextArr = figletTextArr.concat([`${'\n'.repeat(appendNewLines)}`]) // indent subsequent lines after the first if // the line with the selection was indented if (!indentRange.empty) { figletTextArr = figletTextArr.map((line, index) => { - if (index === 0) { return `${line}` } + if (index === 0) return `${line}` return `${indentText}${line}` }) } diff --git a/FIGlet Text.novaextension/extension.json b/FIGlet Text.novaextension/extension.json index d3435cd..9a98489 100644 --- a/FIGlet Text.novaextension/extension.json +++ b/FIGlet Text.novaextension/extension.json @@ -3,7 +3,7 @@ "name": "FIGlet Text", "organization": "Dan Remollino", "description": "Convert selected text to FIGlet. Great for adding readable text to the Minimap, creating l33t text headers, and organizing files.", - "version": "1.9", + "version": "1.10", "categories": ["commands", "formatters"], "entitlements": { @@ -18,7 +18,6 @@ "config": [ { "type": "section", - "required": false, "title": "Font", "children": [ { @@ -476,8 +475,7 @@ }, { "type": "section", - "required": false, - "title": "Text", + "title": "Text Output", "children": [ { "key": "figlet_text.outputWidth", @@ -511,12 +509,11 @@ }, { "type": "section", - "required": false, - "title": "Transformation", + "title": "Comments", "children": [ { "key": "figlet_text.comment", - "title": "Comment FIGlet Output", + "title": "Enable Comments", "description": "FIGlet Text will auto line comment the output. Currently supported in CSS, HTML, Javascript, PHP, Typescript, and SCSS syntaxes.", "type": "boolean", "default": true @@ -534,17 +531,117 @@ "description": "Text string to use as Comment Padding. Defaults to a single space.", "type": "string", "default": " " + } + ] + }, + { + "type": "section", + "title": "Borders", + "description": "Border width and style.", + "children": [ + { + "key": "figlet_text.borders", + "title": "Enable Borders", + "description": "Toggle border output without needing to change individual border settings. This option also toggles Padding output.", + "type": "bool", + "default": false }, + { + "key": "figlet_text.borderTopWidth", + "title": "Top Border Width", + "type": "number", + "default": 2 + }, + { + "key": "figlet_text.borderTopChar", + "title": "Top Border Character", + "type": "string", + "default": "#" + }, + { + "key": "figlet_text.borderRightWidth", + "title": "Right Border Width", + "type": "number", + "default": 3 + }, + { + "key": "figlet_text.borderRightChar", + "title": "Right Border Character", + "type": "string", + "default": "#" + }, + { + "key": "figlet_text.borderBottomWidth", + "title": "Bottom Border Width", + "type": "number", + "default": 2 + }, + { + "key": "figlet_text.borderBottomChar", + "title": "Bottom Border Character", + "type": "string", + "default": "#" + }, + { + "key": "figlet_text.borderLeftWidth", + "title": "Left Border Width", + "type": "number", + "default": 3 + }, + { + "key": "figlet_text.borderLeftChar", + "title": "Left Border Character", + "type": "string", + "default": "#" + } + ] + }, + { + "type": "section", + "title": "Padding", + "description": "Spacing between FIGlet text and border. The 'Enable Borders' option must be checked under the Borders section for these options to have any effect.", + "children": [ + { + "key": "figlet_text.borderTopPadding", + "title": "Top Padding", + "type": "number", + "default": 2 + }, + { + "key": "figlet_text.borderRightPadding", + "title": "Right Padding", + "type": "number", + "default": 5 + }, + { + "key": "figlet_text.borderBottomPadding", + "title": "Bottom Padding", + "type": "number", + "default": 2 + }, + { + "key": "figlet_text.borderLeftPadding", + "title": "Left Padding", + "type": "number", + "default": 5 + } + ] + }, + { + "type": "section", + "title": "Margins", + "description": "Spacing between FIGlet text and your content.", + "children": [ { "key": "figlet_text.prependNewLines", - "title": "Prepend New Lines", + "title": "Top Margin", "description": "Amount of new lines to prepend to the converted text.", "type": "number", "default": 0 }, { "key": "figlet_text.appendNewLines", - "title": "Append New Lines", + "title": "Bottom Margin", "description": "Amount of new lines to append to the converted text.", "type": "number", "default": 0