prompt-bracket-checker.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Stable Diffusion WebUI - Bracket Checker
  2. // By @Bwin4L, @akx, @w-e-w, @Haoming02
  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. function checkBrackets(textArea, counterElem) {
  6. const pairs = [
  7. ['(', ')', 'round brackets'],
  8. ['[', ']', 'square brackets'],
  9. ['{', '}', 'curly brackets']
  10. ];
  11. const counts = {};
  12. const errors = new Set();
  13. let i = 0;
  14. while (i < textArea.value.length) {
  15. let char = textArea.value[i];
  16. let escaped = false;
  17. while (char === '\\' && i + 1 < textArea.value.length) {
  18. escaped = !escaped;
  19. i++;
  20. char = textArea.value[i];
  21. }
  22. if (escaped) {
  23. i++;
  24. continue;
  25. }
  26. for (const [open, close, label] of pairs) {
  27. if (char === open) {
  28. counts[label] = (counts[label] || 0) + 1;
  29. } else if (char === close) {
  30. counts[label] = (counts[label] || 0) - 1;
  31. if (counts[label] < 0) {
  32. errors.add(`Incorrect order of ${label}.`);
  33. }
  34. }
  35. }
  36. i++;
  37. }
  38. for (const [open, close, label] of pairs) {
  39. if (counts[label] == undefined) {
  40. continue;
  41. }
  42. if (counts[label] > 0) {
  43. errors.add(`${open} ... ${close} - Detected ${counts[label]} more opening than closing ${label}.`);
  44. } else if (counts[label] < 0) {
  45. errors.add(`${open} ... ${close} - Detected ${-counts[label]} more closing than opening ${label}.`);
  46. }
  47. }
  48. counterElem.title = [...errors].join('\n');
  49. counterElem.classList.toggle('error', errors.size !== 0);
  50. }
  51. function setupBracketChecking(id_prompt, id_counter) {
  52. const textarea = gradioApp().querySelector(`#${id_prompt} > label > textarea`);
  53. const counter = gradioApp().getElementById(id_counter);
  54. if (textarea && counter) {
  55. onEdit(`${id_prompt}_BracketChecking`, textarea, 400, () => checkBrackets(textarea, counter));
  56. }
  57. }
  58. onUiLoaded(function() {
  59. setupBracketChecking('txt2img_prompt', 'txt2img_token_counter');
  60. setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter');
  61. setupBracketChecking('img2img_prompt', 'img2img_token_counter');
  62. setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter');
  63. });