Skip to content

Commit a541950

Browse files
committed
feat(cli): Add docname glob filter for list subcommand
1 parent 1c53109 commit a541950

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
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/integration/binding.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#
44
# :Author: Shengyu Zhang
55
# :Date: 2021-08-14
6-
# :Version: 20211114
6+
# :Version: 20240828
77

88
function snippet_view() {
9-
selection=$(snippet_list ds)
9+
selection=$(snippet_list --tags ds)
1010
[ -z "$selection" ] && return
1111

1212
# Make sure we have $PAGER
@@ -22,14 +22,14 @@ function snippet_view() {
2222
}
2323

2424
function snippet_edit() {
25-
selection=$(snippet_list ds)
25+
selection=$(snippet_list --tags ds)
2626
[ -z "$selection" ] && return
2727

2828
echo "vim +\$($SNIPPET get --line-start $selection) \$($SNIPPET get --file $selection)"
2929
}
3030

3131
function snippet_url() {
32-
selection=$(snippet_list ds)
32+
selection=$(snippet_list --tags ds)
3333
[ -z "$selection" ] && return
3434

3535
echo "xdg-open \$($SNIPPET get --url $selection)"

src/sphinxnotes/snippet/integration/plugin.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#
44
# :Author: Shengyu Zhang
55
# :Date: 2021-03-20
6-
# :Version: 20211114
6+
# :Version: 20240828
77

88
# Make sure we have $SNIPPET
99
[ -z "$SNIPPET"] && SNIPPET='snippet'
1010

11-
# Arguments: $1: kinds
11+
# Arguments: $*: Extra opts of ``snippet list``
1212
# Returns: snippet_id
1313
function snippet_list() {
14-
$SNIPPET list --tags $1 --width $(($(tput cols) - 2)) | \
14+
$SNIPPET list --width $(($(tput cols) - 2)) "$@" | \
1515
fzf --with-nth 2.. \
1616
--no-hscroll \
1717
--header-lines 1 \

src/sphinxnotes/snippet/integration/plugin.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function! s:SplitID(row)
1313
return split(a:row, ' ')[0]
1414
endfunction
1515

16+
" TODO: extra opts
1617
function! g:SphinxNotesSnippetList(callback, tags)
1718
let l:width = 0.9
1819
let cmd = [s:snippet, 'list',

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)