Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command Line: Add support for line continuation #3326

Merged
merged 13 commits into from Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions plugins/command-line/index.html
Expand Up @@ -49,6 +49,8 @@ <h1>How to use</h1>
</dl>

<p>Optional: To automatically present some lines as output, you can prefix those lines with any string and specify the prefix using the <code class="language-markup">data-filter-output</code> attribute on the <code class="language-markup">&lt;pre></code> element. For example, <code class="language-markup">data-filter-output="(out)"</code> will treat lines beginning with <code class="language-markup">(out)</code> as output and remove the prefix.</p>

<p>Optional: For multi-line commands you can specify the <code class="language-markup">data-continuation-str</code> attribute on the <code class="language-markup">&lt;pre></code> element. For example, <code class="language-markup">data-continuation-str="\"</code> will treat lines ending with <code class="language-markup">\</code> as being continued on the following line. Continued lines will have a prompt as set by the attribute <code class="language-markup">data-continuation-prompt</code> or a default of <code class="language-markup">&gt;</code>.</p>
at055612 marked this conversation as resolved.
Show resolved Hide resolved
</section>

<section>
Expand Down Expand Up @@ -90,6 +92,25 @@ <h2>Windows PowerShell With Output</h2>
d-r-- 10/14/2015 5:06 PM Searches
d-r-- 10/14/2015 5:06 PM Videos</code></pre>

<h2>Line continuation with Output (bash)</h2>
<pre class="command-line" data-filter-output="(out)" data-continuation-str="\" ><code class="language-bash">echo "hello"
(out)hello
echo one \
two \
three
(out)one two three
echo "goodbye"
(out)goodbye</code></pre>

<h2>Line continuation with Output (PowerShell)</h2>
<pre class="command-line" data-prompt="PS C:\Users\Chris>" data-continuation-prompt=">>" data-filter-output="(out)" data-continuation-str=" `"><code class="language-powershell">Write-Host `
'Hello' `
'from' `
'PowerShell!'
(out)Hello from PowerShell!
Write-Host 'Goodbye from PowerShell!'
(out)Goodbye from PowerShell!</code></pre>

</section>

<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
Expand Down
5 changes: 5 additions & 0 deletions plugins/command-line/prism-command-line.css
Expand Up @@ -6,6 +6,7 @@
letter-spacing: -1px;
margin-right: 1em;
pointer-events: none;
text-align: right;

-webkit-user-select: none;
-moz-user-select: none;
Expand All @@ -31,3 +32,7 @@
.command-line-prompt > span[data-prompt]:before {
content: attr(data-prompt);
}

.command-line-prompt > span[data-continuation-prompt]:before {
content: attr(data-continuation-prompt);
}
53 changes: 33 additions & 20 deletions plugins/command-line/prism-command-line.js
Expand Up @@ -12,23 +12,6 @@
? function (s, p) { return s.startsWith(p); }
: function (s, p) { return s.indexOf(p) === 0; };

/**
* Repeats the given string some number of times.
*
* This is just a polyfill for `String.prototype.repeat`.
*
* @param {string} str
* @param {number} times
* @returns {string}
*/
function repeat(str, times) {
var s = '';
for (var i = 0; i < times; i++) {
s += str;
}
return s;
}

/**
* Returns whether the given hook environment has a command line info object.
*
Expand Down Expand Up @@ -79,6 +62,21 @@
}

var codeLines = env.code.split('\n');

/** @type {int[]} */
var continuationLineIdxs = commandLine.continuationLineIdxs = [];
at055612 marked this conversation as resolved.
Show resolved Hide resolved
var lineContinuationStr = pre.getAttribute('data-continuation-str');

// Identify code lines that are a continuation line and thus don't need
// a prompt
if (lineContinuationStr && codeLines.length > 1) {
for (var j = 1; j < codeLines.length; j++) {
if (codeLines.hasOwnProperty(j - 1) && codeLines[j - 1].endsWith(lineContinuationStr)) {
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
continuationLineIdxs.push(j);
}
}
}

commandLine.numberOfLines = codeLines.length;
/** @type {string[]} */
var outputLines = commandLine.outputLines = [];
Expand Down Expand Up @@ -168,15 +166,29 @@
}

// Create the "rows" that will become the command-line prompts. -- cwells
var promptLines;
var promptLines = '';
var rowCount = commandLine.numberOfLines || 0;
var promptText = getAttribute('data-prompt', '');
var promptLine;
if (promptText !== '') {
promptLines = repeat('<span data-prompt="' + promptText + '"></span>', rowCount);
promptLine = '<span data-prompt="' + promptText + '"></span>';
} else {
var user = getAttribute('data-user', 'user');
var host = getAttribute('data-host', 'localhost');
promptLines = repeat('<span data-user="' + user + '" data-host="' + host + '"></span>', rowCount);
promptLine = '<span data-user="' + user + '" data-host="' + host + '"></span>';
}

var continuationLineIdxs = commandLine.continuationLineIdxs || [];
var continuationPromptText = getAttribute('data-continuation-prompt', '>');
var continuationPromptLine = '<span data-continuation-prompt="' + continuationPromptText + '"></span>';

// Assemble all the appropriate prompt/continuation lines
for (var j = 0; j < rowCount; j++) {
if (continuationLineIdxs.includes(j)) {
at055612 marked this conversation as resolved.
Show resolved Hide resolved
promptLines += continuationPromptLine;
} else {
promptLines += promptLine;
}
}

// Create the wrapper element. -- cwells
Expand All @@ -200,3 +212,4 @@
});

}());
// vim: set tabstop=2 shiftwidth=2 noexpandtab:
at055612 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion plugins/command-line/prism-command-line.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/command-line/prism-command-line.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.