Skip to content

Commit

Permalink
Bug fixes and release new version:
Browse files Browse the repository at this point in the history
* Fix `:<NUMBER>` command failing on out-of-range month days
* Fix days with items on it not being highlighted
* Update README
* Add help tip on event/day management pages
  • Loading branch information
dosisod committed Dec 21, 2022
1 parent b317a6d commit 32f6b81
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 20 deletions.
30 changes: 29 additions & 1 deletion README.md
Expand Up @@ -17,7 +17,35 @@ $ cal9000

## Usage

Press `?` to view help.
In short, the following Vim keybindings are supported:

| Key(s) | Action |
|------------|--------|
| `q` | Quit |
| `h` | Go to previous day |
| `j` | Go to next week |
| `J` | Go 4 weeks forward |
| `k` | Go to last week |
| `K` | Go 4 weeks back |
| `l` | Go to next day |
| `u` | Go to to today |
| `i` | Insert an item/event |
| `x` | Delete an event or item |
| `g` | Open event manager |
| `o` | Open the selected day |
| `?` | Open help menu |
| `:command` | Run the command `command`, see below for supported commands |
| *count*`verb` | Run `verb` (`h`/`j`/`k`/`l`, etc) `count` times |

## Commands

Currently supported commands are:

| Command | Description |
|---------------|-------------|
| `h` or `help` | Open help dialog |
| `q` or `quit` | Quit cal9000 |
| *number* | Go to day *number* of the current month |

## Configuration

Expand Down
22 changes: 13 additions & 9 deletions cal9000/render/calendar.py
Expand Up @@ -40,23 +40,27 @@ def render_calendar(date: datetime, db: DB) -> str:
case MonthlyEvent(day=day):
days.add(day)

for row in get_calendar_grid(date):
cols = []
for week in get_calendar_grid(date):
cells = []

for col in row:
for day in week:
color = None

if col == date.day:
if day == date.day:
color = Colors.SELECTED

elif col:
tmp = date.replace(day=col)
elif day:
cell_date = date.replace(day=day)

if tmp.day in days or (tmp.isoweekday() % 7) in weekdays:
if (
cell_date.day in days
or (cell_date.isoweekday() % 7) in weekdays
or db.items.get(cell_date.strftime("%s"))
):
color = Colors.HAS_ITEM

cols.append(render_calendar_cell(col, color))
cells.append(render_calendar_cell(day, color))

lines.append(" ".join(cols))
lines.append(" ".join(cells))

return "\n".join(lines)
6 changes: 3 additions & 3 deletions cal9000/render/day.py
Expand Up @@ -32,7 +32,7 @@ def render_items_for_day(db: DB, date: datetime, index: int) -> str:
)

if len(items) == 0:
out.append("nothing for today")
out.append(f"nothing for today\n\nPress `{Keys.INSERT}` to add item")

return "\n".join(out)

Expand All @@ -55,8 +55,8 @@ def items_for_day(db: DB, date: datetime, keyboard: Keyboard) -> View:
break

if c == Keys.INSERT:
item = prompt_for_new_item()
db.items[date.strftime("%s")].append(item)
if item := prompt_for_new_item():
db.items[date.strftime("%s")].append(item)

elif c == Keys.UP:
if index > 0:
Expand Down
2 changes: 1 addition & 1 deletion cal9000/render/event_manager.py
Expand Up @@ -14,7 +14,7 @@ def interval_to_int(s: str) -> int:

def render_events(events: list[Event], index: int = 0) -> str:
if len(events) == 0:
formatted = "No recurring events"
formatted = f"No recurring events\n\nPress `{Keys.INSERT}` to add event" # noqa: E501

else:
lines: list[str] = []
Expand Down
5 changes: 4 additions & 1 deletion cal9000/render/main.py
@@ -1,3 +1,4 @@
from calendar import monthrange
from datetime import datetime, timedelta

from ..command_bar import CommandBar
Expand Down Expand Up @@ -43,7 +44,9 @@ def main(date: datetime, db: DB, keyboard: Keyboard) -> View:
break

elif (day := cmd_bar.command[1:]).isdigit():
date = date.replace(day=int(day))
max_day = monthrange(date.year, date.month)[1]

