Merge pull request #3377 from Succubyss/fnr Adds /replace

f155cc7e925977197db89af8eb9eb1889da4368f

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +60 -4Ignore whitespace
public/scripts/slash-commands.js+60 -4
@@ -59,7 +59,7 @@ import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockStat
59import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';59import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';
60import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';60import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
61import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';61import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';
62import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';62import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, regexFromString, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
63import { registerVariableCommands, resolveVariable } from './variables.js';63import { registerVariableCommands, resolveVariable } from './variables.js';
64import { background_settings } from './backgrounds.js';64import { background_settings } from './backgrounds.js';
65import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';65import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
@@ -1903,7 +1903,7 @@ export function initDefaultSlashCommands() {
1903 returns: 'uppercase string',1903 returns: 'uppercase string',
1904 unnamedArgumentList: [1904 unnamedArgumentList: [
1905 new SlashCommandArgument(1905 new SlashCommandArgument(
1906 'string', [ARGUMENT_TYPE.STRING], true, false,1906 'text to affect', [ARGUMENT_TYPE.STRING], true, false,
1907 ),1907 ),
1908 ],1908 ],
1909 helpString: 'Converts the provided string to uppercase.',1909 helpString: 'Converts the provided string to uppercase.',
@@ -1915,7 +1915,7 @@ export function initDefaultSlashCommands() {
1915 returns: 'lowercase string',1915 returns: 'lowercase string',
1916 unnamedArgumentList: [1916 unnamedArgumentList: [
1917 new SlashCommandArgument(1917 new SlashCommandArgument(
1918 'string', [ARGUMENT_TYPE.STRING], true, false,1918 'text to affect', [ARGUMENT_TYPE.STRING], true, false,
1919 ),1919 ),
1920 ],1920 ],
1921 helpString: 'Converts the provided string to lowercase.',1921 helpString: 'Converts the provided string to lowercase.',
@@ -1935,7 +1935,7 @@ export function initDefaultSlashCommands() {
1935 ],1935 ],
1936 unnamedArgumentList: [1936 unnamedArgumentList: [
1937 new SlashCommandArgument(1937 new SlashCommandArgument(
1938 'string', [ARGUMENT_TYPE.STRING], true, false,1938 'text to affect', [ARGUMENT_TYPE.STRING], true, false,
1939 ),1939 ),
1940 ],1940 ],
1941 helpString: `1941 helpString: `
@@ -1999,6 +1999,62 @@ export function initDefaultSlashCommands() {
1999 return '';1999 return '';
2000 },2000 },
2001 }));2001 }));
2002 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2003 name: 'replace',
2004 aliases: ['re'],
2005 callback: (async ({mode = 'literal', pattern, replacer = ''}, text) => {
2006 if (pattern === '')
2007 throw new Error("Argument of 'pattern=' cannot be empty");
2008 switch (mode) {
2009 case 'literal':
2010 return text.replaceAll(pattern, replacer);
2011 case 'regex':
2012 return text.replace(regexFromString(pattern), replacer);
2013 default:
2014 throw new Error("Invalid '/replace mode=' argument specified!");
2015 }
2016 }),
2017 returns: 'replaced text',
2018 namedArgumentList: [
2019 SlashCommandNamedArgument.fromProps({
2020 name: 'mode',
2021 description: 'Replaces occurrence(s) of a pattern',
2022 typeList: [ARGUMENT_TYPE.STRING],
2023 defaultValue: 'literal',
2024 enumList: ['literal', 'regex'],
2025 }),
2026 new SlashCommandNamedArgument(
2027 'pattern', 'pattern to search with', [ARGUMENT_TYPE.STRING], true, false,
2028 ),
2029 new SlashCommandNamedArgument(
2030 'replacer', 'replacement text for matches', [ARGUMENT_TYPE.STRING], false, false, '',
2031 ),
2032 ],
2033 unnamedArgumentList: [
2034 new SlashCommandArgument(
2035 'text to affect', [ARGUMENT_TYPE.STRING], true, false,
2036 ),
2037 ],
2038 helpString: `
2039 <div>
2040 Replaces text within the provided string based on the pattern.
2041 </div>
2042 <div>
2043 If <code>mode</code> is <code>literal</code> (or omitted), <code>pattern</code> is a literal search string (case-sensitive).<br />
2044 If <code>mode</code> is <code>regex</code>, <code>pattern</code> is parsed as an ECMAScript Regular Expression.<br />
2045 The <code>replacer</code> replaces based on the <code>pattern</code> in the input text.<br />
2046 If <code>replacer</code> is omitted, the replacement(s) will be an empty string.<br />
2047 </div>
2048 <div>
2049 <strong>Example:</strong>
2050 <pre>/let x Blue house and blue car || </pre>
2051 <pre>/replace pattern="blue" {{var::x}} | /echo |/# Blue house and car ||</pre>
2052 <pre>/replace pattern="blue" replacer="red" {{var::x}} | /echo |/# Blue house and red car ||</pre>
2053 <pre>/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}} | /echo |/# red house and blue car ||</pre>
2054 <pre>/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}} | /echo |/# red house and red car ||</pre>
2055 </div>
2056 `,
2057 }));
20022058
2003 registerVariableCommands();2059 registerVariableCommands();
2004}2060}