|
4 | 4 |
|
5 | 5 | import structlog
|
6 | 6 |
|
7 |
| -from tagstudio.core.query_lang.ast import AST, Constraint, ConstraintType |
| 7 | +from tagstudio.core.query_lang.ast import AST |
8 | 8 | from tagstudio.core.query_lang.parser import Parser
|
9 | 9 |
|
10 | 10 | MAX_SQL_VARIABLES = 32766 # 32766 is the max sql bind parameter count as defined here: https://github.com/sqlite/sqlite/blob/master/src/sqliteLimit.h#L140
|
@@ -72,59 +72,57 @@ class SortingModeEnum(enum.Enum):
|
72 | 72 |
|
73 | 73 |
|
74 | 74 | @dataclass
|
75 |
| -class FilterState: |
| 75 | +class BrowsingState: |
76 | 76 | """Represent a state of the Library grid view."""
|
77 | 77 |
|
78 |
| - # these should remain |
79 |
| - page_size: int |
80 | 78 | page_index: int = 0
|
81 | 79 | sorting_mode: SortingModeEnum = SortingModeEnum.DATE_ADDED
|
82 | 80 | ascending: bool = True
|
83 | 81 |
|
84 |
| - # these should be erased on update |
85 |
| - # Abstract Syntax Tree Of the current Search Query |
86 |
| - ast: AST | None = None |
87 |
| - |
88 |
| - @property |
89 |
| - def limit(self): |
90 |
| - return self.page_size |
| 82 | + query: str | None = None |
91 | 83 |
|
| 84 | + # Abstract Syntax Tree Of the current Search Query |
92 | 85 | @property
|
93 |
| - def offset(self): |
94 |
| - return self.page_size * self.page_index |
| 86 | + def ast(self) -> AST | None: |
| 87 | + if self.query is None: |
| 88 | + return None |
| 89 | + return Parser(self.query).parse() |
95 | 90 |
|
96 | 91 | @classmethod
|
97 |
| - def show_all(cls, page_size: int) -> "FilterState": |
98 |
| - return FilterState(page_size=page_size) |
| 92 | + def show_all(cls) -> "BrowsingState": |
| 93 | + return BrowsingState() |
99 | 94 |
|
100 | 95 | @classmethod
|
101 |
| - def from_search_query(cls, search_query: str, page_size: int) -> "FilterState": |
102 |
| - return cls(ast=Parser(search_query).parse(), page_size=page_size) |
| 96 | + def from_search_query(cls, search_query: str) -> "BrowsingState": |
| 97 | + return cls(query=search_query) |
103 | 98 |
|
104 | 99 | @classmethod
|
105 |
| - def from_tag_id(cls, tag_id: int | str, page_size: int) -> "FilterState": |
106 |
| - return cls(ast=Constraint(ConstraintType.TagID, str(tag_id), []), page_size=page_size) |
| 100 | + def from_tag_id(cls, tag_id: int | str) -> "BrowsingState": |
| 101 | + return cls(query=f"tag_id:{str(tag_id)}") |
107 | 102 |
|
108 | 103 | @classmethod
|
109 |
| - def from_path(cls, path: Path | str, page_size: int) -> "FilterState": |
110 |
| - return cls(ast=Constraint(ConstraintType.Path, str(path).strip(), []), page_size=page_size) |
| 104 | + def from_path(cls, path: Path | str) -> "BrowsingState": |
| 105 | + return cls(query=f'path:"{str(path).strip()}"') |
111 | 106 |
|
112 | 107 | @classmethod
|
113 |
| - def from_mediatype(cls, mediatype: str, page_size: int) -> "FilterState": |
114 |
| - return cls(ast=Constraint(ConstraintType.MediaType, mediatype, []), page_size=page_size) |
| 108 | + def from_mediatype(cls, mediatype: str) -> "BrowsingState": |
| 109 | + return cls(query=f"mediatype:{mediatype}") |
115 | 110 |
|
116 | 111 | @classmethod
|
117 |
| - def from_filetype(cls, filetype: str, page_size: int) -> "FilterState": |
118 |
| - return cls(ast=Constraint(ConstraintType.FileType, filetype, []), page_size=page_size) |
| 112 | + def from_filetype(cls, filetype: str) -> "BrowsingState": |
| 113 | + return cls(query=f"filetype:{filetype}") |
119 | 114 |
|
120 | 115 | @classmethod
|
121 |
| - def from_tag_name(cls, tag_name: str, page_size: int) -> "FilterState": |
122 |
| - return cls(ast=Constraint(ConstraintType.Tag, tag_name, []), page_size=page_size) |
| 116 | + def from_tag_name(cls, tag_name: str) -> "BrowsingState": |
| 117 | + return cls(query=f'tag:"{tag_name}"') |
| 118 | + |
| 119 | + def with_page_index(self, index: int) -> "BrowsingState": |
| 120 | + return replace(self, page_index=index) |
123 | 121 |
|
124 |
| - def with_sorting_mode(self, mode: SortingModeEnum) -> "FilterState": |
| 122 | + def with_sorting_mode(self, mode: SortingModeEnum) -> "BrowsingState": |
125 | 123 | return replace(self, sorting_mode=mode)
|
126 | 124 |
|
127 |
| - def with_sorting_direction(self, ascending: bool) -> "FilterState": |
| 125 | + def with_sorting_direction(self, ascending: bool) -> "BrowsingState": |
128 | 126 | return replace(self, ascending=ascending)
|
129 | 127 |
|
130 | 128 |
|
|
0 commit comments