date = date.replace(day=max(1, min(int(day), max_day)))

elif cmd_bar.append(c):
continue
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Expand Up @@ -2,7 +2,7 @@ attrs==22.1.0
black==22.12.0
click==8.1.3
colorama==0.4.6
coverage==6.5.0
coverage==7.0.0
flake8==6.0.0
iniconfig==1.1.1
isort==5.11.3
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cal9000"
version = "0.1.0"
version = "0.2.0"
description = "Vim enabled version of cal(1)"
authors = ["dosisod"]
license = "GPL-3.0-only"
Expand Down
16 changes: 15 additions & 1 deletion test/render/test_day.py
Expand Up @@ -15,7 +15,9 @@ def test_render_items_for_day_when_there_are_no_items() -> None:
expected = """\
July 1, 2022:
nothing for today"""
nothing for today
Press `i` to add item"""

assert expected == got

Expand Down Expand Up @@ -76,6 +78,18 @@ def test_insert_item_into_day() -> None:
assert item in states[1]


def test_insert_empty_item_into_day_is_ignored() -> None:
kb = keyboard([Keys.INSERT, Keys.QUIT])
db = DB(items=Items(list))

with disable_print():
with patch("builtins.input", lambda _: ""):
states = list(items_for_day(db, datetime.now(), kb))

assert len(states) == 2
assert not db.items


def test_move_up_and_down_in_item_list() -> None:
kb = keyboard(
[Keys.DOWN, Keys.DOWN, Keys.DOWN, Keys.UP, Keys.UP, Keys.UP, Keys.QUIT]
Expand Down
7 changes: 6 additions & 1 deletion test/render/test_event_manager.py
Expand Up @@ -25,7 +25,12 @@ def test_render_events() -> None:
def test_render_events_with_no_events() -> None:
got = render_events([])

expected = "Recurring events:\n\nNo recurring events"
expected = """\
Recurring events:
No recurring events
Press `i` to add event"""

assert got == expected

Expand Down
20 changes: 20 additions & 0 deletions test/render/test_main.py
Expand Up @@ -147,6 +147,26 @@ def test_goto_day_command() -> None:
assert Colors.SELECTED.colorize("15") in states[-1]


def test_goto_day_too_low_clamps_to_first_of_month() -> None:
date = datetime(month=12, day=20, year=2022)
kb = keyboard(command("0") + [Keys.QUIT])

with disable_print():
states = list(main(date, DB(), kb))

assert Colors.SELECTED.colorize(" 1") in states[-1]


def test_goto_day_too_high_clamps_to_last_day_of_month() -> None:
date = datetime(month=12, day=20, year=2022)
kb = keyboard(command("999") + [Keys.QUIT])

with disable_print():
states = list(main(date, DB(), kb))

assert Colors.SELECTED.colorize("31") in states[-1]


def test_apply_verb_count() -> None:
date = datetime(month=10, day=14, year=2022)
kb = keyboard(list("4h\n") + [Keys.QUIT])
Expand Down
23 changes: 22 additions & 1 deletion test/test_calendar.py
Expand Up @@ -3,7 +3,7 @@

from cal9000.config import Colors
from cal9000.events import MonthlyEvent, WeeklyEvent
from cal9000.io import DB
from cal9000.io import DB, Items
from cal9000.render.calendar import (
get_calendar_grid,
render_calendar,
Expand Down Expand Up @@ -120,3 +120,24 @@ def test_render_calendar_with_events_present() -> None:
31 """

assert expected == got


def test_render_calendar_when_daily_item_present() -> None:
selected_date = datetime(month=12, day=20, year=2022)
item_date = datetime(month=12, day=19, year=2022)
items = {item_date.strftime("%s"): ["item 1", "item 2"]}
db = DB(items=Items(list, items))

with patch("cal9000.config.Colors.colorize", colorize_visualizer):
got = render_calendar(selected_date, db)

expected = """\
December 2022
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 -- xx 21 22 23 24
25 26 27 28 29 30 31"""

assert expected == got

0 comments on commit 32f6b81

Please sign in to comment.