Skip to content

Commit d0d630b

Browse files
committed
feat: Add docname glob filter for list subcommand
1 parent 1c53109 commit d0d630b

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/sphinxnotes/snippet/cli.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
from __future__ import annotations
1010
import sys
1111
import argparse
12-
from typing import List
12+
from typing import List, Iterable, Tuple
1313
from os import path
1414
from textwrap import dedent
1515
from shutil import get_terminal_size
1616
import posixpath
1717

1818
from xdg.BaseDirectory import xdg_config_home
19+
from sphinx.util.matching import patmatch
1920

2021
from . import __version__
2122
from .config import Config
22-
from .cache import Cache
23+
from .cache import Cache, IndexID, Index
2324
from .table import tablify, COLUMNS
2425

2526
DEFAULT_CONFIG_FILE = path.join(xdg_config_home, 'sphinxnotes', 'snippet', 'conf.py')
@@ -83,7 +84,14 @@ def main(argv: List[str] = sys.argv[1:]):
8384
help='list snippet indexes, columns of indexes: %s' % COLUMNS,
8485
)
8586
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',
8795
)
8896
listparser.add_argument(
8997
'--width',
@@ -205,9 +213,23 @@ def _on_command_stat(args: argparse.Namespace):
205213
print(f'\t {v} snippets(s)')
206214

207215

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+
208230
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):
211233
print(row)
212234

213235

src/sphinxnotes/snippet/table.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
from __future__ import annotations
10-
from typing import Iterator, Dict
10+
from typing import Iterable, Tuple
1111

1212
from .cache import Index, IndexID
1313
from .utils import ellipsis
@@ -17,8 +17,8 @@
1717
COLUMN_DELIMITER = ' '
1818

1919

20-
def tablify(indexes: Dict[IndexID, Index], tags: str, width: int) -> Iterator[str]:
21-
"""Create a table from sequence of cache.Index."""
20+
def tablify(indexes: Iterable[Tuple[IndexID, Index]], width: int) -> Iterable[str]:
21+
"""Create a table from sequence of indices"""
2222

2323
# Calcuate width
2424
width = width
@@ -41,10 +41,8 @@ def tablify(indexes: Dict[IndexID, Index], tags: str, width: int) -> Iterator[st
4141
yield header
4242

4343
# Write rows
44-
for index_id, index in indexes.items():
44+
for index_id, index in indexes:
4545
# TODO: assert index?
46-
if index[0] not in tags and '*' not in tags:
47-
continue
4846
row = COLUMN_DELIMITER.join(
4947
[
5048
index_id, # ID

0 commit comments

Comments
 (0)