Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add tagging commands #30

Merged
merged 8 commits into from
Nov 12, 2023
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ The base command for the `bookshelf` CLI.

| Name | Description |
|--------------------------------------------------------------------------|--------------------------------------------------------------|
| <a href="https://bookshelf.docs.joss.dev/reference/create/">cancel</a> | Cancel the current chapter of a story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/cancel/">cancel</a> | Cancel the current chapter of a story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/create/">create</a> | Create a new story for your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/finish/">finish </a> | Finish writing a story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/info/">info </a> | Displays the information for a given story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/ls/">ls </a> | List all the current stories on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/rm/">rm </a> | Remove a story from your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/start/">start </a> | Start a new chapter for a story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/stop/">stop </a> | Stop the current chapter of a story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/create/">tag </a> | Add a new tag a story on your bookshelf |
| <a href="https://bookshelf.docs.joss.dev/reference/create/">untag </a> | Remove a tag from a story on your bookshelf |

## 🤝 Contributing

Expand Down
2 changes: 1 addition & 1 deletion bookshelf/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.3"
__version__ = "0.4.0"
22 changes: 22 additions & 0 deletions bookshelf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,25 @@ def cancel_chapter(story_name):
bookshelf_console.render_story_panel(story)
except KeyboardInterrupt:
bookshelf_console.print(f'❌ Current chapter in \'{story_name}\' has been cancelled!')


@bookshelf.command(name='tag')
@click.argument('story_name', type=story_type)
@click.argument('tag', type=str)
def tag_story(story_name: str, tag: str):
"""Add a tag to a story on your bookshelf"""
story = bookshelf_storage.load_story(story_name)
story.add_tag(tag)
bookshelf_console.print(f'🏷️ The tag \'{tag}\' has been added to \'{story_name}\'!')
bookshelf_storage.save_story(story)


@bookshelf.command(name='untag')
@click.argument('story_name', type=story_type)
@click.argument('tag', type=str)
def untag_story(story_name: str, tag: str):
"""Remove a tag from a story on your bookshelf"""
story = bookshelf_storage.load_story(story_name)
story.remove_tag(tag)
bookshelf_console.print(f'🏷️ The tag \'{tag}\' has been removed from \'{story_name}\'!')
bookshelf_storage.save_story(story)
13 changes: 12 additions & 1 deletion bookshelf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def finish_story(self) -> None:
self.finish_chapter()
self.end_date = self.get_last_chapter().end_time

def _has_tag(self, tag: str) -> bool:
return tag in self.tags

def add_tag(self, tag: str) -> None:
if not self._has_tag(tag):
self.tags.append(tag)

def remove_tag(self, tag: str) -> None:
if self._has_tag(tag):
self.tags.remove(tag)

def is_finished(self) -> bool:
return self.end_date is not None

Expand All @@ -96,7 +107,7 @@ def from_json(cls, data: dict) -> 'Story':
end_date = datetime.fromisoformat(data['end_date']) if data['end_date'] else None
tags = data['tags']
chapters = [Chapter.from_json(chapter_data) for chapter_data in data['chapters']]
return cls(name, start_date, end_date, chapters, tags)
return cls(name, start_date, end_date, chapters.copy(), tags.copy())

def in_progress(self) -> bool:
return self.get_last_chapter().in_progress() if len(self.chapters) > 0 else False
Expand Down
2 changes: 2 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default defineConfig({
{ label: 'bookshelf rm', link: '/reference/rm/' },
{ label: 'bookshelf start', link: '/reference/start/' },
{ label: 'bookshelf stop', link: '/reference/stop/' },
{ label: 'bookshelf tag', link: '/reference/tag/' },
{ label: 'bookshelf untag', link: '/reference/untag/' },
]
},
],
Expand Down
24 changes: 24 additions & 0 deletions docs/src/content/docs/reference/tag.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: bookshelf tag
description: Add a new tag a story on your bookshelf
---

### Usage

```bash
bookshelf tag [OPTIONS] STORY_NAME
```

### Description

Use `bookshelf tag` to add a new tag to a story on your bookshelf.

### Options

| Name | Shorthand | Default | Description|
|------- | --------- | ------- | -----------|
| | | | |

### Examples

#### Add a new tag to a story
24 changes: 24 additions & 0 deletions docs/src/content/docs/reference/untag.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: bookshelf untag
description: Remove a tag from a story on your bookshelf
---

### Usage

```bash
bookshelf tag [OPTIONS] STORY_NAME
```

### Description

Use `bookshelf tag` to remove a tag from a story on your bookshelf.

### Options

| Name | Shorthand | Default | Description|
|------- | --------- | ------- | -----------|
| | | | |

### Examples

#### Remove a tag from a story
21 changes: 12 additions & 9 deletions docs/src/content/docs/reference/use-bookshelf-cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ The base command for the `bookshelf` CLI.

### Subcommands

| Name | Description |
| ----------- | ----------- |
| <a href="/reference/create/">create</a> | Create a new story for your bookshelf |
| <a href="/reference/create/">finish </a> | Finish writing a story on your bookshelf |
| <a href="/reference/create/">info </a> | Displays the information for a given story on your bookshelf |
| <a href="/reference/create/">ls </a> | List all the current stories on your bookshelf |
| <a href="/reference/create/">rm </a> | Remove a story from your bookshelf |
| <a href="/reference/create/">start </a> | Start a new chapter for a story on your bookshelf |
| <a href="/reference/create/">stop </a> | Stop the current chapter of a story on your bookshelf |
| Name | Description |
| ----------- | ----------- |
| <a href="/reference/cancel/">cancel</a> | Cancel the current chapter of a story on your bookshelf |
| <a href="/reference/create/">create</a> | Create a new story for your bookshelf |
| <a href="/reference/create/">finish</a> | Finish writing a story on your bookshelf |
| <a href="/reference/create/">info</a> | Displays the information for a given story on your bookshelf |
| <a href="/reference/create/">ls</a> | List all the current stories on your bookshelf |
| <a href="/reference/create/">rm</a> | Remove a story from your bookshelf |
| <a href="/reference/create/">start</a> | Start a new chapter for a story on your bookshelf |
| <a href="/reference/create/">stop</a> | Stop the current chapter of a story on your bookshelf |
| <a href="/reference/create/">tag</a> | Add a new tag a story on your bookshelf |
| <a href="/reference/create/">untag</a> | Remove a tag from a story on your bookshelf |



28 changes: 28 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ def test_story_to_dict(sample_story):
assert_that(story_dict).is_equal_to(STORY_JSON)


def test_add_new_tag_adds_tag():
story = Story.from_json(STORY_JSON)
new_tag = 'new_tag'
story.add_tag(new_tag)
assert_that(story.tags).contains_only('tag1', 'tag2', new_tag)


def test_add_duplicate_tag_does_not_add_tag():
story = Story.from_json(STORY_JSON)
existing_tag = 'tag1'
story.add_tag(existing_tag)
assert_that(story.tags).contains_only('tag1', 'tag2')


def test_remove_existing_tag_removes_tag():
story = Story.from_json(STORY_JSON)
existing_tag = 'tag1'
story.remove_tag(existing_tag)
assert_that(story.tags).contains_only('tag2')


def test_remove_non_existing_tag_does_not_remove_tag():
story = Story.from_json(STORY_JSON)
non_existing_tag = 'non_existing_tag'
story.remove_tag(non_existing_tag)
assert_that(story.tags).contains_only('tag1', 'tag2')


def test_story_from_dict():
story = Story.from_json(STORY_JSON)
assert story.name == STORY_NAME
Expand Down