|
9 | 9 | from __future__ import annotations |
10 | 10 | import sys |
11 | 11 | import argparse |
12 | | -from typing import List |
| 12 | +from typing import List, Iterable, Tuple |
13 | 13 | from os import path |
14 | 14 | from textwrap import dedent |
15 | 15 | from shutil import get_terminal_size |
16 | 16 | import posixpath |
17 | 17 |
|
18 | 18 | from xdg.BaseDirectory import xdg_config_home |
| 19 | +from sphinx.util.matching import patmatch |
19 | 20 |
|
20 | 21 | from . import __version__ |
21 | 22 | from .config import Config |
22 | | -from .cache import Cache |
| 23 | +from .cache import Cache, IndexID, Index |
23 | 24 | from .table import tablify, COLUMNS |
24 | 25 |
|
25 | 26 | DEFAULT_CONFIG_FILE = path.join(xdg_config_home, 'sphinxnotes', 'snippet', 'conf.py') |
@@ -83,7 +84,14 @@ def main(argv: List[str] = sys.argv[1:]): |
83 | 84 | help='list snippet indexes, columns of indexes: %s' % COLUMNS, |
84 | 85 | ) |
85 | 86 | listparser.add_argument( |
86 | | - '--tags', '-t', type=str, default='*', help='list specified tags only' |
| 87 | + '--tags', '-t', type=str, default='*', help='list snippets with specified tags' |
| 88 | + ) |
| 89 | + listparser.add_argument( |
| 90 | + '--docname', |
| 91 | + '-d', |
| 92 | + type=str, |
| 93 | + default='**', |
| 94 | + help='list snippets whose docname matches shell-style glob pattern', |
87 | 95 | ) |
88 | 96 | listparser.add_argument( |
89 | 97 | '--width', |
@@ -205,9 +213,23 @@ def _on_command_stat(args: argparse.Namespace): |
205 | 213 | print(f'\t {v} snippets(s)') |
206 | 214 |
|
207 | 215 |
|
| 216 | +def _filter_list_items( |
| 217 | + cache: Cache, tags: str, docname_glob: str |
| 218 | +) -> Iterable[Tuple[IndexID, Index]]: |
| 219 | + for index_id, index in cache.indexes.items(): |
| 220 | + # Filter by tags. |
| 221 | + if index[0] not in tags and '*' not in tags: |
| 222 | + continue |
| 223 | + # Filter by docname. |
| 224 | + (_, docname), _ = cache.index_id_to_doc_id[index_id] |
| 225 | + if not patmatch(docname, docname_glob): |
| 226 | + continue |
| 227 | + yield (index_id, index) |
| 228 | + |
| 229 | + |
208 | 230 | def _on_command_list(args: argparse.Namespace): |
209 | | - rows = tablify(args.cache.indexes, args.tags, args.width) |
210 | | - for row in rows: |
| 231 | + items = _filter_list_items(args.cache, args.tags, args.docname) |
| 232 | + for row in tablify(items, args.width): |
211 | 233 | print(row) |
212 | 234 |
|
213 | 235 |
|
|
0 commit comments