add /replace command

371208c24d0964ecdeea26f0b4a60ea7c07ceeb7

Succubyss <87207237+Succubyss@users.noreply.github.com>

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