-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Possibly useful mod for the command-line plugin #1021
Comments
@oletizi, I stumbled upon this while trying to find a way to have prompts for switch configuration examples. Having no JS experience, I took a stab at adapting your hack to the changes in the current command-line plugin but can't seem to get it working quite right. Whenever I configure multiple prompts, Here is a fiddle for reference Modified hack... if (promptText !== '') {
if (promptText.includes('|')) {
// promptText format : 1|myPromptText;2-3|myOtherPrompt;4,8|myThirdPrompt
// promptChunks: an array of
var promptChunks = promptText.split(';');
// prompts: an array of line number => prompt text
var prompts = [];
for (var i = 0; i < promptChunks.length; i++) {
// promptSections: 0 - line range; 1 - prompt value
var promptSections = promptChunks[i].split('|');
var promptValue = promptSections[1];
var promptRanges = promptSections[0].split(',');
for (var p = 0; p < promptRanges.length; p++) {
var promptRange = promptRanges[p].split('-');
var promptStart = parseInt(promptRange[0]);
var promptEnd = promptStart;
if (promptRange.length === 2) {
promptEnd = parseInt(promptRange[1]);
}
if (!isNaN(promptStart) && !isNaN(promptEnd)) {
for (var j = promptStart; j <= promptEnd; j++) {
var index = j-1;
prompts[index] = promptValue;
}
}
}
}
for (var i = 0; i < promptLines.length; i++) {
var promptText = typeof prompts[i] === "undefined" ? ": >" : prompts[i];
promptLines[i] = '<span data-prompt="' + promptText + '"></span>';
}
promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
} else {
promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
}
} else {
var user = getAttribute('data-user', 'user');
var host = getAttribute('data-host', 'localhost');
promptLines = promptLines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
} |
Well then, guess I just needed a clearer head; got it sorted out but would appreciate any validation. First, removed the redundant promptLines = promptLines.join(''); Then only ended up with one extra line being marked as undefined (or for (var i = 0; i < promptLines.length-1; i++) { Modified hack (working)... if (promptText !== '') {
if (promptText.includes('|')) {
// promptText format : 1|myPromptText;2-3|myOtherPrompt;4,8|myThirdPrompt
// promptChunks: an array of
var promptChunks = promptText.split(';');
// prompts: an array of line number => prompt text
var prompts = [];
for (var i = 0; i < promptChunks.length; i++) {
// promptSections: 0 - line range; 1 - prompt value
var promptSections = promptChunks[i].split('|');
var promptValue = promptSections[1];
var promptRanges = promptSections[0].split(',');
for (var p = 0; p < promptRanges.length; p++) {
var promptRange = promptRanges[p].split('-');
var promptStart = parseInt(promptRange[0]);
var promptEnd = promptStart;
if (promptRange.length === 2) {
promptEnd = parseInt(promptRange[1]);
}
if (!isNaN(promptStart) && !isNaN(promptEnd)) {
for (var j = promptStart; j <= promptEnd; j++) {
var index = j-1;
prompts[index] = promptValue;
}
}
}
}
for (var i = 0; i < promptLines.length-1; i++) {
var promptText = typeof prompts[i] === "undefined" ? ": >" : prompts[i];
promptLines[i] = '<span data-prompt="' + promptText + '"></span>';
}
promptLines = promptLines.join('');
} else {
promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
}
} else {
var user = getAttribute('data-user', 'user');
var host = getAttribute('data-host', 'localhost');
promptLines = promptLines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
} |
This would be a great enhancement as some routers / switches have several different command modes with their corresponding prompts. Cisco IOS Example: |
@VoIP-Dude - the code snippet I posted above works if you're comfortable modifying your local version yourself. I have an example of what you're talking about on my site. Given, it is a but cumbersome to manage with the <pre class="command-line language-cisco-nxos" data-prompt="1|NX-OSv(config)#;2|NX-OSv#">
<code>end
copy run start</code></pre> |
@derek-shnosh I tried modifying my local copy of prism.js (v1.21.0) with the snippet you posted on Mar 28, 2019 but couldn't get it to work. Are you changing the command-line plugin section in prism.js or adding new lines of code? Have you tested with PrismJS version 1.21.0? I'm not sure if the plugins and languages I'm using makes a difference either but here's a link to the custom version I'm using in case you wanted to have a crack at it and share as a fiddle ;) . Thanks! |
@VoIP-Dude I'm still using whatever version that was current-ish when I fumbled through the code to make the "tweak" work. The Fiddle from my original response has the entire I just took @oletizi's hack and tacked it on to the end of the script, then modified a couple lines to get it working; couldn't tell you which anymore to be honest. Could probably diff his hack and my tweak to find out what changed. |
Hi folks,
I needed to hack in support for multiple different prompts in a single command-line block to the command-line plugin. It's pretty straightforward (see code snippet below).
It adds an option to specify prompts on a per-line basis by setting the prompt attribute to something like this:
Of course, if you don't use the augmented prompt syntax, then the prompt syntax works the standard way.
You may or may not want to add support for this to the command-line module, but I thought since I found it useful, others may, too.
Cheers,
--Orion
The text was updated successfully, but these errors were encountered: