-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding code for autocomplete * Adding documentation for autocomplete * Incrementing version * Fixing issue with test imports
- Loading branch information
Showing
9 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.1.0" | ||
__version__ = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from click import ParamType | ||
from click.shell_completion import CompletionItem | ||
|
||
from bookshelf.storage import BookshelfStorage | ||
|
||
|
||
class StoryType(ParamType): | ||
name = "story" | ||
|
||
def __init__(self, bookshelf_storage: BookshelfStorage) -> None: | ||
super().__init__() | ||
self.bookshelf_storage = bookshelf_storage | ||
|
||
def shell_complete(self, ctx, param, incomplete): | ||
return [ | ||
CompletionItem(story.name, help=story.tags) | ||
for story in self.bookshelf_storage.get_all_stories_matching_incomplete_name(incomplete) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
title: Enabling Autocomplete | ||
description: A guide to setting up autocomplete with bookshelf | ||
--- | ||
import { Tabs, TabItem } from '@astrojs/starlight/components'; | ||
|
||
|
||
All of this information is taken from the [click documentation](https://click.palletsprojects.com/en/8.1.x/shell-completion/), and you should read it if you want to know more. | ||
In order for completion to be used, the user needs to register a special function with their shell. | ||
The script is different for every shell, and Click will output it for bookshelf when called with `_BOOKSHELF_COMPLETE` set to `{shell}_source`. | ||
The built-in shells are bash, zsh, and fish. | ||
|
||
## Using Eval | ||
|
||
<Tabs> | ||
<TabItem label="Bash"> | ||
Add this to ~/.bashrc: | ||
```bash | ||
eval "$(_BOOKSHELF_COMPLETE=bash_source bookshelf)" | ||
``` | ||
</TabItem> | ||
<TabItem label="Zsh"> | ||
Add this to ~/.zshrc: | ||
```bash | ||
eval "$(_BOOKSHELF_COMPLETE=zsh_source bookshelf)" | ||
``` | ||
|
||
</TabItem> | ||
<TabItem label="Fish"> | ||
Add this to ~/.config/fish/completions/bookshelf.fish: | ||
```bash | ||
_BOOKSHELF_COMPLETE=fish_source bookshelf | source | ||
``` | ||
This is the same file used for the activation script method below. For Fish it’s probably always easier to use that method. | ||
</TabItem> | ||
</Tabs> | ||
|
||
## Using an Activation Script | ||
Using eval means that the command is invoked and evaluated every time a shell is started, which can delay shell responsiveness. | ||
To speed it up, write the generated script to a file, then source that. | ||
You can generate the files ahead of time and distribute them with your program to save your users a step. | ||
|
||
<Tabs> | ||
<TabItem label="Bash"> | ||
Save the script somewhere. | ||
```bash | ||
_BOOKSHELF_COMPLETE=bash_source bookshelf > ~/.bookshelf-complete.bash | ||
``` | ||
Source the file in ~/.bashrc. | ||
```bash | ||
. ~/.bookshelf-complete.bash | ||
``` | ||
</TabItem> | ||
<TabItem label="Zsh"> | ||
Save the script somewhere. | ||
```bash | ||
_BOOKSHELF_COMPLETE=zsh_source bookshelf > ~/.bookshelf-complete.zsh | ||
``` | ||
Source the file in ~/.zshrc. | ||
```bash | ||
. ~/.bookshelf-complete.zsh | ||
``` | ||
</TabItem> | ||
<TabItem label="Fish"> | ||
Save the script to ~/.config/fish/completions/bookshelf.fish: | ||
```bash | ||
_BOOKSHELF_COMPLETE=fish_source bookshelf > ~/.config/fish/completions/bookshelf.fish | ||
``` | ||
</TabItem> | ||
</Tabs> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from unittest.mock import MagicMock | ||
from assertpy import assert_that | ||
from click.shell_completion import CompletionItem | ||
|
||
from bookshelf.param_types import StoryType | ||
from test.utils.storage_utils import create_test_story | ||
|
||
|
||
def test_story_type_init(): | ||
# Create a MagicMock object for BookshelfStorage | ||
bookshelf_storage = MagicMock() | ||
story_type = StoryType(bookshelf_storage) | ||
assert_that(story_type.name).is_equal_to('story') | ||
assert_that(story_type.bookshelf_storage).is_equal_to(bookshelf_storage) | ||
|
||
|
||
def test_story_type_shell_complete_filters_stories(): | ||
# Create a MagicMock object for BookshelfStorage | ||
bookshelf_storage = MagicMock() | ||
story_type = StoryType(bookshelf_storage) | ||
|
||
# Mock the behavior of bookshelf_storage.get_all_stories_matching_incomplete_name | ||
bookshelf_storage.get_all_stories_matching_incomplete_name.return_value = [ | ||
create_test_story('SampleStory1'), | ||
create_test_story('SampleStory2') | ||
] | ||
|
||
# Create MagicMock objects for ctx, param, and incomplete | ||
ctx = MagicMock() | ||
param = MagicMock() | ||
incomplete = 'Sam' | ||
|
||
# Call the shell_complete method | ||
completions = story_type.shell_complete(ctx, param, incomplete) | ||
|
||
# Assert that the completions are as expected | ||
expected_completion_1 = CompletionItem('SampleStory1') | ||
expected_completion_2 = CompletionItem('SampleStory2') | ||
|
||
assert_that(len(completions)).is_equal_to(2) | ||
|
||
for completion in completions: | ||
assert_that(completion).is_instance_of(CompletionItem) | ||
assert_that(completion.value).is_in(expected_completion_1.value, expected_completion_2.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters