Skip to content

Commit 3c3085e

Browse files
authored
πŸ”€ Move command does not swap (#118)
* ✨ Create new move; rename old move to swap * πŸ’¬: Ensure printing swap instead of move. * βœ… Add passing test for new move. * πŸ“ Update documentation for move and add for swap.
1 parent 63f1fdc commit 3c3085e

File tree

3 files changed

+117
-5
lines changed

3 files changed

+117
-5
lines changed

β€Ždocs/commands.mdβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ pls --help
2525
β”‚ del Delete a Task β”‚
2626
β”‚ delete Delete a Task (deprecated) β”‚
2727
β”‚ done Mark a task as done βœ“ β”‚
28-
β”‚ edit Edit a task by id ✏️ (Add task name inside quotes) β”‚
29-
β”‚ move Change task order πŸ”€ β”‚
28+
β”‚ edit Edit a task by id ✏️ (Add task name inside quotes) β”‚
29+
β”‚ move Insert a task in a new position β”‚
3030
β”‚ showtasks Show all Tasks πŸ“– (deprecated) β”‚
31+
| swap Swap a task's position with another πŸ”€ β”‚
3132
β”‚ tasks Show all Tasks πŸ“– β”‚
3233
β”‚ undone Mark a task as undone β—‹ β”‚
3334
╰──────────────────────────────────────────────────────────────────────────────╯

β€Žpls_cli/please.pyβ€Ž

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,87 @@ def delete(task_id: int) -> None:
322322

323323
@app.command()
324324
def move(old_id: int, new_id: int) -> None:
325-
"""Change task order πŸ”€"""
325+
"""Change task position by floating 🎈 or sinking βš“"""
326326
settings = Settings().get_settings()
327327
if not settings['tasks']:
328328
center_print(
329329
Rule(
330-
'Sorry, cannot move tasks as the Task list is empty',
330+
'Sorry, cannot move task as the Task list is empty',
331+
style=error_line_style,
332+
),
333+
style=error_text_style,
334+
)
335+
return
336+
337+
if old_id == new_id:
338+
center_print(
339+
Rule('No Updates Made', style=warning_line_style),
340+
style=warning_text_style,
341+
)
342+
return
343+
344+
try:
345+
(not 0 <= old_id - 1 < len(settings['tasks'])) or (
346+
not 0 <= new_id - 1 < len(settings['tasks'])
347+
)
348+
except IndexError:
349+
center_print(
350+
Rule(
351+
'Are you sure you gave me the correct ID to move?',
352+
style=error_line_style,
353+
),
354+
style=error_text_style,
355+
wrap=True,
356+
)
357+
return
358+
359+
try:
360+
if len(settings['tasks']) == 2 and (
361+
old_id - 1 == len(settings['tasks'])
362+
or new_id - 1 == len(settings['tasks'])
363+
):
364+
settings['tasks'][old_id - 1], settings['tasks'][new_id - 1] = (
365+
settings['tasks'][new_id - 1],
366+
settings['tasks'][old_id - 1],
367+
)
368+
elif old_id < new_id:
369+
for x in range(new_id - 1, old_id - 1, -1):
370+
settings['tasks'][old_id - 1], settings['tasks'][x] = (
371+
settings['tasks'][x],
372+
settings['tasks'][old_id - 1],
373+
)
374+
else:
375+
for x in range(new_id - 1, old_id):
376+
settings['tasks'][old_id - 1], settings['tasks'][x] = (
377+
settings['tasks'][x],
378+
settings['tasks'][old_id - 1],
379+
)
380+
381+
382+
Settings().write_settings(settings)
383+
center_print(
384+
Rule('Updated Task List', style=update_line_style),
385+
style=update_text_style,
386+
)
387+
print_tasks(settings['tasks'])
388+
except Exception:
389+
center_print(
390+
Rule(
391+
"Please check the entered ID's values", style=error_line_style
392+
),
393+
style=error_text_style,
394+
)
395+
print_tasks()
396+
397+
398+
@app.command()
399+
def swap(old_id: int, new_id: int) -> None:
400+
"""Swap the positions of two tasks πŸ”€"""
401+
settings = Settings().get_settings()
402+
if not settings['tasks']:
403+
center_print(
404+
Rule(
405+
'Sorry, cannot swap tasks as the Task list is empty',
331406
style=error_line_style,
332407
),
333408
style=error_text_style,
@@ -346,7 +421,7 @@ def move(old_id: int, new_id: int) -> None:
346421
):
347422
center_print(
348423
Rule(
349-
'Are you sure you gave me the correct ID to delete?',
424+
'Are you sure you gave me the correct ID to swap?',
350425
style=error_line_style,
351426
),
352427
style=error_text_style,

β€Žtests/test_pls_cli.pyβ€Ž

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,39 @@ def test_edit_empty_tasks(mock_write_settings, mock_get_settings):
219219
output = result.stdout
220220
assert result.exit_code == 0
221221
assert 'Currently, you have no tasks to edit πŸ“' in output
222+
223+
224+
@patch(
225+
'pls_cli.utils.settings.Settings.get_settings',
226+
return_value={
227+
'user_name': 'Test name',
228+
'initial_setup_done': True,
229+
'tasks': [
230+
{'name': 'Task 1', 'done': False},
231+
{'name': 'Task 2', 'done': False},
232+
{'name': 'Task 3', 'done': False},
233+
{'name': 'Task 4', 'done': False}
234+
],
235+
},
236+
)
237+
@patch('pls_cli.utils.settings.Settings.write_settings')
238+
def test_move_task_success(mock_write_settings, mock_get_settings):
239+
result = runner.invoke(app, ['move', '1', '3'])
240+
output = result.stdout
241+
assert result.exit_code == 0
242+
assert 'Updated Task List' in output
243+
single_spaces = ' '.join(output.split())
244+
assert '1 Task 2 β—‹' in single_spaces
245+
assert '2 Task 3 β—‹' in single_spaces
246+
assert '3 Task 1 β—‹' in single_spaces
247+
assert '4 Task 4 β—‹' in single_spaces
248+
249+
result = runner.invoke(app, ['move', '4', '2'])
250+
output = result.stdout
251+
assert result.exit_code == 0
252+
assert 'Updated Task List' in output
253+
single_spaces = ' '.join(output.split())
254+
assert '1 Task 2 β—‹' in single_spaces
255+
assert '2 Task 4 β—‹' in single_spaces
256+
assert '3 Task 3 β—‹' in single_spaces
257+
assert '4 Task 1 β—‹' in single_spaces

0 commit comments

Comments
Β (0)