Skip to content

Commit

Permalink
Adding cancel command
Browse files Browse the repository at this point in the history
  • Loading branch information
jossmoff committed Nov 9, 2023
1 parent f69a265 commit 4666faa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
27 changes: 23 additions & 4 deletions bookshelf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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!')
3 changes: 3 additions & 0 deletions bookshelf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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/' },
Expand Down
40 changes: 40 additions & 0 deletions docs/src/content/docs/reference/cancel.mdx
Original file line number Diff line number Diff line change
@@ -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
<pre><span style="background-color:#26384B"><font color="#93FF91"> root </font></span><font color="#26384B"></font> <font color="#49FF6D">bookshelf</font> cancel example-story
╭───────────────── 📖 Story 📖 ──────────────────╮
│ <b>✏️ Name:</b> │
│ example-story │
│ <b>🗓️ Start Date:</b> │
│ 22/10/2023 │
│ <b>⏱️ Elapsed Time:</b> │
│ 0 hours, 0 minutes, 0 seconds │
│ <b>🏷️ Tags:</b> │
[]
│ │
[Press <b>CTRL+C</b> to exit]
╰────────────────────────────────────────────────╯</pre>


0 comments on commit 4666faa

Please sign in to comment.