diff --git a/bookshelf/__main__.py b/bookshelf/__main__.py index 92da693..a490a56 100644 --- a/bookshelf/__main__.py +++ b/bookshelf/__main__.py @@ -6,7 +6,7 @@ from bookshelf.storage import BookshelfStorage from bookshelf.models import Chapter, Story from bookshelf.exceptions import StoryAlreadyExistsException, StoryNotFoundException, ChapterInProgressException, \ - StoryAlreadyFinishedException + StoryAlreadyFinishedException, ChapterNotInProgressException from bookshelf.param_types import StoryType bookshelf_storage = BookshelfStorage() @@ -59,10 +59,9 @@ def create_story_entry(story_name: str, force: bool, tags: str, start_chapter: b story.add_chapter(Chapter(start_time)) bookshelf_storage.save_story(story) - bookshelf_console.render_story_panel(story) except KeyboardInterrupt: - pass + bookshelf_console.print(f'📕 Story \'{story_name}\' has been created!') @bookshelf.command(name='start') @@ -88,7 +87,7 @@ def start_chapter_entry(story_name): bookshelf_storage.save_story(story) bookshelf_console.render_story_panel(story) except KeyboardInterrupt: - pass + bookshelf_console.print(f'📖 A new chapter in \'{story_name}\' has been started!') @bookshelf.command(name='stop') @@ -159,3 +158,23 @@ def story_info_entry(story_name: str): bookshelf_console.render_story_panel(story) except KeyboardInterrupt: pass + +@bookshelf.command(name='cancel') +@click.argument('story_name', type=story_type) +def start_chapter_entry(story_name): + """Cancel the current chapter of a story on your bookshelf""" + try: + if not bookshelf_storage.story_exists(story_name): + raise StoryNotFoundException(story_name) + + story = bookshelf_storage.load_story(story_name) + + if not story.in_progress(): + raise ChapterNotInProgressException(story_name) + + story.cancel_chapter() + + bookshelf_storage.save_story(story) + bookshelf_console.render_story_panel(story) + except KeyboardInterrupt: + bookshelf_console.print(f'❌ Current chapter in \'{story_name}\' has been cancelled!') diff --git a/bookshelf/models.py b/bookshelf/models.py index 2e85d78..27a75c2 100644 --- a/bookshelf/models.py +++ b/bookshelf/models.py @@ -61,6 +61,9 @@ def add_chapter(self, chapter: Chapter) -> None: def get_last_chapter(self) -> Chapter: return self.chapters[-1] + def cancel_chapter(self) -> None: + self.chapters.pop() + def finish_chapter(self) -> None: if self.in_progress(): self.get_last_chapter().end_time = datetime.now() diff --git a/docs/astro.config.mjs b/docs/astro.config.mjs index f00fc4a..784f5d2 100644 --- a/docs/astro.config.mjs +++ b/docs/astro.config.mjs @@ -26,6 +26,7 @@ export default defineConfig({ items: [ // Each item here is one entry in the navigation menu. { label: 'Use the bookshelf command line', link: '/reference/use-bookshelf-cli/' }, + { label: 'bookshelf cancel', link: '/reference/cancel/' }, { label: 'bookshelf create', link: '/reference/create/' }, { label: 'bookshelf finish', link: '/reference/finish/' }, { label: 'bookshelf info', link: '/reference/info/' }, diff --git a/docs/src/content/docs/reference/cancel.mdx b/docs/src/content/docs/reference/cancel.mdx new file mode 100644 index 0000000..e450fd4 --- /dev/null +++ b/docs/src/content/docs/reference/cancel.mdx @@ -0,0 +1,40 @@ +--- +title: bookshelf cancel +description: Cancel the current chapter of a story on your bookshelf +--- + +### Usage + +```bash +bookshelf cancel [OPTIONS] STORY_NAME +``` + +### Description + +Use `bookshelf cancel` to cancel the current chapter of a story on your bookshelf. The story must exist and have a chapter in progress. +This command is useful if you need quickly stop tracking an existing story. + +### Options + +| Name | Shorthand | Default | Description | +|------- | --------- | ------- | ----------- | +| | | | | + +### Examples + +#### Cancel a story +
 root  bookshelf cancel example-story
+ ╭───────────────── 📖 Story 📖 ──────────────────╮
+ │ ✏️ Name:                                       │
+ │   example-story                                │
+ │ 🗓️ Start Date:                                 │
+ │   22/10/2023                                   │
+ │ ⏱️ Elapsed Time:                               │
+ │   0 hours, 0 minutes, 0 seconds                │
+ │ 🏷️ Tags:                                       │
+ │   []                                           │
+ │                                                │
+ │ [Press CTRL+C to exit]                         │
+ ╰────────────────────────────────────────────────╯
+ +