Support wildcard extension in arbitrary folder

Now no longer only looks in "extensions/wildcards/widlcards".
This enables the user to call the folder whatever they like.
Fixes #37
This commit is contained in:
Dominik Reh
2022-10-28 15:06:43 +02:00
parent dc77b3f17f
commit 3e71890489
2 changed files with 25 additions and 11 deletions

View File

@@ -365,6 +365,8 @@ function updateSelectionStyle(textArea, newIndex, oldIndex) {
}
}
var wcBasePath = "";
var wcExtBasePath = "";
var wildcardFiles = [];
var wildcardExtFiles = [];
var embeddings = [];
@@ -413,15 +415,15 @@ function autocomplete(textArea, prompt, fixedTag = null) {
let wcFile = wcMatch[0][1];
let wcWord = wcMatch[0][2];
let wcBasePath = "";
var basePath = "";
if (wildcardExtFiles.includes(wcFile))
wcBasePath = "extensions/wildcards/wildcards";
basePath = wcExtBasePath;
else if (wildcardFiles.includes(wcFile))
wcBasePath = "scripts/wildcards";
basePath = wcBasePath;
else
throw "No valid wildcard file found";
let wildcards = readFile(`file/${wcBasePath}/${wcFile}.txt`).split("\n")
let wildcards = readFile(`file/${basePath}/${wcFile}.txt`).split("\n")
.filter(x => x.trim().length > 0); // Remove empty lines
results = wildcards.filter(x => (wcWord !== null && wcWord.length > 0) ? x.toLowerCase().includes(wcWord) : x) // Filter by tagword
@@ -432,7 +434,6 @@ function autocomplete(textArea, prompt, fixedTag = null) {
if (tagword !== "__") {
let lmb = (x) => x.toLowerCase().includes(tagword.replace("__", ""))
tempResults = wildcardFiles.filter(lmb).concat(wildcardExtFiles.filter(lmb)) // Filter by tagword
} else {
tempResults = wildcardFiles.concat(wildcardExtFiles);
}
@@ -612,10 +613,15 @@ onUiUpdate(function () {
// Load wildcards
if (wildcardFiles.length === 0 && acConfig.useWildcards) {
try {
wildcardFiles = readFile("file/tags/temp/wc.txt").split("\n")
let wcFileArr = readFile("file/tags/temp/wc.txt").split("\n");
wcBasePath = wcFileArr[0]; // First line should be the base path
wildcardFiles = wcFileArr.slice(1)
.filter(x => x.trim().length > 0) // Remove empty lines
.map(x => x.trim().replace(".txt", "")); // Remove file extension & newlines
wildcardExtFiles = readFile("file/tags/temp/wce.txt").split("\n")
let wcExtFileArr = readFile("file/tags/temp/wce.txt").split("\n");
wcExtBasePath = wcExtFileArr[0]; // First line should be the base path
wildcardExtFiles = wcExtFileArr.slice(1)
.filter(x => x.trim().length > 0) // Remove empty lines
.map(x => x.trim().replace(".txt", "")); // Remove file extension & newlines
} catch (e) {

View File

@@ -6,12 +6,20 @@ from pathlib import Path
# The path to the folder containing the wildcards and embeddings
FILE_DIR = Path().absolute()
WILDCARD_PATH = FILE_DIR.joinpath('scripts/wildcards')
WILDCARD_EXT_PATH = FILE_DIR.joinpath('extensions/wildcards/wildcards')
EMB_PATH = FILE_DIR.joinpath('embeddings')
EXT_PATH = FILE_DIR.joinpath('extensions')
def find_ext_wildcard_path():
"""Returns the path to the extension wildcards folder"""
found = list(EXT_PATH.rglob('**/wildcards/'))[0]
return found
WILDCARD_EXT_PATH = find_ext_wildcard_path()
# The path to the temporary file
TEMP_PATH = FILE_DIR.joinpath('tags/temp')
def get_wildcards():
"""Returns a list of all wildcards. Works on nested folders."""
wildcard_files = list(WILDCARD_PATH.rglob("*.txt"))
@@ -48,13 +56,13 @@ write_to_temp_file('emb.txt', [])
# Write wildcards to wc.txt if found
if WILDCARD_PATH.exists():
wildcards = get_wildcards()
wildcards = [WILDCARD_PATH.relative_to(FILE_DIR).as_posix()] + get_wildcards()
if wildcards:
write_to_temp_file('wc.txt', wildcards)
# Write extension wildcards to wce.txt if found
if WILDCARD_EXT_PATH.exists():
wildcards_ext = get_ext_wildcards()
wildcards_ext = [WILDCARD_EXT_PATH.relative_to(FILE_DIR).as_posix()] + get_ext_wildcards()
if wildcards_ext:
write_to_temp_file('wce.txt', wildcards_ext)