Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add smartcase and globless path searches #743

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ def get_paths(self, glob: str | None = None) -> list[str]:
with Session(self.engine) as session:
paths = session.scalars(select(Entry.path)).unique()

path_strings: list[str] = list(map(lambda x: x.as_posix(), paths))
return path_strings
path_strings: list[str] = list(map(lambda x: x.as_posix(), paths))
return path_strings

def search_library(
self,
Expand Down
24 changes: 22 additions & 2 deletions tagstudio/src/core/library/alchemy/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import structlog
from sqlalchemy import ColumnElement, and_, distinct, func, or_, select, text
from sqlalchemy.orm import Session
from sqlalchemy.sql.operators import ilike_op
from src.core.media_types import FILETYPE_EQUIVALENTS, MediaCategories
from src.core.query_lang import BaseVisitor
from src.core.query_lang.ast import ANDList, Constraint, ConstraintType, Not, ORList, Property

from .joins import TagEntry
from .models import Entry, Tag, TagAlias

# workaround to have autocompletion in the Editor
# Only import for type checking/autocompletion, will not be imported at runtime.
if TYPE_CHECKING:
from .library import Library
else:
Expand Down Expand Up @@ -97,7 +98,26 @@ def visit_constraint(self, node: Constraint) -> ColumnElement[bool]:
elif node.type == ConstraintType.TagID:
return self.__entry_matches_tag_ids([int(node.value)])
elif node.type == ConstraintType.Path:
return Entry.path.op("GLOB")(node.value)
smartcase = False
glob = False

if node.value == node.value.lower():
smartcase = True
if "*" in node.value:
glob = True

if smartcase and glob:
logger.info("ConstraintType.Path", smartcase=True, glob=True)
return Entry.path.op("GLOB")(ilike_op(Entry.path, f"%{node.value}%"))
elif smartcase:
logger.info("ConstraintType.Path", smartcase=True, glob=False)
return ilike_op(Entry.path, f"%{node.value}%")
elif glob:
logger.info("ConstraintType.Path", smartcase=False, glob=True)
return Entry.path.op("GLOB")(node.value)
else:
logger.info("ConstraintType.Path", smartcase=False, glob=False)
return Entry.path.regexp_match(rf"\b{node.value}\b")
elif node.type == ConstraintType.MediaType:
extensions: set[str] = set[str]()
for media_cat in MediaCategories.ALL_CATEGORIES:
Expand Down
Loading