Skip to content
Merged
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
32 changes: 27 additions & 5 deletions src/sphinxnotes/snippet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
from __future__ import annotations
import sys
import argparse
from typing import List
from typing import List, Iterable, Tuple
from os import path
from textwrap import dedent
from shutil import get_terminal_size
import posixpath

from xdg.BaseDirectory import xdg_config_home
from sphinx.util.matching import patmatch

from . import __version__
from .config import Config
from .cache import Cache
from .cache import Cache, IndexID, Index
from .table import tablify, COLUMNS

DEFAULT_CONFIG_FILE = path.join(xdg_config_home, 'sphinxnotes', 'snippet', 'conf.py')
Expand Down Expand Up @@ -83,7 +84,14 @@ def main(argv: List[str] = sys.argv[1:]):
help='list snippet indexes, columns of indexes: %s' % COLUMNS,
)
listparser.add_argument(
'--tags', '-t', type=str, default='*', help='list specified tags only'
'--tags', '-t', type=str, default='*', help='list snippets with specified tags'
)
listparser.add_argument(
'--docname',
'-d',
type=str,
default='**',
help='list snippets whose docname matches shell-style glob pattern',
)
listparser.add_argument(
'--width',
Expand Down Expand Up @@ -205,9 +213,23 @@ def _on_command_stat(args: argparse.Namespace):
print(f'\t {v} snippets(s)')


def _filter_list_items(
cache: Cache, tags: str, docname_glob: str
) -> Iterable[Tuple[IndexID, Index]]:
for index_id, index in cache.indexes.items():
# Filter by tags.
if index[0] not in tags and '*' not in tags:
continue
# Filter by docname.
(_, docname), _ = cache.index_id_to_doc_id[index_id]
if not patmatch(docname, docname_glob):
continue
yield (index_id, index)


def _on_command_list(args: argparse.Namespace):
rows = tablify(args.cache.indexes, args.tags, args.width)
for row in rows:
items = _filter_list_items(args.cache, args.tags, args.docname)
for row in tablify(items, args.width):
print(row)


Expand Down
8 changes: 4 additions & 4 deletions src/sphinxnotes/snippet/integration/binding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#
# :Author: Shengyu Zhang
# :Date: 2021-08-14
# :Version: 20211114
# :Version: 20240828

function snippet_view() {
selection=$(snippet_list ds)
selection=$(snippet_list --tags ds)
[ -z "$selection" ] && return

# Make sure we have $PAGER
Expand All @@ -22,14 +22,14 @@ function snippet_view() {
}

function snippet_edit() {
selection=$(snippet_list ds)
selection=$(snippet_list --tags ds)
[ -z "$selection" ] && return

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

function snippet_url() {
selection=$(snippet_list ds)
selection=$(snippet_list --tags ds)
[ -z "$selection" ] && return

echo "xdg-open \$($SNIPPET get --url $selection)"
Expand Down
6 changes: 3 additions & 3 deletions src/sphinxnotes/snippet/integration/plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#
# :Author: Shengyu Zhang
# :Date: 2021-03-20
# :Version: 20211114
# :Version: 20240828

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

# Arguments: $1: kinds
# Arguments: $*: Extra opts of ``snippet list``
# Returns: snippet_id
function snippet_list() {
$SNIPPET list --tags $1 --width $(($(tput cols) - 2)) | \
$SNIPPET list --width $(($(tput cols) - 2)) "$@" | \
fzf --with-nth 2.. \
--no-hscroll \
--header-lines 1 \
Expand Down
1 change: 1 addition & 0 deletions src/sphinxnotes/snippet/integration/plugin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function! s:SplitID(row)
return split(a:row, ' ')[0]
endfunction

" TODO: extra opts
function! g:SphinxNotesSnippetList(callback, tags)
let l:width = 0.9
let cmd = [s:snippet, 'list',
Expand Down
10 changes: 4 additions & 6 deletions src/sphinxnotes/snippet/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from __future__ import annotations
from typing import Iterator, Dict
from typing import Iterable, Tuple

from .cache import Index, IndexID
from .utils import ellipsis
Expand All @@ -17,8 +17,8 @@
COLUMN_DELIMITER = ' '


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

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

# Write rows
for index_id, index in indexes.items():
for index_id, index in indexes:
# TODO: assert index?
if index[0] not in tags and '*' not in tags:
continue
row = COLUMN_DELIMITER.join(
[
index_id, # ID
Expand Down