Skip to content

Commit b53832c

Browse files
authoredFeb 23, 2022
Command Line: Add support for command continuation prefix (#3344)
1 parent 4eb928c commit b53832c

File tree

3 files changed

+119
-22
lines changed

3 files changed

+119
-22
lines changed
 

‎plugins/command-line/index.html

+92-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ <h1>How to use</h1>
2525

2626
<p>Add class <strong>command-line</strong> to your <code class="language-markup">&lt;pre></code>. For a server command line, specify the user and host names using the <code class="language-markup">data-user</code> and <code class="language-markup">data-host</code> attributes. The resulting prompt displays a <strong>#</strong> for the root user and <strong>$</strong> for all other users. For any other command line, such as a Windows prompt, you may specify the entire prompt using the <code class="language-markup">data-prompt</code> attribute.</p>
2727

28-
<p>Optional: You may specify the lines to be presented as output (no prompt and no highlighting) through the <code class="language-markup">data-output</code> attribute on the <code class="language-markup">&lt;pre></code> element in the following simple format:</p>
28+
<h2>Optional: Command output (positional)</h2>
29+
<p>You may specify the lines to be presented as output (no prompt and no highlighting) through the <code class="language-markup">data-output</code> attribute on the <code class="language-markup">&lt;pre></code> element in the following simple format:</p>
2930
<ul>
3031
<li>A single number refers to the line with that number</li>
3132
<li>Ranges are denoted by two numbers, separated with a hyphen (-)</li>
@@ -48,26 +49,60 @@ <h1>How to use</h1>
4849
<dd>Lines 1 through 2, line 5, lines 9 through 20</dd>
4950
</dl>
5051

51-
<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>
52+
<h2>Optional: Command output (prefix)</h2>
53+
<p>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>
5254

53-
<p>Output lines are user selectable by default, so if you select the whole content of the code block, it will select the shell commands and any output lines. This may not be desireable if you want to copy/paste just the commands and not the output. If you want to make the output not user selectable then add the following to your CSS:</p>
55+
<p>A blank line will render as an empty line with a prompt. If you want an empty line without a prompt then you can use a line containing just the output prefix, e.g. <code class="language-markup">(out)</code>. See the blank lines in the examples below.</p>
56+
57+
<p>Output lines are user selectable by default, so if you select the whole content of the code block, it will select the shell commands and any output lines. This may not be desirable if you want to copy/paste just the commands and not the output. If you want to make the output not user selectable then add the following to your CSS:</p>
5458

5559
<pre><code class="language-css">.command-line span.token.output {
5660
user-select: none;
5761
}</code></pre>
5862

59-
<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>
63+
<h2>Optional: Multi-line commands</h2>
64+
<p>You can configure the plugin to handle multi-line commands. This can be done in two ways; setting a line continuation string (as in Bash); or explicitly marking continuation lines with a prefix for languages that do not have a continuation string/character, e.g. SQL, Scala, etc..</p>
65+
66+
<dl>
67+
<dt><code class="language-markup">data-continuation-str</code></dt>
68+
<dd>Set this attribute to the line continuation string/character, e.g. for bash <code class="language-markup">data-continuation-str="\"</code></dd>
69+
70+
<dt><code class="language-markup">data-filter-continuation</code></dt>
71+
<dd>This works in a similar way to <code class="language-markup">data-filter-output</code>. Prefix all continuation lines with the value of <code class="language-markup">data-filter-continuation</code> and they will be displayed with the prompt set in <code class="language-markup">data-continuation-prompt</code>. For example, <code class="language-markup">data-filter-continuation="(con)"</code> will treat lines beginning with <code class="language-markup">(con)</code> as continuation lines and remove the prefix.</dd>
72+
73+
<dt><code class="language-markup">data-continuation-prompt</code></dt>
74+
<dd>Set this attribute to define the prompt to be displayed when the command has continued beyond the first line (whether using line continuation or command termination), e.g. for MySQL <code class="language-markup">data-continuation-prompt="-&gt;"</code>. If this attribute is not set then a default of <code class="language-markup">&gt;</code> will be used.</dd>
75+
</dl>
6076
</section>
6177

6278
<section>
6379
<h1>Examples</h1>
6480

81+
<h2>Default Use Without Output</h2>
82+
83+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;&gt;</code></pre>
84+
85+
<pre class="command-line"><code class="language-bash">cd ~/.vim
86+
87+
vim vimrc</code></pre>
88+
6589
<h2>Root User Without Output</h2>
90+
91+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
92+
data-user=&quot;root&quot;
93+
data-host=&quot;localhost&quot;&gt;</code></pre>
94+
6695
<pre class="command-line" data-user="root" data-host="localhost"><code class="language-bash">cd /usr/local/etc
6796
cp php.ini php.ini.bak
6897
vi php.ini</code></pre>
6998

7099
<h2>Non-Root User With Output</h2>
100+
101+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
102+
data-user=&quot;chris&quot;
103+
data-host=&quot;remotehost&quot;
104+
data-output=&quot;2, 4-8&quot;&gt;</code></pre>
105+
71106
<pre class="command-line" data-user="chris" data-host="remotehost" data-output="2, 4-8"><code class="language-bash">pwd
72107
/usr/home/chris/bin
73108
ls -la
@@ -78,6 +113,11 @@ <h2>Non-Root User With Output</h2>
78113
-rwxr-xr-x 1 chris chris 642 Jan 17 14:42 deploy</code></pre>
79114

80115
<h2>Windows PowerShell With Output</h2>
116+
117+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
118+
data-prompt=&quot;PS C:\Users\Chris&gt;&quot;
119+
data-output=&quot;2-19&quot;&gt;</code></pre>
120+
81121
<pre class="command-line" data-prompt="PS C:\Users\Chris>" data-output="2-19"><code class="language-powershell">dir
82122

83123

@@ -99,7 +139,14 @@ <h2>Windows PowerShell With Output</h2>
99139
d-r-- 10/14/2015 5:06 PM Videos</code></pre>
100140

101141
<h2>Line continuation with Output (bash)</h2>
102-
<pre class="command-line" data-filter-output="(out)" data-continuation-str="\" ><code class="language-bash">echo "hello"
142+
143+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
144+
data-filter-output=&quot;(out)&quot;
145+
data-continuation-str=&quot;\&quot; &gt;</code></pre>
146+
147+
148+
<pre class="command-line" data-filter-output="(out)" data-continuation-str="\" ><code class="language-bash">export MY_VAR=123
149+
echo "hello"
103150
(out)hello
104151
echo one \
105152
two \
@@ -110,14 +157,52 @@ <h2>Line continuation with Output (bash)</h2>
110157
(out)goodbye</code></pre>
111158

112159
<h2>Line continuation with Output (PowerShell)</h2>
113-
<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 `
160+
161+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
162+
data-prompt=&quot;ps c:\users\chris&gt;&quot;
163+
data-continuation-prompt=&quot;&gt;&gt;&quot;
164+
data-filter-output=&quot;(out)&quot;
165+
data-continuation-str=&quot; `&quot;&gt;</code></pre>
166+
167+
<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 `
114168
'Hello' `
115169
'from' `
116170
'PowerShell!'
117171
(out)Hello from PowerShell!
118172
Write-Host 'Goodbye from PowerShell!'
119173
(out)Goodbye from PowerShell!</code></pre>
120174

175+
<h2>Line continuation using prefix (MySQL/SQL)</h2>
176+
177+
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
178+
data-prompt=&quot;mysql&gt;&quot;
179+
data-continuation-prompt=&quot;-&gt;&quot;
180+
data-filter-output=&quot;(out)&quot;
181+
data-filter-continuation=&quot;(con)&quot;&gt;</code></pre>
182+
183+
<pre class="command-line" data-prompt="mysql>" data-continuation-prompt="->" data-filter-output="(out)" data-filter-continuation="(con)"><code class="language-sql">set @my_var = 'foo';
184+
set @my_other_var = 'bar';
185+
(out)
186+
CREATE TABLE people (
187+
(con)first_name VARCHAR(30) NOT NULL,
188+
(con)last_name VARCHAR(30) NOT NULL
189+
(con));
190+
(out)Query OK, 0 rows affected (0.09 sec)
191+
(out)
192+
insert into people
193+
(con)values ('John', 'Doe');
194+
(out)Query OK, 1 row affected (0.02 sec)
195+
(out)
196+
select *
197+
(con)from people
198+
(con)order by last_name;
199+
(out)+------------+-----------+
200+
(out)| first_name | last_name |
201+
(out)+------------+-----------+
202+
(out)| John | Doe |
203+
(out)+------------+-----------+
204+
(out)1 row in set (0.00 sec)</code></pre>
205+
121206
</section>
122207

123208
<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
@@ -129,6 +214,7 @@ <h2>Line continuation with Output (PowerShell)</h2>
129214
<script src="assets/code.js"></script>
130215
<script src="components/prism-bash.js"></script>
131216
<script src="components/prism-powershell.js"></script>
217+
<script src="components/prism-sql.js"></script>
132218

133219
</body>
134220
</html>

‎plugins/command-line/prism-command-line.js

+26-15
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,6 @@
7474

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

77-
var continuationLineIndicies = commandLine.continuationLineIndicies = new Set();
78-
var lineContinuationStr = pre.getAttribute('data-continuation-str');
79-
80-
// Identify code lines that are a continuation line and thus don't need
81-
// a prompt
82-
if (lineContinuationStr && codeLines.length > 1) {
83-
for (var j = 1; j < codeLines.length; j++) {
84-
if (codeLines.hasOwnProperty(j - 1)
85-
&& endsWith(codeLines[j - 1], lineContinuationStr)) {
86-
// Mark this line as being a continuation line
87-
continuationLineIndicies.add(j);
88-
}
89-
}
90-
}
91-
9277
commandLine.numberOfLines = codeLines.length;
9378
/** @type {string[]} */
9479
var outputLines = commandLine.outputLines = [];
@@ -127,6 +112,32 @@
127112
}
128113
}
129114

115+
var continuationLineIndicies = commandLine.continuationLineIndicies = new Set();
116+
var lineContinuationStr = pre.getAttribute('data-continuation-str');
117+
var continuationFilter = pre.getAttribute('data-filter-continuation');
118+
119+
// Identify code lines where the command has continued onto subsequent
120+
// lines and thus need a different prompt. Need to do this after the output
121+
// lines have been removed to ensure we don't pick up a continuation string
122+
// in an output line.
123+
for (var j = 0; j < codeLines.length; j++) {
124+
var line = codeLines[j];
125+
if (!line) {
126+
continue;
127+
}
128+
129+
// Record the next line as a continuation if this one ends in a continuation str.
130+
if (lineContinuationStr && endsWith(line, lineContinuationStr)) {
131+
continuationLineIndicies.add(j + 1);
132+
}
133+
// Record this line as a continuation if marked with a continuation prefix
134+
// (that we will remove).
135+
if (j > 0 && continuationFilter && startsWith(line, continuationFilter)) {
136+
codeLines[j] = line.slice(continuationFilter.length);
137+
continuationLineIndicies.add(j);
138+
}
139+
}
140+
130141
env.code = codeLines.join('\n');
131142
});
132143

‎plugins/command-line/prism-command-line.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.