From c2f9e6249cda9911e2cb9fc7d60daf942799f230 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sun, 15 Feb 2026 17:13:43 +0100 Subject: [PATCH] feat(new tools): Shell Commands Formatter and Linearizer --- src/tools/shell-formatter/index.ts | 13 ++ src/tools/shell-formatter/shell-formatter.vue | 25 ++++ src/tools/shell-linearizer/index.ts | 13 ++ .../shell-linearizer/shell-linearizer.vue | 25 ++++ src/utils/shell-formatter.test.ts | 115 ++++++++++++++++++ src/utils/shell-formatter.ts | 91 ++++++++++++++ 6 files changed, 282 insertions(+) create mode 100644 src/tools/shell-formatter/index.ts create mode 100644 src/tools/shell-formatter/shell-formatter.vue create mode 100644 src/tools/shell-linearizer/index.ts create mode 100644 src/tools/shell-linearizer/shell-linearizer.vue create mode 100644 src/utils/shell-formatter.test.ts create mode 100644 src/utils/shell-formatter.ts diff --git a/src/tools/shell-formatter/index.ts b/src/tools/shell-formatter/index.ts new file mode 100644 index 00000000..f427097f --- /dev/null +++ b/src/tools/shell-formatter/index.ts @@ -0,0 +1,13 @@ +import { Terminal2 } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Shell Commands Formatter', + path: '/shell-formatter', + description: 'Format shell commands as multiline shell commands split on arguments', + keywords: ['shell', 'multiline', 'formatter'], + component: () => import('./shell-formatter.vue'), + icon: Terminal2, + createdAt: new Date('2026-02-14'), + category: 'Default', +}); diff --git a/src/tools/shell-formatter/shell-formatter.vue b/src/tools/shell-formatter/shell-formatter.vue new file mode 100644 index 00000000..3e97c92f --- /dev/null +++ b/src/tools/shell-formatter/shell-formatter.vue @@ -0,0 +1,25 @@ + + + diff --git a/src/tools/shell-linearizer/index.ts b/src/tools/shell-linearizer/index.ts new file mode 100644 index 00000000..1fec2686 --- /dev/null +++ b/src/tools/shell-linearizer/index.ts @@ -0,0 +1,13 @@ +import { Terminal2 } from '@vicons/tabler'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Shell Commands Linearizer', + path: '/shell-linearizer', + description: 'Linearize multiline shell commands', + keywords: ['shell', 'multiline', 'linearizer'], + component: () => import('./shell-linearizer.vue'), + icon: Terminal2, + createdAt: new Date('2026-02-14'), + category: 'Default', +}); diff --git a/src/tools/shell-linearizer/shell-linearizer.vue b/src/tools/shell-linearizer/shell-linearizer.vue new file mode 100644 index 00000000..606f1d89 --- /dev/null +++ b/src/tools/shell-linearizer/shell-linearizer.vue @@ -0,0 +1,25 @@ + + + diff --git a/src/utils/shell-formatter.test.ts b/src/utils/shell-formatter.test.ts new file mode 100644 index 00000000..f1d6a56d --- /dev/null +++ b/src/utils/shell-formatter.test.ts @@ -0,0 +1,115 @@ +import { describe, expect, it } from 'vitest'; +import { collapseBackslashLines, formatShellCommand } from './shell-formatter'; + +describe('collapseBackslashLines', () => { + it('collapses lines ending with backslash', () => { + const input = ` +echo hello \\ + world +`; + expect(collapseBackslashLines(input)).toBe(` +echo hello world +`.trim()); + }); + + it('keeps comments untouched', () => { + const input = ` +# comment +echo test +`; + expect(collapseBackslashLines(input)).toBe(` +# comment +echo test +`.trim()); + }); + + it('keeps unrelated lines as-is', () => { + const input = ` +line1 +line2 +`; + expect(collapseBackslashLines(input)).toBe(` +line1 +line2 +`.trim()); + }); + + it('collapses multiple continuation lines', () => { + const input = ` +cmd a \\ + b \\ + c +`; + expect(collapseBackslashLines(input)).toBe('cmd a b c'); + }); + + it('handles mixed comments and commands', () => { + const input = ` +# before +cmd a \\ + b +# after +`; + expect(collapseBackslashLines(input)).toBe(` +# before +cmd a b +# after +`.trim()); + }); +}); + +describe('formatShellCommand', () => { + it('returns empty string for empty input', () => { + expect(formatShellCommand('')).toBe(''); + }); + + it('groups arguments with values when groupArgs=true', () => { + const cmd = 'docker run -d --name mycontainer -p 8080:80 myimage'; + const result = formatShellCommand(cmd); + + expect(result).toBe( + [ + 'docker run \\', + ' -d \\', + ' --name mycontainer \\', + ' -p 8080:80 \\', + ' myimage', + ].join('\n'), + ); + }); + + it('respects custom indentation', () => { + const cmd = 'docker run -d --name mycontainer -p 8080:80 myimage'; + const result = formatShellCommand(cmd, 4); + + expect(result).toBe( + [ + 'docker run \\', + ' -d \\', + ' --name mycontainer \\', + ' -p 8080:80 \\', + ' myimage', + ].join('\n'), + ); + }); + + it('handles quoted arguments correctly', () => { + const cmd = 'docker run --label "some label with spaces" myimage'; + const result = formatShellCommand(cmd); + + expect(result).toBe( + [ + 'docker run \\', + ' --label "some label with spaces" \\', + ' myimage', + ].join('\n'), + ); + }); + + it('handles base command with no args', () => { + const cmd = 'echo hello'; + const result = formatShellCommand(cmd); + + expect(result).toBe('echo hello'); + }); +}); diff --git a/src/utils/shell-formatter.ts b/src/utils/shell-formatter.ts new file mode 100644 index 00000000..5ba7f14f --- /dev/null +++ b/src/utils/shell-formatter.ts @@ -0,0 +1,91 @@ +/** + * Formats a shell command by splitting dash arguments onto new lines, + * with optional grouping and line wrapping. + */ +export function formatShellCommand( + commands: string, + indentSize: number = 2, +): string { + const indent = ' '.repeat(indentSize); + + return collapseBackslashLines(commands).split(/\r?\n/).map((command) => { + if (!command?.trim() || command.trimStart().startsWith('#')) { + return command; + } + + // Split by whitespace while respecting quoted strings + const args = command.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g) || []; + if (args.length === 0) { + return command; + } + + const formatted: string[] = []; + + let hadArgument = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + + if (arg.startsWith('-')) { + if (!hadArgument && formatted.length) { + formatted[formatted.length - 1] += ' \\'; + } + hadArgument = true; + if (i + 1 < args.length && !args[i + 1].startsWith('-')) { + formatted.push(`${indent}${arg} ${args[i + 1]} \\`); + i++; + } + else { + formatted.push(`${indent}${arg} \\`); + } + } + else if (hadArgument) { + formatted.push(`${indent}${arg}`); + } + else { + if (formatted.length) { + formatted[0] = `${formatted[0]} ${arg}`; + } + else { + formatted.push(arg); + } + } + } + + return formatted.join('\n'); + }).join('\n'); +} + +export function collapseBackslashLines(input: string): string { + const lines = input.trim().split(/\r?\n/); + + const out: string[] = []; + let buffer: string[] = []; + + const flush = () => { + if (buffer.length > 0) { + out.push(buffer.join(' ').trim()); + buffer = []; + } + }; + + for (const raw of lines) { + const line = raw.trimEnd(); + + if (line.endsWith('\\')) { + // Remove trailing backslash and continue accumulating + buffer.push(line.slice(0, -1).trim()); + } + else { + if (buffer.length > 0) { + buffer.push(line.trim()); + flush(); + } + else { + out.push(line); + } + } + } + + flush(); + return out.join('\n'); +}