feat(new tools): Shell Commands Formatter and Linearizer

This commit is contained in:
ShareVB committed 2026-02-15 17:16:23 +01:00
1 parent 4956df7650
commit c2f9e6249c
6 files changed
+282

No files matched your search

+13
View File
@@ -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',
});
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { formatShellCommand } from '@/utils/shell-formatter';
const defaultValue = 'docker run -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro --restart always --log-opt max-size=1g nginx';
function transformer(value: string) {
try {
return formatShellCommand(value);
}
catch (e: any) {
return `# ERROR: ${e.toString()}`;
}
}
</script>
<template>
<format-transformer
input-label="Shell commands to format"
:input-default="defaultValue"
input-placeholder="Put your shell commands to format here..."
output-label="Formatted shell commands"
output-language="bash"
:transformer="transformer"
/>
</template>
+13
View File
@@ -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',
});
@@ -0,0 +1,25 @@
<script setup lang="ts">
import { collapseBackslashLines } from '@/utils/shell-formatter';
const defaultValue = 'docker run -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro --restart always --log-opt max-size=1g nginx';
function transformer(value: string) {
try {
return collapseBackslashLines(value);
}
catch (e: any) {
return `# ERROR: ${e.toString()}`;
}
}
</script>
<template>
<format-transformer
input-label="Shell commands to linearize"
:input-default="defaultValue"
input-placeholder="Put your shell commands to linearize here..."
output-label="Linearized shell commands"
output-language="bash"
:transformer="transformer"
/>
</template>
+115
View File
@@ -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');
});
});
+91
View File
@@ -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');
}