Add /profile-get command
| @@ -75,6 +75,31 @@ const collapseSpaces = (s) => s.replace(/\s+/g, ' ').trim(); | ||
| 75 | 75 | const makeUnique = (s, f, i) => { if (!f(s)) { return s; } else { while (f(`${s} (${i})`)) { i++; } return `${s} (${i})`; } }; |
| 76 | 76 | |
| 77 | 77 | /** |
| 78 | + * Finds the best match for the search value. | |
| 79 | + * @param {string} value Search value | |
| 80 | + * @returns {ConnectionProfile|null} Best match or null | |
| 81 | + */ | |
| 82 | +function findProfileByName(value) { | |
| 83 | + // Try to find exact match | |
| 84 | + const profile = extension_settings.connectionManager.profiles.find(p => p.name === value); | |
| 85 | + | |
| 86 | + if (profile) { | |
| 87 | + return profile; | |
| 88 | + } | |
| 89 | + | |
| 90 | + // Try to find fuzzy match | |
| 91 | + const fuse = new Fuse(extension_settings.connectionManager.profiles, { keys: ['name'] }); | |
| 92 | + const results = fuse.search(value); | |
| 93 | + | |
| 94 | + if (results.length === 0) { | |
| 95 | + return null; | |
| 96 | + } | |
| 97 | + | |
| 98 | + const bestMatch = results[0]; | |
| 99 | + return bestMatch.item; | |
| 100 | +} | |
| 101 | + | |
| 102 | +/** | |
| 78 | 103 | * Reads the connection profile from the commands. |
| 79 | 104 | * @param {string} mode Mode of the connection profile |
| 80 | 105 | * @param {ConnectionProfile} profile Connection profile |
| @@ -374,27 +399,16 @@ async function renderDetailsContent(details, detailsContent) { | ||
| 374 | 399 | return NONE; |
| 375 | 400 | } |
| 376 | 401 | |
| 377 | - // Try to find exact match | |
| 402 | + const profile = findProfileByName(value); | |
| 378 | - const profile = extension_settings.connectionManager.profiles.find(p => p.name === value); | |
| 379 | - | |
| 380 | - if (profile) { | |
| 381 | - profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id); | |
| 382 | - profiles.dispatchEvent(new Event('change')); | |
| 383 | - return profile.name; | |
| 384 | - } | |
| 385 | - | |
| 386 | - // Try to find fuzzy match | |
| 387 | - const fuse = new Fuse(extension_settings.connectionManager.profiles, { keys: ['name'] }); | |
| 388 | - const results = fuse.search(value); | |
| 389 | 403 | |
| 390 | 404 | if (results.length === 0!profile) { |
| 391 | 405 | return ''; |
| 392 | 406 | } |
| 393 | 407 | |
| 394 | - const bestMatch = results[0]; | |
| 408 | + profiles.selectedIndex = Array.from(profiles.options).findIndex(o => o.value === profile.id); | |
| 395 | - profiles.value = bestMatch.item.id; | |
| 396 | 409 | profiles.dispatchEvent(new Event('change')); |
| 397 | - return bestMatch.item.name; | |
| 410 | + | |
| 411 | + return profiles.name; | |
| 398 | 412 | }, |
| 399 | 413 | })); |
| 400 | 414 | |
| @@ -448,4 +462,33 @@ async function renderDetailsContent(details, detailsContent) { | ||
| 448 | 462 | saveSettingsDebounced(); |
| 449 | 463 | }, |
| 450 | 464 | })); |
| 465 | + | |
| 466 | + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | |
| 467 | + name: 'profile-get', | |
| 468 | + helpString: 'Get the details of the connection profile. Returns the selected profile if no argument is provided.', | |
| 469 | + returns: 'object of the selected profile', | |
| 470 | + unnamedArgumentList: [ | |
| 471 | + SlashCommandArgument.fromProps({ | |
| 472 | + description: 'Name of the connection profile', | |
| 473 | + enumProvider: profilesProvider, | |
| 474 | + isRequired: false, | |
| 475 | + }), | |
| 476 | + ], | |
| 477 | + callback: async (_args, value) => { | |
| 478 | + if (!value || typeof value !== 'string') { | |
| 479 | + const selectedProfile = extension_settings.connectionManager.selectedProfile; | |
| 480 | + const profile = extension_settings.connectionManager.profiles.find(p => p.id === selectedProfile); | |
| 481 | + if (!profile) { | |
| 482 | + return ''; | |
| 483 | + } | |
| 484 | + return JSON.stringify(profile); | |
| 485 | + } | |
| 486 | + | |
| 487 | + const profile = findProfileByName(value); | |
| 488 | + if (!profile) { | |
| 489 | + return ''; | |
| 490 | + } | |
| 491 | + return JSON.stringify(profile); | |
| 492 | + }, | |
| 493 | + })); | |
| 451 | 494 | })(); |