Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AUTOMATIC1111
GitHub Repository: AUTOMATIC1111/stable-diffusion-webui
Path: blob/master/extensions-builtin/prompt-bracket-checker/javascript/prompt-bracket-checker.js
2447 views
1
// Stable Diffusion WebUI - Bracket checker
2
// By Hingashi no Florin/Bwin4L & @akx
3
// Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs.
4
// If there's a mismatch, the keyword counter turns red and if you hover on it, a tooltip tells you what's wrong.
5
6
function checkBrackets(textArea, counterElt) {
7
var counts = {};
8
(textArea.value.match(/[(){}[\]]/g) || []).forEach(bracket => {
9
counts[bracket] = (counts[bracket] || 0) + 1;
10
});
11
var errors = [];
12
13
function checkPair(open, close, kind) {
14
if (counts[open] !== counts[close]) {
15
errors.push(
16
`${open}...${close} - Detected ${counts[open] || 0} opening and ${counts[close] || 0} closing ${kind}.`
17
);
18
}
19
}
20
21
checkPair('(', ')', 'round brackets');
22
checkPair('[', ']', 'square brackets');
23
checkPair('{', '}', 'curly brackets');
24
counterElt.title = errors.join('\n');
25
counterElt.classList.toggle('error', errors.length !== 0);
26
}
27
28
function setupBracketChecking(id_prompt, id_counter) {
29
var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea");
30
var counter = gradioApp().getElementById(id_counter);
31
32
if (textarea && counter) {
33
textarea.addEventListener("input", () => checkBrackets(textarea, counter));
34
}
35
}
36
37
onUiLoaded(function() {
38
setupBracketChecking('txt2img_prompt', 'txt2img_token_counter');
39
setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter');
40
setupBracketChecking('img2img_prompt', 'img2img_token_counter');
41
setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter');
42
});
43
44